Search in sources :

Example 6 with EnumerationType

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

the class RowTest method testWithFieldValues.

@Test
public void testWithFieldValues() {
    EnumerationType enumerationType = EnumerationType.create("zero", "one", "two");
    Schema schema = Schema.builder().addLogicalTypeField("f1_enum", enumerationType).build();
    Row row = Row.withSchema(schema).withFieldValues(ImmutableMap.of("f1_enum", enumerationType.valueOf("zero"))).build();
    assertEquals(enumerationType.valueOf(0), row.getValue(0));
    assertEquals(enumerationType.valueOf("zero"), row.getLogicalTypeValue(0, EnumerationType.Value.class));
}
Also used : Schema.toSchema(org.apache.beam.sdk.schemas.Schema.toSchema) Schema(org.apache.beam.sdk.schemas.Schema) EnumerationType(org.apache.beam.sdk.schemas.logicaltypes.EnumerationType) Row.toRow(org.apache.beam.sdk.values.Row.toRow) Test(org.junit.Test)

Example 7 with EnumerationType

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

the class RowTest method testLogicalTypeWithRowValueName.

@Test
public void testLogicalTypeWithRowValueName() {
    EnumerationType enumerationType = EnumerationType.create("zero", "one", "two");
    Schema type = Stream.of(Schema.Field.of("f1_enum", FieldType.logicalType(enumerationType))).collect(toSchema());
    Row row = Row.withSchema(type).withFieldValue("f1_enum", enumerationType.valueOf("zero")).build();
    assertEquals(enumerationType.valueOf(0), row.getValue(0));
    assertEquals(enumerationType.valueOf("zero"), row.getLogicalTypeValue(0, EnumerationType.Value.class));
}
Also used : Schema.toSchema(org.apache.beam.sdk.schemas.Schema.toSchema) Schema(org.apache.beam.sdk.schemas.Schema) EnumerationType(org.apache.beam.sdk.schemas.logicaltypes.EnumerationType) Row.toRow(org.apache.beam.sdk.values.Row.toRow) Test(org.junit.Test)

Example 8 with EnumerationType

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

the class RowTest method testLogicalTypeWithRowValueOverride.

@Test
public void testLogicalTypeWithRowValueOverride() {
    EnumerationType enumerationType = EnumerationType.create("zero", "one", "two");
    Schema type = Stream.of(Schema.Field.of("f1_enum", FieldType.logicalType(enumerationType))).collect(toSchema());
    Row row = Row.withSchema(type).withFieldValue("f1_enum", enumerationType.valueOf("zero")).build();
    Row overriddenRow = Row.fromRow(row).withFieldValue("f1_enum", enumerationType.valueOf("one")).build();
    assertEquals(enumerationType.valueOf(1), overriddenRow.getValue(0));
    assertEquals(enumerationType.valueOf("one"), overriddenRow.getLogicalTypeValue(0, EnumerationType.Value.class));
}
Also used : Schema.toSchema(org.apache.beam.sdk.schemas.Schema.toSchema) Schema(org.apache.beam.sdk.schemas.Schema) EnumerationType(org.apache.beam.sdk.schemas.logicaltypes.EnumerationType) Row.toRow(org.apache.beam.sdk.values.Row.toRow) Test(org.junit.Test)

Example 9 with EnumerationType

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

the class RowTest method testLogicalTypeWithRowValue.

@Test
public void testLogicalTypeWithRowValue() {
    EnumerationType enumerationType = EnumerationType.create("zero", "one", "two");
    Schema type = Stream.of(Schema.Field.of("f1_enum", FieldType.logicalType(enumerationType))).collect(toSchema());
    Row row = Row.withSchema(type).addValue(enumerationType.valueOf("zero")).build();
    assertEquals(enumerationType.valueOf(0), row.getValue(0));
    assertEquals(enumerationType.valueOf("zero"), row.getLogicalTypeValue(0, EnumerationType.Value.class));
}
Also used : Schema.toSchema(org.apache.beam.sdk.schemas.Schema.toSchema) Schema(org.apache.beam.sdk.schemas.Schema) EnumerationType(org.apache.beam.sdk.schemas.logicaltypes.EnumerationType) Row.toRow(org.apache.beam.sdk.values.Row.toRow) Test(org.junit.Test)

Example 10 with EnumerationType

use of org.apache.beam.sdk.schemas.logicaltypes.EnumerationType 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

EnumerationType (org.apache.beam.sdk.schemas.logicaltypes.EnumerationType)13 Test (org.junit.Test)9 Schema (org.apache.beam.sdk.schemas.Schema)8 Row (org.apache.beam.sdk.values.Row)7 Schema.toSchema (org.apache.beam.sdk.schemas.Schema.toSchema)4 Row.toRow (org.apache.beam.sdk.values.Row.toRow)4 Map (java.util.Map)3 OneOfType (org.apache.beam.sdk.schemas.logicaltypes.OneOfType)3 Nullable (org.checkerframework.checker.nullness.qual.Nullable)3 BigDecimal (java.math.BigDecimal)2 HashMap (java.util.HashMap)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 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1