Search in sources :

Example 6 with TimestampType

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

the class LogicalTypesTest method testRowType.

@Test
public void testRowType() {
    assertThat(new RowType(Arrays.asList(new RowType.RowField("a", new VarCharType(), "Someone's desc."), new RowType.RowField("b`", new TimestampType())))).satisfies(baseAssertions("ROW<`a` VARCHAR(1) 'Someone''s desc.', `b``` TIMESTAMP(6)>", "ROW<`a` VARCHAR(1) '...', `b``` TIMESTAMP(6)>", new Class[] { Row.class }, new Class[] { Row.class }, new LogicalType[] { new VarCharType(), new TimestampType() }, new RowType(Arrays.asList(new RowType.RowField("a", new VarCharType(), "Different desc."), new RowType.RowField("b`", new TimestampType())))));
    assertThatThrownBy(() -> new RowType(Arrays.asList(new RowType.RowField("b", new VarCharType()), new RowType.RowField("b", new VarCharType()), new RowType.RowField("a", new VarCharType()), new RowType.RowField("a", new TimestampType())))).isInstanceOf(ValidationException.class);
    assertThatThrownBy(() -> new RowType(Collections.singletonList(new RowType.RowField("", new VarCharType())))).isInstanceOf(ValidationException.class);
}
Also used : RowType(org.apache.flink.table.types.logical.RowType) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) TimestampType(org.apache.flink.table.types.logical.TimestampType) ZonedTimestampType(org.apache.flink.table.types.logical.ZonedTimestampType) LogicalType(org.apache.flink.table.types.logical.LogicalType) VarCharType(org.apache.flink.table.types.logical.VarCharType) Row(org.apache.flink.types.Row) Test(org.junit.Test)

Example 7 with TimestampType

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

the class LogicalTypesTest method testArrayType.

@Test
public void testArrayType() {
    assertThat(new ArrayType(new TimestampType())).satisfies(baseAssertions("ARRAY<TIMESTAMP(6)>", "ARRAY<TIMESTAMP(6)>", new Class[] { java.sql.Timestamp[].class, java.time.LocalDateTime[].class, List.class, ArrayList.class }, new Class[] { java.sql.Timestamp[].class, java.time.LocalDateTime[].class, List.class }, new LogicalType[] { new TimestampType() }, new ArrayType(new SmallIntType())));
    assertThat(new ArrayType(new ArrayType(new TimestampType()))).satisfies(baseAssertions("ARRAY<ARRAY<TIMESTAMP(6)>>", "ARRAY<ARRAY<TIMESTAMP(6)>>", new Class[] { java.sql.Timestamp[][].class, java.time.LocalDateTime[][].class }, new Class[] { java.sql.Timestamp[][].class, java.time.LocalDateTime[][].class }, new LogicalType[] { new ArrayType(new TimestampType()) }, new ArrayType(new ArrayType(new SmallIntType()))));
    final LogicalType nestedArray = new ArrayType(new ArrayType(new TimestampType()));
    assertThat(nestedArray).doesNotSupportInputConversion(java.sql.Timestamp[].class).doesNotSupportOutputConversion(java.sql.Timestamp[].class);
}
Also used : ArrayType(org.apache.flink.table.types.logical.ArrayType) LocalDateTime(java.time.LocalDateTime) SmallIntType(org.apache.flink.table.types.logical.SmallIntType) ArrayList(java.util.ArrayList) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) TimestampType(org.apache.flink.table.types.logical.TimestampType) ZonedTimestampType(org.apache.flink.table.types.logical.ZonedTimestampType) LogicalType(org.apache.flink.table.types.logical.LogicalType) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 8 with TimestampType

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

the class LogicalTypesTest method testMultisetType.

