presto-main/src/main/java/io/prestosql/sql/planner/iterative/rule/PushTopNThroughUnion.java/PushTopNThroughUnion/apply
class PushTopNThroughUnion:    @Override
    public Result apply(TopNNode topNNode, Captures captures, Context context)
    {
        UnionNode unionNode = captures.get(CHILD);

        ImmutableList.Builder<PlanNode> sources = ImmutableList.builder();

        for (PlanNode source : unionNode.getSources()) {
            SymbolMapper.Builder symbolMapper = SymbolMapper.builder();
            Set<Symbol> sourceOutputSymbols = ImmutableSet.copyOf(source.getOutputSymbols());

            for (Symbol unionOutput : unionNode.getOutputSymbols()) {
                Set<Symbol> inputSymbols = ImmutableSet.copyOf(unionNode.getSymbolMapping().get(unionOutput));
                Symbol unionInput = getLast(intersection(inputSymbols, sourceOutputSymbols));
                symbolMapper.put(unionOutput, unionInput);
            }
            sources.add(symbolMapper.build().map(topNNode, source, context.getIdAllocator().getNextId()));
        }

        return Result.ofPlanNode(new UnionNode(
                unionNode.getId(),
                sources.build(),
                unionNode.getSymbolMapping(),
                unionNode.getOutputSymbols()));
    }