Search in sources :

Example 1 with TimestampKind

use of org.apache.flink.table.types.logical.TimestampKind in project flink by apache.

the class TimestampToTimestampCastRule method generateExpression.

@Override
public String generateExpression(CodeGeneratorCastRule.Context context, String inputTerm, LogicalType inputLogicalType, LogicalType targetLogicalType) {
    final int inputPrecision = LogicalTypeChecks.getPrecision(inputLogicalType);
    int targetPrecision = LogicalTypeChecks.getPrecision(targetLogicalType);
    if (inputLogicalType.is(LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE) && targetLogicalType.is(LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE)) {
        final TimestampKind inputTimestampKind = ((TimestampType) inputLogicalType).getKind();
        final TimestampKind targetTimestampKind = ((TimestampType) targetLogicalType).getKind();
        if (inputTimestampKind == TimestampKind.ROWTIME || inputTimestampKind == TimestampKind.PROCTIME || targetTimestampKind == TimestampKind.ROWTIME || targetTimestampKind == TimestampKind.PROCTIME) {
            targetPrecision = 3;
        }
    }
    final String operand;
    if (inputLogicalType.is(LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE) && targetLogicalType.is(LogicalTypeRoot.TIMESTAMP_WITH_LOCAL_TIME_ZONE)) {
        operand = staticCall(BuiltInMethods.TIMESTAMP_TO_TIMESTAMP_WITH_LOCAL_ZONE(), inputTerm, context.getSessionTimeZoneTerm());
    } else if (inputLogicalType.is(LogicalTypeRoot.TIMESTAMP_WITH_LOCAL_TIME_ZONE) && targetLogicalType.is(LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE)) {
        operand = staticCall(BuiltInMethods.TIMESTAMP_WITH_LOCAL_ZONE_TO_TIMESTAMP(), inputTerm, context.getSessionTimeZoneTerm());
    } else {
        operand = inputTerm;
    }
    if (inputPrecision <= targetPrecision) {
        return operand;
    } else {
        return staticCall(BuiltInMethods.TRUNCATE_SQL_TIMESTAMP(), operand, targetPrecision);
    }
}
Also used : TimestampType(org.apache.flink.table.types.logical.TimestampType) TimestampKind(org.apache.flink.table.types.logical.TimestampKind)

Example 2 with TimestampKind

use of org.apache.flink.table.types.logical.TimestampKind in project flink by apache.

the class LogicalTypeJsonDeserializer method deserializeTimestamp.

private static LogicalType deserializeTimestamp(LogicalTypeRoot typeRoot, JsonNode logicalTypeNode) {
    final int precision = logicalTypeNode.get(FIELD_NAME_PRECISION).asInt();
    final TimestampKind kind = TimestampKind.valueOf(logicalTypeNode.get(FIELD_NAME_TIMESTAMP_KIND).asText());
    switch(typeRoot) {
        case TIMESTAMP_WITHOUT_TIME_ZONE:
            return new TimestampType(true, kind, precision);
        case TIMESTAMP_WITH_TIME_ZONE:
            return new ZonedTimestampType(true, kind, precision);
        case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
            return new LocalZonedTimestampType(true, kind, precision);
        default:
            throw new TableException("Timestamp type root expected.");
    }
}
Also used : TableException(org.apache.flink.table.api.TableException) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) ZonedTimestampType(org.apache.flink.table.types.logical.ZonedTimestampType) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) TimestampType(org.apache.flink.table.types.logical.TimestampType) ZonedTimestampType(org.apache.flink.table.types.logical.ZonedTimestampType) TimestampKind(org.apache.flink.table.types.logical.TimestampKind)

Aggregations

TimestampKind (org.apache.flink.table.types.logical.TimestampKind)2 TimestampType (org.apache.flink.table.types.logical.TimestampType)2 TableException (org.apache.flink.table.api.TableException)1 LocalZonedTimestampType (org.apache.flink.table.types.logical.LocalZonedTimestampType)1 ZonedTimestampType (org.apache.flink.table.types.logical.ZonedTimestampType)1