Search in sources :

Example 1 with RoutineCharacteristics

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

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

the class AstBuilder method getRoutineCharacteristics.

private RoutineCharacteristics getRoutineCharacteristics(SqlBaseParser.RoutineCharacteristicsContext context) {
    Language language = null;
    Determinism determinism = null;
    NullCallClause nullCallClause = null;
    for (SqlBaseParser.RoutineCharacteristicContext characteristic : context.routineCharacteristic()) {
        if (characteristic.language() != null) {
            if (language != null) {
                throw new ParsingException(format("Duplicate language clause: %s", characteristic.language().getText()), getLocation(characteristic.language()));
            }
            if (characteristic.language().SQL() != null) {
                language = Language.SQL;
            } else {
                language = new Language(((Identifier) visit(characteristic.language().identifier())).getValue());
            }
        } else if (characteristic.determinism() != null) {
            if (determinism != null) {
                throw new ParsingException(format("Duplicate determinism characteristics: %s", characteristic.determinism().getText()), getLocation(characteristic.determinism()));
            }
            determinism = characteristic.determinism().NOT() == null ? DETERMINISTIC : NOT_DETERMINISTIC;
        } else if (characteristic.nullCallClause() != null) {
            if (nullCallClause != null) {
                throw new ParsingException(format("Duplicate null-call clause: %s", characteristic.nullCallClause().getText()), getLocation(characteristic.nullCallClause()));
            }
            nullCallClause = characteristic.nullCallClause().CALLED() != null ? CALLED_ON_NULL_INPUT : RETURNS_NULL_ON_NULL_INPUT;
        } else {
            throw new IllegalArgumentException(format("Unsupported RoutineCharacteristic: %s", characteristic.getText()));
        }
    }
    return new RoutineCharacteristics(Optional.ofNullable(language), Optional.ofNullable(determinism), Optional.ofNullable(nullCallClause));
}
Also used : Determinism(com.facebook.presto.sql.tree.RoutineCharacteristics.Determinism) Identifier(com.facebook.presto.sql.tree.Identifier) Language(com.facebook.presto.sql.tree.RoutineCharacteristics.Language) RoutineCharacteristics(com.facebook.presto.sql.tree.RoutineCharacteristics) AlterRoutineCharacteristics(com.facebook.presto.sql.tree.AlterRoutineCharacteristics) NullCallClause(com.facebook.presto.sql.tree.RoutineCharacteristics.NullCallClause)

Aggregations

AlterRoutineCharacteristics (com.facebook.presto.sql.tree.AlterRoutineCharacteristics)2 RoutineCharacteristics (com.facebook.presto.sql.tree.RoutineCharacteristics)2 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)1 BooleanLiteral (com.facebook.presto.sql.tree.BooleanLiteral)1 CreateFunction (com.facebook.presto.sql.tree.CreateFunction)1 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)1 Identifier (com.facebook.presto.sql.tree.Identifier)1 Return (com.facebook.presto.sql.tree.Return)1 Determinism (com.facebook.presto.sql.tree.RoutineCharacteristics.Determinism)1 Language (com.facebook.presto.sql.tree.RoutineCharacteristics.Language)1 NullCallClause (com.facebook.presto.sql.tree.RoutineCharacteristics.NullCallClause)1 ShowCreateFunction (com.facebook.presto.sql.tree.ShowCreateFunction)1 SqlParameterDeclaration (com.facebook.presto.sql.tree.SqlParameterDeclaration)1 Test (org.testng.annotations.Test)1