Search in sources :

Example 1 with AtomicDataType

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

the class AvroSchemaConverterTest method validateUserSchema.

private void validateUserSchema(DataType actual) {
    final DataType address = DataTypes.ROW(DataTypes.FIELD("num", DataTypes.INT().notNull()), DataTypes.FIELD("street", DataTypes.STRING().notNull()), DataTypes.FIELD("city", DataTypes.STRING().notNull()), DataTypes.FIELD("state", DataTypes.STRING().notNull()), DataTypes.FIELD("zip", DataTypes.STRING().notNull()));
    final DataType user = DataTypes.ROW(DataTypes.FIELD("name", DataTypes.STRING().notNull()), DataTypes.FIELD("favorite_number", DataTypes.INT()), DataTypes.FIELD("favorite_color", DataTypes.STRING()), DataTypes.FIELD("type_long_test", DataTypes.BIGINT()), DataTypes.FIELD("type_double_test", DataTypes.DOUBLE().notNull()), DataTypes.FIELD("type_null_test", DataTypes.NULL()), DataTypes.FIELD("type_bool_test", DataTypes.BOOLEAN().notNull()), DataTypes.FIELD("type_array_string", DataTypes.ARRAY(DataTypes.STRING().notNull()).notNull()), DataTypes.FIELD("type_array_boolean", DataTypes.ARRAY(DataTypes.BOOLEAN().notNull()).notNull()), DataTypes.FIELD("type_nullable_array", DataTypes.ARRAY(DataTypes.STRING().notNull())), DataTypes.FIELD("type_enum", DataTypes.STRING().notNull()), DataTypes.FIELD("type_map", DataTypes.MAP(DataTypes.STRING().notNull(), DataTypes.BIGINT().notNull()).notNull()), DataTypes.FIELD("type_fixed", DataTypes.VARBINARY(16)), DataTypes.FIELD("type_union", new AtomicDataType(new TypeInformationRawType<>(false, Types.GENERIC(Object.class)), Object.class)), DataTypes.FIELD("type_nested", address), DataTypes.FIELD("type_bytes", DataTypes.BYTES().notNull()), DataTypes.FIELD("type_date", DataTypes.DATE().notNull()), DataTypes.FIELD("type_time_millis", DataTypes.TIME(3).notNull()), DataTypes.FIELD("type_time_micros", DataTypes.TIME(6).notNull()), DataTypes.FIELD("type_timestamp_millis", DataTypes.TIMESTAMP(3).notNull()), DataTypes.FIELD("type_timestamp_micros", DataTypes.TIMESTAMP(6).notNull()), DataTypes.FIELD("type_decimal_bytes", DataTypes.DECIMAL(4, 2).notNull()), DataTypes.FIELD("type_decimal_fixed", DataTypes.DECIMAL(4, 2).notNull())).notNull();
    assertEquals(user, actual);
}
Also used : TypeInformationRawType(org.apache.flink.table.types.logical.TypeInformationRawType) AtomicDataType(org.apache.flink.table.types.AtomicDataType) DataType(org.apache.flink.table.types.DataType) AtomicDataType(org.apache.flink.table.types.AtomicDataType)

Example 2 with AtomicDataType

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

the class AvroSchemaConverter method convertToDataType.

