Search in sources :

Example 1 with MapMetaData

use of org.apache.thrift.meta_data.MapMetaData in project beam by apache.

the class ThriftSchema method beamType.

private <EnumT extends Enum<EnumT> & TEnum> FieldType beamType(FieldValueMetaData metadata) {
    if (metadata.isTypedef()) {
        final FieldType beamType = typedefs.get(metadata.getTypedefName());
        if (beamType != null) {
            return beamType;
        }
    }
    switch(metadata.type) {
        case TType.BOOL:
            return FieldType.BOOLEAN;
        case TType.BYTE:
            return FieldType.BYTE;
        case TType.I16:
            return FieldType.INT16;
        case TType.I32:
            return FieldType.INT32;
        case TType.I64:
            return FieldType.INT64;
        case TType.DOUBLE:
            return FieldType.DOUBLE;
        case TType.STRING:
            return metadata.isBinary() ? FieldType.BYTES : FieldType.STRING;
        case TType.SET:
            final FieldValueMetaData setElemMetadata = ((SetMetaData) metadata).elemMetaData;
            return FieldType.iterable(beamType(setElemMetadata));
        case TType.LIST:
            final FieldValueMetaData listElemMetadata = ((ListMetaData) metadata).elemMetaData;
            return FieldType.array(beamType(listElemMetadata));
        case TType.MAP:
            final MapMetaData mapMetadata = ((MapMetaData) metadata);
            return FieldType.map(beamType(mapMetadata.keyMetaData), beamType(mapMetadata.valueMetaData));
        case TType.STRUCT:
            final StructMetaData structMetadata = ((StructMetaData) metadata);
            return FieldType.row(schemaFor(structMetadata.structClass));
        case TType.ENUM:
            @SuppressWarnings("unchecked") final Class<EnumT> enumClass = (Class<EnumT>) ((EnumMetaData) metadata).enumClass;
            // it's either "nullness" or "unsafe", apparently
            @SuppressWarnings("nullness") final EnumT @NonNull [] enumConstants = enumClass.getEnumConstants();
            final String[] enumValues = Stream.of(enumConstants).map(EnumT::name).toArray(String[]::new);
            return FieldType.logicalType(EnumerationType.create(enumValues));
        default:
            throw new IllegalArgumentException("Unsupported thrift type code: " + metadata.type);
    }
}
Also used : SetMetaData(org.apache.thrift.meta_data.SetMetaData) MapMetaData(org.apache.thrift.meta_data.MapMetaData) ListMetaData(org.apache.thrift.meta_data.ListMetaData) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) FieldValueMetaData(org.apache.thrift.meta_data.FieldValueMetaData) StructMetaData(org.apache.thrift.meta_data.StructMetaData) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Aggregations

FieldType (org.apache.beam.sdk.schemas.Schema.FieldType)1 FieldValueMetaData (org.apache.thrift.meta_data.FieldValueMetaData)1 ListMetaData (org.apache.thrift.meta_data.ListMetaData)1 MapMetaData (org.apache.thrift.meta_data.MapMetaData)1 SetMetaData (org.apache.thrift.meta_data.SetMetaData)1 StructMetaData (org.apache.thrift.meta_data.StructMetaData)1 NonNull (org.checkerframework.checker.nullness.qual.NonNull)1