presto-main/src/main/java/io/prestosql/sql/planner/optimizations/LimitPushDown.java/Rewriter/visitLimit
class Rewriter:        @Override
        public PlanNode visitLimit(LimitNode node, RewriteContext<LimitContext> context)
        {
            long count = node.getCount();
            LimitContext limit = context.get();
            if (limit != null) {
                count = Math.min(count, limit.getCount());
            }

            // return empty ValuesNode in case of limit 0
            if (count == 0) {
                return new ValuesNode(idAllocator.getNextId(),
                        node.getOutputSymbols(),
                        ImmutableList.of());
            }

            if (!node.isWithTies() || (limit != null && node.getCount() >= limit.getCount())) {
                // default visitPlan logic will insert the limit node
                return context.rewrite(node.getSource(), new LimitContext(count, false));
            }

            return context.defaultRewrite(node, context.get());
        }