Search in sources :

Example 1 with SortNode

use of com.facebook.presto.sql.planner.plan.SortNode in project presto by prestodb.

the class SortMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    SortNode sortNode = (SortNode) node;
    if (!orderingSchemeMatches(orderBy, sortNode.getOrderingScheme(), symbolAliases)) {
        return NO_MATCH;
    }
    return MatchResult.match();
}
Also used : SortNode(com.facebook.presto.sql.planner.plan.SortNode)

Example 2 with SortNode

use of com.facebook.presto.sql.planner.plan.SortNode 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 3 with SortNode

use of com.facebook.presto.sql.planner.plan.SortNode 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 4 with SortNode

use of com.facebook.presto.sql.planner.plan.SortNode in project presto by prestodb.

the class TestEffectivePredicateExtractor method testSort.

@Test
public void testSort() {
    PlanNode node = new SortNode(Optional.empty(), newId(), filter(baseTableScan, and(equals(AV, BV), equals(BV, CV), lessThan(CV, bigintLiteral(10)))), new OrderingScheme(ImmutableList.of(new Ordering(AV, SortOrder.ASC_NULLS_LAST))), false);
    RowExpression effectivePredicate = effectivePredicateExtractor.extract(node);
    // Pass through
    assertEquals(normalizeConjuncts(effectivePredicate), normalizeConjuncts(equals(AV, BV), equals(BV, CV), lessThan(CV, bigintLiteral(10))));
}
Also used : OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) PlanNode(com.facebook.presto.spi.plan.PlanNode) SortNode(com.facebook.presto.sql.planner.plan.SortNode) Ordering(com.facebook.presto.spi.plan.Ordering) RowExpression(com.facebook.presto.spi.relation.RowExpression) Test(org.testng.annotations.Test)

Example 5 with SortNode

use of com.facebook.presto.sql.planner.plan.SortNode in project presto by prestodb.

the class QueryPlanner method sort.

private PlanBuilder sort(PlanBuilder subPlan, Optional<OrderBy> orderBy, List<Expression> orderByExpressions) {
    if (!orderBy.isPresent() || (isSkipRedundantSort(session)) && analysis.isOrderByRedundant(orderBy.get())) {
        return subPlan;
    }
    PlanNode planNode;
    OrderingScheme orderingScheme = toOrderingScheme(orderByExpressions.stream().map(subPlan::translate).collect(toImmutableList()), orderBy.get().getSortItems().stream().map(PlannerUtils::toSortOrder).collect(toImmutableList()));
    planNode = new SortNode(getSourceLocation(orderBy.get()), idAllocator.getNextId(), subPlan.getRoot(), orderingScheme, false);
    return subPlan.withNewRoot(planNode);
}
Also used : OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) PlannerUtils.toOrderingScheme(com.facebook.presto.sql.planner.PlannerUtils.toOrderingScheme) PlanNode(com.facebook.presto.spi.plan.PlanNode) SortNode(com.facebook.presto.sql.planner.plan.SortNode)

Aggregations

SortNode (com.facebook.presto.sql.planner.plan.SortNode)5 OrderingScheme (com.facebook.presto.spi.plan.OrderingScheme)2 PlanNode (com.facebook.presto.spi.plan.PlanNode)2 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)2 TopNNode (com.facebook.presto.sql.planner.plan.TopNNode)2 SortOrder (com.facebook.presto.spi.block.SortOrder)1 Ordering (com.facebook.presto.spi.plan.Ordering)1 RowExpression (com.facebook.presto.spi.relation.RowExpression)1 PlannerUtils.toOrderingScheme (com.facebook.presto.sql.planner.PlannerUtils.toOrderingScheme)1 LimitNode (com.facebook.presto.sql.planner.plan.LimitNode)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 Test (org.testng.annotations.Test)1