Search in sources :

Example 61 with TypeSignature

use of io.trino.spi.type.TypeSignature in project trino by trinodb.

the class SignatureBinder method expandVarargFormalTypeSignature.

private static List<TypeSignature> expandVarargFormalTypeSignature(List<TypeSignature> formalTypeSignatures, int actualArity) {
    int variableArityArgumentsCount = actualArity - formalTypeSignatures.size() + 1;
    if (variableArityArgumentsCount == 0) {
        return formalTypeSignatures.subList(0, formalTypeSignatures.size() - 1);
    }
    if (variableArityArgumentsCount == 1) {
        return formalTypeSignatures;
    }
    checkArgument(variableArityArgumentsCount > 1 && !formalTypeSignatures.isEmpty());
    ImmutableList.Builder<TypeSignature> builder = ImmutableList.builder();
    builder.addAll(formalTypeSignatures);
    TypeSignature lastTypeSignature = formalTypeSignatures.get(formalTypeSignatures.size() - 1);
    for (int i = 1; i < variableArityArgumentsCount; i++) {
        builder.add(lastTypeSignature);
    }
    return builder.build();
}
Also used : NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList)

Example 62 with TypeSignature

use of io.trino.spi.type.TypeSignature in project trino by trinodb.

the class SignatureBinder method applyBoundVariables.

private static Signature applyBoundVariables(Signature signature, TypeVariables typeVariables, int arity) {
    List<TypeSignature> argumentSignatures;
    if (signature.isVariableArity()) {
        argumentSignatures = expandVarargFormalTypeSignature(signature.getArgumentTypes(), arity);
    } else {
        checkArgument(signature.getArgumentTypes().size() == arity);
        argumentSignatures = signature.getArgumentTypes();
    }
    List<TypeSignature> boundArgumentSignatures = applyBoundVariables(argumentSignatures, typeVariables);
    TypeSignature boundReturnTypeSignature = applyBoundVariables(signature.getReturnType(), typeVariables);
    return new Signature(signature.getName(), ImmutableList.of(), ImmutableList.of(), boundReturnTypeSignature, boundArgumentSignatures, false);
}
Also used : NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature)

Example 63 with TypeSignature

use of io.trino.spi.type.TypeSignature in project trino by trinodb.

the class SignatureBinder method extractBoundVariables.

private static void extractBoundVariables(BoundSignature boundSignature, Signature declaredSignature, Map<String, TypeVariableConstraint> typeVariableConstraints, BoundVariables bindings, Type actualType, TypeSignature declaredTypeSignature) {
    // type without nested type parameters
    if (declaredTypeSignature.getParameters().isEmpty()) {
        String typeVariable = declaredTypeSignature.getBase();
        TypeVariableConstraint typeVariableConstraint = typeVariableConstraints.get(typeVariable);
        if (typeVariableConstraint == null) {
            return;
        }
        if (bindings.containsTypeVariable(typeVariable)) {
            Type existingTypeBinding = bindings.getTypeVariable(typeVariable);
            verifyBoundSignature(actualType.equals(existingTypeBinding), boundSignature, declaredSignature);
        } else {
            bindings.setTypeVariable(typeVariable, actualType);
        }
        return;
    }
    verifyBoundSignature(declaredTypeSignature.getBase().equalsIgnoreCase(actualType.getTypeSignature().getBase()), boundSignature, declaredSignature);
    // type with nested literal parameters
    if (isTypeWithLiteralParameters(declaredTypeSignature)) {
        for (int i = 0; i < declaredTypeSignature.getParameters().size(); i++) {
            TypeSignatureParameter typeSignatureParameter = declaredTypeSignature.getParameters().get(i);
            Long actualLongBinding = actualType.getTypeSignature().getParameters().get(i).getLongLiteral();
            if (typeSignatureParameter.getKind() == ParameterKind.VARIABLE) {
                if (bindings.containsLongVariable(typeSignatureParameter.getVariable())) {
                    Long existingLongBinding = bindings.getLongVariable(typeSignatureParameter.getVariable());
                    verifyBoundSignature(actualLongBinding.equals(existingLongBinding), boundSignature, declaredSignature);
                } else {
                    bindings.setLongVariable(typeSignatureParameter.getVariable(), actualLongBinding);
                }
            } else {
                verify(typeSignatureParameter.getKind() == ParameterKind.LONG);
                verifyBoundSignature(actualLongBinding.equals(typeSignatureParameter.getLongLiteral()), boundSignature, declaredSignature);
            }
        }
        return;
    }
    // type with nested type parameters
    List<Type> actualTypeParameters = actualType.getTypeParameters();
    // unknown types are assumed to have unknown nested types
    if (UNKNOWN.equals(actualType)) {
        actualTypeParameters = Collections.nCopies(declaredTypeSignature.getParameters().size(), UNKNOWN);
    }
    verifyBoundSignature(declaredTypeSignature.getParameters().size() == actualTypeParameters.size(), boundSignature, declaredSignature);
    for (int i = 0; i < declaredTypeSignature.getParameters().size(); i++) {
        TypeSignatureParameter typeSignatureParameter = declaredTypeSignature.getParameters().get(i);
        TypeSignature typeSignature = typeSignatureParameter.getTypeSignatureOrNamedTypeSignature().orElseThrow(() -> new UnsupportedOperationException("Types with both type parameters and literal parameters at the same time are not supported"));
        Type actualTypeParameter = actualTypeParameters.get(i);
        extractBoundVariables(boundSignature, declaredSignature, typeVariableConstraints, bindings, actualTypeParameter, typeSignature);
    }
}
Also used : JsonType(io.trino.type.JsonType) Type(io.trino.spi.type.Type) FunctionType(io.trino.type.FunctionType) RowType(io.trino.spi.type.RowType) UnknownType(io.trino.type.UnknownType) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter)

