presto-main/src/main/java/io/prestosql/sql/planner/iterative/rule/MergeLimits.java/MergeLimits/apply
class MergeLimits:    @Override
    public Result apply(LimitNode parent, Captures captures, Context context)
    {
        LimitNode child = captures.get(CHILD);

        // parent and child are without ties
        if (!child.isWithTies()) {
            return Result.ofPlanNode(
                    new LimitNode(
                            parent.getId(),
                            child.getSource(),
                            Math.min(parent.getCount(), child.getCount()),
                            parent.getTiesResolvingScheme(),
                            parent.isPartial()));
        }

        // parent is without ties and child is with ties
        if (parent.getCount() > child.getCount()) {
            return Result.ofPlanNode(
                    child.replaceChildren(ImmutableList.of(
                            parent.replaceChildren(ImmutableList.of(
                                    child.getSource())))));
        }

        return Result.ofPlanNode(
                parent.replaceChildren(ImmutableList.of(
                        child.getSource())));
    }