Search in sources :

Example 1 with OneOfType

use of org.apache.beam.sdk.schemas.logicaltypes.OneOfType in project beam by apache.

the class AvroUtils method getFieldSchema.

private static org.apache.avro.Schema getFieldSchema(Schema.FieldType fieldType, String fieldName, String namespace) {
    org.apache.avro.Schema baseType;
    switch(fieldType.getTypeName()) {
        case BYTE:
        case INT16:
        case INT32:
            baseType = org.apache.avro.Schema.create(Type.INT);
            break;
        case INT64:
            baseType = org.apache.avro.Schema.create(Type.LONG);
            break;
        case DECIMAL:
            baseType = LogicalTypes.decimal(Integer.MAX_VALUE).addToSchema(org.apache.avro.Schema.create(Type.BYTES));
            break;
        case FLOAT:
            baseType = org.apache.avro.Schema.create(Type.FLOAT);
            break;
        case DOUBLE:
            baseType = org.apache.avro.Schema.create(Type.DOUBLE);
            break;
        case STRING:
            baseType = org.apache.avro.Schema.create(Type.STRING);
            break;
        case DATETIME:
            // TODO: There is a desire to move Beam schema DATETIME to a micros representation. When
            // this is done, this logical type needs to be changed.
            baseType = LogicalTypes.timestampMillis().addToSchema(org.apache.avro.Schema.create(Type.LONG));
            break;
        case BOOLEAN:
            baseType = org.apache.avro.Schema.create(Type.BOOLEAN);
            break;
        case BYTES:
            baseType = org.apache.avro.Schema.create(Type.BYTES);
            break;
        case LOGICAL_TYPE:
            switch(fieldType.getLogicalType().getIdentifier()) {
                case FixedBytes.IDENTIFIER:
                    FixedBytesField fixedBytesField = checkNotNull(FixedBytesField.fromBeamFieldType(fieldType));
                    baseType = fixedBytesField.toAvroType("fixed", namespace + "." + fieldName);
                    break;
                case EnumerationType.IDENTIFIER:
                    EnumerationType enumerationType = fieldType.getLogicalType(EnumerationType.class);
                    baseType = org.apache.avro.Schema.createEnum(fieldName, "", "", enumerationType.getValues());
                    break;
                case OneOfType.IDENTIFIER:
                    OneOfType oneOfType = fieldType.getLogicalType(OneOfType.class);
                    baseType = org.apache.avro.Schema.createUnion(oneOfType.getOneOfSchema().getFields().stream().map(x -> getFieldSchema(x.getType(), x.getName(), namespace)).collect(Collectors.toList()));
                    break;
                case "CHAR":
                case "NCHAR":
                    baseType = buildHiveLogicalTypeSchema("char", (int) fieldType.getLogicalType().getArgument());
                    break;
                case "NVARCHAR":
                case "VARCHAR":
                case "LONGNVARCHAR":
                case "LONGVARCHAR":
                    baseType = buildHiveLogicalTypeSchema("varchar", (int) fieldType.getLogicalType().getArgument());
                    break;
                case "DATE":
                    baseType = LogicalTypes.date().addToSchema(org.apache.avro.Schema.create(Type.INT));
                    break;
                case "TIME":
                    baseType = LogicalTypes.timeMillis().addToSchema(org.apache.avro.Schema.create(Type.INT));
                    break;
                default:
                    throw new RuntimeException("Unhandled logical type " + fieldType.getLogicalType().getIdentifier());
            }
            break;
        case ARRAY:
        case ITERABLE:
            baseType = org.apache.avro.Schema.createArray(getFieldSchema(fieldType.getCollectionElementType(), fieldName, namespace));
            break;
        case MAP:
            if (fieldType.getMapKeyType().getTypeName().isStringType()) {
                // Avro only supports string keys in maps.
                baseType = org.apache.avro.Schema.createMap(getFieldSchema(fieldType.getMapValueType(), fieldName, namespace));
            } else {
                throw new IllegalArgumentException("Avro only supports maps with string keys");
            }
            break;
        case ROW:
            baseType = toAvroSchema(fieldType.getRowSchema(), fieldName, namespace);
            break;
        default:
            throw new IllegalArgumentException("Unexpected type " + fieldType);
    }
    return fieldType.getNullable() ? ReflectData.makeNullable(baseType) : baseType;
}
Also used : OneOfType(org.apache.beam.sdk.schemas.logicaltypes.OneOfType) TypeCasting(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.assign.TypeCasting) ObjectInputStream(java.io.ObjectInputStream) SerializableFunction(org.apache.beam.sdk.transforms.SerializableFunction) SimpleFunction(org.apache.beam.sdk.transforms.SimpleFunction) ByteBuffer(java.nio.ByteBuffer) AvroName(org.apache.avro.reflect.AvroName) BigDecimal(java.math.BigDecimal) ByteArrayInputStream(java.io.ByteArrayInputStream) Strings(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Strings) FixedBytes(org.apache.beam.sdk.schemas.logicaltypes.FixedBytes) JodaTimestampConversion(org.apache.beam.sdk.coders.AvroCoder.JodaTimestampConversion) Map(java.util.Map) Iterables(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables) EnumerationType(org.apache.beam.sdk.schemas.logicaltypes.EnumerationType) Days(org.joda.time.Days) Method(java.lang.reflect.Method) SpecificData(org.apache.avro.specific.SpecificData) Conversions(org.apache.avro.Conversions) Utf8(org.apache.avro.util.Utf8) TypeCreation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.TypeCreation) SchemaCoder(org.apache.beam.sdk.schemas.SchemaCoder) ReadableInstant(org.joda.time.ReadableInstant) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) Collectors(java.util.stream.Collectors) AvroRecordSchema(org.apache.beam.sdk.schemas.AvroRecordSchema) TypeConversion(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.TypeConversion) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) List(java.util.List) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) Preconditions.checkNotNull(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull) TypeDescriptor(org.apache.beam.sdk.values.TypeDescriptor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ConvertType(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.ConvertType) Experimental(org.apache.beam.sdk.annotations.Experimental) CaseFormat(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.CaseFormat) Duration(org.joda.time.Duration) HashMap(java.util.HashMap) SpecificRecord(org.apache.avro.specific.SpecificRecord) ArrayList(java.util.ArrayList) GenericData(org.apache.avro.generic.GenericData) ReflectData(org.apache.avro.reflect.ReflectData) LogicalTypes(org.apache.avro.LogicalTypes) Kind(org.apache.beam.sdk.annotations.Experimental.Kind) SchemaUserTypeCreator(org.apache.beam.sdk.schemas.SchemaUserTypeCreator) Preconditions.checkArgument(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument) Maps(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Maps) AvroIgnore(org.apache.avro.reflect.AvroIgnore) ObjectOutputStream(java.io.ObjectOutputStream) FieldValueTypeInformation(org.apache.beam.sdk.schemas.FieldValueTypeInformation) ForLoadedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription.ForLoadedType) Row(org.apache.beam.sdk.values.Row) Nonnull(javax.annotation.Nonnull) Type(org.apache.avro.Schema.Type) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Field(org.apache.beam.sdk.schemas.Schema.Field) GenericRecord(org.apache.avro.generic.GenericRecord) Duplication(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.Duplication) GenericFixed(org.apache.avro.generic.GenericFixed) LogicalType(org.apache.avro.LogicalType) Compound(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation.Compound) ConvertValueForGetter(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.ConvertValueForGetter) ElementMatchers(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.matcher.ElementMatchers) Lists(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists) IOException(java.io.IOException) AvroCoder(org.apache.beam.sdk.coders.AvroCoder) Schema(org.apache.beam.sdk.schemas.Schema) TypeName(org.apache.beam.sdk.schemas.Schema.TypeName) StackManipulation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation) MethodInvocation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodInvocation) ConvertValueForSetter(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.ConvertValueForSetter) Instant(org.joda.time.Instant) TypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.TypeConversionsFactory) AvroRuntimeException(org.apache.avro.AvroRuntimeException) FieldValueGetter(org.apache.beam.sdk.schemas.FieldValueGetter) AvroRuntimeException(org.apache.avro.AvroRuntimeException) EnumerationType(org.apache.beam.sdk.schemas.logicaltypes.EnumerationType) OneOfType(org.apache.beam.sdk.schemas.logicaltypes.OneOfType)

