Search in sources :

Example 1 with SqlFunction

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

the class BuiltInTypeAndFunctionNamespaceManager method doGetSpecializedFunctionKey.

private SpecializedFunctionKey doGetSpecializedFunctionKey(Signature signature) {
    Iterable<SqlFunction> candidates = getFunctions(null, signature.getName());
    // search for exact match
    Type returnType = functionAndTypeManager.getType(signature.getReturnType());
    List<TypeSignatureProvider> argumentTypeSignatureProviders = fromTypeSignatures(signature.getArgumentTypes());
    for (SqlFunction candidate : candidates) {
        Optional<BoundVariables> boundVariables = new SignatureBinder(functionAndTypeManager, 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(), functionAndTypeManager);
    for (SqlFunction candidate : candidates) {
        SignatureBinder binder = new SignatureBinder(functionAndTypeManager, candidate.getSignature(), true);
        Optional<BoundVariables> boundVariables = binder.bindVariables(argumentTypeSignatureProviders, returnType);
        if (!boundVariables.isPresent()) {
            continue;
        }
        Signature boundSignature = applyBoundVariables(candidate.getSignature(), boundVariables.get(), argumentTypes.size());
        if (!functionAndTypeManager.isTypeOnlyCoercion(functionAndTypeManager.getType(boundSignature.getReturnType()), returnType)) {
            continue;
        }
        boolean nonTypeOnlyCoercion = false;
        for (int i = 0; i < argumentTypes.size(); i++) {
            Type expectedType = functionAndTypeManager.getType(boundSignature.getArgumentTypes().get(i));
            if (!functionAndTypeManager.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.getNameSuffix().startsWith(MAGIC_LITERAL_FUNCTION_PREFIX)) {
        List<TypeSignature> parameterTypes = signature.getArgumentTypes();
        // extract type from function name
        String typeName = signature.getNameSuffix().substring(MAGIC_LITERAL_FUNCTION_PREFIX.length());
        // lookup the type
        Type type = functionAndTypeManager.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);
        Type parameterType = functionAndTypeManager.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) DecimalParametricType(com.facebook.presto.type.DecimalParametricType) UserDefinedType(com.facebook.presto.common.type.UserDefinedType) OperatorType.tryGetOperatorType(com.facebook.presto.common.function.OperatorType.tryGetOperatorType) VarcharParametricType(com.facebook.presto.type.VarcharParametricType) ParametricType(com.facebook.presto.common.type.ParametricType) CharParametricType(com.facebook.presto.type.CharParametricType) Type(com.facebook.presto.common.type.Type) OperatorType(com.facebook.presto.common.function.OperatorType) MapParametricType(com.facebook.presto.type.MapParametricType) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) TypeSignature(com.facebook.presto.common.type.TypeSignature) SignatureBinder.applyBoundVariables(com.facebook.presto.metadata.SignatureBinder.applyBoundVariables) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) TypeSignature(com.facebook.presto.common.type.TypeSignature) SqlFunction(com.facebook.presto.spi.function.SqlFunction)

Example 2 with SqlFunction

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

the class BuiltInTypeAndFunctionNamespaceManager method getFunctionMetadata.

@Override
public FunctionMetadata getFunctionMetadata(FunctionHandle functionHandle) {
    checkArgument(functionHandle instanceof BuiltInFunctionHandle, "Expect BuiltInFunctionHandle");
    Signature signature = ((BuiltInFunctionHandle) functionHandle).getSignature();
    SpecializedFunctionKey functionKey;
    try {
        functionKey = specializedFunctionKeyCache.getUnchecked(signature);
    } catch (UncheckedExecutionException e) {
        throwIfInstanceOf(e.getCause(), PrestoException.class);
        throw e;
    }
    SqlFunction function = functionKey.getFunction();
    Optional<OperatorType> operatorType = tryGetOperatorType(signature.getName());
    if (operatorType.isPresent()) {
        return new FunctionMetadata(operatorType.get(), signature.getArgumentTypes(), signature.getReturnType(), signature.getKind(), JAVA, function.isDeterministic(), function.isCalledOnNullInput());
    } else if (function instanceof SqlInvokedFunction) {
        SqlInvokedFunction sqlFunction = (SqlInvokedFunction) function;
        List<String> argumentNames = sqlFunction.getParameters().stream().map(Parameter::getName).collect(toImmutableList());
        return new FunctionMetadata(signature.getName(), signature.getArgumentTypes(), argumentNames, signature.getReturnType(), signature.getKind(), sqlFunction.getRoutineCharacteristics().getLanguage(), SQL, function.isDeterministic(), function.isCalledOnNullInput(), sqlFunction.getVersion());
    } else {
        return new FunctionMetadata(signature.getName(), signature.getArgumentTypes(), signature.getReturnType(), signature.getKind(), JAVA, function.isDeterministic(), function.isCalledOnNullInput());
    }
}
Also used : FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) TypeSignature(com.facebook.presto.common.type.TypeSignature) Parameter(com.facebook.presto.spi.function.Parameter) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) TypeParameter(com.facebook.presto.common.type.TypeParameter) PrestoException(com.facebook.presto.spi.PrestoException) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) OperatorType.tryGetOperatorType(com.facebook.presto.common.function.OperatorType.tryGetOperatorType) OperatorType(com.facebook.presto.common.function.OperatorType) SqlFunction(com.facebook.presto.spi.function.SqlFunction)

