Search in sources :

Example 6 with StructuredType

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

the class LogicalTypeCasts method supportsStructuredCasting.

private static boolean supportsStructuredCasting(LogicalType sourceType, LogicalType targetType, BiFunction<LogicalType, LogicalType, Boolean> childPredicate) {
    final LogicalTypeRoot sourceRoot = sourceType.getTypeRoot();
    final LogicalTypeRoot targetRoot = targetType.getTypeRoot();
    if (sourceRoot != STRUCTURED_TYPE || targetRoot != STRUCTURED_TYPE) {
        return false;
    }
    final StructuredType sourceStructuredType = (StructuredType) sourceType;
    final StructuredType targetStructuredType = (StructuredType) targetType;
    // non-anonymous structured types must be fully equal
    if (sourceStructuredType.getObjectIdentifier().isPresent() || targetStructuredType.getObjectIdentifier().isPresent()) {
        return false;
    }
    // for anonymous structured types we are a bit more lenient, if they provide similar fields
    // e.g. this is necessary when structured types derived from type information and
    // structured types derived within Table API are slightly different
    final Class<?> sourceClass = sourceStructuredType.getImplementationClass().orElse(null);
    final Class<?> targetClass = targetStructuredType.getImplementationClass().orElse(null);
    if (sourceClass != targetClass) {
        return false;
    }
    final List<String> sourceNames = sourceStructuredType.getAttributes().stream().map(StructuredType.StructuredAttribute::getName).collect(Collectors.toList());
    final List<String> targetNames = sourceStructuredType.getAttributes().stream().map(StructuredType.StructuredAttribute::getName).collect(Collectors.toList());
    if (!sourceNames.equals(targetNames)) {
        return false;
    }
    final List<LogicalType> sourceChildren = sourceType.getChildren();
    final List<LogicalType> targetChildren = targetType.getChildren();
    for (int i = 0; i < sourceChildren.size(); i++) {
        if (!childPredicate.apply(sourceChildren.get(i), targetChildren.get(i))) {
            return false;
        }
    }
    return true;
}
Also used : LogicalType(org.apache.flink.table.types.logical.LogicalType) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) StructuredType(org.apache.flink.table.types.logical.StructuredType)

Example 7 with StructuredType

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

the class LogicalTypeDuplicator method visit.

@Override
public LogicalType visit(StructuredType structuredType) {
    final StructuredType.Builder builder = instantiateStructuredBuilder(structuredType);
    builder.attributes(duplicateStructuredAttributes(structuredType));
    builder.setNullable(structuredType.isNullable());
    builder.setFinal(structuredType.isFinal());
    builder.setInstantiable(structuredType.isInstantiable());
    builder.comparison(structuredType.getComparison());
    structuredType.getSuperType().ifPresent(st -> {
        final LogicalType visited = st.accept(this);
        if (!(visited instanceof StructuredType)) {
            throw new TableException("Unexpected super type. Structured type expected but was: " + visited);
        }
        builder.superType((StructuredType) visited);
    });
    structuredType.getDescription().ifPresent(builder::description);
    return builder.build();
}
Also used : TableException(org.apache.flink.table.api.TableException) LogicalType(org.apache.flink.table.types.logical.LogicalType) StructuredType(org.apache.flink.table.types.logical.StructuredType)

Example 8 with StructuredType

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

the class LogicalTypeJsonSerdeTest method testLogicalTypeSerde.

