Search in sources :

Example 1 with DistinctType

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

the class DataTypeUtilsTest method testExpandDistinctType.

@Test
public void testExpandDistinctType() {
    FieldsDataType dataType = (FieldsDataType) ROW(FIELD("f0", INT()), FIELD("f1", STRING()), FIELD("f2", TIMESTAMP(5).bridgedTo(Timestamp.class)), FIELD("f3", TIMESTAMP(3)));
    LogicalType originalLogicalType = dataType.getLogicalType();
    DistinctType distinctLogicalType = DistinctType.newBuilder(ObjectIdentifier.of("catalog", "database", "type"), originalLogicalType).build();
    DataType distinctDataType = new FieldsDataType(distinctLogicalType, dataType.getChildren());
    ResolvedSchema schema = DataTypeUtils.expandCompositeTypeToSchema(distinctDataType);
    assertThat(schema).isEqualTo(ResolvedSchema.of(Column.physical("f0", INT()), Column.physical("f1", STRING()), Column.physical("f2", TIMESTAMP(5).bridgedTo(Timestamp.class)), Column.physical("f3", TIMESTAMP(3).bridgedTo(LocalDateTime.class))));
}
Also used : LocalDateTime(java.time.LocalDateTime) FieldsDataType(org.apache.flink.table.types.FieldsDataType) DistinctType(org.apache.flink.table.types.logical.DistinctType) LogicalType(org.apache.flink.table.types.logical.LogicalType) DataType(org.apache.flink.table.types.DataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 2 with DistinctType

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

the class LogicalTypeCasts method supportsCasting.

// --------------------------------------------------------------------------------------------
private static boolean supportsCasting(LogicalType sourceType, LogicalType targetType, boolean allowExplicit) {
    // but it might be useful to cast explicitly with knowledge about the data
    if (sourceType.isNullable() && !targetType.isNullable() && !allowExplicit) {
        return false;
    }
    // ignore nullability during compare
    if (sourceType.copy(true).equals(targetType.copy(true))) {
        return true;
    }
    final LogicalTypeRoot sourceRoot = sourceType.getTypeRoot();
    final LogicalTypeRoot targetRoot = targetType.getTypeRoot();
    if (sourceRoot == NULL) {
        // null can be cast to an arbitrary type
        return true;
    } else if (sourceRoot == DISTINCT_TYPE && targetRoot == DISTINCT_TYPE) {
        // possible
        return false;
    } else if (sourceRoot == DISTINCT_TYPE) {
        return supportsCasting(((DistinctType) sourceType).getSourceType(), targetType, allowExplicit);
    } else if (targetRoot == DISTINCT_TYPE) {
        return supportsCasting(sourceType, ((DistinctType) targetType).getSourceType(), allowExplicit);
    } else if (sourceType.is(INTERVAL) && targetType.is(EXACT_NUMERIC)) {
        // field
        return isSingleFieldInterval(sourceType);
    } else if (sourceType.is(EXACT_NUMERIC) && targetType.is(INTERVAL)) {
        // field
        return isSingleFieldInterval(targetType);
    } else if ((sourceType.is(CONSTRUCTED) || sourceType.is(STRUCTURED_TYPE)) && (targetType.is(CONSTRUCTED) || targetType.is(STRUCTURED_TYPE))) {
        if (sourceType.is(CONSTRUCTED) || targetType.is(CONSTRUCTED)) {
            return supportsConstructedCasting(sourceType, targetType, allowExplicit);
        }
        return supportsStructuredCasting(sourceType, targetType, (s, t) -> supportsCasting(s, t, allowExplicit));
    } else if (sourceRoot == RAW && !targetType.is(BINARY_STRING) && !targetType.is(CHARACTER_STRING) || targetRoot == RAW) {
        // the two raw types are not equal (from initial invariant), casting is not possible
        return false;
    } else if (sourceRoot == SYMBOL || targetRoot == SYMBOL) {
        // the two symbol types are not equal (from initial invariant), casting is not possible
        return false;
    }
    if (implicitCastingRules.get(targetRoot).contains(sourceRoot)) {
        return true;
    }
    if (allowExplicit) {
        return explicitCastingRules.get(targetRoot).contains(sourceRoot);
    }
    return false;
}
Also used : DistinctType(org.apache.flink.table.types.logical.DistinctType) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot)

