Java Code Examples for org.apache.calcite.util.TimeString

The following examples show how to use org.apache.calcite.util.TimeString. These examples are extracted from open source projects. 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 Project: Bats   Source File: SqlParserUtil.java    License: 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 2
Source Project: Bats   Source File: SqlLiteral.java    License: 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 3
Source Project: Bats   Source File: RexBuilder.java    License: 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 4
Source Project: Bats   Source File: RexBuilder.java    License: 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 Project: flink   Source File: ExpressionConverterTest.java    License: 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 6
Source Project: flink   Source File: ExpressionConverterTest.java    License: 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 7
Source Project: calcite   Source File: SqlParserUtil.java    License: 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 Project: calcite   Source File: SqlLiteral.java    License: 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 9
Source Project: calcite   Source File: RexBuilder.java    License: 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 10
Source Project: calcite   Source File: RexBuilder.java    License: 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 11
Source Project: calcite   Source File: RexImplicationCheckerTest.java    License: 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 12
Source Project: calcite   Source File: RexBuilderTest.java    License: 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 13
Source Project: Bats   Source File: SqlNodeToRexConverterImpl.java    License: Apache License 2.0 4 votes vote down vote up
public RexNode convertLiteral(
    SqlRexContext cx,
    SqlLiteral literal) {
  RexBuilder rexBuilder = cx.getRexBuilder();
  RelDataTypeFactory typeFactory = cx.getTypeFactory();
  SqlValidator validator = cx.getValidator();
  if (literal.getValue() == null) {
    // Since there is no eq. RexLiteral of SqlLiteral.Unknown we
    // treat it as a cast(null as boolean)
    RelDataType type;
    if (literal.getTypeName() == SqlTypeName.BOOLEAN) {
      type = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
      type = typeFactory.createTypeWithNullability(type, true);
    } else {
      type = validator.getValidatedNodeType(literal);
    }
    return rexBuilder.makeCast(
        type,
        rexBuilder.constantNull());
  }

  BitString bitString;
  SqlIntervalLiteral.IntervalValue intervalValue;
  long l;

  switch (literal.getTypeName()) {
  case DECIMAL:
    // exact number
    BigDecimal bd = literal.getValueAs(BigDecimal.class);
    return rexBuilder.makeExactLiteral(
        bd,
        literal.createSqlType(typeFactory));

  case DOUBLE:
    // approximate type
    // TODO:  preserve fixed-point precision and large integers
    return rexBuilder.makeApproxLiteral(literal.getValueAs(BigDecimal.class));

  case CHAR:
    return rexBuilder.makeCharLiteral(literal.getValueAs(NlsString.class));
  case BOOLEAN:
    return rexBuilder.makeLiteral(literal.getValueAs(Boolean.class));
  case BINARY:
    bitString = literal.getValueAs(BitString.class);
    Preconditions.checkArgument((bitString.getBitCount() % 8) == 0,
        "incomplete octet");

    // An even number of hexits (e.g. X'ABCD') makes whole number
    // of bytes.
    ByteString byteString = new ByteString(bitString.getAsByteArray());
    return rexBuilder.makeBinaryLiteral(byteString);
  case SYMBOL:
    return rexBuilder.makeFlag(literal.getValueAs(Enum.class));
  case TIMESTAMP:
    return rexBuilder.makeTimestampLiteral(
        literal.getValueAs(TimestampString.class),
        ((SqlTimestampLiteral) literal).getPrec());
  case TIME:
    return rexBuilder.makeTimeLiteral(
        literal.getValueAs(TimeString.class),
        ((SqlTimeLiteral) literal).getPrec());
  case DATE:
    return rexBuilder.makeDateLiteral(literal.getValueAs(DateString.class));

  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:
    SqlIntervalQualifier sqlIntervalQualifier =
        literal.getValueAs(SqlIntervalLiteral.IntervalValue.class)
            .getIntervalQualifier();
    return rexBuilder.makeIntervalLiteral(
        literal.getValueAs(BigDecimal.class),
        sqlIntervalQualifier);
  default:
    throw Util.unexpected(literal.getTypeName());
  }
}
 
