use of com.facebook.presto.sql.planner.plan.TopNNode in project presto by prestodb.
the class QueryPlanner method sort.
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;
if (limit.isPresent() && !limit.get().equalsIgnoreCase("all")) {
planNode = new TopNNode(idAllocator.getNextId(), subPlan.getRoot(), Long.parseLong(limit.get()), orderBySymbols.build(), orderings, false);
} else {
planNode = new SortNode(idAllocator.getNextId(), subPlan.getRoot(), orderBySymbols.build(), orderings);
}
return subPlan.withNewRoot(planNode);
}
use of com.facebook.presto.sql.planner.plan.TopNNode in project presto by prestodb.
the class MergeLimitWithSort method apply.
@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
if (!(node instanceof LimitNode)) {
return Optional.empty();
}
LimitNode parent = (LimitNode) node;
PlanNode source = lookup.resolve(parent.getSource());
if (!(source instanceof SortNode)) {
return Optional.empty();
}
SortNode child = (SortNode) source;
return Optional.of(new TopNNode(parent.getId(), child.getSource(), parent.getCount(), child.getOrderBy(), child.getOrderings(), parent.isPartial()));
}
use of com.facebook.presto.sql.planner.plan.TopNNode in project presto by prestodb.
the class MergeLimitWithTopN method apply.
@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
if (!(node instanceof LimitNode)) {
return Optional.empty();
}
LimitNode parent = (LimitNode) node;
PlanNode source = lookup.resolve(parent.getSource());
if (!(source instanceof TopNNode)) {
return Optional.empty();
}
TopNNode child = (TopNNode) source;
return Optional.of(new TopNNode(parent.getId(), child.getSource(), Math.min(parent.getCount(), child.getCount()), child.getOrderBy(), child.getOrderings(), parent.isPartial()));
}
Aggregations