Example 3 with DistinctType

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

the class DataTypeJsonDeserializer method deserializeClass.

private static DataType deserializeClass(LogicalType logicalType, @Nullable JsonNode parentNode, SerdeContext serdeContext) {
    if (parentNode == null) {
        return DataTypes.of(logicalType);
    }
    final DataType dataType;
    switch(logicalType.getTypeRoot()) {
        case ARRAY:
        case MULTISET:
            final DataType elementDataType = deserializeClass(logicalType.getChildren().get(0), parentNode.get(FIELD_NAME_ELEMENT_CLASS), serdeContext);
            dataType = new CollectionDataType(logicalType, elementDataType);
            break;
        case MAP:
            final MapType mapType = (MapType) logicalType;
            final DataType keyDataType = deserializeClass(mapType.getKeyType(), parentNode.get(FIELD_NAME_KEY_CLASS), serdeContext);
            final DataType valueDataType = deserializeClass(mapType.getValueType(), parentNode.get(FIELD_NAME_VALUE_CLASS), serdeContext);
            dataType = new KeyValueDataType(mapType, keyDataType, valueDataType);
            break;
        case ROW:
        case STRUCTURED_TYPE:
            final List<String> fieldNames = LogicalTypeChecks.getFieldNames(logicalType);
            final List<LogicalType> fieldTypes = LogicalTypeChecks.getFieldTypes(logicalType);
            final ArrayNode fieldNodes = (ArrayNode) parentNode.get(FIELD_NAME_FIELDS);
            final Map<String, JsonNode> fieldNodesByName = new HashMap<>();
            if (fieldNodes != null) {
                fieldNodes.forEach(fieldNode -> fieldNodesByName.put(fieldNode.get(FIELD_NAME_FIELD_NAME).asText(), fieldNode));
            }
            final List<DataType> fieldDataTypes = IntStream.range(0, fieldNames.size()).mapToObj(i -> {
                final String fieldName = fieldNames.get(i);
                final LogicalType fieldType = fieldTypes.get(i);
                return deserializeClass(fieldType, fieldNodesByName.get(fieldName), serdeContext);
            }).collect(Collectors.toList());
            dataType = new FieldsDataType(logicalType, fieldDataTypes);
            break;
        case DISTINCT_TYPE:
            final DistinctType distinctType = (DistinctType) logicalType;
            dataType = deserializeClass(distinctType.getSourceType(), parentNode, serdeContext);
            break;
        default:
            dataType = DataTypes.of(logicalType);
    }
    if (!parentNode.has(FIELD_NAME_CONVERSION_CLASS)) {
        return dataType;
    }
    final Class<?> conversionClass = loadClass(parentNode.get(FIELD_NAME_CONVERSION_CLASS).asText(), serdeContext, String.format("conversion class of data type '%s'", dataType));
    return dataType.bridgedTo(conversionClass);
}
Also used : IntStream(java.util.stream.IntStream) FIELD_NAME_ELEMENT_CLASS(org.apache.flink.table.planner.plan.nodes.exec.serde.DataTypeJsonSerializer.FIELD_NAME_ELEMENT_CLASS) DataType(org.apache.flink.table.types.DataType) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) FIELD_NAME_VALUE_CLASS(org.apache.flink.table.planner.plan.nodes.exec.serde.DataTypeJsonSerializer.FIELD_NAME_VALUE_CLASS) JsonParser(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser) HashMap(java.util.HashMap) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) MapType(org.apache.flink.table.types.logical.MapType) ArrayNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode) DeserializationContext(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext) FIELD_NAME_FIELD_NAME(org.apache.flink.table.planner.plan.nodes.exec.serde.DataTypeJsonSerializer.FIELD_NAME_FIELD_NAME) FIELD_NAME_CONVERSION_CLASS(org.apache.flink.table.planner.plan.nodes.exec.serde.DataTypeJsonSerializer.FIELD_NAME_CONVERSION_CLASS) FIELD_NAME_FIELDS(org.apache.flink.table.planner.plan.nodes.exec.serde.DataTypeJsonSerializer.FIELD_NAME_FIELDS) FieldsDataType(org.apache.flink.table.types.FieldsDataType) Map(java.util.Map) FIELD_NAME_KEY_CLASS(org.apache.flink.table.planner.plan.nodes.exec.serde.DataTypeJsonSerializer.FIELD_NAME_KEY_CLASS) Nullable(javax.annotation.Nullable) DataTypes(org.apache.flink.table.api.DataTypes) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) List(java.util.List) FIELD_NAME_TYPE(org.apache.flink.table.planner.plan.nodes.exec.serde.DataTypeJsonSerializer.FIELD_NAME_TYPE) CollectionDataType(org.apache.flink.table.types.CollectionDataType) DistinctType(org.apache.flink.table.types.logical.DistinctType) LogicalType(org.apache.flink.table.types.logical.LogicalType) JsonSerdeUtil.loadClass(org.apache.flink.table.planner.plan.nodes.exec.serde.JsonSerdeUtil.loadClass) Internal(org.apache.flink.annotation.Internal) StdDeserializer(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer) LogicalTypeChecks(org.apache.flink.table.types.logical.utils.LogicalTypeChecks) FieldsDataType(org.apache.flink.table.types.FieldsDataType) HashMap(java.util.HashMap) CollectionDataType(org.apache.flink.table.types.CollectionDataType) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) LogicalType(org.apache.flink.table.types.logical.LogicalType) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) MapType(org.apache.flink.table.types.logical.MapType) DistinctType(org.apache.flink.table.types.logical.DistinctType) DataType(org.apache.flink.table.types.DataType) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) CollectionDataType(org.apache.flink.table.types.CollectionDataType) ArrayNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode)

