Search in sources :

Example 1 with Query

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

the class AstBuilder method visitQueryNoWith.

@Override
public Node visitQueryNoWith(SqlBaseParser.QueryNoWithContext context) {
    QueryBody term = (QueryBody) visit(context.queryTerm());
    Optional<OrderBy> orderBy = Optional.empty();
    if (context.ORDER() != null) {
        orderBy = Optional.of(new OrderBy(getLocation(context.ORDER()), visit(context.sortItem(), SortItem.class)));
    }
    Optional<Offset> offset = Optional.empty();
    if (context.OFFSET() != null) {
        offset = Optional.of(new Offset(Optional.of(getLocation(context.OFFSET())), getTextIfPresent(context.offset).orElseThrow(() -> new IllegalStateException("Missing OFFSET row count"))));
    }
    if (term instanceof QuerySpecification) {
        // When we have a simple query specification
        // followed by order by, offset, limit, fold the order by and limit
        // clauses into the query specification (analyzer/planner
        // expects this structure to resolve references with respect
        // to columns defined in the query specification)
        QuerySpecification query = (QuerySpecification) term;
        return new Query(getLocation(context), Optional.empty(), new QuerySpecification(getLocation(context), query.getSelect(), query.getFrom(), query.getWhere(), query.getGroupBy(), query.getHaving(), orderBy, offset, getTextIfPresent(context.limit)), Optional.empty(), Optional.empty(), Optional.empty());
    }
    return new Query(getLocation(context), Optional.empty(), term, orderBy, offset, getTextIfPresent(context.limit));
}
Also used : OrderBy(com.facebook.presto.sql.tree.OrderBy) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) SortItem(com.facebook.presto.sql.tree.SortItem) Query(com.facebook.presto.sql.tree.Query) WithQuery(com.facebook.presto.sql.tree.WithQuery) QueryBody(com.facebook.presto.sql.tree.QueryBody) Offset(com.facebook.presto.sql.tree.Offset)

Example 2 with Query

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

the class AstBuilder method visitShowStatsForQuery.

@Override
public Node visitShowStatsForQuery(SqlBaseParser.ShowStatsForQueryContext context) {
    QuerySpecification specification = (QuerySpecification) visitQuerySpecification(context.querySpecification());
    Query query = new Query(Optional.empty(), specification, Optional.empty(), Optional.empty(), Optional.empty());
    return new ShowStats(Optional.of(getLocation(context)), new TableSubquery(query));
}
Also used : QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) Query(com.facebook.presto.sql.tree.Query) WithQuery(com.facebook.presto.sql.tree.WithQuery) ShowStats(com.facebook.presto.sql.tree.ShowStats) TableSubquery(com.facebook.presto.sql.tree.TableSubquery)

Example 3 with Query

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

the class TestSqlParser method testSelectWithOffset.

@Test
public void testSelectWithOffset() {
    assertStatement("SELECT * FROM table1 OFFSET 2 ROWS", simpleQuery(selectList(new AllColumns()), new Table(QualifiedName.of("table1")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset("2")), Optional.empty()));
    assertStatement("SELECT * FROM table1 OFFSET 2", new Query(Optional.empty(), new QuerySpecification(selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset("2")), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
    assertStatement("SELECT * FROM table1 order by x OFFSET 2 limit 10", new Query(Optional.empty(), new QuerySpecification(selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new Identifier("x"), ASCENDING, UNDEFINED)))), Optional.of(new Offset("2")), Optional.of("10")), Optional.empty(), Optional.empty(), Optional.empty()));
    Query valuesQuery = query(values(row(new LongLiteral("1"), new StringLiteral("1")), row(new LongLiteral("2"), new StringLiteral("2"))));
    assertStatement("SELECT * FROM (VALUES (1, '1'), (2, '2')) OFFSET 2 ROWS", simpleQuery(selectList(new AllColumns()), subquery(valuesQuery), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset("2")), Optional.empty()));
    assertStatement("SELECT * FROM (VALUES (1, '1'), (2, '2')) OFFSET 2", simpleQuery(selectList(new AllColumns()), subquery(valuesQuery), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset("2")), Optional.empty()));
}
Also used : OrderBy(com.facebook.presto.sql.tree.OrderBy) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) SortItem(com.facebook.presto.sql.tree.SortItem) 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) 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) AllColumns(com.facebook.presto.sql.tree.AllColumns) Offset(com.facebook.presto.sql.tree.Offset) Test(org.testng.annotations.Test)

Example 4 with Query

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

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

the class TestSqlParser method testSubstringBuiltInFunction.

@Test
public void testSubstringBuiltInFunction() {
    final String givenString = "ABCDEF";
    assertStatement(format("SELECT substring('%s' FROM 2)", givenString), new Query(Optional.empty(), new QuerySpecification(selectList(new FunctionCall(QualifiedName.of("substr"), Lists.newArrayList(new StringLiteral(givenString), new LongLiteral("2")))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
    assertStatement(format("SELECT substring('%s' FROM 2 FOR 3)", givenString), new Query(Optional.empty(), new QuerySpecification(selectList(new FunctionCall(QualifiedName.of("substr"), Lists.newArrayList(new StringLiteral(givenString), new LongLiteral("2"), new LongLiteral("3")))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
}
Also used : QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) 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) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Aggregations

Query (com.facebook.presto.sql.tree.Query)40 QueryUtil.simpleQuery (com.facebook.presto.sql.QueryUtil.simpleQuery)21 WithQuery (com.facebook.presto.sql.tree.WithQuery)18 Test (org.testng.annotations.Test)18 QuerySpecification (com.facebook.presto.sql.tree.QuerySpecification)17 AllColumns (com.facebook.presto.sql.tree.AllColumns)16 Identifier (com.facebook.presto.sql.tree.Identifier)13 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)12 Table (com.facebook.presto.sql.tree.Table)12 CreateTableAsSelect (com.facebook.presto.sql.tree.CreateTableAsSelect)9 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)8 OrderBy (com.facebook.presto.sql.tree.OrderBy)8 StringLiteral (com.facebook.presto.sql.tree.StringLiteral)8 QueryUtil.quotedIdentifier (com.facebook.presto.sql.QueryUtil.quotedIdentifier)7 CreateTable (com.facebook.presto.sql.tree.CreateTable)7 DropTable (com.facebook.presto.sql.tree.DropTable)7 QualifiedName (com.facebook.presto.sql.tree.QualifiedName)7 RenameTable (com.facebook.presto.sql.tree.RenameTable)6 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)5 Expression (com.facebook.presto.sql.tree.Expression)5