Search in sources :

Example 6 with TypeSignatureParameter

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

the class SignatureBinder method appendConstraintSolvers.

private boolean appendConstraintSolvers(ImmutableList.Builder<TypeConstraintSolver> resultBuilder, TypeSignature formalTypeSignature, TypeSignatureProvider actualTypeSignatureProvider, boolean allowCoercion) {
    if (FunctionType.NAME.equals(formalTypeSignature.getBase())) {
        List<TypeSignature> formalTypeParameterTypeSignatures = formalTypeSignature.getTypeParametersAsTypeSignatures();
        resultBuilder.add(new FunctionSolver(getLambdaArgumentTypeSignatures(formalTypeSignature), formalTypeParameterTypeSignatures.get(formalTypeParameterTypeSignatures.size() - 1), actualTypeSignatureProvider));
        return true;
    }
    if (actualTypeSignatureProvider.hasDependency()) {
        return false;
    }
    if (formalTypeSignature.getParameters().isEmpty()) {
        TypeVariableConstraint typeVariableConstraint = typeVariableConstraints.get(formalTypeSignature.getBase());
        if (typeVariableConstraint == null) {
            return true;
        }
        Type actualType = typeManager.getType(actualTypeSignatureProvider.getTypeSignature());
        resultBuilder.add(new TypeParameterSolver(formalTypeSignature.getBase(), actualType, typeVariableConstraint.isComparableRequired(), typeVariableConstraint.isOrderableRequired(), Optional.ofNullable(typeVariableConstraint.getVariadicBound())));
        return true;
    }
    Type actualType = typeManager.getType(actualTypeSignatureProvider.getTypeSignature());
    if (isTypeWithLiteralParameters(formalTypeSignature)) {
        resultBuilder.add(new TypeWithLiteralParametersSolver(formalTypeSignature, actualType));
        return true;
    }
    List<TypeSignatureProvider> actualTypeParametersTypeSignatureProvider;
    if (UNKNOWN.equals(actualType)) {
        actualTypeParametersTypeSignatureProvider = Collections.nCopies(formalTypeSignature.getParameters().size(), new TypeSignatureProvider(UNKNOWN.getTypeSignature()));
    } else {
        actualTypeParametersTypeSignatureProvider = fromTypes(actualType.getTypeParameters());
    }
    ImmutableList.Builder<TypeSignature> formalTypeParameterTypeSignatures = ImmutableList.builder();
    for (TypeSignatureParameter formalTypeParameter : formalTypeSignature.getParameters()) {
        Optional<TypeSignature> typeSignature = formalTypeParameter.getTypeSignatureOrNamedTypeSignature();
        if (!typeSignature.isPresent()) {
            throw new UnsupportedOperationException("Types with both type parameters and literal parameters at the same time are not supported");
        }
        formalTypeParameterTypeSignatures.add(typeSignature.get());
    }
    return appendConstraintSolvers(resultBuilder, formalTypeParameterTypeSignatures.build(), actualTypeParametersTypeSignatureProvider, allowCoercion && isCovariantTypeBase(formalTypeSignature.getBase()));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) TypeSignature(com.facebook.presto.spi.type.TypeSignature) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) Type(com.facebook.presto.spi.type.Type) FunctionType(com.facebook.presto.type.FunctionType) TypeSignatureParameter(com.facebook.presto.spi.type.TypeSignatureParameter)

Example 7 with TypeSignatureParameter

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

the class SignatureBinder method checkNoLiteralVariableUsageAcrossTypes.

private static void checkNoLiteralVariableUsageAcrossTypes(TypeSignature typeSignature, Map<String, TypeSignature> existingUsages) {
    List<TypeSignatureParameter> variables = typeSignature.getParameters().stream().filter(TypeSignatureParameter::isVariable).collect(toList());
    for (TypeSignatureParameter variable : variables) {
        TypeSignature existing = existingUsages.get(variable.getVariable());
        if (existing != null && !existing.equals(typeSignature)) {
            throw new UnsupportedOperationException("Literal parameters may not be shared across different types");
        }
        existingUsages.put(variable.getVariable(), typeSignature);
    }
    for (TypeSignatureParameter parameter : typeSignature.getParameters()) {
        if (parameter.isLongLiteral() || parameter.isVariable()) {
            continue;
        }
        checkNoLiteralVariableUsageAcrossTypes(parameter.getTypeSignatureOrNamedTypeSignature().get(), existingUsages);
    }
}
Also used : TypeSignature(com.facebook.presto.spi.type.TypeSignature) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) TypeSignatureParameter(com.facebook.presto.spi.type.TypeSignatureParameter)

Example 8 with TypeSignatureParameter

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

the class MongoPageSource method writeBlock.

