protected Void visitOrderBy(OrderBy node, Integer indentLevel) { for (SortItem sortItem : node.getSortItems()) { process(sortItem, indentLevel); } return null; }
public static OrderBy ordering(SortItem... items) { return new OrderBy(ImmutableList.copyOf(items)); }
@Override protected R visitOrderBy(OrderBy node, C context) { for (SortItem sortItem : node.getSortItems()) { process(sortItem, context); } return null; }
private static Optional<OrderBy> orderBy(List<SortItem> sortItems) { requireNonNull(sortItems, "sortItems is null"); if (sortItems.isEmpty()) { return Optional.empty(); } return Optional.of(new OrderBy(sortItems)); }
static String formatOrderBy(OrderBy orderBy, Optional<List<Expression>> parameters) { return "ORDER BY " + formatSortItems(orderBy.getSortItems(), parameters); }
@Override public Node visitOver(SqlBaseParser.OverContext context) { Optional<OrderBy> orderBy = Optional.empty(); if (context.ORDER() != null) { orderBy = Optional.of(new OrderBy(getLocation(context.ORDER()), visit(context.sortItem(), SortItem.class))); } return new Window( getLocation(context), visit(context.partition, Expression.class), orderBy, visitIfPresent(context.windowFrame(), WindowFrame.class)); }
private PlanBuilder sort(PlanBuilder subPlan, Optional<OrderBy> orderBy, Optional<String> limit, List<Expression> orderByExpressions) { if (!orderBy.isPresent()) { return subPlan; } Iterator<SortItem> sortItems = orderBy.get().getSortItems().iterator(); ImmutableList.Builder<Symbol> orderBySymbols = ImmutableList.builder(); Map<Symbol, SortOrder> orderings = new HashMap<>(); for (Expression fieldOrExpression : orderByExpressions) { Symbol symbol = subPlan.translate(fieldOrExpression); SortItem sortItem = sortItems.next(); if (!orderings.containsKey(symbol)) { orderBySymbols.add(symbol); orderings.put(symbol, toSortOrder(sortItem)); } } PlanNode planNode; OrderingScheme orderingScheme = new OrderingScheme(orderBySymbols.build(), orderings); if (limit.isPresent() && !limit.get().equalsIgnoreCase("all")) { planNode = new TopNNode(idAllocator.getNextId(), subPlan.getRoot(), Long.parseLong(limit.get()), orderingScheme, TopNNode.Step.SINGLE); } else { planNode = new SortNode(idAllocator.getNextId(), subPlan.getRoot(), orderingScheme); } return subPlan.withNewRoot(planNode); }
public FunctionCall getExpectedValue(SymbolAliases aliases) { List<Expression> symbolReferences = toSymbolReferences(args, aliases); if (isWindowFunction) { 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(name, Optional.empty(), Optional.empty(), orderByClause, distinct, symbolReferences); }
OrderBy orderBy = aggregation.getCall().getOrderBy().get(); sortKeys = orderBy.getSortItems().stream() .map(SortItem::getSortKey) .map(Symbol::from) .collect(toImmutableList()); sortOrders = orderBy.getSortItems().stream() .map(QueryPlanner::toSortOrder) .collect(toImmutableList());
@Test public void testSelectWithOrderBy() { assertStatement("SELECT * FROM table1 ORDER BY a", 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("a"), ASCENDING, UNDEFINED)))), Optional.empty()), Optional.empty(), Optional.empty())); }
private void verifySelectDistinct(QuerySpecification node, List<Expression> outputExpressions) { for (SortItem item : node.getOrderBy().get().getSortItems()) { Expression expression = item.getSortKey(); if (expression instanceof LongLiteral) { continue; } Expression rewrittenOrderByExpression = ExpressionTreeRewriter.rewriteWith(new OrderByExpressionRewriter(extractNamedOutputExpressions(node.getSelect())), expression); int index = outputExpressions.indexOf(rewrittenOrderByExpression); if (index == -1) { throw new SemanticException(ORDER_BY_MUST_BE_IN_SELECT, node.getSelect(), "For SELECT DISTINCT, ORDER BY expressions must appear in select list"); } if (!isDeterministic(expression)) { throw new SemanticException(NONDETERMINISTIC_ORDER_BY_EXPRESSION_WITH_SELECT_DISTINCT, expression, "Non deterministic ORDER BY expression is not supported with SELECT DISTINCT"); } } }
orderBy = Optional.of(new OrderBy(getLocation(context.ORDER()), visit(context.sortItem(), SortItem.class)));
List<Expression> sortKeys = node.getOrderBy().get().getSortItems().stream() .map(SortItem::getSortKey) .collect(toImmutableList());
orderBy = Optional.of(new OrderBy(visit(context.sortItem(), SortItem.class)));
toExtract.addAll(windowFunction.getArguments()); toExtract.addAll(window.getPartitionBy()); window.getOrderBy().ifPresent(orderBy -> toExtract.addAll(orderBy.getSortItems())); window.getFrame().ifPresent(toExtract::add);
Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(identifier("x"), DESCENDING, UNDEFINED)))), false, ImmutableList.of(identifier("x")))); 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")))),
for (SortItem sortItem : node.getOrderBy().get().getSortItems()) { Type sortKeyType = process(sortItem.getSortKey(), context); if (!sortKeyType.isOrderable()) {
public static OrderBy ordering(SortItem... items) { return new OrderBy(ImmutableList.copyOf(items)); }
static String formatOrderBy(OrderBy orderBy, Function<QualifiedName, String> tableNameMapper, Optional<Function<String, String>> columnNameMapper, List<String> queryWithTables, char escapeIdentifier) { return "ORDER BY " + formatSortItems(orderBy.getSortItems(), tableNameMapper, columnNameMapper, queryWithTables, escapeIdentifier); }
@Override public Node visitOver(SqlBaseParser.OverContext context) { Optional<OrderBy> orderBy = Optional.empty(); if (context.ORDER() != null) { orderBy = Optional.of(new OrderBy(getLocation(context.ORDER()), visit(context.sortItem(), SortItem.class))); } return new Window( getLocation(context), visit(context.partition, Expression.class), orderBy, visitIfPresent(context.windowFrame(), WindowFrame.class)); }