Search in sources :

Example 1 with CreateFunction

use of com.facebook.presto.sql.tree.CreateFunction in project presto by prestodb.

the class TestSqlParser method testCreateFunction.

@Test
public void testCreateFunction() {
    assertStatement("CREATE FUNCTION tan (x double)\n" + "RETURNS double\n" + "COMMENT 'tangent trigonometric function'\n" + "LANGUAGE SQL\n" + "DETERMINISTIC\n" + "RETURNS NULL ON NULL INPUT\n" + "RETURN sin(x) / cos(x)", new CreateFunction(QualifiedName.of("tan"), false, false, ImmutableList.of(new SqlParameterDeclaration(identifier("x"), "double")), "double", Optional.of("tangent trigonometric function"), new RoutineCharacteristics(SQL, DETERMINISTIC, RETURNS_NULL_ON_NULL_INPUT), new Return(new ArithmeticBinaryExpression(DIVIDE, new FunctionCall(QualifiedName.of("sin"), ImmutableList.of(identifier("x"))), new FunctionCall(QualifiedName.of("cos"), ImmutableList.of(identifier("x")))))));
    CreateFunction createFunctionRand = new CreateFunction(QualifiedName.of("dev", "testing", "rand"), true, false, ImmutableList.of(), "double", Optional.empty(), new RoutineCharacteristics(SQL, NOT_DETERMINISTIC, CALLED_ON_NULL_INPUT), new Return(new FunctionCall(QualifiedName.of("rand"), ImmutableList.of())));
    assertStatement("CREATE OR REPLACE FUNCTION dev.testing.rand ()\n" + "RETURNS double\n" + "LANGUAGE SQL\n" + "NOT DETERMINISTIC\n" + "CALLED ON NULL INPUT\n" + "RETURN rand()", createFunctionRand);
    assertStatement("CREATE OR REPLACE FUNCTION dev.testing.rand ()\n" + "RETURNS double\n" + "RETURN rand()", createFunctionRand);
    CreateFunction createTemporaryFunctionFoo = new CreateFunction(QualifiedName.of("foo"), false, true, ImmutableList.of(), "boolean", Optional.empty(), new RoutineCharacteristics(SQL, NOT_DETERMINISTIC, CALLED_ON_NULL_INPUT), new Return(new BooleanLiteral("true")));
    assertStatement("CREATE TEMPORARY FUNCTION foo() \n" + "RETURNS boolean \n" + "RETURN true", createTemporaryFunctionFoo);
    assertInvalidStatement("CREATE FUNCTION dev.testing.rand () RETURNS double LANGUAGE SQL LANGUAGE SQL RETURN rand()", "Duplicate language clause: SQL");
    assertInvalidStatement("CREATE FUNCTION dev.testing.rand () RETURNS double DETERMINISTIC DETERMINISTIC RETURN rand()", "Duplicate determinism characteristics: DETERMINISTIC");
    assertInvalidStatement("CREATE FUNCTION dev.testing.rand () RETURNS double CALLED ON NULL INPUT CALLED ON NULL INPUT RETURN rand()", "Duplicate null-call clause: CALLEDONNULLINPUT");
}
Also used : ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) Return(com.facebook.presto.sql.tree.Return) RoutineCharacteristics(com.facebook.presto.sql.tree.RoutineCharacteristics) AlterRoutineCharacteristics(com.facebook.presto.sql.tree.AlterRoutineCharacteristics) BooleanLiteral(com.facebook.presto.sql.tree.BooleanLiteral) CreateFunction(com.facebook.presto.sql.tree.CreateFunction) ShowCreateFunction(com.facebook.presto.sql.tree.ShowCreateFunction) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) SqlParameterDeclaration(com.facebook.presto.sql.tree.SqlParameterDeclaration) Test(org.testng.annotations.Test)

Example 2 with CreateFunction

use of com.facebook.presto.sql.tree.CreateFunction in project presto by prestodb.

the class TestCreateFunctionTask method testCreateTemporaryFunction.