private static List<LogicalType> testLogicalTypeSerde() {
    final List<LogicalType> types = Arrays.asList(new BooleanType(), new TinyIntType(), new SmallIntType(), new IntType(), new BigIntType(), new FloatType(), new DoubleType(), new DecimalType(10), new DecimalType(15, 5), CharType.ofEmptyLiteral(), new CharType(), new CharType(5), VarCharType.ofEmptyLiteral(), new VarCharType(), new VarCharType(5), BinaryType.ofEmptyLiteral(), new BinaryType(), new BinaryType(100), VarBinaryType.ofEmptyLiteral(), new VarBinaryType(), new VarBinaryType(100), new DateType(), new TimeType(), new TimeType(3), new TimestampType(), new TimestampType(3), new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3), new TimestampType(false, TimestampKind.ROWTIME, 3), new ZonedTimestampType(), new ZonedTimestampType(3), new ZonedTimestampType(false, TimestampKind.ROWTIME, 3), new LocalZonedTimestampType(), new LocalZonedTimestampType(3), new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3), new LocalZonedTimestampType(false, TimestampKind.ROWTIME, 3), new DayTimeIntervalType(DayTimeIntervalType.DayTimeResolution.DAY_TO_HOUR), new DayTimeIntervalType(false, DayTimeIntervalType.DayTimeResolution.DAY_TO_HOUR, 3, 6), new YearMonthIntervalType(YearMonthIntervalType.YearMonthResolution.YEAR_TO_MONTH), new YearMonthIntervalType(false, YearMonthIntervalType.YearMonthResolution.MONTH, 2), new ZonedTimestampType(), new LocalZonedTimestampType(), new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3), new SymbolType<>(), new ArrayType(new IntType(false)), new ArrayType(new LocalZonedTimestampType(false, TimestampKind.ROWTIME, 3)), new ArrayType(new ZonedTimestampType(false, TimestampKind.ROWTIME, 3)), new ArrayType(new TimestampType()), new ArrayType(CharType.ofEmptyLiteral()), new ArrayType(VarCharType.ofEmptyLiteral()), new ArrayType(BinaryType.ofEmptyLiteral()), new ArrayType(VarBinaryType.ofEmptyLiteral()), new MapType(new BigIntType(), new IntType(false)), new MapType(new TimestampType(false, TimestampKind.ROWTIME, 3), new ZonedTimestampType()), new MapType(CharType.ofEmptyLiteral(), CharType.ofEmptyLiteral()), new MapType(VarCharType.ofEmptyLiteral(), VarCharType.ofEmptyLiteral()), new MapType(BinaryType.ofEmptyLiteral(), BinaryType.ofEmptyLiteral()), new MapType(VarBinaryType.ofEmptyLiteral(), VarBinaryType.ofEmptyLiteral()), new MultisetType(new IntType(false)), new MultisetType(new TimestampType()), new MultisetType(new TimestampType(true, TimestampKind.ROWTIME, 3)), new MultisetType(CharType.ofEmptyLiteral()), new MultisetType(VarCharType.ofEmptyLiteral()), new MultisetType(BinaryType.ofEmptyLiteral()), new MultisetType(VarBinaryType.ofEmptyLiteral()), RowType.of(new BigIntType(), new IntType(false), new VarCharType(200)), RowType.of(new LogicalType[] { new BigIntType(), new IntType(false), new VarCharType(200) }, new String[] { "f1", "f2", "f3" }), RowType.of(new TimestampType(false, TimestampKind.ROWTIME, 3), new TimestampType(false, TimestampKind.REGULAR, 3), new ZonedTimestampType(false, TimestampKind.ROWTIME, 3), new ZonedTimestampType(false, TimestampKind.REGULAR, 3), new LocalZonedTimestampType(false, TimestampKind.ROWTIME, 3), new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3), new LocalZonedTimestampType(false, TimestampKind.REGULAR, 3)), RowType.of(CharType.ofEmptyLiteral(), VarCharType.ofEmptyLiteral(), BinaryType.ofEmptyLiteral(), VarBinaryType.ofEmptyLiteral()), // registered structured type
    StructuredType.newBuilder(ObjectIdentifier.of("cat", "db", "structuredType"), PojoClass.class).attributes(Arrays.asList(new StructuredType.StructuredAttribute("f0", new IntType(true)), new StructuredType.StructuredAttribute("f1", new BigIntType(true)), new StructuredType.StructuredAttribute("f2", new VarCharType(200), "desc"))).comparison(StructuredType.StructuredComparison.FULL).setFinal(false).setInstantiable(false).superType(StructuredType.newBuilder(ObjectIdentifier.of("cat", "db", "structuredType2")).attributes(Collections.singletonList(new StructuredType.StructuredAttribute("f0", new BigIntType(false)))).build()).description("description for StructuredType").build(), // unregistered structured type
    StructuredType.newBuilder(PojoClass.class).attributes(Arrays.asList(new StructuredType.StructuredAttribute("f0", new IntType(true)), new StructuredType.StructuredAttribute("f1", new BigIntType(true)), new StructuredType.StructuredAttribute("f2", new VarCharType(200), "desc"))).build(), // registered distinct type
    DistinctType.newBuilder(ObjectIdentifier.of("cat", "db", "distinctType"), new VarCharType(5)).build(), DistinctType.newBuilder(ObjectIdentifier.of("cat", "db", "distinctType"), new VarCharType(false, 5)).build(), // custom RawType
    new RawType<>(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE), // external RawType
    new RawType<>(Row.class, ExternalSerializer.of(DataTypes.ROW(DataTypes.INT(), DataTypes.STRING()))));
    final List<LogicalType> mutableTypes = new ArrayList<>(types);
    // RawType for MapView
    addRawTypesForMapView(mutableTypes, new VarCharType(100), new VarCharType(100));
    addRawTypesForMapView(mutableTypes, new VarCharType(100), new BigIntType());
    addRawTypesForMapView(mutableTypes, new BigIntType(), new VarCharType(100));
    addRawTypesForMapView(mutableTypes, new BigIntType(), new BigIntType());
    // RawType for ListView
    addRawTypesForListView(mutableTypes, new VarCharType(100));
    addRawTypesForListView(mutableTypes, new BigIntType());
    // RawType for custom MapView
    mutableTypes.add(DataViewUtils.adjustDataViews(MapView.newMapViewDataType(DataTypes.STRING().toInternal(), DataTypes.STRING().bridgedTo(byte[].class)), false).getLogicalType());
    final List<LogicalType> allTypes = new ArrayList<>();
    // consider nullable
    for (LogicalType type : mutableTypes) {
        allTypes.add(type.copy(true));
        allTypes.add(type.copy(false));
    }
    // ignore nullable for NullType
    allTypes.add(new NullType());
    return allTypes;
}
Also used : LocalDateTime(java.time.LocalDateTime) VarBinaryType(org.apache.flink.table.types.logical.VarBinaryType) ArrayList(java.util.ArrayList) LogicalType(org.apache.flink.table.types.logical.LogicalType) BigIntType(org.apache.flink.table.types.logical.BigIntType) MapType(org.apache.flink.table.types.logical.MapType) TinyIntType(org.apache.flink.table.types.logical.TinyIntType) IntType(org.apache.flink.table.types.logical.IntType) BigIntType(org.apache.flink.table.types.logical.BigIntType) SmallIntType(org.apache.flink.table.types.logical.SmallIntType) FloatType(org.apache.flink.table.types.logical.FloatType) TimeType(org.apache.flink.table.types.logical.TimeType) StructuredType(org.apache.flink.table.types.logical.StructuredType) ArrayType(org.apache.flink.table.types.logical.ArrayType) PojoClass(org.apache.flink.table.planner.plan.nodes.exec.serde.DataTypeJsonSerdeTest.PojoClass) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) TimestampType(org.apache.flink.table.types.logical.TimestampType) ZonedTimestampType(org.apache.flink.table.types.logical.ZonedTimestampType) VarCharType(org.apache.flink.table.types.logical.VarCharType) DateType(org.apache.flink.table.types.logical.DateType) MultisetType(org.apache.flink.table.types.logical.MultisetType) BinaryType(org.apache.flink.table.types.logical.BinaryType) VarBinaryType(org.apache.flink.table.types.logical.VarBinaryType) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) ZonedTimestampType(org.apache.flink.table.types.logical.ZonedTimestampType) BooleanType(org.apache.flink.table.types.logical.BooleanType) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) TinyIntType(org.apache.flink.table.types.logical.TinyIntType) DayTimeIntervalType(org.apache.flink.table.types.logical.DayTimeIntervalType) SmallIntType(org.apache.flink.table.types.logical.SmallIntType) DoubleType(org.apache.flink.table.types.logical.DoubleType) DecimalType(org.apache.flink.table.types.logical.DecimalType) CharType(org.apache.flink.table.types.logical.CharType) VarCharType(org.apache.flink.table.types.logical.VarCharType) Row(org.apache.flink.types.Row) NullType(org.apache.flink.table.types.logical.NullType) YearMonthIntervalType(org.apache.flink.table.types.logical.YearMonthIntervalType)