Example 2 with OneOfType

use of org.apache.beam.sdk.schemas.logicaltypes.OneOfType in project beam by apache.

the class ProtoByteBuddyUtils method getProtoFieldValueSetter.

private static <ProtoBuilderT extends MessageLite.Builder> FieldValueSetter<ProtoBuilderT, Object> getProtoFieldValueSetter(Field field, Multimap<String, Method> methods, Class<ProtoBuilderT> builderClass) {
    if (field.getType().isLogicalType(OneOfType.IDENTIFIER)) {
        OneOfType oneOfType = field.getType().getLogicalType(OneOfType.class);
        TreeMap<Integer, FieldValueSetter<ProtoBuilderT, Object>> oneOfSetters = Maps.newTreeMap();
        for (Field oneOfField : oneOfType.getOneOfSchema().getFields()) {
            FieldValueSetter setter = getProtoFieldValueSetter(oneOfField, methods, builderClass);
            oneOfSetters.put(getFieldNumber(oneOfField), setter);
        }
        return createOneOfSetter(field.getName(), oneOfSetters, builderClass);
    } else {
        Method method = getProtoSetter(methods, field.getName(), field.getType());
        return JavaBeanUtils.createSetter(FieldValueTypeInformation.forSetter(method, protoSetterPrefix(field.getType())), new ProtoTypeConversionsFactory());
    }
}
Also used : Field(org.apache.beam.sdk.schemas.Schema.Field) Method(java.lang.reflect.Method) ForLoadedMethod(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription.ForLoadedMethod) FieldValueSetter(org.apache.beam.sdk.schemas.FieldValueSetter) OneOfType(org.apache.beam.sdk.schemas.logicaltypes.OneOfType)

