use of org.apache.beam.sdk.schemas.logicaltypes.OneOfType in project beam by apache.
the class ProtoByteBuddyUtils method createGetter.
private static <ProtoT> FieldValueGetter createGetter(FieldValueTypeInformation fieldValueTypeInformation, TypeConversionsFactory typeConversionsFactory, Class clazz, Multimap<String, Method> methods, Field field, FieldValueTypeSupplier fieldValueTypeSupplier) {
if (field.getType().isLogicalType(OneOfType.IDENTIFIER)) {
OneOfType oneOfType = field.getType().getLogicalType(OneOfType.class);
// The case accessor method in the proto is named getOneOfNameCase.
Method caseMethod = getProtoGetter(methods, field.getName() + "_case", FieldType.logicalType(oneOfType.getCaseEnumType()));
// Create a map of case enum value to getter. This must be sorted, so store in a TreeMap.
TreeMap<Integer, FieldValueGetter<ProtoT, OneOfType.Value>> oneOfGetters = Maps.newTreeMap();
Map<String, FieldValueTypeInformation> oneOfFieldTypes = fieldValueTypeSupplier.get(clazz, oneOfType.getOneOfSchema()).stream().collect(Collectors.toMap(FieldValueTypeInformation::getName, f -> f));
for (Field oneOfField : oneOfType.getOneOfSchema().getFields()) {
int protoFieldIndex = getFieldNumber(oneOfField);
FieldValueGetter oneOfFieldGetter = createGetter(oneOfFieldTypes.get(oneOfField.getName()), typeConversionsFactory, clazz, methods, oneOfField, fieldValueTypeSupplier);
oneOfGetters.put(protoFieldIndex, oneOfFieldGetter);
}
return createOneOfGetter(fieldValueTypeInformation, oneOfGetters, clazz, oneOfType, caseMethod);
} else {
return JavaBeanUtils.createGetter(fieldValueTypeInformation, typeConversionsFactory);
}
}
use of org.apache.beam.sdk.schemas.logicaltypes.OneOfType in project beam by apache.
the class AvroUtils method convertUnionStrict.
private static Object convertUnionStrict(Object value, org.apache.avro.Schema unionAvroSchema, Schema.FieldType fieldType) {
checkTypeName(fieldType.getTypeName(), TypeName.LOGICAL_TYPE, "oneOfType");
checkArgument(fieldType.getLogicalType().getIdentifier().equals(OneOfType.IDENTIFIER));
OneOfType oneOfType = fieldType.getLogicalType(OneOfType.class);
int fieldNumber = GenericData.get().resolveUnion(unionAvroSchema, value);
FieldType baseFieldType = oneOfType.getOneOfSchema().getField(fieldNumber).getType();
Object convertedValue = convertAvroFieldStrict(value, unionAvroSchema.getTypes().get(fieldNumber), baseFieldType);
return oneOfType.createValue(fieldNumber, convertedValue);
}
use of org.apache.beam.sdk.schemas.logicaltypes.OneOfType in project beam by apache.
the class AvroUtils method genericFromBeamField.
@Nullable
private static Object genericFromBeamField(Schema.FieldType fieldType, org.apache.avro.Schema avroSchema, @Nullable Object value) {
TypeWithNullability typeWithNullability = new TypeWithNullability(avroSchema);
if (!fieldType.getNullable().equals(typeWithNullability.nullable)) {
throw new IllegalArgumentException("FieldType " + fieldType + " and AVRO schema " + avroSchema + " don't have matching nullability");
}
if (value == null) {
return value;
}
switch(fieldType.getTypeName()) {
case BYTE:
case INT16:
case INT32:
case INT64:
case FLOAT:
case DOUBLE:
case BOOLEAN:
return value;
case STRING:
return new Utf8((String) value);
case DECIMAL:
BigDecimal decimal = (BigDecimal) value;
LogicalType logicalType = typeWithNullability.type.getLogicalType();
return new Conversions.DecimalConversion().toBytes(decimal, null, logicalType);
case DATETIME:
if (typeWithNullability.type.getType() == Type.INT) {
ReadableInstant instant = (ReadableInstant) value;
return (int) Days.daysBetween(Instant.EPOCH, instant).getDays();
} else if (typeWithNullability.type.getType() == Type.LONG) {
ReadableInstant instant = (ReadableInstant) value;
return (long) instant.getMillis();
} else {
throw new IllegalArgumentException("Can't represent " + fieldType + " as " + typeWithNullability.type.getType());
}
case BYTES:
return ByteBuffer.wrap((byte[]) value);
case LOGICAL_TYPE:
switch(fieldType.getLogicalType().getIdentifier()) {
case FixedBytes.IDENTIFIER:
FixedBytesField fixedBytesField = checkNotNull(FixedBytesField.fromBeamFieldType(fieldType));
byte[] byteArray = (byte[]) value;
if (byteArray.length != fixedBytesField.getSize()) {
throw new IllegalArgumentException("Incorrectly sized byte array.");
}
return GenericData.get().createFixed(null, (byte[]) value, typeWithNullability.type);
case EnumerationType.IDENTIFIER:
EnumerationType enumerationType = fieldType.getLogicalType(EnumerationType.class);
return GenericData.get().createEnum(enumerationType.toString((EnumerationType.Value) value), typeWithNullability.type);
case OneOfType.IDENTIFIER:
OneOfType oneOfType = fieldType.getLogicalType(OneOfType.class);
OneOfType.Value oneOfValue = (OneOfType.Value) value;
FieldType innerFieldType = oneOfType.getFieldType(oneOfValue);
if (typeWithNullability.nullable && oneOfValue.getValue() == null) {
return null;
} else {
return genericFromBeamField(innerFieldType.withNullable(false), typeWithNullability.type.getTypes().get(oneOfValue.getCaseType().getValue()), oneOfValue.getValue());
}
case "NVARCHAR":
case "VARCHAR":
case "LONGNVARCHAR":
case "LONGVARCHAR":
return new Utf8((String) value);
case "DATE":
return Days.daysBetween(Instant.EPOCH, (Instant) value).getDays();
case "TIME":
return (int) ((Instant) value).getMillis();
default:
throw new RuntimeException("Unhandled logical type " + fieldType.getLogicalType().getIdentifier());
}
case ARRAY:
case ITERABLE:
Iterable iterable = (Iterable) value;
List<Object> translatedArray = Lists.newArrayListWithExpectedSize(Iterables.size(iterable));
for (Object arrayElement : iterable) {
translatedArray.add(genericFromBeamField(fieldType.getCollectionElementType(), typeWithNullability.type.getElementType(), arrayElement));
}
return translatedArray;
case MAP:
Map map = Maps.newHashMap();
Map<Object, Object> valueMap = (Map<Object, Object>) value;
for (Map.Entry entry : valueMap.entrySet()) {
Utf8 key = new Utf8((String) entry.getKey());
map.put(key, genericFromBeamField(fieldType.getMapValueType(), typeWithNullability.type.getValueType(), entry.getValue()));
}
return map;
case ROW:
return toGenericRecord((Row) value, typeWithNullability.type);
default:
throw new IllegalArgumentException("Unsupported type " + fieldType);
}
}
Aggregations