Search in sources :

Example 1 with StructuredType

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

the class DataTypeUtilsTest method testExpandStructuredType.

@Test
public void testExpandStructuredType() {
    StructuredType logicalType = StructuredType.newBuilder(ObjectIdentifier.of("catalog", "database", "type")).attributes(Arrays.asList(new StructuredType.StructuredAttribute("f0", DataTypes.INT().getLogicalType()), new StructuredType.StructuredAttribute("f1", DataTypes.STRING().getLogicalType()), new StructuredType.StructuredAttribute("f2", DataTypes.TIMESTAMP(5).getLogicalType()), new StructuredType.StructuredAttribute("f3", DataTypes.TIMESTAMP(3).getLogicalType()))).build();
    List<DataType> dataTypes = Arrays.asList(DataTypes.INT(), DataTypes.STRING(), DataTypes.TIMESTAMP(5).bridgedTo(Timestamp.class), DataTypes.TIMESTAMP(3));
    FieldsDataType dataType = new FieldsDataType(logicalType, dataTypes);
    ResolvedSchema schema = DataTypeUtils.expandCompositeTypeToSchema(dataType);
    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) 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) StructuredType(org.apache.flink.table.types.logical.StructuredType) Test(org.junit.Test)

Example 2 with StructuredType

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

the class LogicalTypeChecksTest method testIsCompositeTypeStructuredType.

@Test
public void testIsCompositeTypeStructuredType() {
    StructuredType logicalType = StructuredType.newBuilder(ObjectIdentifier.of("catalog", "database", "type")).attributes(Arrays.asList(new StructuredType.StructuredAttribute("f0", DataTypes.INT().getLogicalType()), new StructuredType.StructuredAttribute("f1", DataTypes.STRING().getLogicalType()))).build();
    List<DataType> fieldDataTypes = Arrays.asList(DataTypes.INT(), DataTypes.STRING());
    FieldsDataType dataType = new FieldsDataType(logicalType, fieldDataTypes);
    assertThat(LogicalTypeChecks.isCompositeType(dataType.getLogicalType())).isTrue();
}
Also used : FieldsDataType(org.apache.flink.table.types.FieldsDataType) DataType(org.apache.flink.table.types.DataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) StructuredType(org.apache.flink.table.types.logical.StructuredType) Test(org.junit.Test)

Example 3 with StructuredType

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

the class DataTypeExtractorTest method getComplexPojoDataType.

/**
 * Testing data type shared with the Scala tests.
 */
