Search in sources :

Example 1 with SqlParameter

use of com.facebook.presto.spi.function.SqlParameter 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());
}
Also used : Parameter(com.facebook.presto.spi.function.Parameter) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) PrestoException(com.facebook.presto.spi.PrestoException) TypeSignature(com.facebook.presto.common.type.TypeSignature) SqlParameters(com.facebook.presto.spi.function.SqlParameters) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) CodegenScalarFunction(com.facebook.presto.spi.function.CodegenScalarFunction) DEFAULT_NAMESPACE(com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.DEFAULT_NAMESPACE) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) ImmutableList(com.google.common.collect.ImmutableList) Description(com.facebook.presto.spi.function.Description) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) Method(java.lang.reflect.Method) RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) SqlInvokedScalarFunction(com.facebook.presto.spi.function.SqlInvokedScalarFunction) FUNCTION_IMPLEMENTATION_ERROR(com.facebook.presto.spi.StandardErrorCode.FUNCTION_IMPLEMENTATION_ERROR) CALLED_ON_NULL_INPUT(com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause.CALLED_ON_NULL_INPUT) Failures.checkCondition(com.facebook.presto.util.Failures.checkCondition) FunctionsParserHelper.findPublicStaticMethods(com.facebook.presto.operator.annotations.FunctionsParserHelper.findPublicStaticMethods) SqlParameter(com.facebook.presto.spi.function.SqlParameter) ImmutableSet(com.google.common.collect.ImmutableSet) NOT_DETERMINISTIC(com.facebook.presto.spi.function.RoutineCharacteristics.Determinism.NOT_DETERMINISTIC) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) DETERMINISTIC(com.facebook.presto.spi.function.RoutineCharacteristics.Determinism.DETERMINISTIC) Set(java.util.Set) String.format(java.lang.String.format) List(java.util.List) Stream(java.util.stream.Stream) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) RETURNS_NULL_ON_NULL_INPUT(com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause.RETURNS_NULL_ON_NULL_INPUT) FunctionVersion.notVersioned(com.facebook.presto.spi.function.FunctionVersion.notVersioned) Optional(java.util.Optional) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Arrays.stream(java.util.Arrays.stream) SqlType(com.facebook.presto.spi.function.SqlType) Description(com.facebook.presto.spi.function.Description) SqlParameter(com.facebook.presto.spi.function.SqlParameter) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) PrestoException(com.facebook.presto.spi.PrestoException) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) SqlParameters(com.facebook.presto.spi.function.SqlParameters) RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) SqlInvokedScalarFunction(com.facebook.presto.spi.function.SqlInvokedScalarFunction) Parameter(com.facebook.presto.spi.function.Parameter) SqlParameter(com.facebook.presto.spi.function.SqlParameter)

Aggregations

QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)1 TypeSignature (com.facebook.presto.common.type.TypeSignature)1 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)1 DEFAULT_NAMESPACE (com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.DEFAULT_NAMESPACE)1 FunctionsParserHelper.findPublicStaticMethods (com.facebook.presto.operator.annotations.FunctionsParserHelper.findPublicStaticMethods)1 PrestoException (com.facebook.presto.spi.PrestoException)1 FUNCTION_IMPLEMENTATION_ERROR (com.facebook.presto.spi.StandardErrorCode.FUNCTION_IMPLEMENTATION_ERROR)1 CodegenScalarFunction (com.facebook.presto.spi.function.CodegenScalarFunction)1 Description (com.facebook.presto.spi.function.Description)1 FunctionVersion.notVersioned (com.facebook.presto.spi.function.FunctionVersion.notVersioned)1 Parameter (com.facebook.presto.spi.function.Parameter)1 RoutineCharacteristics (com.facebook.presto.spi.function.RoutineCharacteristics)1 DETERMINISTIC (com.facebook.presto.spi.function.RoutineCharacteristics.Determinism.DETERMINISTIC)1 NOT_DETERMINISTIC (com.facebook.presto.spi.function.RoutineCharacteristics.Determinism.NOT_DETERMINISTIC)1 CALLED_ON_NULL_INPUT (com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause.CALLED_ON_NULL_INPUT)1 RETURNS_NULL_ON_NULL_INPUT (com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause.RETURNS_NULL_ON_NULL_INPUT)1 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)1 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)1 SqlInvokedFunction (com.facebook.presto.spi.function.SqlInvokedFunction)1 SqlInvokedScalarFunction (com.facebook.presto.spi.function.SqlInvokedScalarFunction)1