Search in sources :

Example 1 with QuerySpecification

use of io.prestosql.sql.tree.QuerySpecification in project hetu-core by openlookeng.

the class AstBuilder method visitQuerySpecification.

@Override
public Node visitQuerySpecification(SqlBaseParser.QuerySpecificationContext context) {
    Optional<Relation> from = Optional.empty();
    List<SelectItem> selectItems = visit(context.selectItem(), SelectItem.class);
    List<Relation> relations = visit(context.relation(), Relation.class);
    if (!relations.isEmpty()) {
        // synthesize implicit join nodes
        Iterator<Relation> iterator = relations.iterator();
        Relation relation = iterator.next();
        while (iterator.hasNext()) {
            relation = new Join(getLocation(context), Join.Type.IMPLICIT, relation, iterator.next(), Optional.empty());
        }
        from = Optional.of(relation);
    }
    return new QuerySpecification(getLocation(context), new Select(getLocation(context.SELECT()), isDistinct(context.setQuantifier()), selectItems), from, visitIfPresent(context.where, Expression.class), visitIfPresent(context.groupBy(), GroupBy.class), visitIfPresent(context.having, Expression.class), Optional.empty(), Optional.empty(), Optional.empty());
}
Also used : QuerySpecification(io.prestosql.sql.tree.QuerySpecification) SampledRelation(io.prestosql.sql.tree.SampledRelation) Relation(io.prestosql.sql.tree.Relation) AliasedRelation(io.prestosql.sql.tree.AliasedRelation) GroupBy(io.prestosql.sql.tree.GroupBy) SimpleGroupBy(io.prestosql.sql.tree.SimpleGroupBy) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) BindExpression(io.prestosql.sql.tree.BindExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) TryExpression(io.prestosql.sql.tree.TryExpression) SelectItem(io.prestosql.sql.tree.SelectItem) CreateTableAsSelect(io.prestosql.sql.tree.CreateTableAsSelect) Select(io.prestosql.sql.tree.Select) Join(io.prestosql.sql.tree.Join) NaturalJoin(io.prestosql.sql.tree.NaturalJoin)

Example 2 with QuerySpecification

use of io.prestosql.sql.tree.QuerySpecification in project hetu-core by openlookeng.

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<Node> limit = Optional.empty();
    if (context.FETCH() != null) {
        limit = Optional.of(new FetchFirst(Optional.of(getLocation(context.FETCH())), getTextIfPresent(context.fetchFirst), context.TIES() != null));
    } else if (context.LIMIT() != null) {
        limit = Optional.of(new Limit(Optional.of(getLocation(context.LIMIT())), getTextIfPresent(context.limit).orElseThrow(() -> new IllegalStateException("Missing LIMIT value"))));
    }
    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 or fetch,
        // fold the order by, limit, offset or fetch 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, limit), Optional.empty(), Optional.empty(), Optional.empty());
    }
    return new Query(getLocation(context), Optional.empty(), term, orderBy, offset, limit);
}
Also used : OrderBy(io.prestosql.sql.tree.OrderBy) Query(io.prestosql.sql.tree.Query) WithQuery(io.prestosql.sql.tree.WithQuery) Node(io.prestosql.sql.tree.Node) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) Offset(io.prestosql.sql.tree.Offset) QuerySpecification(io.prestosql.sql.tree.QuerySpecification) SortItem(io.prestosql.sql.tree.SortItem) Limit(io.prestosql.sql.tree.Limit) QueryBody(io.prestosql.sql.tree.QueryBody) FetchFirst(io.prestosql.sql.tree.FetchFirst)

Example 3 with QuerySpecification

use of io.prestosql.sql.tree.QuerySpecification in project hetu-core by openlookeng.

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(io.prestosql.sql.tree.QuerySpecification) Query(io.prestosql.sql.tree.Query) WithQuery(io.prestosql.sql.tree.WithQuery) ShowStats(io.prestosql.sql.tree.ShowStats) TableSubquery(io.prestosql.sql.tree.TableSubquery)

