Search in sources :

Example 1 with TypeVariableConstraint

use of com.facebook.presto.spi.function.TypeVariableConstraint in project presto by prestodb.

the class TestSignatureBinder method testBindVarcharTemplateStyle.

@Test
public void testBindVarcharTemplateStyle() {
    Signature function = functionSignature().returnType(parseTypeSignature("T2")).argumentTypes(parseTypeSignature("T1")).typeVariableConstraints(ImmutableList.of(new TypeVariableConstraint("T1", true, false, "varchar", false), new TypeVariableConstraint("T2", true, false, "varchar", false))).build();
    assertThat(function).boundTo(ImmutableList.of("varchar(42)"), "varchar(1)").produces(new BoundVariables(ImmutableMap.of("T1", type("varchar(42)"), "T2", type("varchar(1)")), ImmutableMap.of()));
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) TypeVariableConstraint(com.facebook.presto.spi.function.TypeVariableConstraint) Test(org.testng.annotations.Test)

Example 2 with TypeVariableConstraint

use of com.facebook.presto.spi.function.TypeVariableConstraint 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 3 with TypeVariableConstraint

use of com.facebook.presto.spi.function.TypeVariableConstraint in project presto by prestodb.

the class WindowAnnotationsParser method parse.

private static SqlWindowFunction parse(Class<? extends WindowFunction> clazz, WindowFunctionSignature window) {
    List<TypeVariableConstraint> typeVariables = ImmutableList.of();
    if (!window.typeVariable().isEmpty()) {
        typeVariables = ImmutableList.of(typeVariable(window.typeVariable()));
    }
    List<TypeSignature> argumentTypes = Stream.of(window.argumentTypes()).map(TypeSignature::parseTypeSignature).collect(toImmutableList());
    Signature signature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, window.name()), WINDOW, typeVariables, ImmutableList.of(), parseTypeSignature(window.returnType()), argumentTypes, false);
    return new SqlWindowFunction(new ReflectionWindowFunctionSupplier<>(signature, clazz));
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) WindowFunctionSignature(com.facebook.presto.spi.function.WindowFunctionSignature) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) TypeVariableConstraint(com.facebook.presto.spi.function.TypeVariableConstraint)

Example 4 with TypeVariableConstraint

use of com.facebook.presto.spi.function.TypeVariableConstraint in project presto by prestodb.

the class FunctionsParserHelper method createTypeVariableConstraints.

public static List<TypeVariableConstraint> createTypeVariableConstraints(Iterable<TypeParameter> typeParameters, List<ImplementationDependency> dependencies) {
    Set<String> orderableRequired = new HashSet<>();
    Set<String> comparableRequired = new HashSet<>();
    for (ImplementationDependency dependency : dependencies) {
        if (dependency instanceof OperatorImplementationDependency) {
            OperatorType operator = ((OperatorImplementationDependency) dependency).getOperator();
            if (operator == CAST) {
                continue;
            }
            Set<String> argumentTypes = ((OperatorImplementationDependency) dependency).getArgumentTypes().stream().map(TypeSignature::getBase).collect(toImmutableSet());
            checkArgument(argumentTypes.size() == 1, "Operator dependency must only have arguments of a single type");
            String argumentType = Iterables.getOnlyElement(argumentTypes);
            if (COMPARABLE_TYPE_OPERATORS.contains(operator)) {
                comparableRequired.add(argumentType);
            }
            if (ORDERABLE_TYPE_OPERATORS.contains(operator)) {
                orderableRequired.add(argumentType);
            }
        }
    }
    ImmutableList.Builder<TypeVariableConstraint> typeVariableConstraints = ImmutableList.builder();
    for (TypeParameter typeParameter : typeParameters) {
        String name = typeParameter.value();
        String variadicBound = typeParameter.boundedBy().isEmpty() ? null : typeParameter.boundedBy();
        checkArgument(variadicBound == null || PARAMETRIC_TYPES.contains(variadicBound), "boundedBy must be a parametric type, got %s", variadicBound);
        if (orderableRequired.contains(name)) {
            typeVariableConstraints.add(new TypeVariableConstraint(name, false, true, variadicBound, false));
        } else if (comparableRequired.contains(name)) {
            typeVariableConstraints.add(new TypeVariableConstraint(name, true, false, variadicBound, false));
        } else {
            typeVariableConstraints.add(new TypeVariableConstraint(name, false, false, variadicBound, false));
        }
    }
    return typeVariableConstraints.build();
}
Also used : TypeParameter(com.facebook.presto.spi.function.TypeParameter) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) OperatorType(com.facebook.presto.common.function.OperatorType) TypeVariableConstraint(com.facebook.presto.spi.function.TypeVariableConstraint) HashSet(java.util.HashSet)

Aggregations

TypeVariableConstraint (com.facebook.presto.spi.function.TypeVariableConstraint)4 TypeSignature (com.facebook.presto.common.type.TypeSignature)3 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)2 Signature (com.facebook.presto.spi.function.Signature)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 OperatorType (com.facebook.presto.common.function.OperatorType)1 FunctionType (com.facebook.presto.common.type.FunctionType)1 NamedTypeSignature (com.facebook.presto.common.type.NamedTypeSignature)1 Type (com.facebook.presto.common.type.Type)1 TypeSignatureParameter (com.facebook.presto.common.type.TypeSignatureParameter)1 TypeParameter (com.facebook.presto.spi.function.TypeParameter)1 WindowFunctionSignature (com.facebook.presto.spi.function.WindowFunctionSignature)1 TypeSignatureProvider (com.facebook.presto.sql.analyzer.TypeSignatureProvider)1 HashSet (java.util.HashSet)1 Test (org.testng.annotations.Test)1