Search in sources :

Example 1 with SqlFunction

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

the class TestFunctionRegistry method testDuplicateFunctions.

@Test
public void testDuplicateFunctions() {
    List<SqlFunction> functions = new FunctionListBuilder().scalars(CustomFunctions.class).getFunctions().stream().filter(input -> input.getSignature().getName().equals("custom_add")).collect(toImmutableList());
    Metadata metadata = createTestMetadataManager();
    metadata.getFunctionAndTypeManager().registerBuiltInFunctions(functions);
    int before = metadata.listFunctions(Optional.empty()).size();
    metadata.getFunctionAndTypeManager().registerBuiltInFunctions(functions);
    assertEquals(metadata.listFunctions(Optional.empty()).size(), before);
}
Also used : BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) Arrays(java.util.Arrays) StandardTypes(io.prestosql.spi.type.StandardTypes) TypeVariableConstraint(io.prestosql.spi.function.TypeVariableConstraint) QualifiedName(io.prestosql.sql.tree.QualifiedName) SCALAR(io.prestosql.spi.function.FunctionKind.SCALAR) Signature.mangleOperatorName(io.prestosql.spi.function.Signature.mangleOperatorName) Assert.assertEquals(org.testng.Assert.assertEquals) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) Test(org.testng.annotations.Test) Lists.transform(com.google.common.collect.Lists.transform) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) ImmutableList(com.google.common.collect.ImmutableList) Signature.internalOperator(io.prestosql.spi.function.Signature.internalOperator) OperatorType(io.prestosql.spi.function.OperatorType) RETURN_NULL_ON_NULL(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.NullConvention.RETURN_NULL_ON_NULL) SignatureBuilder(io.prestosql.spi.function.SignatureBuilder) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Type(io.prestosql.spi.type.Type) Signature(io.prestosql.spi.function.Signature) SqlFunction(io.prestosql.spi.function.SqlFunction) Functions(com.google.common.base.Functions) ImmutableSet(com.google.common.collect.ImmutableSet) TypeSignatureProvider.fromTypes(io.prestosql.sql.analyzer.TypeSignatureProvider.fromTypes) MetadataManager.createTestMetadataManager(io.prestosql.metadata.MetadataManager.createTestMetadataManager) TypeSignatureProvider.fromTypeSignatures(io.prestosql.sql.analyzer.TypeSignatureProvider.fromTypeSignatures) Collections.nCopies(java.util.Collections.nCopies) CustomFunctions(io.prestosql.operator.scalar.CustomFunctions) MethodHandles(java.lang.invoke.MethodHandles) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Assert.fail(org.testng.Assert.fail) Set(java.util.Set) Signature.typeVariable(io.prestosql.spi.function.Signature.typeVariable) String.format(java.lang.String.format) ScalarFunction(io.prestosql.spi.function.ScalarFunction) List(java.util.List) Signature.unmangleOperator(io.prestosql.spi.function.Signature.unmangleOperator) HYPER_LOG_LOG(io.prestosql.spi.type.HyperLogLogType.HYPER_LOG_LOG) Collectors.toList(java.util.stream.Collectors.toList) SqlType(io.prestosql.spi.function.SqlType) ArgumentProperty.valueTypeArgumentProperty(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.ArgumentProperty.valueTypeArgumentProperty) Optional(java.util.Optional) Assert.assertTrue(org.testng.Assert.assertTrue) TypeSignature(io.prestosql.spi.type.TypeSignature) Signature.operatorNameWithDefaultFunctionPrefix(io.prestosql.spi.function.Signature.operatorNameWithDefaultFunctionPrefix) TypeVariableConstraint(io.prestosql.spi.function.TypeVariableConstraint) SqlFunction(io.prestosql.spi.function.SqlFunction) Test(org.testng.annotations.Test)

Example 2 with SqlFunction

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

the class BuiltInFunctionNamespaceManager 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 = OperatorType.tryGetOperatorType(signature.getName());
    if (operatorType.isPresent()) {
        return new FunctionMetadata(operatorType.get(), signature.getArgumentTypes(), signature.getReturnType(), signature.getKind(), BUILTIN, function.isDeterministic(), function.isCalledOnNullInput());
    } else {
        return new FunctionMetadata(signature.getName(), signature.getArgumentTypes(), signature.getReturnType(), signature.getKind(), BUILTIN, function.isDeterministic(), function.isCalledOnNullInput());
    }
}
Also used : FunctionMetadata(io.prestosql.spi.function.FunctionMetadata) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) Signature(io.prestosql.spi.function.Signature) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) PrestoException(io.prestosql.spi.PrestoException) OperatorType(io.prestosql.spi.function.OperatorType) SqlFunction(io.prestosql.spi.function.SqlFunction)

Example 3 with SqlFunction

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

the class FunctionResolver method constructFunctionNotFoundErrorMessage.

public 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(io.prestosql.spi.function.SqlFunction)

Example 4 with SqlFunction

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

the class BuiltInFunctionNamespaceManager 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(io.prestosql.spi.PrestoException) TypeSignatureProvider(io.prestosql.sql.analyzer.TypeSignatureProvider) Type(io.prestosql.spi.type.Type) OperatorType(io.prestosql.spi.function.OperatorType) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) SignatureBinder.applyBoundVariables(io.prestosql.metadata.SignatureBinder.applyBoundVariables) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) Signature(io.prestosql.spi.function.Signature) SqlFunction(io.prestosql.spi.function.SqlFunction)

Example 5 with SqlFunction

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

the class TestFunctionAndTypeManager method testListingVisibilityBetaFunctionsDisabled.

@Test
public void testListingVisibilityBetaFunctionsDisabled() {
    FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
    List<SqlFunction> functions = functionAndTypeManager.listFunctions(Optional.of(TEST_SESSION));
    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'");
    assertFalse(names.contains("quantiles_at_values"), "Expected function names " + names + " not to contain 'quantiles_at_values'");
    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(io.prestosql.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) SqlFunction(io.prestosql.spi.function.SqlFunction) Test(org.testng.annotations.Test)

Aggregations

SqlFunction (io.prestosql.spi.function.SqlFunction)11 OperatorType (io.prestosql.spi.function.OperatorType)6 Signature (io.prestosql.spi.function.Signature)6 TypeSignature (io.prestosql.spi.type.TypeSignature)6 Test (org.testng.annotations.Test)6 FunctionAndTypeManager.createTestFunctionAndTypeManager (io.prestosql.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager)5 TypeSignature.parseTypeSignature (io.prestosql.spi.type.TypeSignature.parseTypeSignature)5 BuiltInFunctionHandle (io.prestosql.spi.function.BuiltInFunctionHandle)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 SqlType (io.prestosql.spi.function.SqlType)3 Type (io.prestosql.spi.type.Type)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Lists.transform (com.google.common.collect.Lists.transform)2 Session (io.prestosql.Session)2 CustomFunctions (io.prestosql.operator.scalar.CustomFunctions)2 PrestoException (io.prestosql.spi.PrestoException)2 BuiltInScalarFunctionImplementation (io.prestosql.spi.function.BuiltInScalarFunctionImplementation)2 ArgumentProperty.valueTypeArgumentProperty (io.prestosql.spi.function.BuiltInScalarFunctionImplementation.ArgumentProperty.valueTypeArgumentProperty)2 RETURN_NULL_ON_NULL (io.prestosql.spi.function.BuiltInScalarFunctionImplementation.NullConvention.RETURN_NULL_ON_NULL)2