Search in sources :

Example 6 with Identifier

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

the class TestSqlParser method testNullTreatment.

@Test
public void testNullTreatment() {
    assertExpression("lead(x, 1) ignore nulls over()", new FunctionCall(QualifiedName.of("lead"), Optional.of(new Window(ImmutableList.of(), Optional.empty(), Optional.empty())), Optional.empty(), Optional.empty(), false, true, ImmutableList.of(new Identifier("x"), new LongLiteral("1"))));
    assertExpression("lead(x, 1) respect nulls over()", new FunctionCall(QualifiedName.of("lead"), Optional.of(new Window(ImmutableList.of(), Optional.empty(), Optional.empty())), Optional.empty(), Optional.empty(), false, false, ImmutableList.of(new Identifier("x"), new LongLiteral("1"))));
}
Also used : Window(com.facebook.presto.sql.tree.Window) Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 7 with Identifier

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

the class TestSqlParser method testCreateTableAsSelect.

@Test
public void testCreateTableAsSelect() {
    Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t")));
    Query querySelectColumn = simpleQuery(selectList(new Identifier("a")), table(QualifiedName.of("t")));
    Query querySelectColumns = simpleQuery(selectList(new Identifier("a"), new Identifier("b")), table(QualifiedName.of("t")));
    QualifiedName table = QualifiedName.of("foo");
    assertStatement("CREATE TABLE foo AS SELECT * FROM t", new CreateTableAsSelect(table, query, false, ImmutableList.of(), true, Optional.empty(), Optional.empty()));
    assertStatement("CREATE TABLE foo(x) AS SELECT a FROM t", new CreateTableAsSelect(table, querySelectColumn, false, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty()));
    assertStatement("CREATE TABLE foo(x,y) AS SELECT a,b FROM t", new CreateTableAsSelect(table, querySelectColumns, false, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty()));
    assertStatement("CREATE TABLE IF NOT EXISTS foo AS SELECT * FROM t", new CreateTableAsSelect(table, query, true, ImmutableList.of(), true, Optional.empty(), Optional.empty()));
    assertStatement("CREATE TABLE IF NOT EXISTS foo(x) AS SELECT a FROM t", new CreateTableAsSelect(table, querySelectColumn, true, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty()));
    assertStatement("CREATE TABLE IF NOT EXISTS foo(x,y) AS SELECT a,b FROM t", new CreateTableAsSelect(table, querySelectColumns, true, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty()));
    assertStatement("CREATE TABLE foo AS SELECT * FROM t WITH NO DATA", new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.empty(), Optional.empty()));
    assertStatement("CREATE TABLE foo(x) AS SELECT a FROM t WITH NO DATA", new CreateTableAsSelect(table, querySelectColumn, false, ImmutableList.of(), false, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty()));
    assertStatement("CREATE TABLE foo(x,y) AS SELECT a,b FROM t WITH NO DATA", new CreateTableAsSelect(table, querySelectColumns, false, ImmutableList.of(), false, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty()));
    List<Property> properties = ImmutableList.of(new Property(new Identifier("string"), new StringLiteral("bar")), new Property(new Identifier("long"), new LongLiteral("42")), new Property(new Identifier("computed"), new FunctionCall(QualifiedName.of("concat"), ImmutableList.of(new StringLiteral("ban"), new StringLiteral("ana")))), new Property(new Identifier("a"), new ArrayConstructor(ImmutableList.of(new StringLiteral("v1"), new StringLiteral("v2")))));
    assertStatement("CREATE TABLE foo " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT * FROM t", new CreateTableAsSelect(table, query, false, properties, true, Optional.empty(), Optional.empty()));
    assertStatement("CREATE TABLE foo(x) " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a FROM t", new CreateTableAsSelect(table, querySelectColumn, false, properties, true, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty()));
    assertStatement("CREATE TABLE foo(x,y) " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a,b FROM t", new CreateTableAsSelect(table, querySelectColumns, false, properties, true, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty()));
    assertStatement("CREATE TABLE foo " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT * FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, query, false, properties, false, Optional.empty(), Optional.empty()));
    assertStatement("CREATE TABLE foo(x) " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, querySelectColumn, false, properties, false, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty()));
    assertStatement("CREATE TABLE foo(x,y) " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a,b FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, querySelectColumns, false, properties, false, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty()));
    assertStatement("CREATE TABLE foo COMMENT 'test'" + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT * FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, query, false, properties, false, Optional.empty(), Optional.of("test")));
    assertStatement("CREATE TABLE foo(x) COMMENT 'test'" + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, querySelectColumn, false, properties, false, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.of("test")));
    assertStatement("CREATE TABLE foo(x,y) COMMENT 'test'" + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a,b FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, querySelectColumns, false, properties, false, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.of("test")));
    assertStatement("CREATE TABLE foo(x,y) COMMENT 'test'" + "WITH ( \"string\" = 'bar', \"long\" = 42, computed = 'ban' || 'ana', a = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a,b FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, querySelectColumns, false, properties, false, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.of("test")));
}
Also used : Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) Query(com.facebook.presto.sql.tree.Query) QueryUtil.simpleQuery(com.facebook.presto.sql.QueryUtil.simpleQuery) WithQuery(com.facebook.presto.sql.tree.WithQuery) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) ArrayConstructor(com.facebook.presto.sql.tree.ArrayConstructor) AllColumns(com.facebook.presto.sql.tree.AllColumns) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Property(com.facebook.presto.sql.tree.Property) Test(org.testng.annotations.Test)

