Search in sources :

Example 6 with TypeSignatureProvider

use of com.facebook.presto.sql.analyzer.TypeSignatureProvider in project presto by prestodb.

the class TestFunctionAndTypeManager method testSessionFunctions.

@Test
public void testSessionFunctions() {
    FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
    SqlFunctionId bigintSignature = new SqlFunctionId(QualifiedObjectName.valueOf("presto.default.foo"), ImmutableList.of(parseTypeSignature("bigint")));
    SqlInvokedFunction bigintFunction = new SqlInvokedFunction(bigintSignature.getFunctionName(), ImmutableList.of(new Parameter("x", parseTypeSignature("bigint"))), parseTypeSignature("bigint"), "", RoutineCharacteristics.builder().build(), "", notVersioned());
    SqlFunctionId varcharSignature = new SqlFunctionId(QualifiedObjectName.valueOf("presto.default.foo"), ImmutableList.of(parseTypeSignature("varchar")));
    SqlInvokedFunction varcharFunction = new SqlInvokedFunction(bigintSignature.getFunctionName(), ImmutableList.of(new Parameter("x", parseTypeSignature("varchar"))), parseTypeSignature("varchar"), "", RoutineCharacteristics.builder().build(), "", notVersioned());
    Map<SqlFunctionId, SqlInvokedFunction> sessionFunctions = ImmutableMap.of(bigintSignature, bigintFunction, varcharSignature, varcharFunction);
    assertEquals(functionAndTypeManager.resolveFunction(Optional.of(sessionFunctions), Optional.empty(), bigintSignature.getFunctionName(), ImmutableList.of(new TypeSignatureProvider(parseTypeSignature("bigint")))), new SessionFunctionHandle(bigintFunction));
    assertEquals(functionAndTypeManager.resolveFunction(Optional.of(sessionFunctions), Optional.empty(), varcharSignature.getFunctionName(), ImmutableList.of(new TypeSignatureProvider(parseTypeSignature("varchar")))), new SessionFunctionHandle(varcharFunction));
    assertEquals(functionAndTypeManager.resolveFunction(Optional.of(sessionFunctions), Optional.empty(), bigintSignature.getFunctionName(), ImmutableList.of(new TypeSignatureProvider(parseTypeSignature("int")))), new SessionFunctionHandle(bigintFunction));
}
Also used : TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) SqlFunctionId(com.facebook.presto.spi.function.SqlFunctionId) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) Parameter(com.facebook.presto.spi.function.Parameter) Test(org.testng.annotations.Test)

Example 7 with TypeSignatureProvider

use of com.facebook.presto.sql.analyzer.TypeSignatureProvider in project presto by prestodb.

the class SpatialJoinUtils method getFlippedFunctionHandle.

public static FunctionHandle getFlippedFunctionHandle(CallExpression callExpression, FunctionAndTypeManager functionAndTypeManager) {
    FunctionMetadata callExpressionMetadata = functionAndTypeManager.getFunctionMetadata(callExpression.getFunctionHandle());
    checkArgument(callExpressionMetadata.getOperatorType().isPresent());
    OperatorType operatorType = flip(callExpressionMetadata.getOperatorType().get());
    List<TypeSignatureProvider> typeProviderList = fromTypes(callExpression.getArguments().stream().map(RowExpression::getType).collect(toImmutableList()));
    checkArgument(typeProviderList.size() == 2, "Expected there to be only two arguments in type provider");
    return functionAndTypeManager.resolveOperator(operatorType, ImmutableList.of(typeProviderList.get(1), typeProviderList.get(0)));
}
Also used : TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) RowExpression(com.facebook.presto.spi.relation.RowExpression) OperatorType(com.facebook.presto.common.function.OperatorType)

Example 8 with TypeSignatureProvider

use of com.facebook.presto.sql.analyzer.TypeSignatureProvider in project presto by prestodb.

the class AbstractTestAggregationFunction method getFunction.

protected final InternalAggregationFunction getFunction() {
    List<TypeSignatureProvider> parameterTypes = fromTypeSignatures(Lists.transform(getFunctionParameterTypes(), TypeSignature::parseTypeSignature));
    FunctionHandle functionHandle = functionAndTypeManager.resolveFunction(Optional.empty(), session.getTransactionId(), qualifyObjectName(QualifiedName.of(getFunctionName())), parameterTypes);
    return functionAndTypeManager.getAggregateFunctionImplementation(functionHandle);
}
Also used : TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle)

Example 9 with TypeSignatureProvider