Example 3 with OneOfType

use of org.apache.beam.sdk.schemas.logicaltypes.OneOfType in project beam by apache.

the class AvroUtilsTest method testUnionFieldInAvroSchema.

@Test
public void testUnionFieldInAvroSchema() {
    List<org.apache.avro.Schema.Field> fields = Lists.newArrayList();
    List<org.apache.avro.Schema> unionFields = Lists.newArrayList();
    unionFields.add(org.apache.avro.Schema.create(Type.INT));
    unionFields.add(org.apache.avro.Schema.create(Type.STRING));
    fields.add(new org.apache.avro.Schema.Field("union", org.apache.avro.Schema.createUnion(unionFields), "", null));
    org.apache.avro.Schema avroSchema = org.apache.avro.Schema.createRecord("topLevelRecord", null, null, false, fields);
    OneOfType oneOfType = OneOfType.create(Field.of("int", FieldType.INT32), Field.of("string", FieldType.STRING));
    Schema expectedSchema = Schema.builder().addLogicalTypeField("union", oneOfType).build();
    assertEquals(expectedSchema, AvroUtils.toBeamSchema(avroSchema));
    GenericRecord genericRecord = new GenericRecordBuilder(avroSchema).set("union", 23423).build();
    Row expectedRow = Row.withSchema(expectedSchema).addValue(oneOfType.createValue(0, 23423)).build();
    assertEquals(expectedRow, AvroUtils.toBeamRowStrict(genericRecord, expectedSchema));
}
Also used : Schema(org.apache.beam.sdk.schemas.Schema) Field(org.apache.beam.sdk.schemas.Schema.Field) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) Row(org.apache.beam.sdk.values.Row) GenericRecord(org.apache.avro.generic.GenericRecord) OneOfType(org.apache.beam.sdk.schemas.logicaltypes.OneOfType) Test(org.junit.Test)

Example 4 with OneOfType

use of org.apache.beam.sdk.schemas.logicaltypes.OneOfType in project beam by apache.

the class AvroUtilsTest method testUnionFieldInBeamSchema.

@Test
public void testUnionFieldInBeamSchema() {
    OneOfType oneOfType = OneOfType.create(Field.of("int", FieldType.INT32), Field.of("string", FieldType.STRING));
    Schema beamSchema = Schema.builder().addLogicalTypeField("union", oneOfType).build();
    List<org.apache.avro.Schema.Field> fields = Lists.newArrayList();
    List<org.apache.avro.Schema> unionFields = Lists.newArrayList();
    unionFields.add(org.apache.avro.Schema.create(Type.INT));
    unionFields.add(org.apache.avro.Schema.create(Type.STRING));
    fields.add(new org.apache.avro.Schema.Field("union", org.apache.avro.Schema.createUnion(unionFields), "", null));
    org.apache.avro.Schema avroSchema = org.apache.avro.Schema.createRecord("topLevelRecord", null, null, false, fields);
    GenericRecord expectedGenericRecord = new GenericRecordBuilder(avroSchema).set("union", 23423).build();
    Row row = Row.withSchema(beamSchema).addValue(oneOfType.createValue(0, 23423)).build();
    assertEquals(expectedGenericRecord, AvroUtils.toGenericRecord(row, avroSchema));
}
Also used : Schema(org.apache.beam.sdk.schemas.Schema) Field(org.apache.beam.sdk.schemas.Schema.Field) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) Row(org.apache.beam.sdk.values.Row) GenericRecord(org.apache.avro.generic.GenericRecord) OneOfType(org.apache.beam.sdk.schemas.logicaltypes.OneOfType) Test(org.junit.Test)

