use of org.apache.beam.sdk.schemas.logicaltypes.NanosDuration in project beam by apache.
the class ProtoSchemaTranslator method beamFieldTypeFromSingularProtoField.
private static FieldType beamFieldTypeFromSingularProtoField(Descriptors.FieldDescriptor protoFieldDescriptor) {
Descriptors.FieldDescriptor.Type fieldDescriptor = protoFieldDescriptor.getType();
FieldType fieldType;
switch(fieldDescriptor) {
case INT32:
fieldType = FieldType.INT32;
break;
case INT64:
fieldType = FieldType.INT64;
break;
case FLOAT:
fieldType = FieldType.FLOAT;
break;
case DOUBLE:
fieldType = FieldType.DOUBLE;
break;
case BOOL:
fieldType = FieldType.BOOLEAN;
break;
case STRING:
fieldType = FieldType.STRING;
break;
case BYTES:
fieldType = FieldType.BYTES;
break;
case UINT32:
fieldType = FieldType.logicalType(new UInt32());
break;
case SINT32:
fieldType = FieldType.logicalType(new SInt32());
break;
case FIXED32:
fieldType = FieldType.logicalType(new Fixed32());
break;
case SFIXED32:
fieldType = FieldType.logicalType(new SFixed32());
break;
case UINT64:
fieldType = FieldType.logicalType(new UInt64());
break;
case SINT64:
fieldType = FieldType.logicalType(new SInt64());
break;
case FIXED64:
fieldType = FieldType.logicalType(new Fixed64());
break;
case SFIXED64:
fieldType = FieldType.logicalType(new SFixed64());
break;
case ENUM:
Map<String, Integer> enumValues = Maps.newHashMap();
for (EnumValueDescriptor enumValue : protoFieldDescriptor.getEnumType().getValues()) {
if (enumValues.putIfAbsent(enumValue.getName(), enumValue.getNumber()) != null) {
throw new RuntimeException("Aliased enumerations not currently supported.");
}
}
fieldType = FieldType.logicalType(EnumerationType.create(enumValues));
break;
case MESSAGE:
case GROUP:
String fullName = protoFieldDescriptor.getMessageType().getFullName();
switch(fullName) {
case "google.protobuf.Timestamp":
fieldType = FieldType.logicalType(new NanosInstant());
break;
case "google.protobuf.Int32Value":
case "google.protobuf.UInt32Value":
case "google.protobuf.Int64Value":
case "google.protobuf.UInt64Value":
case "google.protobuf.FloatValue":
case "google.protobuf.DoubleValue":
case "google.protobuf.StringValue":
case "google.protobuf.BoolValue":
case "google.protobuf.BytesValue":
fieldType = beamFieldTypeFromSingularProtoField(protoFieldDescriptor.getMessageType().findFieldByNumber(1));
break;
case "google.protobuf.Duration":
fieldType = FieldType.logicalType(new NanosDuration());
break;
case "google.protobuf.Any":
throw new RuntimeException("Any not yet supported");
default:
fieldType = FieldType.row(getSchema(protoFieldDescriptor.getMessageType()));
}
// all messages are nullable in Proto
if (protoFieldDescriptor.isOptional()) {
fieldType = fieldType.withNullable(true);
}
break;
default:
throw new RuntimeException("Field type not matched.");
}
return fieldType;
}
Aggregations