Search in sources :

Example 11 with TypeSignature

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

the class FunctionRegistry method doGetSpecializedFunctionKey.

private SpecializedFunctionKey doGetSpecializedFunctionKey(Signature signature) {
    Iterable<SqlFunction> candidates = functions.get(QualifiedName.of(signature.getName()));
    // search for exact match
    Type returnType = typeManager.getType(signature.getReturnType());
    List<TypeSignatureProvider> argumentTypeSignatureProviders = fromTypeSignatures(signature.getArgumentTypes());
    for (SqlFunction candidate : candidates) {
        Optional<BoundVariables> boundVariables = new SignatureBinder(typeManager, candidate.getSignature(), false).bindVariables(argumentTypeSignatureProviders, returnType);
        if (boundVariables.isPresent()) {
            return new SpecializedFunctionKey(candidate, boundVariables.get(), argumentTypeSignatureProviders.size());
        }
    }
    // TODO: hack because there could be "type only" coercions (which aren't necessarily included as implicit casts),
    // so do a second pass allowing "type only" coercions
    List<Type> argumentTypes = resolveTypes(signature.getArgumentTypes(), typeManager);
    for (SqlFunction candidate : candidates) {
        SignatureBinder binder = new SignatureBinder(typeManager, candidate.getSignature(), true);
        Optional<BoundVariables> boundVariables = binder.bindVariables(argumentTypeSignatureProviders, returnType);
        if (!boundVariables.isPresent()) {
            continue;
        }
        Signature boundSignature = applyBoundVariables(candidate.getSignature(), boundVariables.get(), argumentTypes.size());
        if (!typeManager.isTypeOnlyCoercion(typeManager.getType(boundSignature.getReturnType()), returnType)) {
            continue;
        }
        boolean nonTypeOnlyCoercion = false;
        for (int i = 0; i < argumentTypes.size(); i++) {
            Type expectedType = typeManager.getType(boundSignature.getArgumentTypes().get(i));
            if (!typeManager.isTypeOnlyCoercion(argumentTypes.get(i), expectedType)) {
                nonTypeOnlyCoercion = true;
                break;
            }
        }
        if (nonTypeOnlyCoercion) {
            continue;
        }
        return new SpecializedFunctionKey(candidate, boundVariables.get(), argumentTypes.size());
    }
    // TODO: this is a hack and should be removed
    if (signature.getName().startsWith(MAGIC_LITERAL_FUNCTION_PREFIX)) {
        List<TypeSignature> parameterTypes = signature.getArgumentTypes();
        // extract type from function name
        String typeName = signature.getName().substring(MAGIC_LITERAL_FUNCTION_PREFIX.length());
        // lookup the type
        Type type = typeManager.getType(parseTypeSignature(typeName));
        requireNonNull(type, format("Type %s not registered", typeName));
        // verify we have one parameter of the proper type
        checkArgument(parameterTypes.size() == 1, "Expected one argument to literal function, but got %s", parameterTypes);
        Type parameterType = typeManager.getType(parameterTypes.get(0));
        requireNonNull(parameterType, format("Type %s not found", parameterTypes.get(0)));
        return new SpecializedFunctionKey(magicLiteralFunction, BoundVariables.builder().setTypeVariable("T", parameterType).setTypeVariable("R", type).build(), 1);
    }
    throw new PrestoException(FUNCTION_IMPLEMENTATION_MISSING, format("%s not found", signature));
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) OperatorType(com.facebook.presto.spi.function.OperatorType) Type(com.facebook.presto.spi.type.Type) VarcharType(com.facebook.presto.spi.type.VarcharType) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) TypeSignature(com.facebook.presto.spi.type.TypeSignature) SignatureBinder.applyBoundVariables(com.facebook.presto.metadata.SignatureBinder.applyBoundVariables) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) TypeSignature(com.facebook.presto.spi.type.TypeSignature)

Example 12 with TypeSignature

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

the class PolymorphicScalarFunction method specialize.