static DataType getComplexPojoDataType(Class<?> complexPojoClass, Class<?> simplePojoClass) {
    final StructuredType.Builder builder = StructuredType.newBuilder(complexPojoClass);
    builder.attributes(Arrays.asList(new StructuredAttribute("mapField", new MapType(VarCharType.STRING_TYPE, new IntType())), new StructuredAttribute("simplePojoField", getSimplePojoDataType(simplePojoClass).getLogicalType()), new StructuredAttribute("someObject", dummyRaw(Object.class).getLogicalType())));
    builder.setFinal(true);
    builder.setInstantiable(true);
    final StructuredType structuredType = builder.build();
    final List<DataType> fieldDataTypes = Arrays.asList(DataTypes.MAP(DataTypes.STRING(), DataTypes.INT()), getSimplePojoDataType(simplePojoClass), dummyRaw(Object.class));
    return new FieldsDataType(structuredType, complexPojoClass, 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) MapType(org.apache.flink.table.types.logical.MapType) StructuredType(org.apache.flink.table.types.logical.StructuredType) IntType(org.apache.flink.table.types.logical.IntType) BigIntType(org.apache.flink.table.types.logical.BigIntType)

Example 4 with StructuredType

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

the class DataTypeExtractorTest method getOuterTupleDataType.

private static DataType getOuterTupleDataType() {
    final StructuredType.Builder builder = StructuredType.newBuilder(Tuple2.class);
    builder.attributes(Arrays.asList(new StructuredAttribute("f0", new IntType()), new StructuredAttribute("f1", getInnerTupleDataType().getLogicalType())));
    builder.setFinal(true);
    builder.setInstantiable(true);
    final StructuredType structuredType = builder.build();
    final List<DataType> fieldDataTypes = Arrays.asList(DataTypes.INT(), getInnerTupleDataType());
    return new FieldsDataType(structuredType, Tuple2.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)

Example 5 with StructuredType

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

the class StructuredObjectConverter method createOrError.

/**
 * Creates a {@link DataStructureConverter} for the given structured type.
 *
 * <p>Note: We do not perform validation if data type and structured type implementation match.
 * This must have been done earlier in the {@link DataTypeFactory}.
 */
@SuppressWarnings("RedundantCast")
private static StructuredObjectConverter<?> createOrError(DataType dataType) {
    final List<DataType> fields = dataType.getChildren();
    final DataStructureConverter<Object, Object>[] fieldConverters = fields.stream().map(dt -> (DataStructureConverter<Object, Object>) DataStructureConverters.getConverter(dt)).toArray(DataStructureConverter[]::new);
    final RowData.FieldGetter[] fieldGetters = IntStream.range(0, fields.size()).mapToObj(pos -> RowData.createFieldGetter(fields.get(pos).getLogicalType(), pos)).toArray(RowData.FieldGetter[]::new);
    final Class<?>[] fieldClasses = fields.stream().map(DataType::getConversionClass).toArray(Class[]::new);
    final StructuredType structuredType = (StructuredType) dataType.getLogicalType();
    final Class<?> implementationClass = structuredType.getImplementationClass().orElseThrow(IllegalStateException::new);
    final int uniqueClassId = nextUniqueClassId.getAndIncrement();
    final String converterName = String.format("%s$%s$Converter", implementationClass.getName().replace('.', '$'), uniqueClassId);
    final String converterCode = generateCode(converterName, implementationClass, getFieldNames(structuredType).toArray(new String[0]), fieldClasses);
    return new StructuredObjectConverter<>(fieldConverters, fieldGetters, converterName, converterCode);
}
Also used : DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory) IntStream(java.util.stream.IntStream) DataType(org.apache.flink.table.types.DataType) RowData(org.apache.flink.table.data.RowData) CompileUtils(org.apache.flink.table.runtime.generated.CompileUtils) ExtractionUtils.getStructuredField(org.apache.flink.table.types.extraction.ExtractionUtils.getStructuredField) LogicalTypeChecks.getFieldNames(org.apache.flink.table.types.logical.utils.LogicalTypeChecks.getFieldNames) ExtractionUtils.getStructuredFieldGetter(org.apache.flink.table.types.extraction.ExtractionUtils.getStructuredFieldGetter) ExtractionUtils.isStructuredFieldDirectlyWritable(org.apache.flink.table.types.extraction.ExtractionUtils.isStructuredFieldDirectlyWritable) StructuredType(org.apache.flink.table.types.logical.StructuredType) TableException(org.apache.flink.table.api.TableException) Field(java.lang.reflect.Field) ExtractionUtils.hasInvokableConstructor(org.apache.flink.table.types.extraction.ExtractionUtils.hasInvokableConstructor) List(java.util.List) GenericRowData(org.apache.flink.table.data.GenericRowData) ExtractionUtils.getStructuredFieldSetter(org.apache.flink.table.types.extraction.ExtractionUtils.getStructuredFieldSetter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExtractionUtils.isStructuredFieldDirectlyReadable(org.apache.flink.table.types.extraction.ExtractionUtils.isStructuredFieldDirectlyReadable) ExtractionUtils.primitiveToWrapper(org.apache.flink.table.types.extraction.ExtractionUtils.primitiveToWrapper) Internal(org.apache.flink.annotation.Internal) Method(java.lang.reflect.Method) ExtractionUtils.getStructuredFieldGetter(org.apache.flink.table.types.extraction.ExtractionUtils.getStructuredFieldGetter) StructuredType(org.apache.flink.table.types.logical.StructuredType) RowData(org.apache.flink.table.data.RowData) GenericRowData(org.apache.flink.table.data.GenericRowData) DataType(org.apache.flink.table.types.DataType)

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