@Override public PlanNode replaceChildren(List<PlanNode> newChildren) { return new TopNNode(getId(), Iterables.getOnlyElement(newChildren), count, orderingScheme, step); } }
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 TopNNode map(TopNNode node, PlanNode source, PlanNodeId newNodeId) { ImmutableList.Builder<Symbol> symbols = ImmutableList.builder(); ImmutableMap.Builder<Symbol, SortOrder> orderings = ImmutableMap.builder(); Set<Symbol> seenCanonicals = new HashSet<>(node.getOrderingScheme().getOrderBy().size()); for (Symbol symbol : node.getOrderingScheme().getOrderBy()) { Symbol canonical = map(symbol); if (seenCanonicals.add(canonical)) { seenCanonicals.add(canonical); symbols.add(canonical); orderings.put(canonical, node.getOrderingScheme().getOrdering(symbol)); } } return new TopNNode( newNodeId, source, node.getCount(), new OrderingScheme(symbols.build(), orderings.build()), node.getStep()); }
@Override public PlanNode visitTopN(TopNNode node, RewriteContext<Set<Symbol>> context) { ImmutableSet.Builder<Symbol> expectedInputs = ImmutableSet.<Symbol>builder() .addAll(context.get()) .addAll(node.getOrderingScheme().getOrderBy()); PlanNode source = context.rewrite(node.getSource(), expectedInputs.build()); return new TopNNode(node.getId(), source, node.getCount(), node.getOrderingScheme(), node.getStep()); }
@Override public Result apply(LimitNode parent, Captures captures, Context context) { TopNNode child = captures.get(CHILD); return Result.ofPlanNode( new TopNNode( parent.getId(), child.getSource(), Math.min(parent.getCount(), child.getCount()), child.getOrderingScheme(), parent.isPartial() ? TopNNode.Step.PARTIAL : TopNNode.Step.SINGLE)); } }
@Override public Result apply(TopNNode single, Captures captures, Context context) { TopNNode partial = new TopNNode( context.getIdAllocator().getNextId(), single.getSource(), single.getCount(), single.getOrderingScheme(), PARTIAL); return Result.ofPlanNode(new TopNNode( context.getIdAllocator().getNextId(), partial, single.getCount(), single.getOrderingScheme(), FINAL)); } }
@Override public PlanNode visitTopN(TopNNode node, RewriteContext<LimitContext> context) { LimitContext limit = context.get(); PlanNode rewrittenSource = context.rewrite(node.getSource()); if (rewrittenSource == node.getSource() && limit == null) { return node; } long count = node.getCount(); if (limit != null) { count = Math.min(count, limit.getCount()); } return new TopNNode(node.getId(), rewrittenSource, count, node.getOrderingScheme(), node.getStep()); }
@Override public Result apply(LimitNode parent, Captures captures, Context context) { SortNode child = captures.get(CHILD); return Result.ofPlanNode( new TopNNode( parent.getId(), child.getSource(), parent.getCount(), child.getOrderingScheme(), parent.isPartial() ? TopNNode.Step.PARTIAL : TopNNode.Step.SINGLE)); } }
@Override @Deprecated public PlanNode visitSort(SortNode node, RewriteContext<LimitContext> context) { LimitContext limit = context.get(); PlanNode rewrittenSource = context.rewrite(node.getSource()); if (limit != null) { return new TopNNode(node.getId(), rewrittenSource, limit.getCount(), node.getOrderingScheme(), TopNNode.Step.SINGLE); } else if (rewrittenSource != node.getSource()) { return new SortNode(node.getId(), rewrittenSource, node.getOrderingScheme()); } return node; }
public TopNNode topN(long count, List<Symbol> orderBy, PlanNode source) { return new TopNNode( idAllocator.getNextId(), source, count, new OrderingScheme( orderBy, Maps.toMap(orderBy, Functions.constant(SortOrder.ASC_NULLS_FIRST))), TopNNode.Step.SINGLE); }
@Test public void testTopN() { PlanNode node = new TopNNode(newId(), filter(baseTableScan, and( equals(AE, BE), equals(BE, CE), lessThan(CE, bigintLiteral(10)))), 1, new OrderingScheme(ImmutableList.of(A), ImmutableMap.of(A, SortOrder.ASC_NULLS_LAST)), TopNNode.Step.PARTIAL); Expression effectivePredicate = effectivePredicateExtractor.extract(node); // Pass through assertEquals(normalizeConjuncts(effectivePredicate), normalizeConjuncts( equals(AE, BE), equals(BE, CE), lessThan(CE, bigintLiteral(10)))); }
@Override public PlanNode visitTopN(TopNNode node, RewriteContext<Void> context) { PlanNode source = context.rewrite(node.getSource()); ImmutableList.Builder<Symbol> symbols = ImmutableList.builder(); ImmutableMap.Builder<Symbol, SortOrder> orderings = ImmutableMap.builder(); for (Symbol symbol : node.getOrderBy()) { Symbol canonical = canonicalize(symbol); symbols.add(canonical); orderings.put(canonical, node.getOrderings().get(symbol)); } return new TopNNode(node.getId(), source, node.getCount(), symbols.build(), orderings.build(), node.isPartial()); }
private PlanBuilder sort(PlanBuilder subPlan, List<SortItem> orderBy, Optional<String> limit, List<FieldOrExpression> orderByExpressions) { if (orderBy.isEmpty()) { return subPlan; } Iterator<SortItem> sortItems = orderBy.iterator(); ImmutableList.Builder<Symbol> orderBySymbols = ImmutableList.builder(); Map<Symbol, SortOrder> orderings = new HashMap<>(); for (FieldOrExpression 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 new PlanBuilder(subPlan.getTranslations(), planNode, subPlan.getSampleWeight()); }
@Override public PlanNode visitTopN(TopNNode node, RewriteContext<Set<Symbol>> context) { ImmutableSet.Builder<Symbol> expectedInputs = ImmutableSet.<Symbol>builder() .addAll(context.get()) .addAll(node.getOrderBy()); PlanNode source = context.rewrite(node.getSource(), expectedInputs.build()); return new TopNNode(node.getId(), source, node.getCount(), node.getOrderBy(), node.getOrderings(), node.isPartial()); }
@Override public PlanNode visitTopN(TopNNode node, List<PlanNode> newChildren) { return new TopNNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getCount(), node.getOrderBy(), node.getOrderings(), node.isPartial()); }
@Override public PlanNode visitTopN(TopNNode node, RewriteContext<LimitContext> context) { LimitContext limit = context.get(); PlanNode rewrittenSource = context.rewrite(node.getSource()); if (rewrittenSource == node.getSource() && limit == null) { return node; } long count = node.getCount(); if (limit != null) { count = Math.min(count, limit.getCount()); } return new TopNNode(node.getId(), rewrittenSource, count, node.getOrderBy(), node.getOrderings(), node.isPartial()); }
@Override public PlanNode visitSort(SortNode node, RewriteContext<LimitContext> context) { LimitContext limit = context.get(); PlanNode rewrittenSource = context.rewrite(node.getSource()); if (limit != null) { return new TopNNode(node.getId(), rewrittenSource, limit.getCount(), node.getOrderBy(), node.getOrderings(), false); } else if (rewrittenSource != node.getSource()) { return new SortNode(node.getId(), rewrittenSource, node.getOrderBy(), node.getOrderings()); } return node; }
@Test public void testTopN() throws Exception { PlanNode node = new TopNNode(newId(), filter(baseTableScan, and( equals(AE, BE), equals(BE, CE), lessThan(CE, number(10)))), 1, ImmutableList.of(A), ImmutableMap.of(A, SortOrder.ASC_NULLS_LAST), true); Expression effectivePredicate = EffectivePredicateExtractor.extract(node, TYPES); // Pass through assertEquals(normalizeConjuncts(effectivePredicate), normalizeConjuncts( equals(AE, BE), equals(BE, CE), lessThan(CE, number(10)))); }
@Override public PlanWithProperties visitTopN(TopNNode node, Context context) { PlanWithProperties child = planChild(node, context.withPreferredProperties(PreferredProperties.any())); if (!child.getProperties().isSingleNode()) { child = withDerivedProperties( new TopNNode(idAllocator.getNextId(), child.getNode(), node.getCount(), node.getOrderBy(), node.getOrderings(), true), child.getProperties()); child = withDerivedProperties( gatheringExchange(idAllocator.getNextId(), child.getNode()), child.getProperties()); } return rebaseAndDeriveProperties(node, child); }