use of io.trino.sql.tree.OrderBy in project trino by trinodb.
the class FunctionCallProvider method getExpectedValue.
@Override
public FunctionCall getExpectedValue(SymbolAliases aliases) {
List<Expression> symbolReferences = toSymbolReferences(args, aliases);
if (isWindowFunction) {
verify(!distinct, "window does not support distinct");
verify(orderBy.isEmpty(), "window does not support order by");
return new ExpectedWindowFunctionCall(symbolReferences);
}
Optional<OrderBy> orderByClause = Optional.empty();
if (!orderBy.isEmpty()) {
orderByClause = Optional.of(new OrderBy(orderBy.stream().map(item -> new SortItem(Symbol.from(aliases.get(item.getField())).toSymbolReference(), item.getOrdering(), item.getNullOrdering())).collect(Collectors.toList())));
}
return new FunctionCall(Optional.empty(), name, Optional.empty(), filter.map(symbol -> symbol.toSymbol(aliases).toSymbolReference()), orderByClause, distinct, Optional.empty(), Optional.empty(), symbolReferences);
}
use of io.trino.sql.tree.OrderBy in project trino by trinodb.
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) {
Expression rowCount;
if (context.offset.INTEGER_VALUE() != null) {
rowCount = new LongLiteral(getLocation(context.offset.INTEGER_VALUE()), context.offset.getText());
} else {
rowCount = new Parameter(getLocation(context.offset.QUESTION_MARK()), parameterPosition);
parameterPosition++;
}
offset = Optional.of(new Offset(Optional.of(getLocation(context.OFFSET())), rowCount));
}
Optional<Node> limit = Optional.empty();
if (context.FETCH() != null) {
Optional<Expression> rowCount = Optional.empty();
if (context.fetchFirst != null) {
if (context.fetchFirst.INTEGER_VALUE() != null) {
rowCount = Optional.of(new LongLiteral(getLocation(context.fetchFirst.INTEGER_VALUE()), context.fetchFirst.getText()));
} else {
rowCount = Optional.of(new Parameter(getLocation(context.fetchFirst.QUESTION_MARK()), parameterPosition));
parameterPosition++;
}
}
limit = Optional.of(new FetchFirst(Optional.of(getLocation(context.FETCH())), rowCount, context.TIES() != null));
} else if (context.LIMIT() != null) {
if (context.limit == null) {
throw new IllegalStateException("Missing LIMIT value");
}
Expression rowCount;
if (context.limit.ALL() != null) {
rowCount = new AllRows(getLocation(context.limit.ALL()));
} else if (context.limit.rowCount().INTEGER_VALUE() != null) {
rowCount = new LongLiteral(getLocation(context.limit.rowCount().INTEGER_VALUE()), context.limit.getText());
} else {
rowCount = new Parameter(getLocation(context.limit.rowCount().QUESTION_MARK()), parameterPosition);
parameterPosition++;
}
limit = Optional.of(new Limit(Optional.of(getLocation(context.LIMIT())), rowCount));
}
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(), query.getWindows(), orderBy, offset, limit), Optional.empty(), Optional.empty(), Optional.empty());
}
return new Query(getLocation(context), Optional.empty(), term, orderBy, offset, limit);
}
use of io.trino.sql.tree.OrderBy in project trino by trinodb.
the class TestSqlParser method testListagg.
@Test
public void testListagg() {
assertExpression("LISTAGG(x) WITHIN GROUP (ORDER BY x)", new FunctionCall(Optional.empty(), QualifiedName.of("LISTAGG"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new Identifier("x", false), ASCENDING, UNDEFINED)))), false, Optional.empty(), Optional.empty(), ImmutableList.of(identifier("x"), new StringLiteral(""), new BooleanLiteral("true"), new StringLiteral("..."), new BooleanLiteral("false"))));
assertExpression("LISTAGG( DISTINCT x) WITHIN GROUP (ORDER BY x)", new FunctionCall(Optional.empty(), QualifiedName.of("LISTAGG"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new Identifier("x", false), ASCENDING, UNDEFINED)))), true, Optional.empty(), Optional.empty(), ImmutableList.of(identifier("x"), new StringLiteral(""), new BooleanLiteral("true"), new StringLiteral("..."), new BooleanLiteral("false"))));
assertExpression("LISTAGG(x, ',') WITHIN GROUP (ORDER BY y)", new FunctionCall(Optional.empty(), QualifiedName.of("LISTAGG"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new Identifier("y", false), ASCENDING, UNDEFINED)))), false, Optional.empty(), Optional.empty(), ImmutableList.of(identifier("x"), new StringLiteral(","), new BooleanLiteral("true"), new StringLiteral("..."), new BooleanLiteral("false"))));
assertExpression("LISTAGG(x, ',' ON OVERFLOW ERROR) WITHIN GROUP (ORDER BY x)", new FunctionCall(Optional.empty(), QualifiedName.of("LISTAGG"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new Identifier("x", false), ASCENDING, UNDEFINED)))), false, Optional.empty(), Optional.empty(), ImmutableList.of(identifier("x"), new StringLiteral(","), new BooleanLiteral("true"), new StringLiteral("..."), new BooleanLiteral("false"))));
assertExpression("LISTAGG(x, ',' ON OVERFLOW TRUNCATE WITH COUNT) WITHIN GROUP (ORDER BY x)", new FunctionCall(Optional.empty(), QualifiedName.of("LISTAGG"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new Identifier("x", false), ASCENDING, UNDEFINED)))), false, Optional.empty(), Optional.empty(), ImmutableList.of(identifier("x"), new StringLiteral(","), new BooleanLiteral("false"), new StringLiteral("..."), new BooleanLiteral("true"))));
assertExpression("LISTAGG(x, ',' ON OVERFLOW TRUNCATE 'HIDDEN' WITHOUT COUNT) WITHIN GROUP (ORDER BY x)", new FunctionCall(Optional.empty(), QualifiedName.of("LISTAGG"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new Identifier("x", false), ASCENDING, UNDEFINED)))), false, Optional.empty(), Optional.empty(), ImmutableList.of(identifier("x"), new StringLiteral(","), new BooleanLiteral("false"), new StringLiteral("HIDDEN"), new BooleanLiteral("false"))));
}
use of io.trino.sql.tree.OrderBy in project trino by trinodb.
the class TestSqlParser method testShowStatsForQuery.
@Test
public void testShowStatsForQuery() {
String[] tableNames = { "t", "s.t", "c.s.t" };
for (String fullName : tableNames) {
QualifiedName qualifiedName = makeQualifiedName(fullName);
// Simple SELECT
assertStatement(format("SHOW STATS FOR (SELECT * FROM %s)", qualifiedName), createShowStats(qualifiedName, ImmutableList.of(new AllColumns()), Optional.empty()));
// SELECT with predicate
assertStatement(format("SHOW STATS FOR (SELECT * FROM %s WHERE field > 0)", qualifiedName), createShowStats(qualifiedName, ImmutableList.of(new AllColumns()), Optional.of(new ComparisonExpression(ComparisonExpression.Operator.GREATER_THAN, new Identifier("field"), new LongLiteral("0")))));
// SELECT with more complex predicate
assertStatement(format("SHOW STATS FOR (SELECT * FROM %s WHERE field > 0 or field < 0)", qualifiedName), createShowStats(qualifiedName, ImmutableList.of(new AllColumns()), Optional.of(LogicalExpression.or(new ComparisonExpression(ComparisonExpression.Operator.GREATER_THAN, new Identifier("field"), new LongLiteral("0")), new ComparisonExpression(ComparisonExpression.Operator.LESS_THAN, new Identifier("field"), new LongLiteral("0"))))));
}
// SELECT with LIMIT
assertThat(statement("SHOW STATS FOR (SELECT * FROM t LIMIT 10)")).isEqualTo(new ShowStats(Optional.of(location(1, 1)), new TableSubquery(new Query(location(1, 17), Optional.empty(), new QuerySpecification(location(1, 17), new Select(location(1, 17), false, ImmutableList.of(new AllColumns(location(1, 24), Optional.empty(), ImmutableList.of()))), Optional.of(new Table(location(1, 31), QualifiedName.of(ImmutableList.of(new Identifier(location(1, 31), "t", false))))), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.of(new Limit(location(1, 33), new LongLiteral(location(1, 39), "10")))), Optional.empty(), Optional.empty(), Optional.empty()))));
// SELECT with ORDER BY ... LIMIT
assertThat(statement("SHOW STATS FOR (SELECT * FROM t ORDER BY field LIMIT 10)")).isEqualTo(new ShowStats(Optional.of(location(1, 1)), new TableSubquery(new Query(location(1, 17), Optional.empty(), new QuerySpecification(location(1, 17), new Select(location(1, 17), false, ImmutableList.of(new AllColumns(location(1, 24), Optional.empty(), ImmutableList.of()))), Optional.of(new Table(location(1, 31), QualifiedName.of(ImmutableList.of(new Identifier(location(1, 31), "t", false))))), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), Optional.of(new OrderBy(location(1, 33), ImmutableList.of(new SortItem(location(1, 42), new Identifier(location(1, 42), "field", false), ASCENDING, UNDEFINED)))), Optional.empty(), Optional.of(new Limit(location(1, 48), new LongLiteral(location(1, 54), "10")))), Optional.empty(), Optional.empty(), Optional.empty()))));
// SELECT with WITH
assertThat(statement("SHOW STATS FOR (\n" + " WITH t AS (SELECT 1 )\n" + " SELECT * FROM t)")).isEqualTo(new ShowStats(Optional.of(location(1, 1)), new TableSubquery(new Query(location(2, 4), Optional.of(new With(location(2, 4), false, ImmutableList.of(new WithQuery(location(2, 9), new Identifier(location(2, 9), "t", false), new Query(location(2, 15), Optional.empty(), new QuerySpecification(location(2, 15), new Select(location(2, 15), false, ImmutableList.of(new SingleColumn(location(2, 22), new LongLiteral(location(2, 22), "1"), Optional.empty()))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty())))), new QuerySpecification(location(3, 4), new Select(location(3, 4), false, ImmutableList.of(new AllColumns(location(3, 11), Optional.empty(), ImmutableList.of()))), Optional.of(new Table(location(3, 18), QualifiedName.of(ImmutableList.of(new Identifier(location(3, 18), "t", false))))), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()))));
}
use of io.trino.sql.tree.OrderBy in project trino by trinodb.
the class TestSqlParser method testWindowSpecification.
@Test
public void testWindowSpecification() {
assertExpression("rank() OVER someWindow", new FunctionCall(Optional.empty(), QualifiedName.of("rank"), Optional.of(new WindowReference(new Identifier("someWindow"))), Optional.empty(), Optional.empty(), false, Optional.empty(), Optional.empty(), ImmutableList.of()));
assertExpression("rank() OVER (someWindow PARTITION BY x ORDER BY y ROWS CURRENT ROW)", new FunctionCall(Optional.empty(), QualifiedName.of("rank"), Optional.of(new WindowSpecification(Optional.of(new Identifier("someWindow")), ImmutableList.of(new Identifier("x")), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new Identifier("y"), ASCENDING, UNDEFINED)))), Optional.of(new WindowFrame(ROWS, new FrameBound(CURRENT_ROW), Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), ImmutableList.of())))), Optional.empty(), Optional.empty(), false, Optional.empty(), Optional.empty(), ImmutableList.of()));
assertExpression("rank() OVER (PARTITION BY x ORDER BY y ROWS CURRENT ROW)", new FunctionCall(Optional.empty(), QualifiedName.of("rank"), Optional.of(new WindowSpecification(Optional.empty(), ImmutableList.of(new Identifier("x")), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new Identifier("y"), ASCENDING, UNDEFINED)))), Optional.of(new WindowFrame(ROWS, new FrameBound(CURRENT_ROW), Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), ImmutableList.of())))), Optional.empty(), Optional.empty(), false, Optional.empty(), Optional.empty(), ImmutableList.of()));
}
Aggregations