Example 3 with SqlFunction

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

the class FunctionSignatureMatcher method constructFunctionNotFoundErrorMessage.

static String constructFunctionNotFoundErrorMessage(QualifiedObjectName functionName, List<TypeSignatureProvider> parameterTypes, Collection<? extends SqlFunction> candidates) {
    String name = toConciseFunctionName(functionName);
    List<String> expectedParameters = new ArrayList<>();
    for (SqlFunction function : candidates) {
        expectedParameters.add(format("%s(%s) %s", name, Joiner.on(", ").join(function.getSignature().getArgumentTypes()), Joiner.on(", ").join(function.getSignature().getTypeVariableConstraints())));
    }
    String parameters = Joiner.on(", ").join(parameterTypes);
    String message = format("Function %s not registered", name);
    if (!expectedParameters.isEmpty()) {
        String expected = Joiner.on(", ").join(expectedParameters);
        message = format("Unexpected parameters (%s) for function %s. Expected: %s", parameters, name, expected);
    }
    return message;
}
Also used : ArrayList(java.util.ArrayList) SqlFunction(com.facebook.presto.spi.function.SqlFunction)

Example 4 with SqlFunction

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

the class TestFunctionAndTypeManager method testConflictingScalarAggregation.

@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "'presto.default.sum' is both an aggregation and a scalar function")
public void testConflictingScalarAggregation() {
    List<SqlFunction> functions = new FunctionListBuilder().scalars(ScalarSum.class).getFunctions();
    FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
    functionAndTypeManager.registerBuiltInFunctions(functions);
}
Also used : FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) SqlFunction(com.facebook.presto.spi.function.SqlFunction) Test(org.testng.annotations.Test)

Example 5 with SqlFunction

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

the class TestFunctionAndTypeManager method testListingVisibilityBetaFunctionsEnabled.

@Test
public void testListingVisibilityBetaFunctionsEnabled() {
    Session session = testSessionBuilder().setCatalog("tpch").setSchema(TINY_SCHEMA_NAME).setSystemProperty(EXPERIMENTAL_FUNCTIONS_ENABLED, "true").build();
    FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
    List<SqlFunction> functions = functionAndTypeManager.listFunctions(session, Optional.empty(), Optional.empty());
    List<String> names = transform(functions, input -> input.getSignature().getNameSuffix());
    assertTrue(names.contains("length"), "Expected function names " + names + " to contain 'length'");
    assertTrue(names.contains("stddev"), "Expected function names " + names + " to contain 'stddev'");
    assertTrue(names.contains("rank"), "Expected function names " + names + " to contain 'rank'");
    assertTrue(names.contains("tdigest_agg"), "Expected function names " + names + " to contain 'tdigest_agg'");
    assertTrue(names.contains("quantiles_at_values"), "Expected function names " + names + " to contain 'tdigest_agg'");
    assertFalse(names.contains("like"), "Expected function names " + names + " not to contain 'like'");
    assertFalse(names.contains("$internal$sum_data_size_for_stats"), "Expected function names " + names + " not to contain '$internal$sum_data_size_for_stats'");
    assertFalse(names.contains("$internal$max_data_size_for_stats"), "Expected function names " + names + " not to contain '$internal$max_data_size_for_stats'");
}
Also used : FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) Session(com.facebook.presto.Session) SqlFunction(com.facebook.presto.spi.function.SqlFunction) Test(org.testng.annotations.Test)

Aggregations

SqlFunction (com.facebook.presto.spi.function.SqlFunction)11 FunctionAndTypeManager.createTestFunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager)5 Test (org.testng.annotations.Test)5 OperatorType (com.facebook.presto.common.function.OperatorType)4 OperatorType.tryGetOperatorType (com.facebook.presto.common.function.OperatorType.tryGetOperatorType)4 TypeSignature (com.facebook.presto.common.type.TypeSignature)4 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)4 Signature (com.facebook.presto.spi.function.Signature)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 Session (com.facebook.presto.Session)2 FunctionListBuilder (com.facebook.presto.metadata.FunctionListBuilder)2 Metadata (com.facebook.presto.metadata.Metadata)2 PrestoException (com.facebook.presto.spi.PrestoException)2 Parameter (com.facebook.presto.spi.function.Parameter)2 SqlInvokedFunction (com.facebook.presto.spi.function.SqlInvokedFunction)2 TypeSignatureProvider (com.facebook.presto.sql.analyzer.TypeSignatureProvider)2 ArrayList (java.util.ArrayList)2 TEST_SESSION (com.facebook.presto.SessionTestUtils.TEST_SESSION)1 EXPERIMENTAL_FUNCTIONS_ENABLED (com.facebook.presto.SystemSessionProperties.EXPERIMENTAL_FUNCTIONS_ENABLED)1