Skip to content

fix(plan): preserve forced index access for ORDER BY - #26851

Merged
XuPeng-SH merged 3 commits into
mainfrom
agent/issue-26814-force-index-order
Aug 9, 2026
Merged

XuPeng-SH merged 3 commits into
mainfrom
agent/issue-26814-force-index-order

Conversation

@XuPeng-SH

Copy link
Copy Markdown
Contributor

Summary

  • let forced ORDER BY access skip a contiguous leading index prefix fixed by single-value equality
  • preserve plain FORCE INDEX as an access-path constraint when the named index cannot provide ordering, leaving Sort to enforce order
  • keep FORCE INDEX FOR ORDER BY order-scoped and leave ordinary optimization unchanged

Root cause

The force-index ORDER BY prepass required ORDER columns to match the physical index from part zero. For an index such as (status, amount, id), it therefore rejected a fixed status prefix followed by ORDER BY amount, id even though that prefix is order-neutral. When the order match failed, base-scan protection also caused an unscoped plain FORCE INDEX to be silently discarded.

The matcher now skips only a contiguous prefix fixed by column = runtime_constant. IN, range, and unconstrained or gapped parts remain order-incompatible. If an unscoped plain force cannot satisfy order, the planner still builds the named covering scan or index backfill join without marking it ordered, so the existing Sort remains authoritative.

Validation

Fresh validation from isolated worktree /home/xupeng/github/matrixone-issue-26814, based on origin/main 0676200da47052659cf46ad9e22d67ab9654e393:

The regression matrix covers ASC/DESC, LIMIT/no LIMIT, equality/IN/range/gapped leading parts, covering/backfill access, invalid hints, unhinted optimization, and exact/distinct row checks.

No row-path benchmark was added: the new work is planner-only and bounded by index parts times scan filters plus ORDER columns.

Fixes #26814

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Fresh evidence for commit 18091e76cd:

The isolated service used private localhost ports and task-local data only; the shared service was not switched or used. CI was not monitored.

@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Aug 9, 2026
@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Independent triple-candidate acceptance on 55 passed the original semantics with a 300k-row noncovering table. Plain FORCE INDEX(idx_tenant_k_pk) remains an Index Table Scan + INDEX backfill + Sort for both equality-prefix-compatible ORDER BY k,id LIMIT 100 and incompatible ORDER BY payload,id LIMIT 100; it no longer silently falls back to the base scan. FORCE INDEX FOR ORDER BY also retains the named index path. This was checked while both competing (k,tenant,PK) and (tenant,k,PK) indexes existed.

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

Blocking review finding. This is submitted as COMMENT because the authenticated account is the PR author and GitHub forbids a self REQUEST_CHANGES review.

The equality-prefix optimization is sound only when each skipped leading key part has one value for the whole scan. isRuntimeConstExpr also accepts volatile row-dependent functions. A public planner witness with a DOUBLE leading key and WHERE a = floor(rand() * 2) ORDER BY b, id selects the order-scoped forced (a,b,id) index and publishes OrderBy, even though rand() produces a different value per row. The explicit Sort currently remains as a correctness backstop, but the claimed order capability and order-scoped access selection are false.

Please require a genuinely row-invariant, non-volatile equality operand in both commuted forms and add this negative regression.

No other blockers were found. Full pkg/sql/plan, latest-main focused tests, build, vet, diff checks, and required CI passed on exact head 45cb6e5.

Comment thread pkg/sql/plan/apply_indices.go Resolved

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

Blocking review finding. This review is COMMENTED only because the authenticated account is the PR author and GitHub forbids a self REQUEST_CHANGES review.

The equality-prefix optimization is sound only when each skipped leading key part has one value for the whole scan. isRuntimeConstExpr also accepts volatile row-dependent functions. A public planner witness with a DOUBLE leading key and WHERE a = floor(rand() * 2) ORDER BY b, id selects the order-scoped forced (a,b,id) index and publishes OrderBy, even though rand() produces a different value per row. The explicit Sort currently remains as a correctness backstop, but the claimed order capability and order-scoped access selection are false.

Please require a genuinely row-invariant, non-volatile equality operand in both commuted forms and add this negative regression.

