Skip to content

perf(top): prune dominated rows in verified reverse-ordered batches - #31847

Merged
XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/fix-31803-topn-direction
Oct 30, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/fix-31803-topn-direction

Conversation

@XuPeng-SH

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #31803

What this PR does / why we need it:

Small Top-N over input ordered opposite to the requested keys repeatedly copies temporary winners and fixes the heap. On 200k composite-primary-key rows this makes DESC LIMIT and shallow OFFSET much slower than ASC. Both directions already use a bounded Top heap; the defect is input-order-sensitive replacement cost, not a missing DESC heap orientation. Deep offsets use a different Order pipeline.

Before processing a batch, Top can now prove that its keys are reverse ordered under the complete ORDER comparator. Each omitted prefix row is then dominated by at least K rows in that batch, so only the final K rows need enter the existing fixed-width, bounded-resident or spill path. K already includes OFFSET. All input batches and order expressions are still consumed; this is not early scan termination.

A cheap same-batch progress check avoids speculative full verification on common tie plateaus. It only selects fallback, and every actual prefix omission still requires full adjacent verification. Constant, array and JSON order keys retain ordinary processing: constant comparator binding expands vectors, while array/JSON comparison does not provide the transitivity required by this proof for every value. Cancellation checks remain bounded. No candidate cache, persistent state, planner, wire, format or configuration change is added. Equal-key ties retain SQL's existing unspecified ordering. Existing JSON comparator nontransitivity is not fixed by this PR. Some nonmonotone inputs can still incur an extra verification pass; no universal speedup is claimed.

Validation:

  • Exact baseline public two-CN reproduction, 200k rows, warm scans and independent row oracle: LIMIT5 ASC/DESC medians 5.08/62.23ms; OFFSET100 5.07/121.75ms; full sort 525.8/526.6ms. Physical plans confirm Top for both small/shallow directions and Order for deep OFFSET.
  • Operator benchmark on final production source: fixed-width ascending-input DESC K5/K105 57.16/108.18ms → 9.42/10.51ms; varchar 95.21/137.99ms → 10.18/12.24ms. Reversing input reverses the original penalty. Shuffled, endpoint-trap and tie-plateau controls challenge the optimization; timings are observations on a shared host, not universal latency guarantees.
  • Owning Top normal tests pass. Regression coverage includes mixed directions, NULL, scalar NaN, arrays/JSON fallback, computed/non-leading/duplicate keys, constant payloads, late winners, multi-batch results, resident compaction/spill migration, cancellation and cleanup. Existing resident tests retain their migration assertions using nonmonotone input.
  • Final candidate public two-CN SQL passes the same independent row oracles: DESC LIMIT5 62.23→9.84ms (~6.3x), shallow OFFSET100 121.75→10.83ms (~11.2x); candidate ASC is 4.23/4.91ms, so direction parity is not claimed. Six additional public controls cover default NULL ordering, mixed directions, computed keys, varchar OFFSET, LIMIT0 and empty OFFSET. Test databases are gone and both owned CNs exited gracefully.
  • Full owning Top normal and race suites pass on the final staged delta. Formatting, package-scoped go vet and pinned golangci-lint v2.14.0 pass (0 issues).
  • Repository-pinned MatrixOne molint passes on the affected package. Independent gpt-6.1-terra/max final overall review reports PASS with no material blocker; committed content matches the complete reviewed/tested delta.

Design and independent final review: actual gpt-6.1-terra/max. Validated commit: bec800ab33f02ce12fa0e19f090da3b7816bc142. CI will run after publication; pending or skipped checks are not passes.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH

XuPeng-SH commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: bec800ab33f02ce12fa0e19f090da3b7816bc142

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: bec800ab33f02ce12fa0e19f090da3b7816bc142

Assessment: approve

The pruning proof preserves Top correctness across fixed, resident and spill paths. No actionable defects remain; the documented fallback cost is acceptable against the measured gains.

Model: gpt-6-astra / xhigh

Session: 01a1869d-820c-7212-9788-aecfb86a4141 · slot 0 · generation 2

Validation:

  • Full Top suite passed: 60 tests and 50 subcases, without failures or skips.

  • Package vet and changed-file formatting checks passed.

  • Exact-base/candidate benchmarks passed independent row oracles across 40 combinations; targeted workloads improved approximately 6–16×.

  • A bounded eight-control rerun measured 6–11% overhead for constructed late-inversion inputs.

  • Source, native provenance, discussion coverage, receipts and logs reconciled in /home/xupeng/.local/state/mo-bot/jobs/8c948360bf8b7d5f5d1e3369/attempt-1/validation-summary.json.

Limitations:

  • Remote CI remains pending.

  • Performance measurements are operator-level results on a shared host; universal speedup is not claimed.

  • Author-reported public two-CN SQL, race and lint results were not independently verified.

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working kind/enhancement size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Performance]: DESC ORDER BY with LIMIT misses the top-N fast path

2 participants