org.apache.calcite.util.TimeString Java Examples

The following examples show how to use org.apache.calcite.util.TimeString. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExpressionConverterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimeLiteralBiggerPrecision() {
	RexNode rex = converter.visit(valueLiteral(
		LocalTime.parse("12:12:12.12345"),
		DataTypes.TIME(5).notNull()));
	// TODO planner supports up to TIME(3)
	assertThat(
		((RexLiteral) rex).getValueAs(TimeString.class),
		equalTo(new TimeString("12:12:12.123")));
	assertThat(rex.getType().getSqlTypeName(), equalTo(SqlTypeName.TIME));
	assertThat(rex.getType().getPrecision(), equalTo(3));
}
 
Example #2
Source File: RexBuilderTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void checkTime(RexNode node) {
  assertThat(node.toString(), is("02:56:15"));
  RexLiteral literal = (RexLiteral) node;
  assertThat(literal.getValue() instanceof Calendar, is(true));
  assertThat(literal.getValue2() instanceof Integer, is(true));
  assertThat(literal.getValue3() instanceof Integer, is(true));
  assertThat((Integer) literal.getValue2(), is(MOON_TIME));
  assertThat(literal.getValueAs(Calendar.class), notNullValue());
  assertThat(literal.getValueAs(TimeString.class), notNullValue());
}
 
Example #3
Source File: RexImplicationCheckerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testSimpleTime() {
  final Fixture f = new Fixture();
  final TimeString t = TimeString.fromCalendarFields(Util.calendar());
  final RexNode node1 = f.lt(f.t, f.timeLiteral(t));
  final RexNode node2 = f.le(f.t, f.timeLiteral(t));
  f.checkImplies(node1, node2);
  f.checkNotImplies(node2, node1);
}
 
Example #4
Source File: RexBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static Comparable zeroValue(RelDataType type) {
  switch (type.getSqlTypeName()) {
  case CHAR:
    return new NlsString(Spaces.of(type.getPrecision()), null, null);
  case VARCHAR:
    return new NlsString("", null, null);
  case BINARY:
    return new ByteString(new byte[type.getPrecision()]);
  case VARBINARY:
    return ByteString.EMPTY;
  case TINYINT:
  case SMALLINT:
  case INTEGER:
  case BIGINT:
  case DECIMAL:
  case FLOAT:
  case REAL:
  case DOUBLE:
    return BigDecimal.ZERO;
  case BOOLEAN:
    return false;
  case TIME:
  case DATE:
  case TIMESTAMP:
    return DateTimeUtils.ZERO_CALENDAR;
  case TIME_WITH_LOCAL_TIME_ZONE:
    return new TimeString(0, 0, 0);
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
    return new TimestampString(0, 0, 0, 0, 0, 0);
  default:
    throw Util.unexpected(type.getSqlTypeName());
  }
}
 
Example #5
Source File: RexBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a Time with local time-zone literal.
 */
public RexLiteral makeTimeWithLocalTimeZoneLiteral(
    TimeString time,
    int precision) {
  return makeLiteral(Objects.requireNonNull(time),
      typeFactory.createSqlType(SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE, precision),
      SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE);
}
 
Example #6
Source File: SqlLiteral.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public static SqlTimeLiteral createTime(
    Calendar calendar,
    int precision,
    SqlParserPos pos) {
  return createTime(TimeString.fromCalendarFields(calendar), precision, pos);
}
 
Example #7
Source File: SqlParserUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static SqlTimeLiteral parseTimeLiteral(String s, SqlParserPos pos) {
  final String dateStr = parseString(s);
  final DateTimeUtils.PrecisionTime pt =
      DateTimeUtils.parsePrecisionDateTimeLiteral(dateStr,
          Format.PER_THREAD.get().time, DateTimeUtils.UTC_ZONE, -1);
  if (pt == null) {
    throw SqlUtil.newContextException(pos,
        RESOURCE.illegalLiteral("TIME", s,
            RESOURCE.badFormat(DateTimeUtils.TIME_FORMAT_STRING).str()));
  }
  final TimeString t = TimeString.fromCalendarFields(pt.getCalendar())
      .withFraction(pt.getFraction());
  return SqlLiteral.createTime(t, pt.getPrecision(), pos);
}
 
