@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()); }
@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 PhysicalOperation visitTopN(TopNNode node, LocalExecutionPlanContext context) { PhysicalOperation source = node.getSource().accept(this, context); List<Symbol> orderBySymbols = node.getOrderBy(); List<Integer> sortChannels = new ArrayList<>(); List<SortOrder> sortOrders = new ArrayList<>(); for (Symbol symbol : orderBySymbols) { sortChannels.add(source.getLayout().get(symbol)); sortOrders.add(node.getOrderings().get(symbol)); } OperatorFactory operator = new TopNOperatorFactory( context.getNextOperatorId(), node.getId(), source.getTypes(), (int) node.getCount(), sortChannels, sortOrders, node.isPartial()); return new PhysicalOperation(operator, source.getLayout(), source); }
@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()); }