use of com.facebook.presto.spi.StandardErrorCode.FUNCTION_NOT_FOUND in project presto by prestodb.
the class FunctionRegistry method resolveFunction.
public Signature resolveFunction(QualifiedName name, List<TypeSignatureProvider> parameterTypes) {
Collection<SqlFunction> allCandidates = functions.get(name);
List<SqlFunction> exactCandidates = allCandidates.stream().filter(function -> function.getSignature().getTypeVariableConstraints().isEmpty()).collect(Collectors.toList());
Optional<Signature> match = matchFunctionExact(exactCandidates, parameterTypes);
if (match.isPresent()) {
return match.get();
}
List<SqlFunction> genericCandidates = allCandidates.stream().filter(function -> !function.getSignature().getTypeVariableConstraints().isEmpty()).collect(Collectors.toList());
match = matchFunctionExact(genericCandidates, parameterTypes);
if (match.isPresent()) {
return match.get();
}
match = matchFunctionWithCoercion(allCandidates, parameterTypes);
if (match.isPresent()) {
return match.get();
}
List<String> expectedParameters = new ArrayList<>();
for (SqlFunction function : allCandidates) {
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);
}
if (name.getSuffix().startsWith(MAGIC_LITERAL_FUNCTION_PREFIX)) {
// extract type from function name
String typeName = name.getSuffix().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).getTypeSignature());
requireNonNull(parameterType, format("Type %s not found", parameterTypes.get(0)));
return getMagicLiteralFunctionSignature(type);
}
throw new PrestoException(FUNCTION_NOT_FOUND, message);
}
Aggregations