Search in sources :

Example 11 with TypeSignatureParameter

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

Example 12 with TypeSignatureParameter

use of com.facebook.presto.common.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 = functionAndTypeManager.getType(actualTypeSignatureProvider.getTypeSignature());
        resultBuilder.add(new TypeParameterSolver(formalTypeSignature.getBase(), actualType, typeVariableConstraint));
        return true;
    }
    Type actualType = functionAndTypeManager.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) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TypeVariableConstraint(com.facebook.presto.spi.function.TypeVariableConstraint) TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) Type(com.facebook.presto.common.type.Type) FunctionType(com.facebook.presto.common.type.FunctionType) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter)

Example 13 with TypeSignatureParameter

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

the class SignatureBinder method applyBoundVariables.

public static Type applyBoundVariables(FunctionAndTypeManager functionAndTypeManager, TypeSignature typeSignature, BoundVariables boundVariables) {
    String baseType = typeSignature.getBase();
    if (boundVariables.containsTypeVariable(baseType)) {
        checkState(typeSignature.getParameters().isEmpty(), "Type parameters cannot have parameters");
        Type type = boundVariables.getTypeVariable(baseType);
        if (type instanceof TypeWithName) {
            return ((TypeWithName) type).getType();
        }
        return type;
    }
    List<TypeSignatureParameter> parameters = typeSignature.getParameters().stream().map(typeSignatureParameter -> applyBoundVariables(typeSignatureParameter, boundVariables)).collect(toList());
    return functionAndTypeManager.getParameterizedType(baseType, parameters);
}
Also used : StandardTypes(com.facebook.presto.common.type.StandardTypes) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) LongVariableConstraint(com.facebook.presto.spi.function.LongVariableConstraint) HashMap(java.util.HashMap) TypeSignatureProvider.fromTypes(com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes) TypeSignature(com.facebook.presto.common.type.TypeSignature) HashSet(java.util.HashSet) TypeCalculation.calculateLiteralValue(com.facebook.presto.type.TypeCalculation.calculateLiteralValue) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toMap(java.util.stream.Collectors.toMap) Verify.verify(com.google.common.base.Verify.verify) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) ParameterKind(com.facebook.presto.common.type.ParameterKind) Type(com.facebook.presto.common.type.Type) TypeWithName(com.facebook.presto.common.type.TypeWithName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) VerifyException(com.google.common.base.VerifyException) ImmutableSet(com.google.common.collect.ImmutableSet) TypeVariableConstraint(com.facebook.presto.spi.function.TypeVariableConstraint) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) FunctionType(com.facebook.presto.common.type.FunctionType) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Objects(java.util.Objects) List(java.util.List) UNKNOWN(com.facebook.presto.common.type.UnknownType.UNKNOWN) Collectors.toList(java.util.stream.Collectors.toList) Signature(com.facebook.presto.spi.function.Signature) Function.identity(java.util.function.Function.identity) TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) Optional(java.util.Optional) Collections(java.util.Collections) Type(com.facebook.presto.common.type.Type) FunctionType(com.facebook.presto.common.type.FunctionType) TypeWithName(com.facebook.presto.common.type.TypeWithName) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter)

Example 14 with TypeSignatureParameter

use of com.facebook.presto.common.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 = output.beginBlockEntry();
            ((List<?>) value).forEach(element -> appendTo(type.getTypeParameters().get(0), element, builder));
            output.closeEntry();
            return;
        }
    } else if (isMapType(type)) {
        if (value instanceof List<?>) {
            BlockBuilder builder = output.beginBlockEntry();
            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);
                }
            }
            output.closeEntry();
            return;
        } else if (value instanceof Map) {
            BlockBuilder builder = output.beginBlockEntry();
            Map<?, ?> document = (Map<?, ?>) value;
            for (Map.Entry<?, ?> entry : document.entrySet()) {
                appendTo(type.getTypeParameters().get(0), entry.getKey(), builder);
                appendTo(type.getTypeParameters().get(1), entry.getValue(), builder);
            }
            output.closeEntry();
            return;
        }
    } else if (isRowType(type)) {
        if (value instanceof Map) {
            Map<?, ?> mapValue = (Map<?, ?>) value;
            BlockBuilder builder = output.beginBlockEntry();
            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));
            }
            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)), builder);
            }
            output.closeEntry();
            return;
        } else if (value instanceof List<?>) {
            List<?> listValue = (List<?>) value;
            BlockBuilder builder = output.beginBlockEntry();
            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();
                }
            }
            output.closeEntry();
            return;
        }
    } else {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Block: " + type.getTypeSignature());
    }
    // not a convertible value
    output.appendNull();
}
Also used : ArrayList(java.util.ArrayList) PrestoException(com.facebook.presto.spi.PrestoException) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Map(java.util.Map) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 15 with TypeSignatureParameter

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

