Search in sources :

Example 6 with StructuredAttribute

use of org.apache.flink.table.types.logical.StructuredType.StructuredAttribute 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 7 with StructuredAttribute

use of org.apache.flink.table.types.logical.StructuredType.StructuredAttribute 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 8 with StructuredAttribute

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

the class LogicalTypeJsonDeserializer method deserializeStructuredTypeFromPlan.

private static LogicalType deserializeStructuredTypeFromPlan(JsonNode logicalTypeNode, SerdeContext serdeContext) {
    final ObjectIdentifier identifier;
    if (logicalTypeNode.has(FIELD_NAME_OBJECT_IDENTIFIER)) {
        identifier = ObjectIdentifierJsonDeserializer.deserialize(logicalTypeNode.get(FIELD_NAME_OBJECT_IDENTIFIER).asText(), serdeContext);
    } else {
        identifier = null;
    }
    final Class<?> implementationClass;
    if (logicalTypeNode.has(FIELD_NAME_IMPLEMENTATION_CLASS)) {
        implementationClass = loadClass(logicalTypeNode.get(FIELD_NAME_IMPLEMENTATION_CLASS).asText(), serdeContext, "structured type");
    } else {
        implementationClass = null;
    }
    final StructuredType.Builder builder;
    if (identifier != null && implementationClass != null) {
        builder = StructuredType.newBuilder(identifier, implementationClass);
    } else if (identifier != null) {
        builder = StructuredType.newBuilder(identifier);
    } else {
        builder = StructuredType.newBuilder(implementationClass);
    }
    if (logicalTypeNode.has(FIELD_NAME_DESCRIPTION)) {
        builder.description(logicalTypeNode.get(FIELD_NAME_FIELD_DESCRIPTION).asText());
    }
    final ArrayNode attributeNodes = (ArrayNode) logicalTypeNode.get(FIELD_NAME_ATTRIBUTES);
    final List<StructuredAttribute> attributes = new ArrayList<>();
    for (JsonNode attributeNode : attributeNodes) {
        final String attributeName = attributeNode.get(FIELD_NAME_ATTRIBUTE_NAME).asText();
        final LogicalType attributeType = deserialize(attributeNode.get(FIELD_NAME_ATTRIBUTE_TYPE), serdeContext);
        final String attributeDescription;
        if (attributeNode.has(FIELD_NAME_ATTRIBUTE_DESCRIPTION)) {
            attributeDescription = attributeNode.get(FIELD_NAME_ATTRIBUTE_DESCRIPTION).asText();
        } else {
            attributeDescription = null;
        }
        attributes.add(new StructuredAttribute(attributeName, attributeType, attributeDescription));
    }
    builder.attributes(attributes);
    if (logicalTypeNode.has(FIELD_NAME_FINAL)) {
        builder.setFinal(logicalTypeNode.get(FIELD_NAME_FINAL).asBoolean());
    }
    if (logicalTypeNode.has(FIELD_NAME_INSTANTIABLE)) {
        builder.setInstantiable(logicalTypeNode.get(FIELD_NAME_INSTANTIABLE).asBoolean());
    }
    if (logicalTypeNode.has(FIELD_NAME_COMPARISON)) {
        builder.comparison(StructuredComparison.valueOf(logicalTypeNode.get(FIELD_NAME_COMPARISON).asText()));
    }
    if (logicalTypeNode.has(FIELD_NAME_SUPER_TYPE)) {
        final StructuredType superType = (StructuredType) deserialize(logicalTypeNode.get(FIELD_NAME_SUPER_TYPE), serdeContext);
        builder.superType(superType);
    }
    return builder.build();
}
Also used : ArrayList(java.util.ArrayList) StructuredAttribute(org.apache.flink.table.types.logical.StructuredType.StructuredAttribute) LogicalType(org.apache.flink.table.types.logical.LogicalType) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) ArrayNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier) StructuredType(org.apache.flink.table.types.logical.StructuredType)

Example 9 with StructuredAttribute

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

the class LogicalTypeJsonSerializer method serializeStructuredType.

private static void serializeStructuredType(StructuredType structuredType, JsonGenerator jsonGenerator, SerializerProvider serializerProvider, boolean serializeCatalogObjects) throws IOException {
    if (structuredType.getObjectIdentifier().isPresent()) {
        serializerProvider.defaultSerializeField(FIELD_NAME_OBJECT_IDENTIFIER, structuredType.getObjectIdentifier().get(), jsonGenerator);
    }
    if (structuredType.getDescription().isPresent()) {
        jsonGenerator.writeStringField(FIELD_NAME_DESCRIPTION, structuredType.getDescription().get());
    }
    if (structuredType.getImplementationClass().isPresent()) {
        serializerProvider.defaultSerializeField(FIELD_NAME_IMPLEMENTATION_CLASS, structuredType.getImplementationClass().get(), jsonGenerator);
    }
    jsonGenerator.writeFieldName(FIELD_NAME_ATTRIBUTES);
    jsonGenerator.writeStartArray();
    for (StructuredAttribute attribute : structuredType.getAttributes()) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField(FIELD_NAME_ATTRIBUTE_NAME, attribute.getName());
        jsonGenerator.writeFieldName(FIELD_NAME_ATTRIBUTE_TYPE);
        serializeInternal(attribute.getType(), jsonGenerator, serializerProvider, serializeCatalogObjects);
        if (attribute.getDescription().isPresent()) {
            jsonGenerator.writeStringField(FIELD_NAME_ATTRIBUTE_DESCRIPTION, attribute.getDescription().get());
        }
        jsonGenerator.writeEndObject();
    }
    jsonGenerator.writeEndArray();
    if (!structuredType.isFinal()) {
        jsonGenerator.writeBooleanField(FIELD_NAME_FINAL, false);
    }
    if (!structuredType.isInstantiable()) {
        jsonGenerator.writeBooleanField(FIELD_NAME_INSTANTIABLE, false);
    }
    if (structuredType.getComparison() != StructuredComparison.NONE) {
        jsonGenerator.writeStringField(FIELD_NAME_COMPARISON, structuredType.getComparison().name());
    }
    if (structuredType.getSuperType().isPresent()) {
        serializerProvider.defaultSerializeField(FIELD_NAME_SUPER_TYPE, structuredType.getSuperType().get(), jsonGenerator);
    }
}
Also used : StructuredAttribute(org.apache.flink.table.types.logical.StructuredType.StructuredAttribute)

Aggregations

StructuredAttribute (org.apache.flink.table.types.logical.StructuredType.StructuredAttribute)9 StructuredType (org.apache.flink.table.types.logical.StructuredType)7 DataType (org.apache.flink.table.types.DataType)6 FieldsDataType (org.apache.flink.table.types.FieldsDataType)6 BigIntType (org.apache.flink.table.types.logical.BigIntType)5 IntType (org.apache.flink.table.types.logical.IntType)5 BooleanType (org.apache.flink.table.types.logical.BooleanType)3 ArrayList (java.util.ArrayList)2 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)1 RelDataTypeFieldImpl (org.apache.calcite.rel.type.RelDataTypeFieldImpl)1 JsonNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectIdentifier (org.apache.flink.table.catalog.ObjectIdentifier)1 LogicalType (org.apache.flink.table.types.logical.LogicalType)1 MapType (org.apache.flink.table.types.logical.MapType)1