Search in sources :

Example 1 with UnknownLogicalType

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

the class SchemaTranslation method fieldTypeFromProtoWithoutNullable.

private static FieldType fieldTypeFromProtoWithoutNullable(SchemaApi.FieldType protoFieldType) {
    switch(protoFieldType.getTypeInfoCase()) {
        case ATOMIC_TYPE:
            switch(protoFieldType.getAtomicType()) {
                case BYTE:
                    return FieldType.of(TypeName.BYTE);
                case INT16:
                    return FieldType.of(TypeName.INT16);
                case INT32:
                    return FieldType.of(TypeName.INT32);
                case INT64:
                    return FieldType.of(TypeName.INT64);
                case FLOAT:
                    return FieldType.of(TypeName.FLOAT);
                case DOUBLE:
                    return FieldType.of(TypeName.DOUBLE);
                case STRING:
                    return FieldType.of(TypeName.STRING);
                case BOOLEAN:
                    return FieldType.of(TypeName.BOOLEAN);
                case BYTES:
                    return FieldType.of(TypeName.BYTES);
                case UNSPECIFIED:
                    throw new IllegalArgumentException("Encountered UNSPECIFIED AtomicType");
                default:
                    throw new IllegalArgumentException("Encountered unknown AtomicType: " + protoFieldType.getAtomicType());
            }
        case ROW_TYPE:
            return FieldType.row(schemaFromProto(protoFieldType.getRowType().getSchema()));
        case ARRAY_TYPE:
            return FieldType.array(fieldTypeFromProto(protoFieldType.getArrayType().getElementType()));
        case ITERABLE_TYPE:
            return FieldType.iterable(fieldTypeFromProto(protoFieldType.getIterableType().getElementType()));
        case MAP_TYPE:
            return FieldType.map(fieldTypeFromProto(protoFieldType.getMapType().getKeyType()), fieldTypeFromProto(protoFieldType.getMapType().getValueType()));
        case LOGICAL_TYPE:
            String urn = protoFieldType.getLogicalType().getUrn();
            Class<? extends LogicalType<?, ?>> logicalTypeClass = STANDARD_LOGICAL_TYPES.get(urn);
            if (logicalTypeClass != null) {
                try {
                    return FieldType.logicalType(logicalTypeClass.getConstructor().newInstance());
                } catch (NoSuchMethodException e) {
                    throw new RuntimeException(String.format("Standard logical type '%s' does not have a zero-argument constructor.", urn), e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(String.format("Standard logical type '%s' has a zero-argument constructor, but it is not accessible.", urn), e);
                } catch (ReflectiveOperationException e) {
                    throw new RuntimeException(String.format("Error instantiating logical type '%s' with zero-argument constructor.", urn), e);
                }
            }
            // but not yet in Java. (BEAM-7554)
            if (urn.equals(URN_BEAM_LOGICAL_DATETIME)) {
                return FieldType.DATETIME;
            } else if (urn.equals(URN_BEAM_LOGICAL_DECIMAL)) {
                return FieldType.DECIMAL;
            } else if (urn.equals(URN_BEAM_LOGICAL_JAVASDK)) {
                return FieldType.logicalType((LogicalType) SerializableUtils.deserializeFromByteArray(protoFieldType.getLogicalType().getPayload().toByteArray(), "logicalType"));
            } else {
                @Nullable FieldType argumentType = null;
                @Nullable Object argumentValue = null;
                if (protoFieldType.getLogicalType().hasArgumentType()) {
                    argumentType = fieldTypeFromProto(protoFieldType.getLogicalType().getArgumentType());
                    argumentValue = fieldValueFromProto(argumentType, protoFieldType.getLogicalType().getArgument());
                }
                return FieldType.logicalType(new UnknownLogicalType(urn, protoFieldType.getLogicalType().getPayload().toByteArray(), argumentType, argumentValue, fieldTypeFromProto(protoFieldType.getLogicalType().getRepresentation())));
            }
        default:
            throw new IllegalArgumentException("Unexpected type_info: " + protoFieldType.getTypeInfoCase());
    }
}
Also used : UnknownLogicalType(org.apache.beam.sdk.schemas.logicaltypes.UnknownLogicalType) ByteString(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString) Nullable(org.checkerframework.checker.nullness.qual.Nullable) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType)

Example 2 with UnknownLogicalType

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

the class SchemaTranslation method fieldTypeToProto.

