Search in sources :

Example 11 with FieldsDataType

use of org.apache.flink.table.types.FieldsDataType in project flink by apache.

the class DataTypeExtractorTest method getSimplePojoDataType.

/**
 * Testing data type shared with the Scala tests.
 */
static DataType getSimplePojoDataType(Class<?> simplePojoClass) {
    final StructuredType.Builder builder = StructuredType.newBuilder(simplePojoClass);
    builder.attributes(Arrays.asList(new StructuredAttribute("intField", new IntType(true)), new StructuredAttribute("primitiveBooleanField", new BooleanType(false)), new StructuredAttribute("primitiveIntField", new IntType(false)), new StructuredAttribute("stringField", VarCharType.STRING_TYPE)));
    builder.setFinal(true);
    builder.setInstantiable(true);
    final StructuredType structuredType = builder.build();
    final List<DataType> fieldDataTypes = Arrays.asList(DataTypes.INT(), DataTypes.BOOLEAN().notNull().bridgedTo(boolean.class), DataTypes.INT().notNull().bridgedTo(int.class), DataTypes.STRING());
    return new FieldsDataType(structuredType, simplePojoClass, fieldDataTypes);
}
Also used : FieldsDataType(org.apache.flink.table.types.FieldsDataType) BooleanType(org.apache.flink.table.types.logical.BooleanType) 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 12 with FieldsDataType

use of org.apache.flink.table.types.FieldsDataType in project flink by apache.

the class DataTypeExtractorTest method getPojoWithCustomOrderDataType.

/**
 * Testing data type shared with the Scala tests.
 */
public static DataType getPojoWithCustomOrderDataType(Class<?> pojoClass) {
    final StructuredType.Builder builder = StructuredType.newBuilder(pojoClass);
    builder.attributes(Arrays.asList(new StructuredAttribute("z", new BigIntType()), new StructuredAttribute("y", new BooleanType()), new StructuredAttribute("x", new IntType())));
    builder.setFinal(true);
    builder.setInstantiable(true);
    final StructuredType structuredType = builder.build();
    final List<DataType> fieldDataTypes = Arrays.asList(DataTypes.BIGINT(), DataTypes.BOOLEAN(), DataTypes.INT());
    return new FieldsDataType(structuredType, pojoClass, fieldDataTypes);
}
Also used : FieldsDataType(org.apache.flink.table.types.FieldsDataType) BooleanType(org.apache.flink.table.types.logical.BooleanType) StructuredAttribute(org.apache.flink.table.types.logical.StructuredType.StructuredAttribute) BigIntType(org.apache.flink.table.types.logical.BigIntType) 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 13 with FieldsDataType

use of org.apache.flink.table.types.FieldsDataType 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 14 with FieldsDataType

use of org.apache.flink.table.types.FieldsDataType in project flink by apache.

the class CommonPythonUtil method extractDataViewSpecs.

public static DataViewSpec[] extractDataViewSpecs(int index, DataType accType) {
    if (!(accType instanceof FieldsDataType)) {
        return new DataViewSpec[0];
    }
    FieldsDataType compositeAccType = (FieldsDataType) accType;
    if (includesDataView(compositeAccType)) {
        LogicalType logicalType = compositeAccType.getLogicalType();
        if (logicalType instanceof RowType) {
            List<DataType> childrenDataTypes = compositeAccType.getChildren();
            return IntStream.range(0, childrenDataTypes.size()).mapToObj(i -> {
                DataType childDataType = childrenDataTypes.get(i);
                LogicalType childLogicalType = childDataType.getLogicalType();
                if ((childLogicalType instanceof RowType) && includesDataView((FieldsDataType) childDataType)) {
                    throw new TableException("For Python AggregateFunction, DataView cannot be used in the" + " nested columns of the accumulator. ");
                } else if ((childLogicalType instanceof StructuredType) && ListView.class.isAssignableFrom(((StructuredType) childLogicalType).getImplementationClass().get())) {
                    return new ListViewSpec("agg" + index + "$" + ((RowType) logicalType).getFieldNames().get(i), i, childDataType.getChildren().get(0));
                } else if ((childLogicalType instanceof StructuredType) && MapView.class.isAssignableFrom(((StructuredType) childLogicalType).getImplementationClass().get())) {
                    return new MapViewSpec("agg" + index + "$" + ((RowType) logicalType).getFieldNames().get(i), i, childDataType.getChildren().get(0), false);
                }
                return null;
            }).filter(Objects::nonNull).toArray(DataViewSpec[]::new);
        } else {
            throw new TableException("For Python AggregateFunction you can only use DataView in " + "Row type.");
        }
    } else {
        return new DataViewSpec[0];
    }
}
Also used : FieldsDataType(org.apache.flink.table.types.FieldsDataType) TableException(org.apache.flink.table.api.TableException) MapViewSpec(org.apache.flink.table.runtime.dataview.MapViewSpec) DataViewSpec(org.apache.flink.table.runtime.dataview.DataViewSpec) ListViewSpec(org.apache.flink.table.runtime.dataview.ListViewSpec) LogicalType(org.apache.flink.table.types.logical.LogicalType) RowType(org.apache.flink.table.types.logical.RowType) DataType(org.apache.flink.table.types.DataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) MapView(org.apache.flink.table.api.dataview.MapView) StructuredType(org.apache.flink.table.types.logical.StructuredType)

Aggregations

DataType (org.apache.flink.table.types.DataType)14 FieldsDataType (org.apache.flink.table.types.FieldsDataType)14 StructuredType (org.apache.flink.table.types.logical.StructuredType)11 StructuredAttribute (org.apache.flink.table.types.logical.StructuredType.StructuredAttribute)8 BigIntType (org.apache.flink.table.types.logical.BigIntType)6 IntType (org.apache.flink.table.types.logical.IntType)6 LogicalType (org.apache.flink.table.types.logical.LogicalType)5 CollectionDataType (org.apache.flink.table.types.CollectionDataType)4 KeyValueDataType (org.apache.flink.table.types.KeyValueDataType)4 BooleanType (org.apache.flink.table.types.logical.BooleanType)4 MapType (org.apache.flink.table.types.logical.MapType)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 IntStream (java.util.stream.IntStream)3 Nullable (javax.annotation.Nullable)3 ResolvedSchema (org.apache.flink.table.catalog.ResolvedSchema)3 Test (org.junit.Test)3 Timestamp (java.sql.Timestamp)2 LocalDateTime (java.time.LocalDateTime)2 Arrays (java.util.Arrays)2