Search in sources :

Example 1 with StructType

use of io.delta.standalone.types.StructType in project presto by prestodb.

the class DeltaTypeUtils method convertDeltaDataTypePrestoDataType.

/**
 * Convert given Delta data type to Presto data type signature.
 *
 * @param tableName  Used in error messages when an unsupported data type is encountered.
 * @param columnName Used in error messages when an unsupported data type is encountered.
 * @param deltaType  Data type to convert
 * @return
 */
public static TypeSignature convertDeltaDataTypePrestoDataType(SchemaTableName tableName, String columnName, DataType deltaType) {
    checkArgument(deltaType != null);
    if (deltaType instanceof StructType) {
        StructType deltaStructType = (StructType) deltaType;
        ImmutableList.Builder<TypeSignatureParameter> typeSignatureBuilder = ImmutableList.builder();
        Arrays.stream(deltaStructType.getFields()).forEach(field -> {
            String rowFieldName = field.getName().toLowerCase(Locale.US);
            TypeSignature rowFieldType = convertDeltaDataTypePrestoDataType(tableName, columnName + "." + field.getName(), field.getDataType());
            typeSignatureBuilder.add(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName(rowFieldName, false)), rowFieldType)));
        });
        return new TypeSignature(StandardTypes.ROW, typeSignatureBuilder.build());
    } else if (deltaType instanceof ArrayType) {
        ArrayType deltaArrayType = (ArrayType) deltaType;
        TypeSignature elementType = convertDeltaDataTypePrestoDataType(tableName, columnName, deltaArrayType.getElementType());
        return new TypeSignature(ARRAY, ImmutableList.of(TypeSignatureParameter.of(elementType)));
    } else if (deltaType instanceof MapType) {
        MapType deltaMapType = (MapType) deltaType;
        TypeSignature keyType = convertDeltaDataTypePrestoDataType(tableName, columnName, deltaMapType.getKeyType());
        TypeSignature valueType = convertDeltaDataTypePrestoDataType(tableName, columnName, deltaMapType.getValueType());
        return new TypeSignature(MAP, ImmutableList.of(TypeSignatureParameter.of(keyType), TypeSignatureParameter.of(valueType)));
    }
    return convertDeltaPrimitiveTypeToPrestoPrimitiveType(tableName, columnName, deltaType).getTypeSignature();
}
Also used : ArrayType(io.delta.standalone.types.ArrayType) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) StructType(io.delta.standalone.types.StructType) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ImmutableList(com.google.common.collect.ImmutableList) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) MapType(io.delta.standalone.types.MapType)

Aggregations

NamedTypeSignature (com.facebook.presto.common.type.NamedTypeSignature)1 RowFieldName (com.facebook.presto.common.type.RowFieldName)1 TypeSignature (com.facebook.presto.common.type.TypeSignature)1 TypeSignatureParameter (com.facebook.presto.common.type.TypeSignatureParameter)1 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayType (io.delta.standalone.types.ArrayType)1 MapType (io.delta.standalone.types.MapType)1 StructType (io.delta.standalone.types.StructType)1