PlanNode subplan = node.getSources().get(i);
PlanNode rewrittenSource = planRewriter.rewrite(subplan, upstreamDistinct);
if (rewrittenSource instanceof UnionNode) {
// Absorb source's subplans if it is also a UnionNode
UnionNode rewrittenUnion = (UnionNode) rewrittenSource;
flattenedSources.addAll(rewrittenUnion.getSources());
for (Map.Entry<Symbol, Collection<Symbol>> entry : node.getSymbolMapping().asMap().entrySet()) {
Symbol inputSymbol = Iterables.get(entry.getValue(), i);
flattenedSymbolMap.putAll(entry.getKey(), rewrittenUnion.getSymbolMapping().get(inputSymbol));
}
}
else {
flattenedSources.add(rewrittenSource);
for (Map.Entry<Symbol, Collection<Symbol>> entry : node.getSymbolMapping().asMap().entrySet()) {
flattenedSymbolMap.put(entry.getKey(), Iterables.get(entry.getValue(), i));
}
}
}
return new UnionNode(node.getId(), flattenedSources.build(), flattenedSymbolMap.build());
}