Search in sources :

Example 6 with Parameter

use of com.facebook.presto.spi.function.Parameter in project presto by prestodb.

the class TestMySqlFunctionNamespaceManager method testTooManyParameters.

@Test(expectedExceptions = PrestoException.class, expectedExceptionsMessageRegExp = "Function has more than 100 parameters: 101")
public void testTooManyParameters() {
    List<Parameter> parameters = nCopies(101, new Parameter("x", parseTypeSignature(DOUBLE)));
    createFunction(createFunctionTangent(parameters), false);
}
Also used : Parameter(com.facebook.presto.spi.function.Parameter) Test(org.testng.annotations.Test)

Example 7 with Parameter

use of com.facebook.presto.spi.function.Parameter in project presto by prestodb.

the class SqlInvokedFunctionRowMapper method map.

@Override
public SqlInvokedFunction map(ResultSet rs, StatementContext ctx) throws SQLException {
    String catalog = rs.getString("catalog_name");
    String schema = rs.getString("schema_name");
    String functionName = rs.getString("function_name");
    List<Parameter> parameters = PARAMETERS_CODEC.fromJson(rs.getString("parameters"));
    String returnType = rs.getString("return_type");
    String description = rs.getString("description");
    RoutineCharacteristics routineCharacteristics = ROUTINE_CHARACTERISTICS_CODEC.fromJson(rs.getString("routine_characteristics"));
    String body = rs.getString("body");
    String version = String.valueOf(rs.getLong("version"));
    return new SqlInvokedFunction(QualifiedObjectName.valueOf(catalog, schema, functionName), parameters, parseTypeSignature(returnType), description, routineCharacteristics, body, withVersion(version));
}
Also used : RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) Parameter(com.facebook.presto.spi.function.Parameter)

Example 8 with Parameter

use of com.facebook.presto.spi.function.Parameter 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)

Example 9 with Parameter

use of com.facebook.presto.spi.function.Parameter in project presto by prestodb.

the class CreateFunctionTask method createSqlInvokedFunction.