Example 5 with OneOfType

use of org.apache.beam.sdk.schemas.logicaltypes.OneOfType in project beam by apache.

the class FromRowUsingCreator method fromValue.

@SuppressWarnings("unchecked")
@Nullable
private <ValueT> ValueT fromValue(FieldType type, ValueT value, Type fieldType, FieldValueTypeInformation fieldValueTypeInformation, Factory<List<FieldValueTypeInformation>> typeFactory) {
    FieldValueTypeInformation elementType = fieldValueTypeInformation.getElementType();
    FieldValueTypeInformation keyType = fieldValueTypeInformation.getMapKeyType();
    FieldValueTypeInformation valueType = fieldValueTypeInformation.getMapValueType();
    if (value == null) {
        return null;
    }
    if (TypeName.ROW.equals(type.getTypeName())) {
        return (ValueT) fromRow((Row) value, (Class) fieldType, typeFactory);
    } else if (TypeName.ARRAY.equals(type.getTypeName())) {
        return (ValueT) fromCollectionValue(type.getCollectionElementType(), (Collection) value, elementType, typeFactory);
    } else if (TypeName.ITERABLE.equals(type.getTypeName())) {
        return (ValueT) fromIterableValue(type.getCollectionElementType(), (Iterable) value, elementType, typeFactory);
    }
    if (TypeName.MAP.equals(type.getTypeName())) {
        return (ValueT) fromMapValue(type.getMapKeyType(), type.getMapValueType(), (Map) value, keyType, valueType, typeFactory);
    } else {
        if (type.isLogicalType(OneOfType.IDENTIFIER)) {
            OneOfType oneOfType = type.getLogicalType(OneOfType.class);
            EnumerationType oneOfEnum = oneOfType.getCaseEnumType();
            OneOfType.Value oneOfValue = (OneOfType.Value) value;
            FieldValueTypeInformation oneOfFieldValueTypeInformation = checkNotNull(fieldValueTypeInformation.getOneOfTypes().get(oneOfEnum.toString(oneOfValue.getCaseType())));
            Object fromValue = fromValue(oneOfType.getFieldType(oneOfValue), oneOfValue.getValue(), oneOfFieldValueTypeInformation.getRawType(), oneOfFieldValueTypeInformation, typeFactory);
            return (ValueT) oneOfType.createValue(oneOfValue.getCaseType(), fromValue);
        } else if (type.getTypeName().isLogicalType()) {
            return (ValueT) type.getLogicalType().toBaseType(value);
        }
        return value;
    }
}
Also used : EnumerationType(org.apache.beam.sdk.schemas.logicaltypes.EnumerationType) Row(org.apache.beam.sdk.values.Row) Map(java.util.Map) OneOfType(org.apache.beam.sdk.schemas.logicaltypes.OneOfType) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

OneOfType (org.apache.beam.sdk.schemas.logicaltypes.OneOfType)8 Map (java.util.Map)4 Field (org.apache.beam.sdk.schemas.Schema.Field)4 EnumerationType (org.apache.beam.sdk.schemas.logicaltypes.EnumerationType)4 Row (org.apache.beam.sdk.values.Row)4 Nullable (org.checkerframework.checker.nullness.qual.Nullable)4 Method (java.lang.reflect.Method)3 GenericRecord (org.apache.avro.generic.GenericRecord)3 GenericRecordBuilder (org.apache.avro.generic.GenericRecordBuilder)3 Schema (org.apache.beam.sdk.schemas.Schema)3 FieldType (org.apache.beam.sdk.schemas.Schema.FieldType)3 BigDecimal (java.math.BigDecimal)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 AvroRuntimeException (org.apache.avro.AvroRuntimeException)2 Conversions (org.apache.avro.Conversions)2 LogicalType (org.apache.avro.LogicalType)2 Utf8 (org.apache.avro.util.Utf8)2 Experimental (org.apache.beam.sdk.annotations.Experimental)2