Example 4 with QuerySpecification

use of io.prestosql.sql.tree.QuerySpecification in project hetu-core by openlookeng.

the class TestSqlParser method testAggregationWithOrderBy.

@Test
public void testAggregationWithOrderBy() {
    assertExpression("array_agg(x ORDER BY x DESC)", new FunctionCall(QualifiedName.of("array_agg"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(identifier("x"), DESCENDING, UNDEFINED)))), false, ImmutableList.of(identifier("x"))));
    assertStatement("SELECT array_agg(x ORDER BY t.y) FROM t", new Query(Optional.empty(), new QuerySpecification(selectList(new FunctionCall(QualifiedName.of("array_agg"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new DereferenceExpression(new Identifier("t"), identifier("y")), ASCENDING, UNDEFINED)))), false, ImmutableList.of(new Identifier("x")))), Optional.of(table(QualifiedName.of("t"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
}
Also used : OrderBy(io.prestosql.sql.tree.OrderBy) QuerySpecification(io.prestosql.sql.tree.QuerySpecification) SortItem(io.prestosql.sql.tree.SortItem) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) Identifier(io.prestosql.sql.tree.Identifier) QueryUtil.quotedIdentifier(io.prestosql.sql.QueryUtil.quotedIdentifier) QueryUtil.simpleQuery(io.prestosql.sql.QueryUtil.simpleQuery) Query(io.prestosql.sql.tree.Query) WithQuery(io.prestosql.sql.tree.WithQuery) FunctionCall(io.prestosql.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 5 with QuerySpecification

use of io.prestosql.sql.tree.QuerySpecification in project hetu-core by openlookeng.

the class TestSqlParser method testSelectWithOffset.

@Test
public void testSelectWithOffset() {
    assertStatement("SELECT * FROM table1 OFFSET 2 ROWS", 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 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()));
    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 : QuerySpecification(io.prestosql.sql.tree.QuerySpecification) Table(io.prestosql.sql.tree.Table) CreateTable(io.prestosql.sql.tree.CreateTable) VacuumTable(io.prestosql.sql.tree.VacuumTable) DropTable(io.prestosql.sql.tree.DropTable) RenameTable(io.prestosql.sql.tree.RenameTable) QueryUtil.simpleQuery(io.prestosql.sql.QueryUtil.simpleQuery) Query(io.prestosql.sql.tree.Query) WithQuery(io.prestosql.sql.tree.WithQuery) StringLiteral(io.prestosql.sql.tree.StringLiteral) LongLiteral(io.prestosql.sql.tree.LongLiteral) AllColumns(io.prestosql.sql.tree.AllColumns) Offset(io.prestosql.sql.tree.Offset) Test(org.testng.annotations.Test)

Aggregations

QuerySpecification (io.prestosql.sql.tree.QuerySpecification)22 Query (io.prestosql.sql.tree.Query)15 WithQuery (io.prestosql.sql.tree.WithQuery)12 Table (io.prestosql.sql.tree.Table)11 LongLiteral (io.prestosql.sql.tree.LongLiteral)10 Test (org.testng.annotations.Test)10 FunctionCall (io.prestosql.sql.tree.FunctionCall)9 Identifier (io.prestosql.sql.tree.Identifier)9 QueryUtil.simpleQuery (io.prestosql.sql.QueryUtil.simpleQuery)8 CreateTable (io.prestosql.sql.tree.CreateTable)8 DropTable (io.prestosql.sql.tree.DropTable)8 StringLiteral (io.prestosql.sql.tree.StringLiteral)8 Expression (io.prestosql.sql.tree.Expression)7 OrderBy (io.prestosql.sql.tree.OrderBy)7 Select (io.prestosql.sql.tree.Select)7 SortItem (io.prestosql.sql.tree.SortItem)7 CreateTableAsSelect (io.prestosql.sql.tree.CreateTableAsSelect)6 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)6 Node (io.prestosql.sql.tree.Node)6 Limit (io.prestosql.sql.tree.Limit)5