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);
}
}
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.");
}
}
Aggregations