Search in sources :

Example 16 with TypeSignatureParameter

use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.

the class ColumnInfo method setTypeInfo.

public static void setTypeInfo(Builder builder, TypeSignature type) {
    builder.setColumnType(getType(type));
    ImmutableList.Builder<Integer> parameterTypes = ImmutableList.builder();
    for (TypeSignatureParameter parameter : type.getParameters()) {
        parameterTypes.add(getType(parameter));
    }
    builder.setColumnParameterTypes(parameterTypes.build());
    switch(type.getBase()) {
        case "boolean":
            builder.setColumnDisplaySize(5);
            break;
        case "bigint":
            builder.setSigned(true);
            builder.setPrecision(19);
            builder.setScale(0);
            builder.setColumnDisplaySize(20);
            break;
        case "integer":
            builder.setSigned(true);
            builder.setPrecision(10);
            builder.setScale(0);
            builder.setColumnDisplaySize(11);
            break;
        case "smallint":
            builder.setSigned(true);
            builder.setPrecision(5);
            builder.setScale(0);
            builder.setColumnDisplaySize(6);
            break;
        case "tinyint":
            builder.setSigned(true);
            builder.setPrecision(3);
            builder.setScale(0);
            builder.setColumnDisplaySize(4);
            break;
        case "real":
            builder.setSigned(true);
            builder.setPrecision(9);
            builder.setScale(0);
            builder.setColumnDisplaySize(16);
            break;
        case "double":
            builder.setSigned(true);
            builder.setPrecision(17);
            builder.setScale(0);
            builder.setColumnDisplaySize(24);
            break;
        case "char":
            builder.setSigned(false);
            builder.setPrecision(type.getParameters().get(0).getLongLiteral().intValue());
            builder.setColumnDisplaySize(type.getParameters().get(0).getLongLiteral().intValue());
            builder.setScale(0);
            break;
        case "varchar":
            builder.setSigned(false);
            builder.setPrecision(Math.min(type.getParameters().get(0).getLongLiteral().intValue(), VARCHAR_MAX));
            builder.setColumnDisplaySize(Math.min(type.getParameters().get(0).getLongLiteral().intValue(), VARCHAR_MAX));
            builder.setScale(0);
            break;
        case "varbinary":
            builder.setSigned(true);
            builder.setPrecision(VARBINARY_MAX);
            builder.setScale(0);
            builder.setColumnDisplaySize(VARBINARY_MAX);
            break;
        case "time":
            builder.setSigned(true);
            builder.setPrecision(3);
            builder.setScale(0);
            builder.setColumnDisplaySize(TIME_MAX);
            break;
        case "time with time zone":
            builder.setSigned(true);
            builder.setPrecision(3);
            builder.setScale(0);
            builder.setColumnDisplaySize(TIME_WITH_TIME_ZONE_MAX);
            break;
        case "timestamp":
            builder.setSigned(true);
            builder.setPrecision(3);
            builder.setScale(0);
            builder.setColumnDisplaySize(TIMESTAMP_MAX);
            break;
        case "timestamp with time zone":
            builder.setSigned(true);
            builder.setPrecision(3);
            builder.setScale(0);
            builder.setColumnDisplaySize(TIMESTAMP_WITH_TIME_ZONE_MAX);
            break;
        case "date":
            builder.setSigned(true);
            builder.setScale(0);
            builder.setColumnDisplaySize(DATE_MAX);
            break;
        case "interval year to month":
            builder.setColumnDisplaySize(TIMESTAMP_MAX);
            break;
        case "interval day to second":
            builder.setColumnDisplaySize(TIMESTAMP_MAX);
            break;
        case "decimal":
            builder.setSigned(true);
            // dot and sign
            builder.setColumnDisplaySize(type.getParameters().get(0).getLongLiteral().intValue() + 2);
            builder.setPrecision(type.getParameters().get(0).getLongLiteral().intValue());
            builder.setScale(type.getParameters().get(1).getLongLiteral().intValue());
            break;
    }
}
Also used : TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ImmutableList(com.google.common.collect.ImmutableList)

Example 17 with TypeSignatureParameter

use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.

the class TypeConverter method toHiveTypeInfo.