Example #8
Source File: ExpressionConverterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimeLiteral() {
	RexNode rex = converter.visit(valueLiteral(
		LocalTime.parse("12:12:12.12345"),
		DataTypes.TIME(2).notNull()));
	assertThat(
		((RexLiteral) rex).getValueAs(TimeString.class),
		equalTo(new TimeString("12:12:12.12")));
	assertThat(rex.getType().getSqlTypeName(), equalTo(SqlTypeName.TIME));
	assertThat(rex.getType().getPrecision(), equalTo(2));
}
 
Example #9
Source File: RexBuilder.java    From Bats with Apache License 2.0 5 votes vote down vote up
private static Comparable zeroValue(RelDataType type) {
  switch (type.getSqlTypeName()) {
  case CHAR:
    return new NlsString(Spaces.of(type.getPrecision()), null, null);
  case VARCHAR:
    return new NlsString("", null, null);
  case BINARY:
    return new ByteString(new byte[type.getPrecision()]);
  case VARBINARY:
    return ByteString.EMPTY;
  case TINYINT:
  case SMALLINT:
  case INTEGER:
  case BIGINT:
  case DECIMAL:
  case FLOAT:
  case REAL:
  case DOUBLE:
    return BigDecimal.ZERO;
  case BOOLEAN:
    return false;
  case TIME:
  case DATE:
  case TIMESTAMP:
    return DateTimeUtils.ZERO_CALENDAR;
  case TIME_WITH_LOCAL_TIME_ZONE:
    return new TimeString(0, 0, 0);
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
    return new TimestampString(0, 0, 0, 0, 0, 0);
  default:
    throw Util.unexpected(type.getSqlTypeName());
  }
}
 
Example #10
Source File: RexBuilder.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a Time with local time-zone literal.
 */
public RexLiteral makeTimeWithLocalTimeZoneLiteral(
    TimeString time,
    int precision) {
  return makeLiteral(Objects.requireNonNull(time),
      typeFactory.createSqlType(SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE, precision),
      SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE);
}
 
Example #11
Source File: SqlParserUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static SqlTimeLiteral parseTimeLiteral(String s, SqlParserPos pos) {
  final String dateStr = parseString(s);
  final DateTimeUtils.PrecisionTime pt =
      DateTimeUtils.parsePrecisionDateTimeLiteral(dateStr,
          Format.PER_THREAD.get().time, DateTimeUtils.UTC_ZONE, -1);
  if (pt == null) {
    throw SqlUtil.newContextException(pos,
        RESOURCE.illegalLiteral("TIME", s,
            RESOURCE.badFormat(DateTimeUtils.TIME_FORMAT_STRING).str()));
  }
  final TimeString t = TimeString.fromCalendarFields(pt.getCalendar())
      .withFraction(pt.getFraction());
  return SqlLiteral.createTime(t, pt.getPrecision(), pos);
}
 
Example #12
Source File: SqlLiteral.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public static SqlTimeLiteral createTime(
    Calendar calendar,
    int precision,
    SqlParserPos pos) {
  return createTime(TimeString.fromCalendarFields(calendar), precision, pos);
}
 
Example #13
Source File: SqlTimeLiteral.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public SqlTimeLiteral clone(SqlParserPos pos) {
  return new SqlTimeLiteral((TimeString) value, precision, hasTimeZone, pos);
}
 
