Skip to content

perf(plan): push down regular index cursor limit - #25577

Merged
XuPeng-SH merged 5 commits into
matrixorigin:mainfrom
LeftHandCold:agent/optimize-regular-index-cursor-limit
Jul 10, 2026
Merged

XuPeng-SH merged 5 commits into
matrixorigin:mainfrom
LeftHandCold:agent/optimize-regular-index-cursor-limit

Conversation

@LeftHandCold

@LeftHandCold LeftHandCold commented Jul 10, 2026 •

Copy link
Copy Markdown
Collaborator

What type of PR is this?

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

Which issue(s) this PR fixes:

issue #23965

What this PR does / why we need it:

This completes the safe index-reader LIMIT pushdown for regular secondary-index cursor pagination when:

  • every secondary-index prefix part is fixed by equality;
  • the only residual predicate is <, <=, >, or >= on the base-table primary key;
  • the query orders by that primary key and uses a positive literal LIMIT without OFFSET or rank semantics.

The planner combines the fixed prefix and cursor into a prefix_in_range predicate on the hidden serialized index key. The range is evaluated before the ordered reader LIMIT, so the reader can continue from the cursor boundary and return the requested rows without scanning and sorting the full prefix group.

The original primary-key cursor predicate remains in the scan filter list as a correctness recheck.

Root Cause

PR #25545 correctly disabled static reader LIMIT pushdown whenever an index scan had residual filters. Before that guard, the reader could take the first N entries in hidden-key order and only then apply id < cursor, which could return too few rows or zero rows.

That guard restored correctness but also disabled early termination for cursor predicates that can be represented exactly as a range on the same hidden ordered key. As a result, the cursor query still read the complete prefix group before the final Top-N sort.

Safety and compatibility

  • No storage, index format, reader API, or SQL semantics are changed.
  • The optimization is enabled only by the cursor-range rewrite performed in the two existing regular-index ORDER BY LIMIT planner paths.
  • Static reader LIMIT is applied only when the SORT path reaches the table scan directly. A FILTER or cardinality-reducing INNER/SEMI JOIN on that path disables static LIMIT pushdown.
  • TopValue messaging and ordered scanning remain enabled across those FILTER/JOIN paths, preserving the existing dynamic optimization without risking an incomplete result.
  • Additional residual filters, incomplete index prefixes, non-primary-key predicates, computed cursor expressions, float primary keys, type mismatches, OFFSET, rank semantics, and non-literal LIMIT values retain the existing conservative behavior.
  • Prepared cursor parameters and casts over stable literal/parameter values are supported.
  • In-memory and unsorted-block behavior continues to use the existing fallback paths.

Testing

  • go test -count=1 ./pkg/sql/plan
  • go test -count=1 ./pkg/vm/engine/readutil
  • go test -count=1 ./pkg/vm/engine/tae/blockio
  • Added planner regressions proving static reader LIMIT is not pushed through FILTER, INNER JOIN, or SEMI JOIN.
  • Extended regular_index_order_limit_cursor BVT coverage for <, <=, >, and >= cursor boundaries.

@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 →

@LeftHandCold

Copy link
Copy Markdown
Collaborator Author

The review is valid and has been addressed in ae7314f2e0.

  • gatherLeavesForMessageFromTopToScan now returns both the scan ID and whether a static reader LIMIT is safe.
  • Traversal through FILTER, INNER JOIN, or SEMI JOIN marks static LIMIT pushdown unsafe, preventing the reader from stopping before those operators reject rows.
  • Existing TopValue messaging and ordered scanning are preserved on those paths.
  • Added regressions for FILTER, INNER JOIN, and SEMI JOIN, while retaining the direct scan positive case.

Validation: go test -count=1 ./pkg/sql/plan passed after merging the latest main.

@XuPeng-SH XuPeng-SH 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-reviewed the latest head after the requested changes. The previous correctness blocker is resolved: static reader LIMIT pushdown is now explicitly disabled across FILTER and cardinality-reducing INNER/SEMI JOIN paths, while TopValue messaging and ordered scanning remain available. The added regressions cover those unsafe paths and the direct-scan positive path.

I also rechecked range-bound construction, reversed comparisons, open/closed flags, cursor type/stability guards, OFFSET/rank/non-literal LIMIT fallbacks, and the persisted/in-memory/unsorted reader behavior. No blocking findings remain.