use of com.facebook.presto.sql.analyzer.TypeSignatureProvider 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 10 with TypeSignatureProvider

use of com.facebook.presto.sql.analyzer.TypeSignatureProvider in project presto by prestodb.

the class FunctionAndTypeManager method resolveFunctionInternal.

private FunctionHandle resolveFunctionInternal(Optional<TransactionId> transactionId, QualifiedObjectName functionName, List<TypeSignatureProvider> parameterTypes) {
    FunctionNamespaceManager<?> functionNamespaceManager = getServingFunctionNamespaceManager(functionName.getCatalogSchemaName()).orElse(null);
    if (functionNamespaceManager == null) {
        throw new PrestoException(FUNCTION_NOT_FOUND, constructFunctionNotFoundErrorMessage(functionName, parameterTypes, ImmutableList.of()));
    }
    Optional<FunctionNamespaceTransactionHandle> transactionHandle = transactionId.map(id -> transactionManager.getFunctionNamespaceTransaction(id, functionName.getCatalogName()));
    if (functionNamespaceManager.canResolveFunction()) {
        return functionNamespaceManager.resolveFunction(transactionHandle, functionName, parameterTypes.stream().map(TypeSignatureProvider::getTypeSignature).collect(toImmutableList()));
    }
    Collection<? extends SqlFunction> candidates = functionNamespaceManager.getFunctions(transactionHandle, functionName);
    Optional<Signature> match = functionSignatureMatcher.match(candidates, parameterTypes, true);
    if (match.isPresent()) {
        return functionNamespaceManager.getFunctionHandle(transactionHandle, match.get());
    }
    if (functionName.getObjectName().startsWith(MAGIC_LITERAL_FUNCTION_PREFIX)) {
        // extract type from function functionName
        String typeName = functionName.getObjectName().substring(MAGIC_LITERAL_FUNCTION_PREFIX.length());
        // lookup the type
        Type type = getType(parseTypeSignature(typeName));
        // verify we have one parameter of the proper type
        checkArgument(parameterTypes.size() == 1, "Expected one argument to literal function, but got %s", parameterTypes);
        return new BuiltInFunctionHandle(getMagicLiteralFunctionSignature(type));
    }
    throw new PrestoException(FUNCTION_NOT_FOUND, constructFunctionNotFoundErrorMessage(functionName, parameterTypes, candidates));
}
Also used : FunctionNamespaceTransactionHandle(com.facebook.presto.spi.function.FunctionNamespaceTransactionHandle) TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) UserDefinedType(com.facebook.presto.common.type.UserDefinedType) CastType.toOperatorType(com.facebook.presto.metadata.CastType.toOperatorType) ParametricType(com.facebook.presto.common.type.ParametricType) DistinctType(com.facebook.presto.common.type.DistinctType) Type(com.facebook.presto.common.type.Type) OperatorType(com.facebook.presto.common.function.OperatorType) TypeSignature(com.facebook.presto.common.type.TypeSignature) LiteralEncoder.getMagicLiteralFunctionSignature(com.facebook.presto.sql.planner.LiteralEncoder.getMagicLiteralFunctionSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) PrestoException(com.facebook.presto.spi.PrestoException)

Aggregations

TypeSignatureProvider (com.facebook.presto.sql.analyzer.TypeSignatureProvider)10 TypeSignature (com.facebook.presto.common.type.TypeSignature)5 PrestoException (com.facebook.presto.spi.PrestoException)5 Type (com.facebook.presto.common.type.Type)4 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)4 Signature (com.facebook.presto.spi.function.Signature)4 OperatorType (com.facebook.presto.common.function.OperatorType)3 SignatureBinder.applyBoundVariables (com.facebook.presto.metadata.SignatureBinder.applyBoundVariables)3 ImmutableList (com.google.common.collect.ImmutableList)3 FunctionType (com.facebook.presto.common.type.FunctionType)2 ParametricType (com.facebook.presto.common.type.ParametricType)2 UserDefinedType (com.facebook.presto.common.type.UserDefinedType)2 FunctionAndTypeManager.createTestFunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager)2 OperatorType (com.facebook.presto.spi.function.OperatorType)2 TypeVariableConstraint (com.facebook.presto.spi.function.TypeVariableConstraint)2 Type (com.facebook.presto.spi.type.Type)2 TypeSignature (com.facebook.presto.spi.type.TypeSignature)2 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)2 VarcharType (com.facebook.presto.spi.type.VarcharType)2 TypeSignatureProvider.fromTypes (com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes)2