Search in sources :

Example 1 with TypeVariableConstraint

use of io.prestosql.spi.function.TypeVariableConstraint in project hetu-core by openlookeng.

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"), new TypeVariableConstraint("T2", true, false, "varchar"))).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.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) Signature(io.prestosql.spi.function.Signature) TypeSignature(io.prestosql.spi.type.TypeSignature) TypeVariableConstraint(io.prestosql.spi.function.TypeVariableConstraint) Test(org.testng.annotations.Test)

Example 2 with TypeVariableConstraint

use of io.prestosql.spi.function.TypeVariableConstraint in project hetu-core by openlookeng.

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();
        if (orderableRequired.contains(name)) {
            typeVariableConstraints.add(orderableTypeParameter(name));
        } else if (comparableRequired.contains(name)) {
            typeVariableConstraints.add(comparableTypeParameter(name));
        } else {
            typeVariableConstraints.add(typeVariable(name));
        }
    }
    return typeVariableConstraints.build();
}
Also used : Signature.orderableTypeParameter(io.prestosql.spi.function.Signature.orderableTypeParameter) Signature.comparableTypeParameter(io.prestosql.spi.function.Signature.comparableTypeParameter) TypeParameter(io.prestosql.spi.function.TypeParameter) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) OperatorType(io.prestosql.spi.function.OperatorType) TypeVariableConstraint(io.prestosql.spi.function.TypeVariableConstraint) HashSet(java.util.HashSet)

Example 3 with TypeVariableConstraint

use of io.prestosql.spi.function.TypeVariableConstraint in project hetu-core by openlookeng.

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.isComparableRequired(), typeVariableConstraint.isOrderableRequired(), Optional.ofNullable(typeVariableConstraint.getVariadicBound())));
        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 = TypeSignatureProvider.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(io.prestosql.spi.function.TypeVariableConstraint) TypeSignatureProvider(io.prestosql.sql.analyzer.TypeSignatureProvider) NamedTypeSignature(io.prestosql.spi.type.NamedTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) Type(io.prestosql.spi.type.Type) FunctionType(io.prestosql.spi.type.FunctionType) TypeSignatureParameter(io.prestosql.spi.type.TypeSignatureParameter)

Example 4 with TypeVariableConstraint

use of io.prestosql.spi.function.TypeVariableConstraint in project hetu-core by openlookeng.

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.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) WindowFunctionSignature(io.prestosql.spi.function.WindowFunctionSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) Signature(io.prestosql.spi.function.Signature) TypeVariableConstraint(io.prestosql.spi.function.TypeVariableConstraint)

Aggregations

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