Example 14
Source Project: Bats   Source File: SqlTimeLiteral.java    License: 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 15
Source Project: Bats   Source File: SqlTimeLiteral.java    License: Apache License 2.0 4 votes vote down vote up
/** Converts this literal to a {@link TimeString}. */
protected TimeString getTime() {
  return (TimeString) value;
}
 
Example 16
Source Project: Bats   Source File: SqlTimeLiteral.java    License: 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 17
Source Project: Bats   Source File: SqlLiteral.java    License: 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 18
Source Project: Bats   Source File: SqlLiteral.java    License: 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 19
Source Project: Bats   Source File: SqlLiteral.java    License: 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 20
Source Project: Bats   Source File: RexBuilder.java    License: 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 21
Source Project: Bats   Source File: RexBuilder.java    License: 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 22
Source Project: Bats   Source File: RexBuilder.java    License: Apache License 2.0 4 votes vote down vote up
/** Converts the type of a value to comply with
 * {@link org.apache.calcite.rex.RexLiteral#valueMatchesType}. */
private static Object clean(Object o, RelDataType type) {
  if (o == null) {
    return null;
  }
  switch (type.getSqlTypeName()) {
  case TINYINT:
  case SMALLINT:
  case INTEGER:
  case BIGINT:
  case DECIMAL:
  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:
    if (o instanceof BigDecimal) {
      return o;
    }
    return new BigDecimal(((Number) o).longValue());
  case FLOAT:
    if (o instanceof BigDecimal) {
      return o;
    }
    return new BigDecimal(((Number) o).doubleValue(), MathContext.DECIMAL32)
        .stripTrailingZeros();
  case REAL:
  case DOUBLE:
    if (o instanceof BigDecimal) {
      return o;
    }
    return new BigDecimal(((Number) o).doubleValue(), MathContext.DECIMAL64)
        .stripTrailingZeros();
  case CHAR:
  case VARCHAR:
    if (o instanceof NlsString) {
      return o;
    }
    return new NlsString((String) o, type.getCharset().name(),
        type.getCollation());
  case TIME:
    if (o instanceof TimeString) {
      return o;
    } else if (o instanceof Calendar) {
      if (!((Calendar) o).getTimeZone().equals(DateTimeUtils.UTC_ZONE)) {
        throw new AssertionError();
      }
      return TimeString.fromCalendarFields((Calendar) o);
    } else {
      return TimeString.fromMillisOfDay((Integer) o);
    }
  case TIME_WITH_LOCAL_TIME_ZONE:
    if (o instanceof TimeString) {
      return o;
    } else {
      return TimeString.fromMillisOfDay((Integer) o);
    }
  case DATE:
    if (o instanceof DateString) {
      return o;
    } else if (o instanceof Calendar) {
      if (!((Calendar) o).getTimeZone().equals(DateTimeUtils.UTC_ZONE)) {
        throw new AssertionError();
      }
      return DateString.fromCalendarFields((Calendar) o);
    } else {
      return DateString.fromDaysSinceEpoch((Integer) o);
    }
  case TIMESTAMP:
    if (o instanceof TimestampString) {
      return o;
    } else if (o instanceof Calendar) {
      if (!((Calendar) o).getTimeZone().equals(DateTimeUtils.UTC_ZONE)) {
        throw new AssertionError();
      }
      return TimestampString.fromCalendarFields((Calendar) o);
    } else {
      return TimestampString.fromMillisSinceEpoch((Long) o);
    }
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
    if (o instanceof TimestampString) {
      return o;
    } else {
      return TimestampString.fromMillisSinceEpoch((Long) o);
    }
  default:
    return o;
  }
}
 
Example 23
Source Project: Bats   Source File: RexToSqlNodeConverterImpl.java    License: 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 24
Source Project: Bats   Source File: RexLiteral.java    License: 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 25
public boolean visit(SQLTimeExpr x) {
    String literal = ((SQLCharExpr) x.getLiteral()).getText();
    TimeString ds = new TimeString(literal);
    sqlNode = SqlLiteral.createTime(ds, 0, SqlParserPos.ZERO);
    return false;
}
 
