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);
}
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());
}
}
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;
}
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));
}
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'");
}
Aggregations