use of org.apache.flink.table.expressions.TimePointUnit in project flink by apache.
the class ExpressionConverter method visit.
@Override
public RexNode visit(ValueLiteralExpression valueLiteral) {
LogicalType type = fromDataTypeToLogicalType(valueLiteral.getOutputDataType());
RexBuilder rexBuilder = relBuilder.getRexBuilder();
FlinkTypeFactory typeFactory = (FlinkTypeFactory) relBuilder.getTypeFactory();
RelDataType relDataType = typeFactory.createFieldTypeFromLogicalType(type);
if (valueLiteral.isNull()) {
return rexBuilder.makeNullLiteral(relDataType);
}
Object value = null;
switch(type.getTypeRoot()) {
case DECIMAL:
case TINYINT:
case SMALLINT:
case INTEGER:
case BIGINT:
case FLOAT:
case DOUBLE:
value = extractValue(valueLiteral, BigDecimal.class);
break;
case VARCHAR:
case CHAR:
value = extractValue(valueLiteral, String.class);
break;
case BINARY:
case VARBINARY:
value = new ByteString(extractValue(valueLiteral, byte[].class));
break;
case INTERVAL_YEAR_MONTH:
// convert to total months
value = BigDecimal.valueOf(extractValue(valueLiteral, Period.class).toTotalMonths());
break;
case INTERVAL_DAY_TIME:
// TODO planner supports only milliseconds precision
// convert to total millis
value = BigDecimal.valueOf(extractValue(valueLiteral, Duration.class).toMillis());
break;
case DATE:
value = DateString.fromDaysSinceEpoch((int) extractValue(valueLiteral, LocalDate.class).toEpochDay());
break;
case TIME_WITHOUT_TIME_ZONE:
// TODO type factory strips the precision, for literals we can be more lenient
// already
// Moreover conversion from long supports precision up to TIME(3) planner does not
// support higher
// precisions
TimeType timeType = (TimeType) type;
int precision = timeType.getPrecision();
relDataType = typeFactory.createSqlType(SqlTypeName.TIME, Math.min(precision, 3));
value = TimeString.fromMillisOfDay(extractValue(valueLiteral, LocalTime.class).get(ChronoField.MILLI_OF_DAY));
break;
case TIMESTAMP_WITHOUT_TIME_ZONE:
LocalDateTime datetime = extractValue(valueLiteral, LocalDateTime.class);
value = fromLocalDateTime(datetime);
break;
case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
// normalize to UTC
Instant instant = extractValue(valueLiteral, Instant.class);
value = fromLocalDateTime(instant.atOffset(ZoneOffset.UTC).toLocalDateTime());
break;
default:
value = extractValue(valueLiteral, Object.class);
if (value instanceof TimePointUnit) {
value = commonToCalcite((TimePointUnit) value);
} else if (value instanceof TimeIntervalUnit) {
value = commonToCalcite((TimeIntervalUnit) value);
}
break;
}
return rexBuilder.makeLiteral(value, relDataType, // RexBuilder#makeCast.
true);
}
Aggregations