Example 26
Source Project: dremio-oss   Source File: ValueExpressions.java    License: Apache License 2.0 4 votes vote down vote up
public static LogicalExpression getTime(TimeString time) {
  LocalTime localTime = LocalTime.parse(time.toString(), DateTimes.CALCITE_LOCAL_TIME_FORMATTER);
  return new TimeExpression(Math.toIntExact(NANOSECONDS.toMillis(localTime.toNanoOfDay())));
}
 
Example 27
Source Project: dremio-oss   Source File: ConstExecutor.java    License: Apache License 2.0 4 votes vote down vote up
private static TimeString toTimeString(long epoch) {
  LocalTime localTime = LocalTime.ofNanoOfDay(java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(epoch));

  return new TimeString(localTime.format(DateTimes.CALCITE_LOCAL_TIME_FORMATTER));
}
 
Example 28
Source Project: calcite   Source File: SqlNodeToRexConverterImpl.java    License: Apache License 2.0 4 votes vote down vote up
public RexNode convertLiteral(
    SqlRexContext cx,
    SqlLiteral literal) {
  RexBuilder rexBuilder = cx.getRexBuilder();
  RelDataTypeFactory typeFactory = cx.getTypeFactory();
  SqlValidator validator = cx.getValidator();
  if (literal.getValue() == null) {
    // Since there is no eq. RexLiteral of SqlLiteral.Unknown we
    // treat it as a cast(null as boolean)
    RelDataType type;
    if (literal.getTypeName() == SqlTypeName.BOOLEAN) {
      type = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
      type = typeFactory.createTypeWithNullability(type, true);
    } else {
      type = validator.getValidatedNodeType(literal);
    }
    return rexBuilder.makeNullLiteral(type);
  }

  BitString bitString;
  SqlIntervalLiteral.IntervalValue intervalValue;
  long l;

  switch (literal.getTypeName()) {
  case DECIMAL:
    // exact number
    BigDecimal bd = literal.getValueAs(BigDecimal.class);
    return rexBuilder.makeExactLiteral(
        bd,
        literal.createSqlType(typeFactory));

  case DOUBLE:
    // approximate type
    // TODO:  preserve fixed-point precision and large integers
    return rexBuilder.makeApproxLiteral(literal.getValueAs(BigDecimal.class));

  case CHAR:
    return rexBuilder.makeCharLiteral(literal.getValueAs(NlsString.class));
  case BOOLEAN:
    return rexBuilder.makeLiteral(literal.getValueAs(Boolean.class));
  case BINARY:
    bitString = literal.getValueAs(BitString.class);
    Preconditions.checkArgument((bitString.getBitCount() % 8) == 0,
        "incomplete octet");

    // An even number of hexits (e.g. X'ABCD') makes whole number
    // of bytes.
    ByteString byteString = new ByteString(bitString.getAsByteArray());
    return rexBuilder.makeBinaryLiteral(byteString);
  case SYMBOL:
    return rexBuilder.makeFlag(literal.getValueAs(Enum.class));
  case TIMESTAMP:
    return rexBuilder.makeTimestampLiteral(
        literal.getValueAs(TimestampString.class),
        ((SqlTimestampLiteral) literal).getPrec());
  case TIME:
    return rexBuilder.makeTimeLiteral(
        literal.getValueAs(TimeString.class),
        ((SqlTimeLiteral) literal).getPrec());
  case DATE:
    return rexBuilder.makeDateLiteral(literal.getValueAs(DateString.class));

  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:
    SqlIntervalQualifier sqlIntervalQualifier =
        literal.getValueAs(SqlIntervalLiteral.IntervalValue.class)
            .getIntervalQualifier();
    return rexBuilder.makeIntervalLiteral(
        literal.getValueAs(BigDecimal.class),
        sqlIntervalQualifier);
  default:
    throw Util.unexpected(literal.getTypeName());
  }
}
 
Example 29
Source Project: calcite   Source File: SqlTimeLiteral.java    License: 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 30
Source Project: calcite   Source File: SqlTimeLiteral.java    License: Apache License 2.0 4 votes vote down vote up
/** Converts this literal to a {@link TimeString}. */
protected TimeString getTime() {
  return (TimeString) value;
}