Example #14
Source File: RexBuilderTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Tests {@link RexBuilder#makeTimeLiteral(TimeString, int)}. */
@Test void testTimeLiteral() {
  final RelDataTypeFactory typeFactory =
      new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  RelDataType timeType = typeFactory.createSqlType(SqlTypeName.TIME);
  final RelDataType timeType3 =
      typeFactory.createSqlType(SqlTypeName.TIME, 3);
  final RelDataType timeType9 =
      typeFactory.createSqlType(SqlTypeName.TIME, 9);
  final RelDataType timeType18 =
      typeFactory.createSqlType(SqlTypeName.TIME, 18);
  final RexBuilder builder = new RexBuilder(typeFactory);

  // Old way: provide a Calendar
  final Calendar calendar = Util.calendar();
  calendar.set(1969, Calendar.JULY, 21, 2, 56, 15); // one small step
  calendar.set(Calendar.MILLISECOND, 0);
  checkTime(builder.makeLiteral(calendar, timeType, false));

  // Old way #2: Provide a Long
  checkTime(builder.makeLiteral(MOON_TIME, timeType, false));

  // The new way
  final TimeString t = new TimeString(2, 56, 15);
  assertThat(t.getMillisOfDay(), is(10575000));
  checkTime(builder.makeLiteral(t, timeType, false));

  // Now with milliseconds
  final TimeString t2 = t.withMillis(56);
  assertThat(t2.getMillisOfDay(), is(10575056));
  assertThat(t2.toString(), is("02:56:15.056"));
  final RexNode literal2 = builder.makeLiteral(t2, timeType3, false);
  assertThat(((RexLiteral) literal2).getValueAs(TimeString.class)
      .toString(), is("02:56:15.056"));

  // Now with nanoseconds
  final TimeString t3 = t.withNanos(2345678);
  assertThat(t3.getMillisOfDay(), is(10575002));
  final RexNode literal3 = builder.makeLiteral(t3, timeType9, false);
  assertThat(((RexLiteral) literal3).getValueAs(TimeString.class)
      .toString(), is("02:56:15.002"));

  // Now with a very long fraction
  final TimeString t4 = t.withFraction("102030405060708090102");
  assertThat(t4.getMillisOfDay(), is(10575102));
  final RexNode literal4 = builder.makeLiteral(t4, timeType18, false);
  assertThat(((RexLiteral) literal4).getValueAs(TimeString.class)
      .toString(), is("02:56:15.102"));

  // toString
  assertThat(t2.round(1).toString(), is("02:56:15"));
  assertThat(t2.round(2).toString(), is("02:56:15.05"));
  assertThat(t2.round(3).toString(), is("02:56:15.056"));
  assertThat(t2.round(4).toString(), is("02:56:15.056"));

  assertThat(t2.toString(6), is("02:56:15.056000"));
  assertThat(t2.toString(1), is("02:56:15.0"));
  assertThat(t2.toString(0), is("02:56:15"));

  assertThat(t2.round(0).toString(), is("02:56:15"));
  assertThat(t2.round(0).toString(0), is("02:56:15"));
  assertThat(t2.round(0).toString(1), is("02:56:15.0"));
  assertThat(t2.round(0).toString(2), is("02:56:15.00"));

  assertThat(TimeString.fromMillisOfDay(53560123).toString(),
      is("14:52:40.123"));
}
 
Example #15
Source File: RexProgramTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test void testSimplifyCastLiteral3() {
  // Default TimeZone is "America/Los_Angeles" (DummyDataContext)
  final RexLiteral literalDate = rexBuilder.makeDateLiteral(new DateString("2011-07-20"));
  final RexLiteral literalTime = rexBuilder.makeTimeLiteral(new TimeString("12:34:56"), 0);
  final RexLiteral literalTimestamp = rexBuilder.makeTimestampLiteral(
      new TimestampString("2011-07-20 12:34:56"), 0);
  final RexLiteral literalTimeLTZ =
      rexBuilder.makeTimeWithLocalTimeZoneLiteral(
          new TimeString(1, 23, 45), 0);
  final RexLiteral timeLTZChar1 = rexBuilder.makeLiteral("12:34:45 America/Los_Angeles");
  final RexLiteral timeLTZChar2 = rexBuilder.makeLiteral("12:34:45 UTC");
  final RexLiteral timeLTZChar3 = rexBuilder.makeLiteral("12:34:45 GMT+01");
  final RexLiteral timestampLTZChar1 = rexBuilder.makeLiteral("2011-07-20 12:34:56 Asia/Tokyo");
  final RexLiteral timestampLTZChar2 = rexBuilder.makeLiteral("2011-07-20 12:34:56 GMT+01");
  final RexLiteral timestampLTZChar3 = rexBuilder.makeLiteral("2011-07-20 12:34:56 UTC");
  final RexLiteral literalTimestampLTZ =
      rexBuilder.makeTimestampWithLocalTimeZoneLiteral(
          new TimestampString(2011, 7, 20, 8, 23, 45), 0);

  final RelDataType dateType =
      typeFactory.createSqlType(SqlTypeName.DATE);
  final RelDataType timeType =
      typeFactory.createSqlType(SqlTypeName.TIME);
  final RelDataType timestampType =
      typeFactory.createSqlType(SqlTypeName.TIMESTAMP);
  final RelDataType timeLTZType =
      typeFactory.createSqlType(SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE);
  final RelDataType timestampLTZType =
      typeFactory.createSqlType(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE);
  final RelDataType varCharType =
      typeFactory.createSqlType(SqlTypeName.VARCHAR, 40);

  checkSimplify(cast(timeLTZChar1, timeLTZType),
      "20:34:45:TIME_WITH_LOCAL_TIME_ZONE(0)");
  checkSimplify(cast(timeLTZChar2, timeLTZType),
      "12:34:45:TIME_WITH_LOCAL_TIME_ZONE(0)");
  checkSimplify(cast(timeLTZChar3, timeLTZType),
      "11:34:45:TIME_WITH_LOCAL_TIME_ZONE(0)");
  checkSimplifyUnchanged(cast(literalTimeLTZ, timeLTZType));
  checkSimplify(cast(timestampLTZChar1, timestampLTZType),
      "2011-07-20 03:34:56:TIMESTAMP_WITH_LOCAL_TIME_ZONE(0)");
  checkSimplify(cast(timestampLTZChar2, timestampLTZType),
      "2011-07-20 11:34:56:TIMESTAMP_WITH_LOCAL_TIME_ZONE(0)");
  checkSimplify(cast(timestampLTZChar3, timestampLTZType),
      "2011-07-20 12:34:56:TIMESTAMP_WITH_LOCAL_TIME_ZONE(0)");
  checkSimplifyUnchanged(cast(literalTimestampLTZ, timestampLTZType));
  checkSimplify(cast(literalDate, timestampLTZType),
      "2011-07-20 07:00:00:TIMESTAMP_WITH_LOCAL_TIME_ZONE(0)");
  checkSimplify(cast(literalTime, timestampLTZType),
      "2011-07-20 19:34:56:TIMESTAMP_WITH_LOCAL_TIME_ZONE(0)");
  checkSimplify(cast(literalTimestamp, timestampLTZType),
      "2011-07-20 19:34:56:TIMESTAMP_WITH_LOCAL_TIME_ZONE(0)");
  checkSimplify(cast(literalTimestamp, dateType),
      "2011-07-20");
  checkSimplify(cast(literalTimestampLTZ, dateType),
      "2011-07-20");
  checkSimplify(cast(literalTimestampLTZ, timeType),
      "01:23:45");
  checkSimplify(cast(literalTimestampLTZ, timestampType),
      "2011-07-20 01:23:45");
  checkSimplify(cast(literalTimeLTZ, timeType),
      "17:23:45");
  checkSimplify(cast(literalTime, timeLTZType),
      "20:34:56:TIME_WITH_LOCAL_TIME_ZONE(0)");
  checkSimplify(cast(literalTimestampLTZ, timeLTZType),
      "08:23:45:TIME_WITH_LOCAL_TIME_ZONE(0)");
  checkSimplify(cast(literalTimeLTZ, varCharType),
      "'17:23:45 America/Los_Angeles':VARCHAR(40)");
  checkSimplify(cast(literalTimestampLTZ, varCharType),
      "'2011-07-20 01:23:45 America/Los_Angeles':VARCHAR(40)");
  checkSimplify(cast(literalTimeLTZ, timestampType),
      "2011-07-19 18:23:45");
  checkSimplify(cast(literalTimeLTZ, timestampLTZType),
      "2011-07-20 01:23:45:TIMESTAMP_WITH_LOCAL_TIME_ZONE(0)");
}
 
Example #16
Source File: RexImplicationCheckerTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RexNode timeLiteral(TimeString t) {
  return rexBuilder.makeTimeLiteral(t, timeDataType.getPrecision());
}
 
Example #17
Source File: SqlTimeLiteral.java    From Bats with Apache License 2.0 4 votes vote down vote up
SqlTimeLiteral(TimeString t, int precision, boolean hasTimeZone,
    SqlParserPos pos) {
  super(t, hasTimeZone, SqlTypeName.TIME, precision, pos);
  Preconditions.checkArgument(this.precision >= 0);
}
 
Example #18
Source File: RexLiteral.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * @return whether value is appropriate for its type (we have rules about
 * these things)
 */
public static boolean valueMatchesType(
    Comparable value,
    SqlTypeName typeName,
    boolean strict) {
  if (value == null) {
    return true;
  }
  switch (typeName) {
  case BOOLEAN:
    // Unlike SqlLiteral, we do not allow boolean null.
    return value instanceof Boolean;
  case NULL:
    return false; // value should have been null
  case INTEGER: // not allowed -- use Decimal
  case TINYINT:
  case SMALLINT:
    if (strict) {
      throw Util.unexpected(typeName);
    }
    // fall through
  case DECIMAL:
  case DOUBLE:
  case FLOAT:
  case REAL:
  case BIGINT:
    return value instanceof BigDecimal;
  case DATE:
    return value instanceof DateString;
  case TIME:
    return value instanceof TimeString;
  case TIME_WITH_LOCAL_TIME_ZONE:
    return value instanceof TimeString;
  case TIMESTAMP:
    return value instanceof TimestampString;
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
    return value instanceof TimestampString;
  case INTERVAL_YEAR:
  case INTERVAL_YEAR_MONTH:
  case INTERVAL_MONTH:
  case INTERVAL_DAY:
  case INTERVAL_DAY_HOUR:
  case INTERVAL_DAY_MINUTE:
  case INTERVAL_DAY_SECOND:
  case INTERVAL_HOUR:
  case INTERVAL_HOUR_MINUTE:
  case INTERVAL_HOUR_SECOND:
  case INTERVAL_MINUTE:
  case INTERVAL_MINUTE_SECOND:
  case INTERVAL_SECOND:
    // The value of a DAY-TIME interval (whatever the start and end units,
    // even say HOUR TO MINUTE) is in milliseconds (perhaps fractional
    // milliseconds). The value of a YEAR-MONTH interval is in months.
    return value instanceof BigDecimal;
  case VARBINARY: // not allowed -- use Binary
    if (strict) {
      throw Util.unexpected(typeName);
    }
    // fall through
  case BINARY:
    return value instanceof ByteString;
  case VARCHAR: // not allowed -- use Char
    if (strict) {
      throw Util.unexpected(typeName);
    }
    // fall through
  case CHAR:
    // A SqlLiteral's charset and collation are optional; not so a
    // RexLiteral.
    return (value instanceof NlsString)
        && (((NlsString) value).getCharset() != null)
        && (((NlsString) value).getCollation() != null);
  case SYMBOL:
    return value instanceof Enum;
  case ROW:
  case MULTISET:
    return value instanceof List;
  case ANY:
    // Literal of type ANY is not legal. "CAST(2 AS ANY)" remains
    // an integer literal surrounded by a cast function.
    return false;
  default:
    throw Util.unexpected(typeName);
  }
}
 
Example #19
Source File: RexToSqlNodeConverterImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public SqlNode convertLiteral(RexLiteral literal) {
  // Numeric
  if (SqlTypeFamily.EXACT_NUMERIC.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createExactNumeric(
        literal.getValue().toString(),
        SqlParserPos.ZERO);
  }

  if (SqlTypeFamily.APPROXIMATE_NUMERIC.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createApproxNumeric(
        literal.getValue().toString(),
        SqlParserPos.ZERO);
  }

  // Timestamp
  if (SqlTypeFamily.TIMESTAMP.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createTimestamp(
        literal.getValueAs(TimestampString.class),
        0,
        SqlParserPos.ZERO);
  }

  // Date
  if (SqlTypeFamily.DATE.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createDate(
        literal.getValueAs(DateString.class),
        SqlParserPos.ZERO);
  }

  // Time
  if (SqlTypeFamily.TIME.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createTime(
        literal.getValueAs(TimeString.class),
        0,
        SqlParserPos.ZERO);
  }

  // String
  if (SqlTypeFamily.CHARACTER.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createCharString(
        ((NlsString) (literal.getValue())).getValue(),
        SqlParserPos.ZERO);
  }

  // Boolean
  if (SqlTypeFamily.BOOLEAN.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createBoolean(
        (Boolean) literal.getValue(),
        SqlParserPos.ZERO);
  }

  // Null
  if (SqlTypeFamily.NULL == literal.getTypeName().getFamily()) {
    return SqlLiteral.createNull(SqlParserPos.ZERO);
  }

  return null;
}
 
Example #20
Source File: SqlTimeLiteral.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** Converts this literal to a {@link TimeString}. */
protected TimeString getTime() {
  return (TimeString) value;
}
 
Example #21
Source File: SqlTimeLiteral.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public SqlTimeLiteral clone(SqlParserPos pos) {
  return new SqlTimeLiteral((TimeString) value, precision, hasTimeZone, pos);
}
 
Example #22
Source File: RexBuilder.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a Time literal.
 */
public RexLiteral makeTimeLiteral(TimeString time, int precision) {
  return makeLiteral(Objects.requireNonNull(time),
      typeFactory.createSqlType(SqlTypeName.TIME, precision),
      SqlTypeName.TIME);
}
 
Example #23
Source File: RexBuilder.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** @deprecated Use {@link #makeTimeLiteral(TimeString, int)}. */
@Deprecated // to be removed before 2.0
public RexLiteral makeTimeLiteral(Calendar calendar, int precision) {
  return makeTimeLiteral(TimeString.fromCalendarFields(calendar), precision);
}
 
Example #24
Source File: SqlLiteral.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static SqlTimeLiteral createTime(
    TimeString t,
    int precision,
    SqlParserPos pos) {
  return new SqlTimeLiteral(t, precision, false, pos);
}
 
Example #25
Source File: SqlLiteral.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * @return whether value is appropriate for its type (we have rules about
 * these things)
 */
public static boolean valueMatchesType(
    Object value,
    SqlTypeName typeName) {
  switch (typeName) {
  case BOOLEAN:
    return (value == null) || (value instanceof Boolean);
  case NULL:
    return value == null;
  case DECIMAL:
  case DOUBLE:
    return value instanceof BigDecimal;
  case DATE:
    return value instanceof DateString;
  case TIME:
    return value instanceof TimeString;
  case TIMESTAMP:
    return value instanceof TimestampString;
  case INTERVAL_YEAR:
  case INTERVAL_YEAR_MONTH:
  case INTERVAL_MONTH:
  case INTERVAL_DAY:
  case INTERVAL_DAY_HOUR:
  case INTERVAL_DAY_MINUTE:
  case INTERVAL_DAY_SECOND:
  case INTERVAL_HOUR:
  case INTERVAL_HOUR_MINUTE:
  case INTERVAL_HOUR_SECOND:
  case INTERVAL_MINUTE:
  case INTERVAL_MINUTE_SECOND:
  case INTERVAL_SECOND:
    return value instanceof SqlIntervalLiteral.IntervalValue;
  case BINARY:
    return value instanceof BitString;
  case CHAR:
    return value instanceof NlsString;
  case SYMBOL:
    return (value instanceof Enum)
        || (value instanceof SqlSampleSpec);
  case MULTISET:
    return true;
  case INTEGER: // not allowed -- use Decimal
  case VARCHAR: // not allowed -- use Char
  case VARBINARY: // not allowed -- use Binary
  default:
    throw Util.unexpected(typeName);
  }
}
 
Example #26
Source File: SqlLiteral.java    From calcite with Apache License 2.0 4 votes vote down vote up
public <T> T getValueAs(Class<T> clazz) {
  if (clazz.isInstance(value)) {
    return clazz.cast(value);
  }
  switch (typeName) {
  case CHAR:
    if (clazz == String.class) {
      return clazz.cast(((NlsString) value).getValue());
    }
    break;
  case BINARY:
    if (clazz == byte[].class) {
      return clazz.cast(((BitString) value).getAsByteArray());
    }
    break;
  case DECIMAL:
    if (clazz == Long.class) {
      return clazz.cast(((BigDecimal) value).unscaledValue().longValue());
    }
    // fall through
  case BIGINT:
  case INTEGER:
  case SMALLINT:
  case TINYINT:
  case DOUBLE:
  case REAL:
  case FLOAT:
    if (clazz == Long.class) {
      return clazz.cast(((BigDecimal) value).longValue());
    } else if (clazz == Integer.class) {
      return clazz.cast(((BigDecimal) value).intValue());
    } else if (clazz == Short.class) {
      return clazz.cast(((BigDecimal) value).shortValue());
    } else if (clazz == Byte.class) {
      return clazz.cast(((BigDecimal) value).byteValue());
    } else if (clazz == Double.class) {
      return clazz.cast(((BigDecimal) value).doubleValue());
    } else if (clazz == Float.class) {
      return clazz.cast(((BigDecimal) value).floatValue());
    }
    break;
  case DATE:
    if (clazz == Calendar.class) {
      return clazz.cast(((DateString) value).toCalendar());
    }
    break;
  case TIME:
    if (clazz == Calendar.class) {
      return clazz.cast(((TimeString) value).toCalendar());
    }
    break;
  case TIMESTAMP:
    if (clazz == Calendar.class) {
      return clazz.cast(((TimestampString) value).toCalendar());
    }
    break;
  case INTERVAL_YEAR:
  case INTERVAL_YEAR_MONTH:
  case INTERVAL_MONTH:
    final SqlIntervalLiteral.IntervalValue valMonth =
        (SqlIntervalLiteral.IntervalValue) value;
    if (clazz == Long.class) {
      return clazz.cast(valMonth.getSign()
          * SqlParserUtil.intervalToMonths(valMonth));
    } else if (clazz == BigDecimal.class) {
      return clazz.cast(BigDecimal.valueOf(getValueAs(Long.class)));
    } else if (clazz == TimeUnitRange.class) {
      return clazz.cast(valMonth.getIntervalQualifier().timeUnitRange);
    }
    break;
  case INTERVAL_DAY:
  case INTERVAL_DAY_HOUR:
  case INTERVAL_DAY_MINUTE:
  case INTERVAL_DAY_SECOND:
  case INTERVAL_HOUR:
  case INTERVAL_HOUR_MINUTE:
  case INTERVAL_HOUR_SECOND:
  case INTERVAL_MINUTE:
  case INTERVAL_MINUTE_SECOND:
  case INTERVAL_SECOND:
    final SqlIntervalLiteral.IntervalValue valTime =
        (SqlIntervalLiteral.IntervalValue) value;
    if (clazz == Long.class) {
      return clazz.cast(valTime.getSign()
          * SqlParserUtil.intervalToMillis(valTime));
    } else if (clazz == BigDecimal.class) {
      return clazz.cast(BigDecimal.valueOf(getValueAs(Long.class)));
    } else if (clazz == TimeUnitRange.class) {
      return clazz.cast(valTime.getIntervalQualifier().timeUnitRange);
    }
    break;
  }
  throw new AssertionError("cannot cast " + value + " as " + clazz);
}
 
Example #27
Source File: SqlLiteral.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * @return whether value is appropriate for its type (we have rules about
 * these things)
 */
public static boolean valueMatchesType(
    Object value,
    SqlTypeName typeName) {
  switch (typeName) {
  case BOOLEAN:
    return (value == null) || (value instanceof Boolean);
  case NULL:
    return value == null;
  case DECIMAL:
  case DOUBLE:
    return value instanceof BigDecimal;
  case DATE:
    return value instanceof DateString;
  case TIME:
    return value instanceof TimeString;
  case TIMESTAMP:
    return value instanceof TimestampString;
  case INTERVAL_YEAR:
  case INTERVAL_YEAR_MONTH:
  case INTERVAL_MONTH:
  case INTERVAL_DAY:
  case INTERVAL_DAY_HOUR:
  case INTERVAL_DAY_MINUTE:
  case INTERVAL_DAY_SECOND:
  case INTERVAL_HOUR:
  case INTERVAL_HOUR_MINUTE:
  case INTERVAL_HOUR_SECOND:
  case INTERVAL_MINUTE:
  case INTERVAL_MINUTE_SECOND:
  case INTERVAL_SECOND:
    return value instanceof SqlIntervalLiteral.IntervalValue;
  case BINARY:
    return value instanceof BitString;
  case CHAR:
    return value instanceof NlsString;
  case SYMBOL:
    return (value instanceof Enum)
        || (value instanceof SqlSampleSpec);
  case MULTISET:
    return true;
  case INTEGER: // not allowed -- use Decimal
  case VARCHAR: // not allowed -- use Char
  case VARBINARY: // not allowed -- use Binary
  default:
    throw Util.unexpected(typeName);
  }
}
 
Example #28
Source File: RexBuilder.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a Time literal.
 */
public RexLiteral makeTimeLiteral(TimeString time, int precision) {
  return makeLiteral(Objects.requireNonNull(time),
      typeFactory.createSqlType(SqlTypeName.TIME, precision),
      SqlTypeName.TIME);
}
 
Example #29
Source File: SqlTimeLiteral.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Converts this literal to a {@link TimeString}. */
protected TimeString getTime() {
  return (TimeString) value;
}
 
Example #30
Source File: SqlTimeLiteral.java    From calcite with Apache License 2.0 4 votes vote down vote up
SqlTimeLiteral(TimeString t, int precision, boolean hasTimeZone,
    SqlParserPos pos) {
  super(t, hasTimeZone, SqlTypeName.TIME, precision, pos);
  Preconditions.checkArgument(this.precision >= 0);
}