Skip to content

fix: guard regular index ordered limit pushdown - #25545

Merged
XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
LeftHandCold:agent/fix-regular-index-cursor-limit-main
Jul 9, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
LeftHandCold:agent/fix-regular-index-cursor-limit-main

Conversation

@LeftHandCold

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:

The regular secondary index ORDER BY primary-key LIMIT optimization can push a static limit down to the scan/index reader when the secondary-index prefix is fixed.
With an extra primary-key cursor predicate, for example id < ..., the scan candidates no longer exactly match the SQL filter. Applying the static limit before the residual filter can return too few rows, or even zero rows.

This PR keeps the hidden-key ordering rewrite and TopValue message path, but only enables static limit pushdown when the index scan filter list is exactly the full-prefix equality filter.
Residual filters continue to be applied before the final LIMIT semantics are observed.

Added a BVT regression case:
test/distributed/cases/optimizer/regular_index_order_limit_cursor.sql

Tested with:

env GOCACHE=/private/tmp/mo-go-build-cache CGO_CFLAGS='-I/Users/shenjiangwei/Work/code/view/matrixone/cgo -I/Users/shenjiangwei/Work/code/view/matrixone/thirdparties/install/include' CGO_LDFLAGS='-Wl,-w,-rpath,/Users/shenjiangwei/Work/code/view/matrixone/cgo -Wl,-rpath,/Users/shenjiangwei/Work/code/view/matrixone/thirdparties/install/lib -L/Users/shenjiangwei/Work/code/view/matrixone/thirdparties/install/lib -L/Users/shenjiangwei/Work/code/view/matrixone/cgo -lmo' go test ./pkg/sql/plan -count=1

@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 merged commit c051b7f into matrixorigin:main Jul 9, 2026
23 of 24 checks passed
@mergify mergify Bot added dequeued and removed queued labels Jul 9, 2026
XuPeng-SH added a commit that referenced this pull request Jul 10, 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 #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.

---------

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
- [x] BUG
- [ ] 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:

The regular secondary index ORDER BY primary-key LIMIT optimization can
push a static limit down to the scan/index reader when the
secondary-index prefix is fixed.
With an extra primary-key cursor predicate, for example `id < ...`, the
scan candidates no longer exactly match the SQL filter. Applying the
static limit before the residual filter can return too few rows, or even
zero rows.

This PR keeps the hidden-key ordering rewrite and TopValue message path,
but only enables static limit pushdown when the index scan filter list
is exactly the full-prefix equality filter.
Residual filters continue to be applied before the final LIMIT semantics
are observed.

Added a BVT regression case:
`test/distributed/cases/optimizer/regular_index_order_limit_cursor.sql`

Tested with:

`env GOCACHE=/private/tmp/mo-go-build-cache
CGO_CFLAGS='-I/Users/shenjiangwei/Work/code/view/matrixone/cgo
-I/Users/shenjiangwei/Work/code/view/matrixone/thirdparties/install/include'
CGO_LDFLAGS='-Wl,-w,-rpath,/Users/shenjiangwei/Work/code/view/matrixone/cgo
-Wl,-rpath,/Users/shenjiangwei/Work/code/view/matrixone/thirdparties/install/lib
-L/Users/shenjiangwei/Work/code/view/matrixone/thirdparties/install/lib
-L/Users/shenjiangwei/Work/code/view/matrixone/cgo -lmo' go test
./pkg/sql/plan -count=1`

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
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
- [x] BUG
- [ ] 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:

The regular secondary index ORDER BY primary-key LIMIT optimization can
push a static limit down to the scan/index reader when the
secondary-index prefix is fixed.
With an extra primary-key cursor predicate, for example `id < ...`, the
scan candidates no longer exactly match the SQL filter. Applying the
static limit before the residual filter can return too few rows, or even
zero rows.

This PR keeps the hidden-key ordering rewrite and TopValue message path,
but only enables static limit pushdown when the index scan filter list
is exactly the full-prefix equality filter.
Residual filters continue to be applied before the final LIMIT semantics
are observed.

Added a BVT regression case:
`test/distributed/cases/optimizer/regular_index_order_limit_cursor.sql`

Tested with:

`env GOCACHE=/private/tmp/mo-go-build-cache
CGO_CFLAGS='-I/Users/shenjiangwei/Work/code/view/matrixone/cgo
-I/Users/shenjiangwei/Work/code/view/matrixone/thirdparties/install/include'
CGO_LDFLAGS='-Wl,-w,-rpath,/Users/shenjiangwei/Work/code/view/matrixone/cgo
-Wl,-rpath,/Users/shenjiangwei/Work/code/view/matrixone/thirdparties/install/lib
-L/Users/shenjiangwei/Work/code/view/matrixone/thirdparties/install/lib
-L/Users/shenjiangwei/Work/code/view/matrixone/cgo -lmo' go test
./pkg/sql/plan -count=1`

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
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
- [x] BUG
- [ ] 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:

The regular secondary index ORDER BY primary-key LIMIT optimization can
push a static limit down to the scan/index reader when the
secondary-index prefix is fixed.
With an extra primary-key cursor predicate, for example `id < ...`, the
scan candidates no longer exactly match the SQL filter. Applying the
static limit before the residual filter can return too few rows, or even
zero rows.

This PR keeps the hidden-key ordering rewrite and TopValue message path,
but only enables static limit pushdown when the index scan filter list
is exactly the full-prefix equality filter.
Residual filters continue to be applied before the final LIMIT semantics
are observed.

Added a BVT regression case:
`test/distributed/cases/optimizer/regular_index_order_limit_cursor.sql`

Tested with:

`env GOCACHE=/private/tmp/mo-go-build-cache
CGO_CFLAGS='-I/Users/shenjiangwei/Work/code/view/matrixone/cgo
-I/Users/shenjiangwei/Work/code/view/matrixone/thirdparties/install/include'
CGO_LDFLAGS='-Wl,-w,-rpath,/Users/shenjiangwei/Work/code/view/matrixone/cgo
-Wl,-rpath,/Users/shenjiangwei/Work/code/view/matrixone/thirdparties/install/lib
-L/Users/shenjiangwei/Work/code/view/matrixone/thirdparties/install/lib
-L/Users/shenjiangwei/Work/code/view/matrixone/cgo -lmo' go test
./pkg/sql/plan -count=1`

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
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/bug Something isn't working 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.

4 participants