Skip to content

fix(plan): preserve pagination across LIMIT optimizations - #25587

Merged
XuPeng-SH merged 4 commits into
matrixorigin:mainfrom
XuPeng-SH:optimize_limit
Jul 11, 2026
Merged

XuPeng-SH merged 4 commits into
matrixorigin:mainfrom
XuPeng-SH:optimize_limit

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Jul 10, 2026 •

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 #25586

What this PR does / why we need it:

This PR makes LIMIT/OFFSET ownership explicit across planner rewrites and ensures that internal candidate pruning never replaces user-visible pagination.

  • folds constant LIMIT/OFFSET expressions early and normalizes OFFSET 0;
  • uses an overflow-safe LIMIT + OFFSET candidate budget for master, ANN, and single-stream fulltext index paths;
  • preserves original pagination through vector plugin dispatch and on the final result node;
  • safely skips internal pruning for dynamic offsets, overflow, explicit fulltext ordering, and independent fulltext intersections;
  • evaluates prepared LIMIT expressions once during table-function Prepare and makes hidden index-reader expressions visible to prepared-plan visitors;
  • hardens stats, Top-to-scan messaging, range extraction, reader setup, ANN/fulltext allocation, and HNSW signaling against wrong results, nil dereferences, overflow, lost wakeups, and OOM amplification.

CODEOWNERS update

The CODEOWNERS changes are intentional and are part of the requested maintainer ownership update; they are not generated files or incidental churn from the LIMIT work.

  • SQL planning/execution, pipeline, and related protobuf ownership moves from @ouyuanning to @XuPeng-SH, while existing co-owners remain unchanged.
  • pSpool and testutil remove the former owner while retaining their other current owners.
  • multi_update is assigned to @XuPeng.
  • proxy ownership moves from @volgariver6 to @XuPeng-SH, while @gouhongshen remains a co-owner.

Keeping these entries in this PR makes GitHub review routing and future maintenance responsibility match the intended ownership immediately after merge.

Multi-angle review

  • Performance: removes eager O(LIMIT) allocations, bounds ANN backend/result capacities, avoids LIMIT 0 over-fetch, preserves Top/ordered/index paths, and delays TopValue shuffle mutations until the optimization is accepted.
  • Accuracy: keeps final LIMIT/OFFSET separate from candidate budgets across plugin boundaries, preserves duplicate distance predicates as residual filters, supports one-sided ranges, and makes pagination stats idempotent.
  • Unhappy paths: covers uint64 overflow, NULL/dynamic expressions, invalid reader metadata, fulltext 3x overflow, HNSW condition signaling, extreme GPU limits, and partial Prepare failures.

Validation

  • affected Go packages pass, including targeted race tests;
  • go vet passes for all changed packages;
  • make build passes after rebasing onto current main;
  • mo-tester BVT: fulltext 16/16, master index 231/231, IVF mode 88/88, IVF advanced 41/41, vector edge cases 13/13;
  • 100k-row operator benchmark: Top 9.90 ms and 2.27 MB versus full Order 20.68 ms and 6.55 MB, about 2.1x faster and 65% less memory.

GPU search implementations use shared, CPU-tested LIMIT bounds. GPU runtime validation was not available because the local NVIDIA driver and cuVS libraries are unavailable.

@XuPeng-SH
XuPeng-SH requested a review from a user July 10, 2026 22:00
@XuPeng-SH
XuPeng-SH requested a review from fengttt as a code owner July 10, 2026 22:00
@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 →

@mergify mergify Bot added the kind/bug Something isn't working label Jul 10, 2026
@matrix-meow matrix-meow added the size/XL Denotes a PR that changes [1000, 1999] lines label Jul 10, 2026

@gouhongshen gouhongshen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

这组修改修复了主索引分页,但当前还不能合入:包含未说明的 CODEOWNERS 所有权转移,并且仍有极值分页与向量重写的语义缺口。请处理以下行级问题后再请求 review。

Comment thread CODEOWNERS Resolved
Comment thread pkg/sql/plan/apply_indices.go Resolved
Comment thread pkg/sql/plan/apply_indices_vector.go Resolved
Comment thread pkg/sql/plan/message.go Resolved

@gouhongshen gouhongshen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-review complete. The overflow boundary, vector pagination ownership, and direct-projection regular-index pushdown issues are fixed with targeted regression coverage. Approved.


if sortNode != nil {
sortNode.Children[0] = idxID
sortNode.Limit = DeepCopyExpr(paginationLimit)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All these deepcopy, nobody cares why

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 size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: master index applies LIMIT/OFFSET twice and returns wrong pages

4 participants