private SqlInvokedFunction createSqlInvokedFunction(CreateFunction statement, Metadata metadata, Analysis analysis) {
    QualifiedObjectName functionName = statement.isTemporary() ? QualifiedObjectName.valueOf(SESSION_NAMESPACE, statement.getFunctionName().getSuffix()) : qualifyObjectName(statement.getFunctionName());
    List<Parameter> parameters = statement.getParameters().stream().map(parameter -> new Parameter(parameter.getName().toString(), parseTypeSignature(parameter.getType()))).collect(toImmutableList());
    TypeSignature returnType = parseTypeSignature(statement.getReturnType());
    String description = statement.getComment().orElse("");
    RoutineCharacteristics routineCharacteristics = RoutineCharacteristics.builder().setLanguage(new RoutineCharacteristics.Language(statement.getCharacteristics().getLanguage().getLanguage())).setDeterminism(RoutineCharacteristics.Determinism.valueOf(statement.getCharacteristics().getDeterminism().name())).setNullCallClause(RoutineCharacteristics.NullCallClause.valueOf(statement.getCharacteristics().getNullCallClause().name())).build();
    RoutineBody body = statement.getBody();
    if (statement.getBody() instanceof Return) {
        Expression bodyExpression = ((Return) statement.getBody()).getExpression();
        Type bodyType = analysis.getType(bodyExpression);
        // Coerce expressions in body if necessary
        bodyExpression = ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<Void>() {

            @Override
            public Expression rewriteExpression(Expression expression, Void context, ExpressionTreeRewriter<Void> treeRewriter) {
                Expression rewritten = treeRewriter.defaultRewrite(expression, null);
                Type coercion = analysis.getCoercion(expression);
                if (coercion != null) {
                    return new Cast(rewritten, coercion.getTypeSignature().toString(), false, analysis.isTypeOnlyCoercion(expression));
                }
                return rewritten;
            }
        }, bodyExpression, null);
        if (!bodyType.equals(metadata.getType(returnType))) {
            // Casting is safe here, since we have verified at analysis time that the actual type of the body is coercible to declared return type.
            bodyExpression = new Cast(bodyExpression, statement.getReturnType());
        }
        body = new Return(bodyExpression);
    }
    return new SqlInvokedFunction(functionName, parameters, returnType, description, routineCharacteristics, formatSql(body, Optional.empty()), notVersioned());
}
Also used : ExpressionTreeRewriter(com.facebook.presto.sql.tree.ExpressionTreeRewriter) WarningCollector(com.facebook.presto.spi.WarningCollector) Parameter(com.facebook.presto.spi.function.Parameter) Analyzer(com.facebook.presto.sql.analyzer.Analyzer) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) SESSION_NAMESPACE(com.facebook.presto.metadata.SessionFunctionHandle.SESSION_NAMESPACE) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) PrestoException(com.facebook.presto.spi.PrestoException) CreateFunction(com.facebook.presto.sql.tree.CreateFunction) TypeSignature(com.facebook.presto.common.type.TypeSignature) Inject(javax.inject.Inject) ALREADY_EXISTS(com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) Cast(com.facebook.presto.sql.tree.Cast) SqlFunctionHandle(com.facebook.presto.spi.function.SqlFunctionHandle) TransactionManager(com.facebook.presto.transaction.TransactionManager) FunctionAndTypeManager.qualifyObjectName(com.facebook.presto.metadata.FunctionAndTypeManager.qualifyObjectName) Type(com.facebook.presto.common.type.Type) RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) Futures.immediateFuture(com.google.common.util.concurrent.Futures.immediateFuture) Session(com.facebook.presto.Session) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) RoutineBody(com.facebook.presto.sql.tree.RoutineBody) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) String.format(java.lang.String.format) SqlParser(com.facebook.presto.sql.parser.SqlParser) ExpressionRewriter(com.facebook.presto.sql.tree.ExpressionRewriter) List(java.util.List) SqlFunctionId(com.facebook.presto.spi.function.SqlFunctionId) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Expression(com.facebook.presto.sql.tree.Expression) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) Return(com.facebook.presto.sql.tree.Return) FunctionVersion.notVersioned(com.facebook.presto.spi.function.FunctionVersion.notVersioned) SqlFormatter.formatSql(com.facebook.presto.sql.SqlFormatter.formatSql) Analysis(com.facebook.presto.sql.analyzer.Analysis) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Metadata(com.facebook.presto.metadata.Metadata) AccessControl(com.facebook.presto.security.AccessControl) Cast(com.facebook.presto.sql.tree.Cast) ExpressionTreeRewriter(com.facebook.presto.sql.tree.ExpressionTreeRewriter) Return(com.facebook.presto.sql.tree.Return) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Type(com.facebook.presto.common.type.Type) ExpressionRewriter(com.facebook.presto.sql.tree.ExpressionRewriter) RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) Expression(com.facebook.presto.sql.tree.Expression) Parameter(com.facebook.presto.spi.function.Parameter) RoutineBody(com.facebook.presto.sql.tree.RoutineBody)

Example 10 with Parameter

use of com.facebook.presto.spi.function.Parameter in project presto by prestodb.

the class TestMySqlFunctionNamespaceManager method testLargeParameterCount.

public void testLargeParameterCount() {
    List<Parameter> parameters = nCopies(100, new Parameter("x", parseTypeSignature(DOUBLE)));
    createFunction(createFunctionTangent(parameters), false);
}
Also used : Parameter(com.facebook.presto.spi.function.Parameter)

Aggregations

Parameter (com.facebook.presto.spi.function.Parameter)14 SqlInvokedFunction (com.facebook.presto.spi.function.SqlInvokedFunction)8 Test (org.testng.annotations.Test)5 TypeSignature (com.facebook.presto.common.type.TypeSignature)4 PrestoException (com.facebook.presto.spi.PrestoException)4 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)3 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)3 RoutineCharacteristics (com.facebook.presto.spi.function.RoutineCharacteristics)3 SqlFunctionId (com.facebook.presto.spi.function.SqlFunctionId)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 List (java.util.List)3 TypeSignatureParameter (com.facebook.presto.common.type.TypeSignatureParameter)2 FunctionVersion.notVersioned (com.facebook.presto.spi.function.FunctionVersion.notVersioned)2 String.format (java.lang.String.format)2 Optional (java.util.Optional)2 Session (com.facebook.presto.Session)1 OperatorType (com.facebook.presto.common.function.OperatorType)1 OperatorType.tryGetOperatorType (com.facebook.presto.common.function.OperatorType.tryGetOperatorType)1 Type (com.facebook.presto.common.type.Type)1 TypeParameter (com.facebook.presto.common.type.TypeParameter)1