Search in sources :

Example 1 with TimePointUnit

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);
}
Also used : LocalDateTime(java.time.LocalDateTime) TimestampStringUtils.fromLocalDateTime(org.apache.flink.table.planner.utils.TimestampStringUtils.fromLocalDateTime) TimeIntervalUnit(org.apache.flink.table.expressions.TimeIntervalUnit) LocalTime(java.time.LocalTime) ByteString(org.apache.calcite.avatica.util.ByteString) Instant(java.time.Instant) LogicalTypeDataTypeConverter.fromDataTypeToLogicalType(org.apache.flink.table.runtime.types.LogicalTypeDataTypeConverter.fromDataTypeToLogicalType) LogicalType(org.apache.flink.table.types.logical.LogicalType) Period(java.time.Period) RelDataType(org.apache.calcite.rel.type.RelDataType) Duration(java.time.Duration) TimeString(org.apache.calcite.util.TimeString) DateString(org.apache.calcite.util.DateString) ByteString(org.apache.calcite.avatica.util.ByteString) BigDecimal(java.math.BigDecimal) TimeType(org.apache.flink.table.types.logical.TimeType) TimePointUnit(org.apache.flink.table.expressions.TimePointUnit) FlinkTypeFactory(org.apache.flink.table.planner.calcite.FlinkTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder)

Aggregations

BigDecimal (java.math.BigDecimal)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 Period (java.time.Period)1 ByteString (org.apache.calcite.avatica.util.ByteString)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RexBuilder (org.apache.calcite.rex.RexBuilder)1 DateString (org.apache.calcite.util.DateString)1 TimeString (org.apache.calcite.util.TimeString)1 TimeIntervalUnit (org.apache.flink.table.expressions.TimeIntervalUnit)1 TimePointUnit (org.apache.flink.table.expressions.TimePointUnit)1 FlinkTypeFactory (org.apache.flink.table.planner.calcite.FlinkTypeFactory)1 TimestampStringUtils.fromLocalDateTime (org.apache.flink.table.planner.utils.TimestampStringUtils.fromLocalDateTime)1 LogicalTypeDataTypeConverter.fromDataTypeToLogicalType (org.apache.flink.table.runtime.types.LogicalTypeDataTypeConverter.fromDataTypeToLogicalType)1 LogicalType (org.apache.flink.table.types.logical.LogicalType)1 TimeType (org.apache.flink.table.types.logical.TimeType)1