Local validation on the latest main merge passed for pkg/sql/plan, pkg/vm/engine/readutil, and pkg/vm/engine/tae/blockio, including targeted race repetitions and go vet. CI is green.

Non-blocking follow-up: an execution-level assertion that the cursor case keeps index-scan inputRows near LIMIT would protect the performance objective in addition to the current result-correctness BVT.

@XuPeng-SH
XuPeng-SH merged commit 899e3a2 into matrixorigin:main Jul 10, 2026
17 of 20 checks passed
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [x] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#23965

## What this PR does / why we need it:

This completes the safe index-reader LIMIT pushdown for regular
secondary-index cursor pagination when:

- every secondary-index prefix part is fixed by equality;
- the only residual predicate is `<`, `<=`, `>`, or `>=` on the
base-table primary key;
- the query orders by that primary key and uses a positive literal LIMIT
without OFFSET or rank semantics.

The planner combines the fixed prefix and cursor into a
`prefix_in_range` predicate on the hidden serialized index key. The
range is evaluated before the ordered reader LIMIT, so the reader can
continue from the cursor boundary and return the requested rows without
scanning and sorting the full prefix group.

The original primary-key cursor predicate remains in the scan filter
list as a correctness recheck.

## Root Cause

PR matrixorigin#25545 correctly disabled static reader LIMIT pushdown whenever an
index scan had residual filters. Before that guard, the reader could
take the first N entries in hidden-key order and only then apply `id <
cursor`, which could return too few rows or zero rows.

That guard restored correctness but also disabled early termination for
cursor predicates that can be represented exactly as a range on the same
hidden ordered key. As a result, the cursor query still read the
complete prefix group before the final Top-N sort.

## Safety and compatibility

- No storage, index format, reader API, or SQL semantics are changed.
- The optimization is enabled only by the cursor-range rewrite performed
in the two existing regular-index ORDER BY LIMIT planner paths.
- Static reader LIMIT is applied only when the SORT path reaches the
table scan directly. A FILTER or cardinality-reducing INNER/SEMI JOIN on
that path disables static LIMIT pushdown.
- TopValue messaging and ordered scanning remain enabled across those
FILTER/JOIN paths, preserving the existing dynamic optimization without
risking an incomplete result.
- Additional residual filters, incomplete index prefixes,
non-primary-key predicates, computed cursor expressions, float primary
keys, type mismatches, OFFSET, rank semantics, and non-literal LIMIT
values retain the existing conservative behavior.
- Prepared cursor parameters and casts over stable literal/parameter
values are supported.
- In-memory and unsorted-block behavior continues to use the existing
fallback paths.

## Testing

- `go test -count=1 ./pkg/sql/plan`
- `go test -count=1 ./pkg/vm/engine/readutil`
- `go test -count=1 ./pkg/vm/engine/tae/blockio`
- Added planner regressions proving static reader LIMIT is not pushed
through FILTER, INNER JOIN, or SEMI JOIN.
- Extended `regular_index_order_limit_cursor` BVT coverage for `<`,
`<=`, `>`, and `>=` cursor boundaries.

---------

Co-authored-by: XuPeng-SH <[email protected]>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [x] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#23965

## What this PR does / why we need it:

This completes the safe index-reader LIMIT pushdown for regular
secondary-index cursor pagination when:

- every secondary-index prefix part is fixed by equality;
- the only residual predicate is `<`, `<=`, `>`, or `>=` on the
base-table primary key;
- the query orders by that primary key and uses a positive literal LIMIT
without OFFSET or rank semantics.

The planner combines the fixed prefix and cursor into a
`prefix_in_range` predicate on the hidden serialized index key. The
range is evaluated before the ordered reader LIMIT, so the reader can
continue from the cursor boundary and return the requested rows without
scanning and sorting the full prefix group.

The original primary-key cursor predicate remains in the scan filter
list as a correctness recheck.

## Root Cause

PR matrixorigin#25545 correctly disabled static reader LIMIT pushdown whenever an
index scan had residual filters. Before that guard, the reader could
take the first N entries in hidden-key order and only then apply `id <
cursor`, which could return too few rows or zero rows.

