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());
}
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);
}
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));
}
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()));
}
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()));
}
Aggregations