No other blockers were found. Full pkg/sql/plan, latest-main focused tests, build, vet, diff checks, and required CI passed on exact head 45cb6e5.

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

Final complete deep review of 06acd00 against latest main 5430351: no blocking findings.

I reviewed the full four-file diff, not only the follow-up commit. The review covered plain and ORDER-scoped FORCE precedence, equality-fixed composite-index prefixes, covering and backfill paths, compatible and incompatible ORDER BY shapes, ascending/descending order, literal/parameter/volatile operands in both commuted forms, malformed/unknown expression fail-closed behavior, and the distributed SQL/result fixtures. I also cross-checked the newly merged #26848 index-range residual path; it does not conflict with this rewrite.

The prior volatile-expression finding is fixed: skipped index parts now require one statement-stable value for the scan, and nested rand() no longer claims ordered-index capability. No resource ownership, cleanup, wait-for, retry, concurrency, restart-generation, or unbounded-growth edges are introduced.

Validation on the exact rebased head: the new regression failed before the fix and passes after it; focused planner tests pass; go test ./pkg/sql/plan/... passes; go vet ./pkg/sql/plan/... passes; diff checks are clean; and range-diff confirms the original PR commit did not drift during rebase. The branch was 0 behind latest main at review time. CI was not polled.

GitHub does not allow the PR author to approve their own PR, so this no-finding result is submitted as COMMENT; the requested non-author reviewer remains the approval gate.

@XuPeng-SH
XuPeng-SH marked this pull request as ready for review August 9, 2026 15:58
@XuPeng-SH
XuPeng-SH requested a review from aunjgr as a code owner August 9, 2026 15:58
@XuPeng-SH
XuPeng-SH requested a review from a user August 9, 2026 15:58
@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 630856f into main Aug 9, 2026
31 checks passed
@XuPeng-SH
XuPeng-SH deleted the agent/issue-26814-force-index-order branch August 9, 2026 16:01
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
)

## Summary

- let forced ORDER BY access skip a contiguous leading index prefix
fixed by single-value equality
- preserve plain `FORCE INDEX` as an access-path constraint when the
named index cannot provide ordering, leaving `Sort` to enforce order
- keep `FORCE INDEX FOR ORDER BY` order-scoped and leave ordinary
optimization unchanged

## Root cause

The force-index ORDER BY prepass required ORDER columns to match the
physical index from part zero. For an index such as `(status, amount,
id)`, it therefore rejected a fixed status prefix followed by `ORDER BY
amount, id` even though that prefix is order-neutral. When the order
match failed, base-scan protection also caused an unscoped plain `FORCE
INDEX` to be silently discarded.

The matcher now skips only a contiguous prefix fixed by `column =
runtime_constant`. IN, range, and unconstrained or gapped parts remain
order-incompatible. If an unscoped plain force cannot satisfy order, the
planner still builds the named covering scan or index backfill join
without marking it ordered, so the existing Sort remains authoritative.

## Validation

Fresh validation from isolated worktree
`/home/xupeng/github/matrixone-issue-26814`, based on `origin/main`
`0676200da47052659cf46ad9e22d67ab9654e393`:

- regression reproduced before the production change: equality-prefix
and plain-force fallback cases failed to retain the index
- focused force/order planner tests: pass
- full `pkg/sql/plan` suite: pass
- package `go build` and `go vet` with repository-local CGo paths: pass
- whole `make build`: pass
- isolated candidate-binary BVT `optimizer/index_hint.sql`: 53/53
statements pass
- pairwise merge plus focused/full planner suites with draft matrixorigin#26848:
pass
- pairwise merge plus focused/full planner suites with draft matrixorigin#26850 (for
matrixorigin#26803): pass

The regression matrix covers ASC/DESC, LIMIT/no LIMIT,
equality/IN/range/gapped leading parts, covering/backfill access,
invalid hints, unhinted optimization, and exact/distinct row checks.

No row-path benchmark was added: the new work is planner-only and
bounded by index parts times scan filters plus ORDER columns.

Fixes matrixorigin#26814
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
)

## Summary

