presto-main/src/main/java/io/prestosql/sql/planner/iterative/rule/PushLimitThroughUnion.java/PushLimitThroughUnion/apply
class PushLimitThroughUnion:    @Override
    public Result apply(LimitNode parent, Captures captures, Context context)
    {
        UnionNode unionNode = captures.get(CHILD);
        ImmutableList.Builder<PlanNode> builder = ImmutableList.builder();
        boolean shouldApply = false;
        for (PlanNode source : unionNode.getSources()) {
            // This check is to ensure that we don't fire the optimizer if it was previously applied.
            if (isAtMost(source, context.getLookup(), parent.getCount())) {
                builder.add(source);
            }
            else {
                shouldApply = true;
                builder.add(new LimitNode(context.getIdAllocator().getNextId(), source, parent.getCount(), true));
            }
        }

        if (!shouldApply) {
            return Result.empty();
        }

        return Result.ofPlanNode(
                parent.replaceChildren(ImmutableList.of(
                        unionNode.replaceChildren(builder.build()))));
    }