private void writeBlock(BlockBuilder output, Type type, Object value) {
    if (isArrayType(type)) {
        if (value instanceof List<?>) {
            BlockBuilder builder = createParametersBlockBuilder(type, ((List<?>) value).size());
            ((List<?>) value).forEach(element -> appendTo(type.getTypeParameters().get(0), element, builder));
            type.writeObject(output, builder.build());
            return;
        }
    } else if (isMapType(type)) {
        if (value instanceof List<?>) {
            BlockBuilder builder = createParametersBlockBuilder(type, ((List<?>) value).size());
            for (Object element : (List<?>) value) {
                if (!(element instanceof Map<?, ?>)) {
                    continue;
                }
                Map<?, ?> document = (Map<?, ?>) element;
                if (document.containsKey("key") && document.containsKey("value")) {
                    appendTo(type.getTypeParameters().get(0), document.get("key"), builder);
                    appendTo(type.getTypeParameters().get(1), document.get("value"), builder);
                }
            }
            type.writeObject(output, builder.build());
            return;
        }
    } else if (isRowType(type)) {
        if (value instanceof Map) {
            Map<?, ?> mapValue = (Map<?, ?>) value;
            BlockBuilder builder = createParametersBlockBuilder(type, mapValue.size());
            List<String> fieldNames = type.getTypeSignature().getParameters().stream().map(TypeSignatureParameter::getNamedTypeSignature).map(NamedTypeSignature::getName).collect(Collectors.toList());
            checkState(fieldNames.size() == type.getTypeParameters().size(), "fieldName doesn't match with type size : %s", type);
            for (int index = 0; index < type.getTypeParameters().size(); index++) {
                appendTo(type.getTypeParameters().get(index), mapValue.get(fieldNames.get(index).toString()), builder);
            }
            type.writeObject(output, builder.build());
            return;
        } else if (value instanceof List<?>) {
            List<?> listValue = (List<?>) value;
            BlockBuilder builder = createParametersBlockBuilder(type, listValue.size());
            for (int index = 0; index < type.getTypeParameters().size(); index++) {
                if (index < listValue.size()) {
                    appendTo(type.getTypeParameters().get(index), listValue.get(index), builder);
                } else {
                    builder.appendNull();
                }
            }
            type.writeObject(output, builder.build());
            return;
        }
    } else {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Block: " + type.getTypeSignature());
    }
    // not a convertible value
    output.appendNull();
}
Also used : TypeSignatureParameter(com.facebook.presto.spi.type.TypeSignatureParameter) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) PrestoException(com.facebook.presto.spi.PrestoException) Map(java.util.Map) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder)

Example 9 with TypeSignatureParameter

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

the class HiveTypeTranslator method translate.

@Override
public TypeInfo translate(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;
        int varcharLength = varcharType.getLength();
        if (varcharLength <= HiveVarchar.MAX_VARCHAR_LENGTH) {
            return getVarcharTypeInfo(varcharLength);
        } else if (varcharLength == VarcharType.UNBOUNDED_LENGTH) {
            return HIVE_STRING.getTypeInfo();
        } else {
            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 = translate(type.getTypeParameters().get(0));
        return getListTypeInfo(elementType);
    }
    if (isMapType(type)) {
        TypeInfo keyType = translate(type.getTypeParameters().get(0));
        TypeInfo valueType = translate(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();
            fieldNames.add(namedTypeSignature.getName());
        }
        return getStructTypeInfo(fieldNames.build(), type.getTypeParameters().stream().map(this::translate).collect(toList()));
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", type));
}
Also used : VarcharType(com.facebook.presto.spi.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) PrestoException(com.facebook.presto.spi.PrestoException) 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) TypeInfoFactory.getMapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getMapTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) TypeInfoFactory.getListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getListTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeSignatureParameter(com.facebook.presto.spi.type.TypeSignatureParameter) DecimalType(com.facebook.presto.spi.type.DecimalType) CharType(com.facebook.presto.spi.type.CharType)

Example 10 with TypeSignatureParameter

use of com.facebook.presto.spi.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.toString()) {
        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 "varchar":
            builder.setSigned(true);
            builder.setPrecision(VARCHAR_MAX);
            builder.setScale(0);
            builder.setColumnDisplaySize(VARCHAR_MAX);
            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.spi.type.TypeSignatureParameter) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

TypeSignatureParameter (com.facebook.presto.spi.type.TypeSignatureParameter)12 NamedTypeSignature (com.facebook.presto.spi.type.NamedTypeSignature)9 ImmutableList (com.google.common.collect.ImmutableList)7 Type (com.facebook.presto.spi.type.Type)6 TypeSignature (com.facebook.presto.spi.type.TypeSignature)6 PrestoException (com.facebook.presto.spi.PrestoException)4 VarcharType.createUnboundedVarcharType (com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 CharType (com.facebook.presto.spi.type.CharType)2 CharType.createCharType (com.facebook.presto.spi.type.CharType.createCharType)2 DecimalType (com.facebook.presto.spi.type.DecimalType)2 DecimalType.createDecimalType (com.facebook.presto.spi.type.DecimalType.createDecimalType)2 VarcharType (com.facebook.presto.spi.type.VarcharType)2 VarcharType.createVarcharType (com.facebook.presto.spi.type.VarcharType.createVarcharType)2 Set (java.util.Set)2 TypeInfoUtils.getTypeInfoFromTypeString (org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils.getTypeInfoFromTypeString)2 OBJECT_ID (com.facebook.presto.mongodb.ObjectIdType.OBJECT_ID)1 OrcType (com.facebook.presto.orc.metadata.OrcType)1 ColumnHandle (com.facebook.presto.spi.ColumnHandle)1