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