Example 9 with StructuredType

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

the class SchemaTranslator method patchStructuredDataType.

private static DataType patchStructuredDataType(DataType dataType, String patchedFieldName, DataType patchedFieldDataType) {
    final StructuredType type = (StructuredType) dataType.getLogicalType();
    final List<String> oldFieldNames = flattenToNames(dataType);
    final List<DataType> oldFieldDataTypes = dataType.getChildren();
    final Class<?> oldConversion = dataType.getConversionClass();
    final DataTypes.Field[] fields = patchFields(oldFieldNames, oldFieldDataTypes, patchedFieldName, patchedFieldDataType);
    final DataType newDataType = DataTypes.STRUCTURED(type.getImplementationClass().orElseThrow(IllegalStateException::new), fields).bridgedTo(oldConversion);
    if (!type.isNullable()) {
        return newDataType.notNull();
    }
    return newDataType;
}
Also used : DataType(org.apache.flink.table.types.DataType) AbstractDataType(org.apache.flink.table.types.AbstractDataType) StructuredType(org.apache.flink.table.types.logical.StructuredType)

Example 10 with StructuredType

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

the class DataTypeExtractorTest method getPojoWithRawSelfReferenceDataType.

private static DataType getPojoWithRawSelfReferenceDataType() {
    final StructuredType.Builder builder = StructuredType.newBuilder(PojoWithRawSelfReference.class);
    builder.attributes(Arrays.asList(new StructuredAttribute("integer", new IntType()), new StructuredAttribute("reference", dummyRaw(PojoWithRawSelfReference.class).getLogicalType())));
    builder.setFinal(true);
    builder.setInstantiable(true);
    final StructuredType structuredType = builder.build();
    final List<DataType> fieldDataTypes = Arrays.asList(DataTypes.INT(), dummyRaw(PojoWithRawSelfReference.class));
    return new FieldsDataType(structuredType, PojoWithRawSelfReference.class, fieldDataTypes);
}
Also used : FieldsDataType(org.apache.flink.table.types.FieldsDataType) StructuredAttribute(org.apache.flink.table.types.logical.StructuredType.StructuredAttribute) DataType(org.apache.flink.table.types.DataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) StructuredType(org.apache.flink.table.types.logical.StructuredType) IntType(org.apache.flink.table.types.logical.IntType) BigIntType(org.apache.flink.table.types.logical.BigIntType)

Aggregations

StructuredType (org.apache.flink.table.types.logical.StructuredType)19 DataType (org.apache.flink.table.types.DataType)11 FieldsDataType (org.apache.flink.table.types.FieldsDataType)9 BigIntType (org.apache.flink.table.types.logical.BigIntType)7 IntType (org.apache.flink.table.types.logical.IntType)7 StructuredAttribute (org.apache.flink.table.types.logical.StructuredType.StructuredAttribute)7 LogicalType (org.apache.flink.table.types.logical.LogicalType)6 BooleanType (org.apache.flink.table.types.logical.BooleanType)4 ArrayList (java.util.ArrayList)3 TableException (org.apache.flink.table.api.TableException)3 Test (org.junit.Test)3 LocalDateTime (java.time.LocalDateTime)2 List (java.util.List)2 IntStream (java.util.stream.IntStream)2 Internal (org.apache.flink.annotation.Internal)2 RowData (org.apache.flink.table.data.RowData)2 MapType (org.apache.flink.table.types.logical.MapType)2 RowType (org.apache.flink.table.types.logical.RowType)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1