- let forced ORDER BY access skip a contiguous leading index prefix
fixed by single-value equality
- preserve plain `FORCE INDEX` as an access-path constraint when the
named index cannot provide ordering, leaving `Sort` to enforce order
- keep `FORCE INDEX FOR ORDER BY` order-scoped and leave ordinary
optimization unchanged

## Root cause

The force-index ORDER BY prepass required ORDER columns to match the
physical index from part zero. For an index such as `(status, amount,
id)`, it therefore rejected a fixed status prefix followed by `ORDER BY
amount, id` even though that prefix is order-neutral. When the order
match failed, base-scan protection also caused an unscoped plain `FORCE
INDEX` to be silently discarded.

The matcher now skips only a contiguous prefix fixed by `column =
runtime_constant`. IN, range, and unconstrained or gapped parts remain
order-incompatible. If an unscoped plain force cannot satisfy order, the
planner still builds the named covering scan or index backfill join
without marking it ordered, so the existing Sort remains authoritative.

## Validation

Fresh validation from isolated worktree
`/home/xupeng/github/matrixone-issue-26814`, based on `origin/main`
`0676200da47052659cf46ad9e22d67ab9654e393`:

- regression reproduced before the production change: equality-prefix
and plain-force fallback cases failed to retain the index
- focused force/order planner tests: pass
- full `pkg/sql/plan` suite: pass
- package `go build` and `go vet` with repository-local CGo paths: pass
- whole `make build`: pass
- isolated candidate-binary BVT `optimizer/index_hint.sql`: 53/53
statements pass
- pairwise merge plus focused/full planner suites with draft matrixorigin#26848:
pass
- pairwise merge plus focused/full planner suites with draft matrixorigin#26850 (for
matrixorigin#26803): pass

The regression matrix covers ASC/DESC, LIMIT/no LIMIT,
equality/IN/range/gapped leading parts, covering/backfill access,
invalid hints, unhinted optimization, and exact/distinct row checks.

No row-path benchmark was added: the new work is planner-only and
bounded by index parts times scan filters plus ORDER columns.

Fixes matrixorigin#26814
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
)

## Summary

- let forced ORDER BY access skip a contiguous leading index prefix
fixed by single-value equality
- preserve plain `FORCE INDEX` as an access-path constraint when the
named index cannot provide ordering, leaving `Sort` to enforce order
- keep `FORCE INDEX FOR ORDER BY` order-scoped and leave ordinary
optimization unchanged

## Root cause

The force-index ORDER BY prepass required ORDER columns to match the
physical index from part zero. For an index such as `(status, amount,
id)`, it therefore rejected a fixed status prefix followed by `ORDER BY
amount, id` even though that prefix is order-neutral. When the order
match failed, base-scan protection also caused an unscoped plain `FORCE
INDEX` to be silently discarded.

The matcher now skips only a contiguous prefix fixed by `column =
runtime_constant`. IN, range, and unconstrained or gapped parts remain
order-incompatible. If an unscoped plain force cannot satisfy order, the
planner still builds the named covering scan or index backfill join
without marking it ordered, so the existing Sort remains authoritative.

## Validation

Fresh validation from isolated worktree
`/home/xupeng/github/matrixone-issue-26814`, based on `origin/main`
`0676200da47052659cf46ad9e22d67ab9654e393`:

- regression reproduced before the production change: equality-prefix
and plain-force fallback cases failed to retain the index
- focused force/order planner tests: pass
- full `pkg/sql/plan` suite: pass
- package `go build` and `go vet` with repository-local CGo paths: pass
- whole `make build`: pass
- isolated candidate-binary BVT `optimizer/index_hint.sql`: 53/53
statements pass
- pairwise merge plus focused/full planner suites with draft matrixorigin#26848:
pass
- pairwise merge plus focused/full planner suites with draft matrixorigin#26850 (for
matrixorigin#26803): pass

The regression matrix covers ASC/DESC, LIMIT/no LIMIT,
equality/IN/range/gapped leading parts, covering/backfill access,
invalid hints, unhinted optimization, and exact/distinct row checks.

No row-path benchmark was added: the new work is planner-only and
bounded by index parts times scan filters plus ORDER columns.

Fixes matrixorigin#26814

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

size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: ORDER BY silently discards a valid plain FORCE INDEX access path

2 participants