@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
    List<TypeSignature> resolvedParameterTypeSignatures = applyBoundVariables(getSignature().getArgumentTypes(), boundVariables);
    List<Type> resolvedParameterTypes = resolveTypes(resolvedParameterTypeSignatures, typeManager);
    TypeSignature resolvedReturnTypeSignature = applyBoundVariables(getSignature().getReturnType(), boundVariables);
    Type resolvedReturnType = typeManager.getType(resolvedReturnTypeSignature);
    SpecializeContext context = new SpecializeContext(boundVariables, resolvedParameterTypes, resolvedReturnType, typeManager, functionRegistry);
    Optional<Method> matchingMethod = Optional.empty();
    Optional<MethodsGroup> matchingMethodsGroup = Optional.empty();
    for (MethodsGroup candidateMethodsGroup : methodsGroups) {
        for (Method candidateMethod : candidateMethodsGroup.getMethods()) {
            if (matchesParameterAndReturnTypes(candidateMethod, resolvedParameterTypes, resolvedReturnType) && predicateIsTrue(candidateMethodsGroup, context)) {
                if (matchingMethod.isPresent()) {
                    if (onlyFirstMatchedMethodHasPredicate(matchingMethodsGroup.get(), candidateMethodsGroup)) {
                        continue;
                    }
                    throw new IllegalStateException("two matching methods (" + matchingMethod.get().getName() + " and " + candidateMethod.getName() + ") for parameter types " + resolvedParameterTypeSignatures);
                }
                matchingMethod = Optional.of(candidateMethod);
                matchingMethodsGroup = Optional.of(candidateMethodsGroup);
            }
        }
    }
    checkState(matchingMethod.isPresent(), "no matching method for parameter types %s", resolvedParameterTypes);
    List<Object> extraParameters = computeExtraParameters(matchingMethodsGroup.get(), context);
    MethodHandle matchingMethodHandle = applyExtraParameters(matchingMethod.get(), extraParameters);
    return new ScalarFunctionImplementation(nullableResult, nullableArguments, nullFlags, matchingMethodHandle, deterministic);
}
Also used : ScalarFunctionImplementation(com.facebook.presto.operator.scalar.ScalarFunctionImplementation) MethodsGroup(com.facebook.presto.metadata.SqlScalarFunctionBuilder.MethodsGroup) Method(java.lang.reflect.Method) SpecializeContext(com.facebook.presto.metadata.SqlScalarFunctionBuilder.SpecializeContext) TypeSignature(com.facebook.presto.spi.type.TypeSignature) Type(com.facebook.presto.spi.type.Type) MethodHandle(java.lang.invoke.MethodHandle)

Example 13 with TypeSignature

use of com.facebook.presto.spi.type.TypeSignature 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 14 with TypeSignature

use of com.facebook.presto.spi.type.TypeSignature 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 15 with TypeSignature

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

the class AggregationCompiler method getInputTypesSignatures.

private static List<TypeSignature> getInputTypesSignatures(Method inputFunction) {
    // FIXME Literal parameters should be part of class annotations.
    ImmutableList.Builder<TypeSignature> builder = ImmutableList.builder();
    Set<String> literalParameters = getLiteralParameter(inputFunction);
    Annotation[][] parameterAnnotations = inputFunction.getParameterAnnotations();
    for (Annotation[] annotations : parameterAnnotations) {
        for (Annotation annotation : annotations) {
            if (annotation instanceof SqlType) {
                String typeName = ((SqlType) annotation).value();
                builder.add(parseTypeSignature(typeName, literalParameters));
            }
        }
    }
    return builder.build();
}
Also used : TypeSignature(com.facebook.presto.spi.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) ImmutableList(com.google.common.collect.ImmutableList) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) SqlType(com.facebook.presto.spi.function.SqlType) Annotation(java.lang.annotation.Annotation)

Aggregations

TypeSignature (com.facebook.presto.spi.type.TypeSignature)34 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)19 ImmutableList (com.google.common.collect.ImmutableList)14 NamedTypeSignature (com.facebook.presto.spi.type.NamedTypeSignature)10 Test (org.testng.annotations.Test)10 Signature (com.facebook.presto.metadata.Signature)9 PrestoException (com.facebook.presto.spi.PrestoException)8 Type (com.facebook.presto.spi.type.Type)6 TypeSignatureParameter (com.facebook.presto.spi.type.TypeSignatureParameter)6 ImmutableSet (com.google.common.collect.ImmutableSet)6 Slice (io.airlift.slice.Slice)6 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)5 SCALAR (com.facebook.presto.metadata.FunctionKind.SCALAR)5 SqlScalarFunction (com.facebook.presto.metadata.SqlScalarFunction)5 SqlScalarFunctionBuilder (com.facebook.presto.metadata.SqlScalarFunctionBuilder)5 SpecializeContext (com.facebook.presto.metadata.SqlScalarFunctionBuilder.SpecializeContext)5 SqlType (com.facebook.presto.spi.function.SqlType)5 DecimalType (com.facebook.presto.spi.type.DecimalType)5 Decimals (com.facebook.presto.spi.type.Decimals)5 Decimals.encodeUnscaledValue (com.facebook.presto.spi.type.Decimals.encodeUnscaledValue)5