private static DataType convertToDataType(Schema schema) {
    switch(schema.getType()) {
        case RECORD:
            final List<Schema.Field> schemaFields = schema.getFields();
            final DataTypes.Field[] fields = new DataTypes.Field[schemaFields.size()];
            for (int i = 0; i < schemaFields.size(); i++) {
                final Schema.Field field = schemaFields.get(i);
                fields[i] = DataTypes.FIELD(field.name(), convertToDataType(field.schema()));
            }
            return DataTypes.ROW(fields).notNull();
        case ENUM:
            return DataTypes.STRING().notNull();
        case ARRAY:
            return DataTypes.ARRAY(convertToDataType(schema.getElementType())).notNull();
        case MAP:
            return DataTypes.MAP(DataTypes.STRING().notNull(), convertToDataType(schema.getValueType())).notNull();
        case UNION:
            final Schema actualSchema;
            final boolean nullable;
            if (schema.getTypes().size() == 2 && schema.getTypes().get(0).getType() == Schema.Type.NULL) {
                actualSchema = schema.getTypes().get(1);
                nullable = true;
            } else if (schema.getTypes().size() == 2 && schema.getTypes().get(1).getType() == Schema.Type.NULL) {
                actualSchema = schema.getTypes().get(0);
                nullable = true;
            } else if (schema.getTypes().size() == 1) {
                actualSchema = schema.getTypes().get(0);
                nullable = false;
            } else {
                // use Kryo for serialization
                return new AtomicDataType(new TypeInformationRawType<>(false, Types.GENERIC(Object.class)));
            }
            DataType converted = convertToDataType(actualSchema);
            return nullable ? converted.nullable() : converted;
        case FIXED:
            // logical decimal type
            if (schema.getLogicalType() instanceof LogicalTypes.Decimal) {
                final LogicalTypes.Decimal decimalType = (LogicalTypes.Decimal) schema.getLogicalType();
                return DataTypes.DECIMAL(decimalType.getPrecision(), decimalType.getScale()).notNull();
            }
            // convert fixed size binary data to primitive byte arrays
            return DataTypes.VARBINARY(schema.getFixedSize()).notNull();
        case STRING:
            // convert Avro's Utf8/CharSequence to String
            return DataTypes.STRING().notNull();
        case BYTES:
            // logical decimal type
            if (schema.getLogicalType() instanceof LogicalTypes.Decimal) {
                final LogicalTypes.Decimal decimalType = (LogicalTypes.Decimal) schema.getLogicalType();
                return DataTypes.DECIMAL(decimalType.getPrecision(), decimalType.getScale()).notNull();
            }
            return DataTypes.BYTES().notNull();
        case INT:
            // logical date and time type
            final org.apache.avro.LogicalType logicalType = schema.getLogicalType();
            if (logicalType == LogicalTypes.date()) {
                return DataTypes.DATE().notNull();
            } else if (logicalType == LogicalTypes.timeMillis()) {
                return DataTypes.TIME(3).notNull();
            }
            return DataTypes.INT().notNull();
        case LONG:
            // logical timestamp type
            if (schema.getLogicalType() == LogicalTypes.timestampMillis()) {
                return DataTypes.TIMESTAMP(3).notNull();
            } else if (schema.getLogicalType() == LogicalTypes.timestampMicros()) {
                return DataTypes.TIMESTAMP(6).notNull();
            } else if (schema.getLogicalType() == LogicalTypes.timeMillis()) {
                return DataTypes.TIME(3).notNull();
            } else if (schema.getLogicalType() == LogicalTypes.timeMicros()) {
                return DataTypes.TIME(6).notNull();
            }
            return DataTypes.BIGINT().notNull();
        case FLOAT:
            return DataTypes.FLOAT().notNull();
        case DOUBLE:
            return DataTypes.DOUBLE().notNull();
        case BOOLEAN:
            return DataTypes.BOOLEAN().notNull();
        case NULL:
            return DataTypes.NULL();
    }
    throw new IllegalArgumentException("Unsupported Avro type '" + schema.getType() + "'.");
}
Also used : AtomicDataType(org.apache.flink.table.types.AtomicDataType) Schema(org.apache.avro.Schema) AvroRowDeserializationSchema(org.apache.flink.formats.avro.AvroRowDeserializationSchema) AvroRowSerializationSchema(org.apache.flink.formats.avro.AvroRowSerializationSchema) LogicalTypes(org.apache.avro.LogicalTypes) DataType(org.apache.flink.table.types.DataType) AtomicDataType(org.apache.flink.table.types.AtomicDataType)

Example 3 with AtomicDataType

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

the class LogicalWindowJsonDeserializer method deserializeFieldReferenceExpression.