Example 64 with TypeSignature

use of io.trino.spi.type.TypeSignature in project trino by trinodb.

the class SignatureBinder method appendConstraintSolvers.

private boolean appendConstraintSolvers(ImmutableList.Builder<TypeConstraintSolver> resultBuilder, TypeSignature formalTypeSignature, TypeSignatureProvider actualTypeSignatureProvider, boolean allowCoercion) {
    if (FunctionType.NAME.equalsIgnoreCase(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());
        for (TypeSignature castToSignature : typeVariableConstraint.getCastableTo()) {
            appendTypeRelationshipConstraintSolver(resultBuilder, castToSignature, actualTypeSignatureProvider, EXPLICIT_COERCION_TO);
        }
        for (TypeSignature castFromSignature : typeVariableConstraint.getCastableFrom()) {
            appendTypeRelationshipConstraintSolver(resultBuilder, castFromSignature, actualTypeSignatureProvider, EXPLICIT_COERCION_FROM);
        }
        if (typeVariableConstraint.getVariadicBound() != null && !typeVariableConstraint.getVariadicBound().equalsIgnoreCase(actualType.getTypeSignature().getBase())) {
            return actualType == UNKNOWN;
        }
        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.isEmpty()) {
            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) TypeSignatureProvider(io.trino.sql.analyzer.TypeSignatureProvider) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) JsonType(io.trino.type.JsonType) Type(io.trino.spi.type.Type) FunctionType(io.trino.type.FunctionType) RowType(io.trino.spi.type.RowType) UnknownType(io.trino.type.UnknownType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter)

Example 65 with TypeSignature

use of io.trino.spi.type.TypeSignature in project trino by trinodb.

the class TypeRegistry method addType.

public void addType(String alias, Type type) {
    requireNonNull(alias, "alias is null");
    requireNonNull(type, "type is null");
    Type existingType = types.putIfAbsent(new TypeSignature(alias), type);
    checkState(existingType == null || existingType.equals(type), "Alias %s is already mapped to %s", alias, type);
}
Also used : DecimalParametricType(io.trino.type.DecimalParametricType) VarcharParametricType(io.trino.type.VarcharParametricType) Re2JRegexpType(io.trino.type.Re2JRegexpType) ParametricType(io.trino.spi.type.ParametricType) Type(io.trino.spi.type.Type) CharParametricType(io.trino.type.CharParametricType) OperatorType(io.trino.spi.function.OperatorType) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureTranslator.toTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.toTypeSignature)

Aggregations

TypeSignature (io.trino.spi.type.TypeSignature)78 Test (org.testng.annotations.Test)49 TypeSignatureTranslator.parseTypeSignature (io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature)35 ImmutableList (com.google.common.collect.ImmutableList)19 Signature (io.trino.metadata.Signature)17 NamedTypeSignature (io.trino.spi.type.NamedTypeSignature)17 Type (io.trino.spi.type.Type)15 ArrayType (io.trino.spi.type.ArrayType)14 List (java.util.List)13 BoundSignature (io.trino.metadata.BoundSignature)11 Optional (java.util.Optional)10 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 ImmutableSet (com.google.common.collect.ImmutableSet)9 TrinoException (io.trino.spi.TrinoException)9 DecimalType (io.trino.spi.type.DecimalType)9 TypeSignatureParameter (io.trino.spi.type.TypeSignatureParameter)9 Objects.requireNonNull (java.util.Objects.requireNonNull)9 BIGINT (io.trino.spi.type.BigintType.BIGINT)8 SqlScalarFunction (io.trino.metadata.SqlScalarFunction)7 ADD (io.trino.spi.function.OperatorType.ADD)7