private static TypeInfo toHiveTypeInfo(Type type) {
    if (BOOLEAN.equals(type)) {
        return HIVE_BOOLEAN.getTypeInfo();
    }
    if (BIGINT.equals(type)) {
        return HIVE_LONG.getTypeInfo();
    }
    if (INTEGER.equals(type)) {
        return HIVE_INT.getTypeInfo();
    }
    if (SMALLINT.equals(type)) {
        return HIVE_SHORT.getTypeInfo();
    }
    if (TINYINT.equals(type)) {
        return HIVE_BYTE.getTypeInfo();
    }
    if (REAL.equals(type)) {
        return HIVE_FLOAT.getTypeInfo();
    }
    if (DOUBLE.equals(type)) {
        return HIVE_DOUBLE.getTypeInfo();
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        if (varcharType.isUnbounded()) {
            return HIVE_STRING.getTypeInfo();
        }
        if (varcharType.getLengthSafe() <= HiveVarchar.MAX_VARCHAR_LENGTH) {
            return getVarcharTypeInfo(varcharType.getLengthSafe());
        }
        throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported VARCHAR types: VARCHAR(<=%d), VARCHAR.", type, HiveVarchar.MAX_VARCHAR_LENGTH));
    }
    if (type instanceof CharType) {
        CharType charType = (CharType) type;
        int charLength = charType.getLength();
        if (charLength <= HiveChar.MAX_CHAR_LENGTH) {
            return getCharTypeInfo(charLength);
        }
        throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported CHAR types: CHAR(<=%d).", type, HiveChar.MAX_CHAR_LENGTH));
    }
    if (VARBINARY.equals(type)) {
        return HIVE_BINARY.getTypeInfo();
    }
    if (DATE.equals(type)) {
        return HIVE_DATE.getTypeInfo();
    }
    if (TIMESTAMP.equals(type)) {
        return HIVE_TIMESTAMP.getTypeInfo();
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale());
    }
    if (isArrayType(type)) {
        TypeInfo elementType = toHiveTypeInfo(type.getTypeParameters().get(0));
        return getListTypeInfo(elementType);
    }
    if (isMapType(type)) {
        TypeInfo keyType = toHiveTypeInfo(type.getTypeParameters().get(0));
        TypeInfo valueType = toHiveTypeInfo(type.getTypeParameters().get(1));
        return getMapTypeInfo(keyType, valueType);
    }
    if (isRowType(type)) {
        ImmutableList.Builder<String> fieldNames = ImmutableList.builder();
        for (TypeSignatureParameter parameter : type.getTypeSignature().getParameters()) {
            if (!parameter.isNamedTypeSignature()) {
                throw new IllegalArgumentException(format("Expected all parameters to be named type, but got %s", parameter));
            }
            NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
            if (!namedTypeSignature.getName().isPresent()) {
                throw new PrestoException(NOT_SUPPORTED, format("Anonymous row type is not supported in Hive. Please give each field a name: %s", type));
            }
            fieldNames.add(namedTypeSignature.getName().get());
        }
        return getStructTypeInfo(fieldNames.build(), type.getTypeParameters().stream().map(TypeConverter::toHiveTypeInfo).collect(toList()));
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", type));
}
Also used : VarcharType(com.facebook.presto.common.type.VarcharType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) PrestoException(com.facebook.presto.spi.PrestoException) TypeInfoFactory.getMapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getMapTypeInfo) TypeInfoFactory.getListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getListTypeInfo) TypeInfoFactory.getCharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getCharTypeInfo) TypeInfoFactory.getStructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getStructTypeInfo) TypeInfoFactory.getVarcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) DecimalType(com.facebook.presto.common.type.DecimalType) CharType(com.facebook.presto.common.type.CharType)

Example 18 with TypeSignatureParameter

use of com.facebook.presto.common.type.TypeSignatureParameter 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)

Example 19 with TypeSignatureParameter

use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.

the class HiveUtil method translateHiveUnsupportedTypeSignatureForTemporaryTable.

private static TypeSignature translateHiveUnsupportedTypeSignatureForTemporaryTable(TypeSignature typeSignature) {
    List<TypeSignatureParameter> parameters = typeSignature.getParameters();
    if (typeSignature.getBase().equals("unknown")) {
        return new TypeSignature(StandardTypes.BOOLEAN);
    }
    if (typeSignature.getBase().equals(StandardTypes.ROW)) {
        ImmutableList.Builder<TypeSignatureParameter> updatedParameters = ImmutableList.builder();
        for (int i = 0; i < parameters.size(); i++) {
            TypeSignatureParameter typeSignatureParameter = parameters.get(i);
            checkArgument(typeSignatureParameter.isNamedTypeSignature(), "unexpected row type signature parameter: %s", typeSignatureParameter);
            NamedTypeSignature namedTypeSignature = typeSignatureParameter.getNamedTypeSignature();
            updatedParameters.add(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(namedTypeSignature.getFieldName().orElse(new RowFieldName("_field_" + i, false))), translateHiveUnsupportedTypeSignatureForTemporaryTable(namedTypeSignature.getTypeSignature()))));
        }
        return new TypeSignature(StandardTypes.ROW, updatedParameters.build());
    }
    if (!parameters.isEmpty()) {
        ImmutableList.Builder<TypeSignatureParameter> updatedParameters = ImmutableList.builder();
        for (TypeSignatureParameter parameter : parameters) {
            switch(parameter.getKind()) {
                case LONG:
                case VARIABLE:
                    updatedParameters.add(parameter);
                    continue;
                case TYPE:
                    updatedParameters.add(TypeSignatureParameter.of(translateHiveUnsupportedTypeSignatureForTemporaryTable(parameter.getTypeSignature())));
                    break;
                case NAMED_TYPE:
                    NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
                    updatedParameters.add(TypeSignatureParameter.of(new NamedTypeSignature(namedTypeSignature.getFieldName(), translateHiveUnsupportedTypeSignatureForTemporaryTable(namedTypeSignature.getTypeSignature()))));
                    break;
                default:
                    throw new IllegalArgumentException("Unexpected parameter type: " + parameter.getKind());
            }
        }
        return new TypeSignature(typeSignature.getBase(), updatedParameters.build());
    }
    return typeSignature;
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature)