private FieldReferenceExpression deserializeFieldReferenceExpression(JsonNode input, JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    String name = input.get(FIELD_NAME_FIELD_NAME).asText();
    int fieldIndex = input.get(FIELD_NAME_FIELD_INDEX).asInt();
    int inputIndex = input.get(FIELD_NAME_INPUT_INDEX).asInt();
    LogicalType type = deserializationContext.readValue(input.get(FIELD_NAME_FIELD_TYPE).traverse(jsonParser.getCodec()), LogicalType.class);
    return new FieldReferenceExpression(name, new AtomicDataType(type), inputIndex, fieldIndex);
}
Also used : AtomicDataType(org.apache.flink.table.types.AtomicDataType) LogicalType(org.apache.flink.table.types.logical.LogicalType) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression)

Example 4 with AtomicDataType

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

the class TypeTransformationsTest method testLegacyRawToTypeInfoRaw.

@Test
public void testLegacyRawToTypeInfoRaw() {
    DataType dataType = DataTypes.ROW(DataTypes.FIELD("a", DataTypes.STRING()), DataTypes.FIELD("b", DataTypes.DECIMAL(10, 3)), DataTypes.FIELD("c", createLegacyRaw()), DataTypes.FIELD("d", DataTypes.ARRAY(createLegacyRaw())));
    TypeInformation<TypeTransformationsTest> typeInformation = TypeExtractor.getForClass(TypeTransformationsTest.class);
    DataType rawDataType = new AtomicDataType(new TypeInformationRawType<>(typeInformation));
    DataType expected = DataTypes.ROW(DataTypes.FIELD("a", DataTypes.STRING()), DataTypes.FIELD("b", DataTypes.DECIMAL(10, 3)), DataTypes.FIELD("c", rawDataType), DataTypes.FIELD("d", DataTypes.ARRAY(rawDataType)));
    assertEquals(expected, DataTypeUtils.transform(dataType, legacyRawToTypeInfoRaw()));
}
Also used : AtomicDataType(org.apache.flink.table.types.AtomicDataType) DataType(org.apache.flink.table.types.DataType) AtomicDataType(org.apache.flink.table.types.AtomicDataType) Test(org.junit.Test)

Example 5 with AtomicDataType

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

the class LegacyRawTypeTransformation method transform.

@Override
public DataType transform(DataType typeToTransform) {
    LogicalType logicalType = typeToTransform.getLogicalType();
    if (logicalType instanceof LegacyTypeInformationType && logicalType.getTypeRoot() == LogicalTypeRoot.RAW) {
        TypeInformation<?> typeInfo = ((LegacyTypeInformationType<?>) logicalType).getTypeInformation();
        DataType rawDataType = new AtomicDataType(new TypeInformationRawType<>(typeInfo));
        return logicalType.isNullable() ? rawDataType : rawDataType.notNull();
    }
    return typeToTransform;
}
Also used : AtomicDataType(org.apache.flink.table.types.AtomicDataType) LogicalType(org.apache.flink.table.types.logical.LogicalType) DataType(org.apache.flink.table.types.DataType) AtomicDataType(org.apache.flink.table.types.AtomicDataType) LegacyTypeInformationType(org.apache.flink.table.types.logical.LegacyTypeInformationType)

Aggregations

AtomicDataType (org.apache.flink.table.types.AtomicDataType)5 DataType (org.apache.flink.table.types.DataType)4 LogicalType (org.apache.flink.table.types.logical.LogicalType)2 LogicalTypes (org.apache.avro.LogicalTypes)1 Schema (org.apache.avro.Schema)1 AvroRowDeserializationSchema (org.apache.flink.formats.avro.AvroRowDeserializationSchema)1 AvroRowSerializationSchema (org.apache.flink.formats.avro.AvroRowSerializationSchema)1 FieldReferenceExpression (org.apache.flink.table.expressions.FieldReferenceExpression)1 LegacyTypeInformationType (org.apache.flink.table.types.logical.LegacyTypeInformationType)1 TypeInformationRawType (org.apache.flink.table.types.logical.TypeInformationRawType)1 Test (org.junit.Test)1