private static SchemaApi.FieldType fieldTypeToProto(FieldType fieldType, boolean serializeLogicalType) {
    SchemaApi.FieldType.Builder builder = SchemaApi.FieldType.newBuilder();
    switch(fieldType.getTypeName()) {
        case ROW:
            builder.setRowType(SchemaApi.RowType.newBuilder().setSchema(schemaToProto(fieldType.getRowSchema(), serializeLogicalType)));
            break;
        case ARRAY:
            builder.setArrayType(SchemaApi.ArrayType.newBuilder().setElementType(fieldTypeToProto(fieldType.getCollectionElementType(), serializeLogicalType)));
            break;
        case ITERABLE:
            builder.setIterableType(SchemaApi.IterableType.newBuilder().setElementType(fieldTypeToProto(fieldType.getCollectionElementType(), serializeLogicalType)));
            break;
        case MAP:
            builder.setMapType(SchemaApi.MapType.newBuilder().setKeyType(fieldTypeToProto(fieldType.getMapKeyType(), serializeLogicalType)).setValueType(fieldTypeToProto(fieldType.getMapValueType(), serializeLogicalType)).build());
            break;
        case LOGICAL_TYPE:
            LogicalType logicalType = fieldType.getLogicalType();
            SchemaApi.LogicalType.Builder logicalTypeBuilder;
            if (STANDARD_LOGICAL_TYPES.containsKey(logicalType.getIdentifier())) {
                Preconditions.checkArgument(logicalType.getArgumentType() == null, "Logical type '%s' cannot be used as a logical type, it has a non-null argument type.", logicalType.getIdentifier());
                logicalTypeBuilder = SchemaApi.LogicalType.newBuilder().setRepresentation(fieldTypeToProto(logicalType.getBaseType(), serializeLogicalType)).setUrn(logicalType.getIdentifier());
            } else if (logicalType instanceof UnknownLogicalType) {
                logicalTypeBuilder = SchemaApi.LogicalType.newBuilder().setUrn(logicalType.getIdentifier()).setPayload(ByteString.copyFrom(((UnknownLogicalType) logicalType).getPayload())).setRepresentation(fieldTypeToProto(logicalType.getBaseType(), serializeLogicalType));
                if (logicalType.getArgumentType() != null) {
                    logicalTypeBuilder.setArgumentType(fieldTypeToProto(logicalType.getArgumentType(), serializeLogicalType)).setArgument(fieldValueToProto(logicalType.getArgumentType(), logicalType.getArgument()));
                }
            } else {
                logicalTypeBuilder = SchemaApi.LogicalType.newBuilder().setRepresentation(fieldTypeToProto(logicalType.getBaseType(), serializeLogicalType)).setUrn(URN_BEAM_LOGICAL_JAVASDK);
                if (logicalType.getArgumentType() != null) {
                    logicalTypeBuilder = logicalTypeBuilder.setArgumentType(fieldTypeToProto(logicalType.getArgumentType(), serializeLogicalType)).setArgument(fieldValueToProto(logicalType.getArgumentType(), logicalType.getArgument()));
                }
                if (serializeLogicalType) {
                    logicalTypeBuilder = logicalTypeBuilder.setPayload(ByteString.copyFrom(SerializableUtils.serializeToByteArray(logicalType)));
                }
            }
            builder.setLogicalType(logicalTypeBuilder.build());
            break;
        // but not yet in Java. (BEAM-7554)
        case DATETIME:
            builder.setLogicalType(SchemaApi.LogicalType.newBuilder().setUrn(URN_BEAM_LOGICAL_DATETIME).setRepresentation(fieldTypeToProto(FieldType.INT64, serializeLogicalType)).build());
            break;
        case DECIMAL:
            builder.setLogicalType(SchemaApi.LogicalType.newBuilder().setUrn(URN_BEAM_LOGICAL_DECIMAL).setRepresentation(fieldTypeToProto(FieldType.BYTES, serializeLogicalType)).build());
            break;
        case BYTE:
            builder.setAtomicType(SchemaApi.AtomicType.BYTE);
            break;
        case INT16:
            builder.setAtomicType(SchemaApi.AtomicType.INT16);
            break;
        case INT32:
            builder.setAtomicType(SchemaApi.AtomicType.INT32);
            break;
        case INT64:
            builder.setAtomicType(SchemaApi.AtomicType.INT64);
            break;
        case FLOAT:
            builder.setAtomicType(SchemaApi.AtomicType.FLOAT);
            break;
        case DOUBLE:
            builder.setAtomicType(SchemaApi.AtomicType.DOUBLE);
            break;
        case STRING:
            builder.setAtomicType(SchemaApi.AtomicType.STRING);
            break;
        case BOOLEAN:
            builder.setAtomicType(SchemaApi.AtomicType.BOOLEAN);
            break;
        case BYTES:
            builder.setAtomicType(SchemaApi.AtomicType.BYTES);
            break;
    }
    builder.setNullable(fieldType.getNullable());
    return builder.build();
}
Also used : UnknownLogicalType(org.apache.beam.sdk.schemas.logicaltypes.UnknownLogicalType) UnknownLogicalType(org.apache.beam.sdk.schemas.logicaltypes.UnknownLogicalType) LogicalType(org.apache.beam.sdk.schemas.Schema.LogicalType) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType)

Aggregations

FieldType (org.apache.beam.sdk.schemas.Schema.FieldType)2 UnknownLogicalType (org.apache.beam.sdk.schemas.logicaltypes.UnknownLogicalType)2 LogicalType (org.apache.beam.sdk.schemas.Schema.LogicalType)1 ByteString (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1