Example 20 with TypeSignatureParameter

use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.

the class OrcType method toOrcType.

private static List<OrcType> toOrcType(int nextFieldTypeIndex, Type type) {
    if (BOOLEAN.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.BOOLEAN));
    }
    if (TINYINT.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.BYTE));
    }
    if (SMALLINT.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.SHORT));
    }
    if (INTEGER.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.INT));
    }
    if (BIGINT.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.LONG));
    }
    if (DOUBLE.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.DOUBLE));
    }
    if (REAL.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.FLOAT));
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        if (varcharType.isUnbounded()) {
            return ImmutableList.of(new OrcType(OrcTypeKind.STRING));
        }
        return ImmutableList.of(new OrcType(OrcTypeKind.VARCHAR, varcharType.getLengthSafe()));
    }
    if (type instanceof CharType) {
        return ImmutableList.of(new OrcType(OrcTypeKind.CHAR, ((CharType) type).getLength()));
    }
    if (VARBINARY.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.BINARY));
    }
    if (DATE.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.DATE));
    }
    if (TIMESTAMP.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.TIMESTAMP));
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return ImmutableList.of(new OrcType(OrcTypeKind.DECIMAL, decimalType.getPrecision(), decimalType.getScale()));
    }
    if (type.getTypeSignature().getBase().equals(ARRAY)) {
        return createOrcArrayType(nextFieldTypeIndex, type.getTypeParameters().get(0));
    }
    if (type.getTypeSignature().getBase().equals(MAP)) {
        return createOrcMapType(nextFieldTypeIndex, type.getTypeParameters().get(0), type.getTypeParameters().get(1));
    }
    if (type.getTypeSignature().getBase().equals(ROW)) {
        List<String> fieldNames = new ArrayList<>();
        for (int i = 0; i < type.getTypeSignature().getParameters().size(); i++) {
            TypeSignatureParameter parameter = type.getTypeSignature().getParameters().get(i);
            fieldNames.add(parameter.getNamedTypeSignature().getName().orElse("field" + i));
        }
        List<Type> fieldTypes = type.getTypeParameters();
        return createOrcRowType(nextFieldTypeIndex, fieldNames, fieldTypes);
    }
    throw new NotSupportedException(format("Unsupported Hive type: %s", type));
}
Also used : DecimalType(com.facebook.presto.common.type.DecimalType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) VarcharType(com.facebook.presto.common.type.VarcharType) VarcharType(com.facebook.presto.common.type.VarcharType) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ArrayList(java.util.ArrayList) DecimalType(com.facebook.presto.common.type.DecimalType) CharType(com.facebook.presto.common.type.CharType) NotSupportedException(com.facebook.presto.common.NotSupportedException)

Aggregations

TypeSignatureParameter (com.facebook.presto.common.type.TypeSignatureParameter)21 NamedTypeSignature (com.facebook.presto.common.type.NamedTypeSignature)15 ImmutableList (com.google.common.collect.ImmutableList)13 TypeSignature (com.facebook.presto.common.type.TypeSignature)11 Type (com.facebook.presto.common.type.Type)9 RowFieldName (com.facebook.presto.common.type.RowFieldName)8 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)8 PrestoException (com.facebook.presto.spi.PrestoException)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Optional (java.util.Optional)5 CharType (com.facebook.presto.common.type.CharType)4 DecimalType (com.facebook.presto.common.type.DecimalType)4 VarcharType (com.facebook.presto.common.type.VarcharType)4 Map (java.util.Map)4 Collectors.toList (java.util.stream.Collectors.toList)4 RowType (com.facebook.presto.common.type.RowType)3 StandardTypes (com.facebook.presto.common.type.StandardTypes)3 Objects.requireNonNull (java.util.Objects.requireNonNull)3 Set (java.util.Set)3