@Test
public void testMultisetType() {
    assertThat(new MultisetType(new TimestampType())).satisfies(baseAssertions("MULTISET<TIMESTAMP(6)>", "MULTISET<TIMESTAMP(6)>", new Class[] { Map.class, HashMap.class, TreeMap.class }, new Class[] { Map.class }, new LogicalType[] { new TimestampType() }, new MultisetType(new SmallIntType())));
    assertThat(new MultisetType(new MultisetType(new TimestampType()))).satisfies(baseAssertions("MULTISET<MULTISET<TIMESTAMP(6)>>", "MULTISET<MULTISET<TIMESTAMP(6)>>", new Class[] { Map.class, HashMap.class, TreeMap.class }, new Class[] { Map.class }, new LogicalType[] { new MultisetType(new TimestampType()) }, new MultisetType(new MultisetType(new SmallIntType()))));
}
Also used : SmallIntType(org.apache.flink.table.types.logical.SmallIntType) HashMap(java.util.HashMap) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) TimestampType(org.apache.flink.table.types.logical.TimestampType) ZonedTimestampType(org.apache.flink.table.types.logical.ZonedTimestampType) LogicalType(org.apache.flink.table.types.logical.LogicalType) TreeMap(java.util.TreeMap) MultisetType(org.apache.flink.table.types.logical.MultisetType) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 9 with TimestampType

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

the class DefaultSchemaResolver method adjustRowtimeAttribute.

private Column adjustRowtimeAttribute(List<WatermarkSpec> watermarkSpecs, Column column) {
    final String name = column.getName();
    final DataType dataType = column.getDataType();
    final boolean hasWatermarkSpec = watermarkSpecs.stream().anyMatch(s -> s.getRowtimeAttribute().equals(name));
    if (hasWatermarkSpec && isStreamingMode) {
        switch(dataType.getLogicalType().getTypeRoot()) {
            case TIMESTAMP_WITHOUT_TIME_ZONE:
                final TimestampType originalType = (TimestampType) dataType.getLogicalType();
                final LogicalType rowtimeType = new TimestampType(originalType.isNullable(), TimestampKind.ROWTIME, originalType.getPrecision());
                return column.copy(replaceLogicalType(dataType, rowtimeType));
            case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
                final LocalZonedTimestampType timestampLtzType = (LocalZonedTimestampType) dataType.getLogicalType();
                final LogicalType rowtimeLtzType = new LocalZonedTimestampType(timestampLtzType.isNullable(), TimestampKind.ROWTIME, timestampLtzType.getPrecision());
                return column.copy(replaceLogicalType(dataType, rowtimeLtzType));
            default:
                throw new ValidationException("Invalid data type of expression for rowtime definition. " + "The field must be of type TIMESTAMP(p) or TIMESTAMP_LTZ(p)," + " the supported precision 'p' is from 0 to 3.");
        }
    }
    return column;
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) DataType(org.apache.flink.table.types.DataType) TimestampType(org.apache.flink.table.types.logical.TimestampType) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) DataTypeUtils.replaceLogicalType(org.apache.flink.table.types.utils.DataTypeUtils.replaceLogicalType) LogicalType(org.apache.flink.table.types.logical.LogicalType)

Example 10 with TimestampType

use of org.apache.flink.table.types.logical.TimestampType 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)

Aggregations

TimestampType (org.apache.flink.table.types.logical.TimestampType)28 LogicalType (org.apache.flink.table.types.logical.LogicalType)17 LocalZonedTimestampType (org.apache.flink.table.types.logical.LocalZonedTimestampType)13 RowType (org.apache.flink.table.types.logical.RowType)13 DecimalType (org.apache.flink.table.types.logical.DecimalType)12 IntType (org.apache.flink.table.types.logical.IntType)11 VarCharType (org.apache.flink.table.types.logical.VarCharType)10 ArrayType (org.apache.flink.table.types.logical.ArrayType)9 SmallIntType (org.apache.flink.table.types.logical.SmallIntType)9 BigIntType (org.apache.flink.table.types.logical.BigIntType)8 DateType (org.apache.flink.table.types.logical.DateType)7 FloatType (org.apache.flink.table.types.logical.FloatType)7 TinyIntType (org.apache.flink.table.types.logical.TinyIntType)7 ArrayList (java.util.ArrayList)6 BooleanType (org.apache.flink.table.types.logical.BooleanType)6 DoubleType (org.apache.flink.table.types.logical.DoubleType)6 ZonedTimestampType (org.apache.flink.table.types.logical.ZonedTimestampType)6 MapType (org.apache.flink.table.types.logical.MapType)5 Test (org.junit.Test)5 BigDecimal (java.math.BigDecimal)4