That guard restored correctness but also disabled early termination for
cursor predicates that can be represented exactly as a range on the same
hidden ordered key. As a result, the cursor query still read the
complete prefix group before the final Top-N sort.

## Safety and compatibility

- No storage, index format, reader API, or SQL semantics are changed.
- The optimization is enabled only by the cursor-range rewrite performed
in the two existing regular-index ORDER BY LIMIT planner paths.
- Static reader LIMIT is applied only when the SORT path reaches the
table scan directly. A FILTER or cardinality-reducing INNER/SEMI JOIN on
that path disables static LIMIT pushdown.
- TopValue messaging and ordered scanning remain enabled across those
FILTER/JOIN paths, preserving the existing dynamic optimization without
risking an incomplete result.
- Additional residual filters, incomplete index prefixes,
non-primary-key predicates, computed cursor expressions, float primary
keys, type mismatches, OFFSET, rank semantics, and non-literal LIMIT
values retain the existing conservative behavior.
- Prepared cursor parameters and casts over stable literal/parameter
values are supported.
- In-memory and unsorted-block behavior continues to use the existing
fallback paths.

## Testing

- `go test -count=1 ./pkg/sql/plan`
- `go test -count=1 ./pkg/vm/engine/readutil`
- `go test -count=1 ./pkg/vm/engine/tae/blockio`
- Added planner regressions proving static reader LIMIT is not pushed
through FILTER, INNER JOIN, or SEMI JOIN.
- Extended `regular_index_order_limit_cursor` BVT coverage for `<`,
`<=`, `>`, and `>=` cursor boundaries.

---------

Co-authored-by: XuPeng-SH <[email protected]>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [x] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#23965

## What this PR does / why we need it:

This completes the safe index-reader LIMIT pushdown for regular
secondary-index cursor pagination when:

- every secondary-index prefix part is fixed by equality;
- the only residual predicate is `<`, `<=`, `>`, or `>=` on the
base-table primary key;
- the query orders by that primary key and uses a positive literal LIMIT
without OFFSET or rank semantics.

The planner combines the fixed prefix and cursor into a
`prefix_in_range` predicate on the hidden serialized index key. The
range is evaluated before the ordered reader LIMIT, so the reader can
continue from the cursor boundary and return the requested rows without
scanning and sorting the full prefix group.

The original primary-key cursor predicate remains in the scan filter
list as a correctness recheck.

## Root Cause

PR matrixorigin#25545 correctly disabled static reader LIMIT pushdown whenever an
index scan had residual filters. Before that guard, the reader could
take the first N entries in hidden-key order and only then apply `id <
cursor`, which could return too few rows or zero rows.

That guard restored correctness but also disabled early termination for
cursor predicates that can be represented exactly as a range on the same
hidden ordered key. As a result, the cursor query still read the
complete prefix group before the final Top-N sort.

## Safety and compatibility

- No storage, index format, reader API, or SQL semantics are changed.
- The optimization is enabled only by the cursor-range rewrite performed
in the two existing regular-index ORDER BY LIMIT planner paths.
- Static reader LIMIT is applied only when the SORT path reaches the
table scan directly. A FILTER or cardinality-reducing INNER/SEMI JOIN on
that path disables static LIMIT pushdown.
- TopValue messaging and ordered scanning remain enabled across those
FILTER/JOIN paths, preserving the existing dynamic optimization without
risking an incomplete result.
- Additional residual filters, incomplete index prefixes,
non-primary-key predicates, computed cursor expressions, float primary
keys, type mismatches, OFFSET, rank semantics, and non-literal LIMIT
values retain the existing conservative behavior.
- Prepared cursor parameters and casts over stable literal/parameter
values are supported.
- In-memory and unsorted-block behavior continues to use the existing
fallback paths.

## Testing

- `go test -count=1 ./pkg/sql/plan`
- `go test -count=1 ./pkg/vm/engine/readutil`
- `go test -count=1 ./pkg/vm/engine/tae/blockio`
- Added planner regressions proving static reader LIMIT is not pushed
through FILTER, INNER JOIN, or SEMI JOIN.
- Extended `regular_index_order_limit_cursor` BVT coverage for `<`,
`<=`, `>`, and `>=` cursor boundaries.

---------

Co-authored-by: XuPeng-SH <[email protected]>

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/enhancement kind/test-ci size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants