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);
}
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() + "'.");
}
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);
}
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()));
}
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;
}
Aggregations