Example 8 with Identifier

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

the class TestSqlParser method testRevoke.

@Test
public void testRevoke() {
    assertStatement("REVOKE INSERT, DELETE ON t FROM u", new Revoke(false, Optional.of(ImmutableList.of("INSERT", "DELETE")), false, QualifiedName.of("t"), new PrincipalSpecification(PrincipalSpecification.Type.UNSPECIFIED, new Identifier("u"))));
    assertStatement("REVOKE GRANT OPTION FOR SELECT ON t FROM ROLE PUBLIC", new Revoke(true, Optional.of(ImmutableList.of("SELECT")), false, QualifiedName.of("t"), new PrincipalSpecification(PrincipalSpecification.Type.ROLE, new Identifier("PUBLIC"))));
    assertStatement("REVOKE ALL PRIVILEGES ON TABLE t FROM USER u", new Revoke(false, Optional.empty(), true, QualifiedName.of("t"), new PrincipalSpecification(PrincipalSpecification.Type.USER, new Identifier("u"))));
    assertStatement("REVOKE taco ON TABLE \"t\" FROM \"u\"", new Revoke(false, Optional.of(ImmutableList.of("taco")), true, QualifiedName.of("t"), new PrincipalSpecification(PrincipalSpecification.Type.UNSPECIFIED, new Identifier("u"))));
}
Also used : Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) Revoke(com.facebook.presto.sql.tree.Revoke) PrincipalSpecification(com.facebook.presto.sql.tree.PrincipalSpecification) Test(org.testng.annotations.Test)

Example 9 with Identifier

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

the class TestSqlParser method testUnnest.

@Test
public void testUnnest() {
    assertStatement("SELECT * FROM t CROSS JOIN UNNEST(a)", simpleQuery(selectList(new AllColumns()), new Join(Join.Type.CROSS, new Table(QualifiedName.of("t")), new Unnest(ImmutableList.of(new Identifier("a")), false), Optional.empty())));
    assertStatement("SELECT * FROM t CROSS JOIN UNNEST(a, b) WITH ORDINALITY", simpleQuery(selectList(new AllColumns()), new Join(Join.Type.CROSS, new Table(QualifiedName.of("t")), new Unnest(ImmutableList.of(new Identifier("a"), new Identifier("b")), true), Optional.empty())));
    assertStatement("SELECT * FROM t FULL JOIN UNNEST(a) AS tmp (c) ON true", simpleQuery(selectList(new AllColumns()), new Join(Join.Type.FULL, new Table(QualifiedName.of("t")), new AliasedRelation(new Unnest(ImmutableList.of(new Identifier("a")), false), new Identifier("tmp"), ImmutableList.of(new Identifier("c"))), Optional.of(new JoinOn(BooleanLiteral.TRUE_LITERAL)))));
}
Also used : CreateTable(com.facebook.presto.sql.tree.CreateTable) DropTable(com.facebook.presto.sql.tree.DropTable) Table(com.facebook.presto.sql.tree.Table) RenameTable(com.facebook.presto.sql.tree.RenameTable) Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) NaturalJoin(com.facebook.presto.sql.tree.NaturalJoin) Join(com.facebook.presto.sql.tree.Join) AllColumns(com.facebook.presto.sql.tree.AllColumns) Unnest(com.facebook.presto.sql.tree.Unnest) JoinOn(com.facebook.presto.sql.tree.JoinOn) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation) Test(org.testng.annotations.Test)

