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));
}
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));
}
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));
}
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));
}
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;
}
}
Aggregations