use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimeString in project flink by apache.
the class AbstractSqlCallContext method getLiteralValueAs.
/**
* Bridges to {@link ValueLiteralExpression#getValueAs(Class)}.
*/
@SuppressWarnings("unchecked")
protected static <T> T getLiteralValueAs(LiteralValueAccessor accessor, Class<T> clazz) {
Preconditions.checkArgument(!clazz.isPrimitive());
Object convertedValue = null;
if (clazz == Duration.class) {
final long longVal = accessor.getValueAs(Long.class);
convertedValue = Duration.ofMillis(longVal);
} else if (clazz == Period.class) {
final long longVal = accessor.getValueAs(Long.class);
if (longVal <= Integer.MAX_VALUE && longVal >= Integer.MIN_VALUE) {
convertedValue = Period.ofMonths((int) longVal);
}
} else if (clazz == java.time.LocalDate.class) {
final DateString dateString = accessor.getValueAs(DateString.class);
convertedValue = java.time.LocalDate.parse(dateString.toString());
} else if (clazz == java.time.LocalTime.class) {
final TimeString timeString = accessor.getValueAs(TimeString.class);
convertedValue = java.time.LocalTime.parse(timeString.toString());
} else if (clazz == java.time.LocalDateTime.class) {
final TimestampString timestampString = accessor.getValueAs(TimestampString.class);
convertedValue = java.time.LocalDateTime.parse(timestampString.toString().replace(' ', 'T'));
} else if (clazz == java.time.Instant.class) {
// timestamp string is in UTC, convert back to an instant
final TimestampString timestampString = accessor.getValueAs(TimestampString.class);
convertedValue = java.time.LocalDateTime.parse(timestampString.toString().replace(' ', 'T')).atOffset(ZoneOffset.UTC).toInstant();
}
if (convertedValue != null) {
return (T) convertedValue;
}
return accessor.getValueAs(clazz);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimeString in project hazelcast by hazelcast.
the class RexToExpressionTest method test_time.
@Test
public void test_time() {
RexLiteral literal = literal(new TimeString("12:23:34"), new TimeString("12:23:35"), SqlTypeName.TIME);
Range<?> converted = convert(literal);
assertThat(converted).isEqualToComparingFieldByField(range(LocalTime.of(12, 23, 34), LocalTime.of(12, 23, 35)));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimeString in project hazelcast by hazelcast.
the class HazelcastSqlToRelConverter method convertCast.
/**
* Convert CAST expression fixing several Apache Calcite problems with literals along the way (see inline JavaDoc).
*/
private RexNode convertCast(SqlCall call, Blackboard blackboard) {
SqlNode operand = call.operand(0);
RexNode convertedOperand = blackboard.convertExpression(operand);
RelDataType from = validator.getValidatedNodeType(operand);
RelDataType to = validator.getValidatedNodeType(call);
QueryDataType fromType = HazelcastTypeUtils.toHazelcastType(from);
QueryDataType toType = HazelcastTypeUtils.toHazelcastType(to);
Literal literal = LiteralUtils.literal(convertedOperand);
if (literal != null && ((RexLiteral) convertedOperand).getTypeName() != SqlTypeName.NULL) {
// types to ensure that we throw consistent error messages for all literal-related conversions errors.
try {
// The literal's type might be different from the operand type for example here:
// CAST(CAST(42 AS SMALLINT) AS TINYINT)
// The operand of the outer cast is validated as a SMALLINT, however the operand, thanks to the
// simplification in RexBuilder.makeCast(), is converted to a literal [42:SMALLINT]. And LiteralUtils converts
// this operand to [42:TINYINT] - we have to use the literal's type instead of the validated operand type.
QueryDataType actualFromType = HazelcastTypeUtils.toHazelcastTypeFromSqlTypeName(literal.getTypeName());
toType.getConverter().convertToSelf(actualFromType.getConverter(), literal.getValue());
} catch (Exception e) {
throw literalConversionException(validator, call, literal, toType, e);
}
// DOUBLE literals are converted to a string with scientific conventions (e.g., 1.1E1 instead of 11.0);
if (SqlTypeName.CHAR_TYPES.contains(to.getSqlTypeName())) {
return getRexBuilder().makeLiteral(literal.getStringValue(), to, true);
}
// To workaround the problem, we perform the conversion manually.
if (SqlTypeName.CHAR_TYPES.contains(from.getSqlTypeName()) && to.getSqlTypeName() == SqlTypeName.TIME) {
LocalTime time = fromType.getConverter().asTime(literal.getStringValue());
TimeString timeString = new TimeString(time.getHour(), time.getMinute(), time.getSecond());
return getRexBuilder().makeLiteral(timeString, to, true);
}
// be "true". See CastFunctionIntegrationTest.testApproximateTypeSimplification - it will fail without this fix.
if (fromType.getTypeFamily().isNumeric()) {
if (toType.getTypeFamily().isNumericApproximate()) {
Converter converter = Converters.getConverter(literal.getValue().getClass());
Object convertedValue = toType.getConverter().convertToSelf(converter, literal.getValue());
return getRexBuilder().makeLiteral(convertedValue, to, false);
}
}
}
if (literal != null && HazelcastTypeUtils.isJsonType(to)) {
return getRexBuilder().makeCall(HazelcastJsonParseFunction.INSTANCE, convertedOperand);
}
// Delegate to Apache Calcite.
return getRexBuilder().makeCast(to, convertedOperand);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimeString in project calcite by apache.
the class RexImplicationCheckerTest method testSimpleTime.
@Test
public 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);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimeString in project calcite by apache.
the class RexProgramTest method testSimplifyCastLiteral3.
@Test
public 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");
checkSimplify(cast(timeLTZChar2, timeLTZType), "12:34:45");
checkSimplify(cast(timeLTZChar3, timeLTZType), "11:34:45");
checkSimplify(cast(literalTimeLTZ, timeLTZType), "01:23:45");
checkSimplify(cast(timestampLTZChar1, timestampLTZType), "2011-07-20 03:34:56");
checkSimplify(cast(timestampLTZChar2, timestampLTZType), "2011-07-20 11:34:56");
checkSimplify(cast(timestampLTZChar3, timestampLTZType), "2011-07-20 12:34:56");
checkSimplify(cast(literalTimestampLTZ, timestampLTZType), "2011-07-20 08:23:45");
checkSimplify(cast(literalDate, timestampLTZType), "2011-07-20 07:00:00");
checkSimplify(cast(literalTime, timestampLTZType), "2011-07-20 19:34:56");
checkSimplify(cast(literalTimestamp, timestampLTZType), "2011-07-20 19:34:56");
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");
checkSimplify(cast(literalTimestampLTZ, timeLTZType), "08:23:45");
checkSimplify(cast(literalTimeLTZ, varCharType), "'17:23:45 America/Los_Angeles'");
checkSimplify(cast(literalTimestampLTZ, varCharType), "'2011-07-20 01:23:45 America/Los_Angeles'");
checkSimplify(cast(literalTimeLTZ, timestampType), "2011-07-19 18:23:45");
checkSimplify(cast(literalTimeLTZ, timestampLTZType), "2011-07-20 01:23:45");
}
Aggregations