/** * Transforms a plan like P->C->X to C->P->X */ public static PlanNode transpose(PlanNode parent, PlanNode child) { return child.replaceChildren(ImmutableList.of( parent.replaceChildren( child.getSources()))); }
@Override protected PlanNode visitPlan(PlanNode node, Void context) { List<PlanNode> children = node.getSources().stream() .map(child -> child.accept(this, context)) .collect(Collectors.toList()); return node.replaceChildren(children); }
private PlanWithProperties rebaseAndDeriveProperties(PlanNode node, List<PlanWithProperties> children) { PlanNode result = node.replaceChildren( children.stream() .map(PlanWithProperties::getNode) .collect(toList())); return new PlanWithProperties(result, deriveProperties(result, children.stream().map(PlanWithProperties::getProperties).collect(toList()))); }
/** * Return an identical copy of the given node with its children replaced */ public static PlanNode replaceChildren(PlanNode node, List<PlanNode> children) { for (int i = 0; i < node.getSources().size(); i++) { if (children.get(i) != node.getSources().get(i)) { return node.replaceChildren(children); } } return node; } }
private PlanNode insertChildrenAndRewrite(PlanNode node) { return node.replaceChildren( node.getSources().stream() .map(child -> new GroupReference( idAllocator.getNextId(), insertRecursive(child), child.getOutputSymbols())) .collect(Collectors.toList())); }
/** * Recurse through a series of preceding ExchangeNodes and ProjectNodes to find the preceding PARTIAL aggregation */ private Optional<PlanNode> recurseToPartial(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator) { if (node instanceof AggregationNode && ((AggregationNode) node).getStep() == AggregationNode.Step.PARTIAL) { return Optional.of(addGatheringIntermediate((AggregationNode) node, idAllocator)); } if (!(node instanceof ExchangeNode) && !(node instanceof ProjectNode)) { return Optional.empty(); } ImmutableList.Builder<PlanNode> builder = ImmutableList.builder(); for (PlanNode source : node.getSources()) { Optional<PlanNode> planNode = recurseToPartial(lookup.resolve(source), lookup, idAllocator); if (!planNode.isPresent()) { return Optional.empty(); } builder.add(planNode.get()); } return Optional.of(node.replaceChildren(builder.build())); }
return Optional.empty(); return Optional.of(node.replaceChildren(newChildrenBuilder.build()));
@Override public Result apply(PlanNode node, Captures captures, Context context) { return Result.ofPlanNode(node.replaceChildren(node.getSources())); } }