use of com.facebook.presto.spi.function.SqlInvokedScalarFunction in project presto by prestodb.
the class SqlInvokedScalarFromAnnotationsParser method findScalarsInFunctionSetClass.
private static List<Method> findScalarsInFunctionSetClass(Class<?> clazz) {
Set<Method> methods = findPublicStaticMethods(clazz, ImmutableSet.of(SqlInvokedScalarFunction.class, SqlType.class, SqlParameter.class, SqlParameters.class, Description.class), ImmutableSet.of(ScalarFunction.class, ScalarOperator.class, CodegenScalarFunction.class));
for (Method method : methods) {
checkCondition(method.isAnnotationPresent(SqlInvokedScalarFunction.class) && method.isAnnotationPresent(SqlType.class), FUNCTION_IMPLEMENTATION_ERROR, "Function-defining method [%s] is missing @SqlInvokedScalarFunction or @SqlType", method);
checkReturnString(method);
}
return ImmutableList.copyOf(methods);
}
use of com.facebook.presto.spi.function.SqlInvokedScalarFunction in project presto by prestodb.
the class SqlInvokedScalarFromAnnotationsParser method findScalarsInFunctionDefinitionClass.
private static List<Method> findScalarsInFunctionDefinitionClass(Class<?> clazz) {
Set<Method> methods = findPublicStaticMethods(clazz, ImmutableSet.of(SqlInvokedScalarFunction.class, SqlType.class, SqlParameter.class, SqlParameters.class, Description.class), ImmutableSet.of(ScalarFunction.class, ScalarOperator.class));
for (Method method : methods) {
checkCondition(!method.isAnnotationPresent(SqlInvokedScalarFunction.class) && !method.isAnnotationPresent(Description.class), FUNCTION_IMPLEMENTATION_ERROR, "Function-defining method [%s] cannot have @SqlInvokedScalarFunction", method);
checkCondition(method.isAnnotationPresent(SqlType.class), FUNCTION_IMPLEMENTATION_ERROR, "Function-defining method [%s] is missing @SqlType", method);
checkReturnString(method);
}
return ImmutableList.copyOf(methods);
}
use of com.facebook.presto.spi.function.SqlInvokedScalarFunction in project presto by prestodb.
the class SqlInvokedScalarFromAnnotationsParser method parseFunctionDefinition.
public static List<SqlInvokedFunction> parseFunctionDefinition(Class<?> clazz) {
checkArgument(clazz.isAnnotationPresent(SqlInvokedScalarFunction.class), "Class is not annotated with SqlInvokedScalarFunction: %s", clazz.getName());
SqlInvokedScalarFunction header = clazz.getAnnotation(SqlInvokedScalarFunction.class);
Optional<String> description = Optional.ofNullable(clazz.getAnnotation(Description.class)).map(Description::value);
return findScalarsInFunctionDefinitionClass(clazz).stream().map(method -> createSqlInvokedFunctions(method, Optional.of(header), description)).flatMap(List::stream).collect(toImmutableList());
}
use of com.facebook.presto.spi.function.SqlInvokedScalarFunction in project presto by prestodb.
the class SqlInvokedScalarFromAnnotationsParser method createSqlInvokedFunctions.
private static List<SqlInvokedFunction> createSqlInvokedFunctions(Method method, Optional<SqlInvokedScalarFunction> header, Optional<String> description) {
SqlInvokedScalarFunction functionHeader = header.orElse(method.getAnnotation(SqlInvokedScalarFunction.class));
String functionDescription = description.orElse(method.isAnnotationPresent(Description.class) ? method.getAnnotation(Description.class).value() : "");
TypeSignature returnType = parseTypeSignature(method.getAnnotation(SqlType.class).value());
// Parameter
checkCondition(!method.isAnnotationPresent(SqlParameter.class) || !method.isAnnotationPresent(SqlParameters.class), FUNCTION_IMPLEMENTATION_ERROR, "Function-defining method [%s] is annotated with both @SqlParameter and @SqlParameters", method);
List<Parameter> parameters;
if (method.isAnnotationPresent(SqlParameter.class)) {
parameters = ImmutableList.of(getParameterFromAnnotation(method.getAnnotation(SqlParameter.class)));
} else if (method.isAnnotationPresent(SqlParameters.class)) {
parameters = stream(method.getAnnotation(SqlParameters.class).value()).map(SqlInvokedScalarFromAnnotationsParser::getParameterFromAnnotation).collect(toImmutableList());
} else {
parameters = ImmutableList.of();
}
// Routine characteristics
RoutineCharacteristics routineCharacteristics = RoutineCharacteristics.builder().setLanguage(RoutineCharacteristics.Language.SQL).setDeterminism(functionHeader.deterministic() ? DETERMINISTIC : NOT_DETERMINISTIC).setNullCallClause(functionHeader.calledOnNullInput() ? CALLED_ON_NULL_INPUT : RETURNS_NULL_ON_NULL_INPUT).build();
String body;
try {
body = (String) method.invoke(null);
} catch (ReflectiveOperationException e) {
throw new PrestoException(FUNCTION_IMPLEMENTATION_ERROR, format("Failed to get function body for method [%s]", method), e);
}
return Stream.concat(Stream.of(functionHeader.value()), stream(functionHeader.alias())).map(name -> new SqlInvokedFunction(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, name), parameters, returnType, functionDescription, routineCharacteristics, body, notVersioned())).collect(toImmutableList());
}
Aggregations