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

        if (parent.isWithTies()) {
            if (parent.getCount() < child.getCount()) {
                return Result.empty();
            }
            else {
                return Result.ofPlanNode(child);
            }
        }

        return Result.ofPlanNode(
                new TopNNode(
                        parent.getId(),
                        child.getSource(),
                        Math.min(parent.getCount(), child.getCount()),
                        child.getOrderingScheme(),
                        parent.isPartial() ? TopNNode.Step.PARTIAL : TopNNode.Step.SINGLE));
    }