Example 10 with Identifier

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

the class TestSqlParser method testCreateRole.

@Test
public void testCreateRole() throws Exception {
    assertStatement("CREATE ROLE role", new CreateRole(new Identifier("role"), Optional.empty()));
    assertStatement("CREATE ROLE role1 WITH ADMIN admin", new CreateRole(new Identifier("role1"), Optional.of(new GrantorSpecification(GrantorSpecification.Type.PRINCIPAL, Optional.of(new PrincipalSpecification(PrincipalSpecification.Type.UNSPECIFIED, new Identifier("admin")))))));
    assertStatement("CREATE ROLE \"role\" WITH ADMIN \"admin\"", new CreateRole(new Identifier("role"), Optional.of(new GrantorSpecification(GrantorSpecification.Type.PRINCIPAL, Optional.of(new PrincipalSpecification(PrincipalSpecification.Type.UNSPECIFIED, new Identifier("admin")))))));
    assertStatement("CREATE ROLE \"ro le\" WITH ADMIN \"ad min\"", new CreateRole(new Identifier("ro le"), Optional.of(new GrantorSpecification(GrantorSpecification.Type.PRINCIPAL, Optional.of(new PrincipalSpecification(PrincipalSpecification.Type.UNSPECIFIED, new Identifier("ad min")))))));
    assertStatement("CREATE ROLE \"!@#$%^&*'\" WITH ADMIN \"ад\"\"мін\"", new CreateRole(new Identifier("!@#$%^&*'"), Optional.of(new GrantorSpecification(GrantorSpecification.Type.PRINCIPAL, Optional.of(new PrincipalSpecification(PrincipalSpecification.Type.UNSPECIFIED, new Identifier("ад\"мін")))))));
    assertStatement("CREATE ROLE role2 WITH ADMIN USER admin1", new CreateRole(new Identifier("role2"), Optional.of(new GrantorSpecification(GrantorSpecification.Type.PRINCIPAL, Optional.of(new PrincipalSpecification(PrincipalSpecification.Type.USER, new Identifier("admin1")))))));
    assertStatement("CREATE ROLE role2 WITH ADMIN ROLE role1", new CreateRole(new Identifier("role2"), Optional.of(new GrantorSpecification(GrantorSpecification.Type.PRINCIPAL, Optional.of(new PrincipalSpecification(PrincipalSpecification.Type.ROLE, new Identifier("role1")))))));
    assertStatement("CREATE ROLE role2 WITH ADMIN CURRENT_USER", new CreateRole(new Identifier("role2"), Optional.of(new GrantorSpecification(GrantorSpecification.Type.CURRENT_USER, Optional.empty()))));
    assertStatement("CREATE ROLE role2 WITH ADMIN CURRENT_ROLE", new CreateRole(new Identifier("role2"), Optional.of(new GrantorSpecification(GrantorSpecification.Type.CURRENT_ROLE, Optional.empty()))));
}
Also used : Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) PrincipalSpecification(com.facebook.presto.sql.tree.PrincipalSpecification) GrantorSpecification(com.facebook.presto.sql.tree.GrantorSpecification) CreateRole(com.facebook.presto.sql.tree.CreateRole) Test(org.testng.annotations.Test)

Aggregations

Identifier (com.facebook.presto.sql.tree.Identifier)46 Test (org.testng.annotations.Test)31 QueryUtil.quotedIdentifier (com.facebook.presto.sql.QueryUtil.quotedIdentifier)27 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)15 Query (com.facebook.presto.sql.tree.Query)11 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)10 AllColumns (com.facebook.presto.sql.tree.AllColumns)9 DropTable (com.facebook.presto.sql.tree.DropTable)9 QualifiedName (com.facebook.presto.sql.tree.QualifiedName)9 QuerySpecification (com.facebook.presto.sql.tree.QuerySpecification)9 Table (com.facebook.presto.sql.tree.Table)9 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)8 CreateTable (com.facebook.presto.sql.tree.CreateTable)8 Expression (com.facebook.presto.sql.tree.Expression)8 QueryUtil.simpleQuery (com.facebook.presto.sql.QueryUtil.simpleQuery)7 SingleColumn (com.facebook.presto.sql.tree.SingleColumn)7 StringLiteral (com.facebook.presto.sql.tree.StringLiteral)7 WithQuery (com.facebook.presto.sql.tree.WithQuery)7 CreateTableAsSelect (com.facebook.presto.sql.tree.CreateTableAsSelect)6 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)6