@Test
public void testCreateTemporaryFunction() {
    SqlParser parser = new SqlParser();
    String sqlString = "CREATE TEMPORARY FUNCTION foo() RETURNS int RETURN 1";
    CreateFunction statement = (CreateFunction) parser.createStatement(sqlString, ParsingOptions.builder().build());
    TransactionManager transactionManager = createTestTransactionManager();
    QueryStateMachine stateMachine = createQueryStateMachine(sqlString, TEST_SESSION, false, transactionManager, executorService, metadataManager);
    WarningCollector warningCollector = stateMachine.getWarningCollector();
    CreateFunctionTask createFunctionTask = new CreateFunctionTask(parser);
    createFunctionTask.execute(statement, transactionManager, metadataManager, new AllowAllAccessControl(), TEST_SESSION, emptyList(), warningCollector);
    assertEquals(createFunctionTask.getAddedSessionFunctions().size(), 1);
}
Also used : TaskTestUtils.createQueryStateMachine(com.facebook.presto.execution.TaskTestUtils.createQueryStateMachine) InMemoryTransactionManager.createTestTransactionManager(com.facebook.presto.transaction.InMemoryTransactionManager.createTestTransactionManager) TransactionManager(com.facebook.presto.transaction.TransactionManager) AllowAllAccessControl(com.facebook.presto.security.AllowAllAccessControl) CreateFunction(com.facebook.presto.sql.tree.CreateFunction) SqlParser(com.facebook.presto.sql.parser.SqlParser) WarningCollector(com.facebook.presto.spi.WarningCollector) Test(org.testng.annotations.Test)

Example 3 with CreateFunction

use of com.facebook.presto.sql.tree.CreateFunction in project presto by prestodb.

the class TestCreateFunctionTask method testCreateTemporaryFunctionWithSameNameFails.

@Test(expectedExceptions = PrestoException.class, expectedExceptionsMessageRegExp = "Session function .* has already been defined")
public void testCreateTemporaryFunctionWithSameNameFails() {
    SqlParser parser = new SqlParser();
    String sqlString = "CREATE TEMPORARY FUNCTION foo() RETURNS int RETURN 1";
    CreateFunction statement = (CreateFunction) parser.createStatement(sqlString, ParsingOptions.builder().build());
    TransactionManager transactionManager = createTestTransactionManager();
    QueryStateMachine stateMachine = createQueryStateMachine(sqlString, TEST_SESSION, false, transactionManager, executorService, metadataManager);
    WarningCollector warningCollector = stateMachine.getWarningCollector();
    CreateFunctionTask createFunctionTask = new CreateFunctionTask(parser);
    createFunctionTask.execute(statement, transactionManager, metadataManager, new AllowAllAccessControl(), TEST_SESSION, emptyList(), warningCollector);
    createFunctionTask.execute(statement, transactionManager, metadataManager, new AllowAllAccessControl(), TEST_SESSION, emptyList(), warningCollector);
}
Also used : TaskTestUtils.createQueryStateMachine(com.facebook.presto.execution.TaskTestUtils.createQueryStateMachine) InMemoryTransactionManager.createTestTransactionManager(com.facebook.presto.transaction.InMemoryTransactionManager.createTestTransactionManager) TransactionManager(com.facebook.presto.transaction.TransactionManager) AllowAllAccessControl(com.facebook.presto.security.AllowAllAccessControl) CreateFunction(com.facebook.presto.sql.tree.CreateFunction) SqlParser(com.facebook.presto.sql.parser.SqlParser) WarningCollector(com.facebook.presto.spi.WarningCollector) Test(org.testng.annotations.Test)

Example 4 with CreateFunction

use of com.facebook.presto.sql.tree.CreateFunction 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)

Aggregations

CreateFunction (com.facebook.presto.sql.tree.CreateFunction)4 WarningCollector (com.facebook.presto.spi.WarningCollector)3 SqlParser (com.facebook.presto.sql.parser.SqlParser)3 TransactionManager (com.facebook.presto.transaction.TransactionManager)3 Test (org.testng.annotations.Test)3 TaskTestUtils.createQueryStateMachine (com.facebook.presto.execution.TaskTestUtils.createQueryStateMachine)2 AllowAllAccessControl (com.facebook.presto.security.AllowAllAccessControl)2 Return (com.facebook.presto.sql.tree.Return)2 InMemoryTransactionManager.createTestTransactionManager (com.facebook.presto.transaction.InMemoryTransactionManager.createTestTransactionManager)2 Session (com.facebook.presto.Session)1 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)1 Type (com.facebook.presto.common.type.Type)1 TypeSignature (com.facebook.presto.common.type.TypeSignature)1 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)1 FunctionAndTypeManager.qualifyObjectName (com.facebook.presto.metadata.FunctionAndTypeManager.qualifyObjectName)1 Metadata (com.facebook.presto.metadata.Metadata)1 SESSION_NAMESPACE (com.facebook.presto.metadata.SessionFunctionHandle.SESSION_NAMESPACE)1 AccessControl (com.facebook.presto.security.AccessControl)1 PrestoException (com.facebook.presto.spi.PrestoException)1 ALREADY_EXISTS (com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS)1