Example 4 with DistinctType

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

the class LogicalTypeChecksTest method testIsCompositeTypeDistinctType.

@Test
public void testIsCompositeTypeDistinctType() {
    DataType dataType = ROW(FIELD("f0", INT()), FIELD("f1", STRING()));
    DistinctType distinctType = DistinctType.newBuilder(ObjectIdentifier.of("catalog", "database", "type"), dataType.getLogicalType()).build();
    assertThat(LogicalTypeChecks.isCompositeType(distinctType)).isTrue();
}
Also used : DistinctType(org.apache.flink.table.types.logical.DistinctType) DataType(org.apache.flink.table.types.DataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) Test(org.junit.Test)

Example 5 with DistinctType

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

the class ComparableTypeStrategy method areDistinctTypesComparable.

private boolean areDistinctTypesComparable(LogicalType firstType, LogicalType secondType) {
    DistinctType firstDistinctType = (DistinctType) firstType;
    DistinctType secondDistinctType = (DistinctType) secondType;
    return firstType.equals(secondType) && areComparable(firstDistinctType.getSourceType(), secondDistinctType.getSourceType());
}
Also used : DistinctType(org.apache.flink.table.types.logical.DistinctType)

Aggregations

DistinctType (org.apache.flink.table.types.logical.DistinctType)6 DataType (org.apache.flink.table.types.DataType)3 FieldsDataType (org.apache.flink.table.types.FieldsDataType)3 LogicalType (org.apache.flink.table.types.logical.LogicalType)3 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 Internal (org.apache.flink.annotation.Internal)2 IOException (java.io.IOException)1 Timestamp (java.sql.Timestamp)1 LocalDateTime (java.time.LocalDateTime)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1 JsonParser (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser)1 DeserializationContext (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext)1 JsonNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode)1 StdDeserializer (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer)1 ArrayNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode)1 DataTypes (org.apache.flink.table.api.DataTypes)1