Search in sources :

Example 1 with NanosInstant

use of org.apache.beam.sdk.schemas.logicaltypes.NanosInstant 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;
}
Also used : SInt64(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.SInt64) SInt32(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.SInt32) NanosDuration(org.apache.beam.sdk.schemas.logicaltypes.NanosDuration) SFixed32(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.SFixed32) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) SFixed64(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.SFixed64) NanosInstant(org.apache.beam.sdk.schemas.logicaltypes.NanosInstant) SFixed32(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.SFixed32) Fixed32(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.Fixed32) Fixed64(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.Fixed64) SFixed64(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.SFixed64) UInt64(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.UInt64) UInt32(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.UInt32) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor)

Aggregations

EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 Fixed32 (org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.Fixed32)1 Fixed64 (org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.Fixed64)1 SFixed32 (org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.SFixed32)1 SFixed64 (org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.SFixed64)1 SInt32 (org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.SInt32)1 SInt64 (org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.SInt64)1 UInt32 (org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.UInt32)1 UInt64 (org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.UInt64)1 FieldType (org.apache.beam.sdk.schemas.Schema.FieldType)1 NanosDuration (org.apache.beam.sdk.schemas.logicaltypes.NanosDuration)1 NanosInstant (org.apache.beam.sdk.schemas.logicaltypes.NanosInstant)1