the class ColumnIOConverter method constructField.

public static Optional<Field> constructField(Type type, ColumnIO columnIO) {
    if (columnIO == null) {
        return Optional.empty();
    }
    boolean required = columnIO.getType().getRepetition() != OPTIONAL;
    int repetitionLevel = columnRepetitionLevel(columnIO);
    int definitionLevel = columnDefinitionLevel(columnIO);
    if (ROW.equals(type.getTypeSignature().getBase())) {
        GroupColumnIO groupColumnIO = (GroupColumnIO) columnIO;
        List<Type> parameters = type.getTypeParameters();
        ImmutableList.Builder<Optional<Field>> fieldsBuilder = ImmutableList.builder();
        List<TypeSignatureParameter> fields = type.getTypeSignature().getParameters();
        boolean structHasParameters = false;
        for (int i = 0; i < fields.size(); i++) {
            NamedTypeSignature namedTypeSignature = fields.get(i).getNamedTypeSignature();
            String name = namedTypeSignature.getName().get().toLowerCase(Locale.ENGLISH);
            Optional<Field> field = constructField(parameters.get(i), lookupColumnByName(groupColumnIO, name));
            structHasParameters |= field.isPresent();
            fieldsBuilder.add(field);
        }
        if (structHasParameters) {
            return Optional.of(new GroupField(type, repetitionLevel, definitionLevel, required, fieldsBuilder.build()));
        }
        return Optional.empty();
    } else if (MAP.equals(type.getTypeSignature().getBase())) {
        GroupColumnIO groupColumnIO = (GroupColumnIO) columnIO;
        MapType mapType = (MapType) type;
        GroupColumnIO keyValueColumnIO = getMapKeyValueColumn(groupColumnIO);
        if (keyValueColumnIO.getChildrenCount() != 2) {
            return Optional.empty();
        }
        Optional<Field> keyField = constructField(mapType.getKeyType(), keyValueColumnIO.getChild(0));
        Optional<Field> valueField = constructField(mapType.getValueType(), keyValueColumnIO.getChild(1));
        return Optional.of(new GroupField(type, repetitionLevel, definitionLevel, required, ImmutableList.of(keyField, valueField)));
    } else if (ARRAY.equals(type.getTypeSignature().getBase())) {
        GroupColumnIO groupColumnIO = (GroupColumnIO) columnIO;
        List<Type> types = type.getTypeParameters();
        if (groupColumnIO.getChildrenCount() != 1) {
            return Optional.empty();
        }
        Optional<Field> field = constructField(types.get(0), getArrayElementColumn(groupColumnIO.getChild(0)));
        return Optional.of(new GroupField(type, repetitionLevel, definitionLevel, required, ImmutableList.of(field)));
    }
    PrimitiveColumnIO primitiveColumnIO = (PrimitiveColumnIO) columnIO;
    RichColumnDescriptor column = new RichColumnDescriptor(primitiveColumnIO.getColumnDescriptor(), columnIO.getType().asPrimitiveType());
    return Optional.of(new PrimitiveField(type, repetitionLevel, definitionLevel, required, column, primitiveColumnIO.getId()));
}
Also used : Optional(java.util.Optional) ImmutableList(com.google.common.collect.ImmutableList) GroupField(com.facebook.presto.parquet.GroupField) RichColumnDescriptor(com.facebook.presto.parquet.RichColumnDescriptor) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) MapType(com.facebook.presto.common.type.MapType) GroupField(com.facebook.presto.parquet.GroupField) PrimitiveField(com.facebook.presto.parquet.PrimitiveField) Field(com.facebook.presto.parquet.Field) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) PrimitiveField(com.facebook.presto.parquet.PrimitiveField)

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