Search in sources :

Example 1 with TopNNode

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);
}
Also used : SortItem(com.facebook.presto.sql.tree.SortItem) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IdentityLinkedHashMap(com.facebook.presto.util.maps.IdentityLinkedHashMap) Expression(com.facebook.presto.sql.tree.Expression) SortNode(com.facebook.presto.sql.planner.plan.SortNode) ImmutableList(com.google.common.collect.ImmutableList) SortOrder(com.facebook.presto.spi.block.SortOrder) TopNNode(com.facebook.presto.sql.planner.plan.TopNNode)

Example 2 with TopNNode

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()));
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) LimitNode(com.facebook.presto.sql.planner.plan.LimitNode) SortNode(com.facebook.presto.sql.planner.plan.SortNode) TopNNode(com.facebook.presto.sql.planner.plan.TopNNode)

Example 3 with TopNNode

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()));
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) LimitNode(com.facebook.presto.sql.planner.plan.LimitNode) TopNNode(com.facebook.presto.sql.planner.plan.TopNNode)

Aggregations

PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)3 TopNNode (com.facebook.presto.sql.planner.plan.TopNNode)3 LimitNode (com.facebook.presto.sql.planner.plan.LimitNode)2 SortNode (com.facebook.presto.sql.planner.plan.SortNode)2 SortOrder (com.facebook.presto.spi.block.SortOrder)1 Expression (com.facebook.presto.sql.tree.Expression)1 SortItem (com.facebook.presto.sql.tree.SortItem)1 IdentityLinkedHashMap (com.facebook.presto.util.maps.IdentityLinkedHashMap)1 ImmutableList (com.google.common.collect.ImmutableList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1