Skip to content

fix(plan): honor mysql index hints - #25573

Merged
XuPeng-SH merged 16 commits into
matrixorigin:mainfrom
ck89119:issue-25356-main
Jul 13, 2026
Merged

XuPeng-SH merged 16 commits into
matrixorigin:mainfrom
ck89119:issue-25356-main

Conversation

@ck89119

@ck89119 ck89119 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:

issue #25356

What this PR does / why we need it:

This PR adds planner support for MySQL index hints on regular secondary indexes. It records table-scan index hints during binding, validates hinted index names, filters regular-index candidates for scan-scope USE/FORCE/IGNORE hints, and returns the MySQL-compatible ER_KEY_DOES_NOT_EXIST / SQLSTATE 42000 error when a hinted index is missing.

It also adds regression coverage for composite secondary indexes, missing hinted indexes, and composite-primary-key tables with composite secondary indexes.

Validation:

  • go test -v -count=1 -run 'Test.*IndexHint|TestFilterRegularIndexesByScanHints|TestRecordIndexHintsValidatesNames' ./pkg/sql/plan\n- go test -v -count=1 ./pkg/sql/plan\n- go test -v -count=1 ./pkg/common/moerr\n- go build ./pkg/common/moerr ./pkg/sql/plan\n- go vet ./pkg/common/moerr ./pkg/sql/plan\n- mo-tester/run.sh -p test/distributed/cases/optimizer/index_hint.sql -g -n\n- git diff --check

@ck89119
ck89119 requested a review from a user July 10, 2026 08:54
@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 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.

[P1] IGNORE/USE/FORCE INDEX FOR ORDER BY is bypassed after a covering-index rewrite.

indexHintsByScan is keyed by node ID. tryIndexOnlyScan creates a replacement index-table scan but does not call inheritIndexHints, so the ORDER BY hint is lost. More importantly, even after adding that inheritance, the later handleMessageFromTopToScan pass independently turns the regular index into an ordered Top-N scan without calling regularIndexScanAllowedByOrderHints. The earlier gate in applyRegularIndexTopSort therefore does not close the optimizer pipeline.

Reproducer (fails on this head 10/10):

select id, a
from index_hint_t ignore index for order by (idx_a)
where a = 1
order by id
limit 10;

The resulting idx_a scan gets OrderBy, a MsgTopValue, and IndexReaderParam, so the optimizer uses the explicitly ignored index to provide ORDER BY / LIMIT ordering. This violates the PR's advertised scoped-hint contract.

Please (1) propagate the hint state when tryIndexOnlyScan replaces a scan node, and (2) gate the post-index handleMessageFromTopToScan ordered-read/Top-N transformation with the same ORDER BY eligibility predicate. Add a full planner regression (not only a direct candidate-filter test) that asserts the ignored index can still serve the WHERE access but has no ordered-read state: no IndexReaderParam, no scan OrderBy, and no Top-N receive message. I validated this combined fix locally: the new regression and the index-hint suite passed repeatedly, and go test ./pkg/sql/plan -count=1 passed.

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

The previous ORDER BY-hint blocker still is not fully closed on the latest head.

  1. tryIndexOnlyScan() still creates a replacement index scan without inheriting the original scan's hint state.

In the latest head, pkg/sql/plan/apply_indices.go still returns the new idxTableNodeID from tryIndexOnlyScan() without an inheritIndexHints(...) call. So for covering-index rewrites, the replacement scan can still lose IGNORE/USE/FORCE INDEX FOR ORDER BY state before the later optimizer passes run.

  1. The later handleMessageFromTopToScan() pass still bypasses the ORDER BY hint gate.

The PR added regularIndexScanAllowedByOrderHints() and now checks it in buildRegularIndexTopSortContext(), but pkg/sql/plan/message.go still independently pushes scanNode.OrderBy / IndexReaderParam for the regular-index ordered-read path without consulting the same predicate. So even if you fix the hint inheritance problem above, an ignored regular index can still be turned into the ordered Top-N read by this later pass.

This is the same closure problem as before: the hint contract is not enforced end-to-end across the full optimizer pipeline. Please fix both sides together:

  • inherit hint state when a scan node is replaced by tryIndexOnlyScan();
  • gate handleMessageFromTopToScan() with the same ORDER BY eligibility check before it adds ordered-read state.

The new tests also still miss this exact unhappy path. The added BVT covers positive FORCE INDEX FOR ORDER BY cases, but there is still no negative regression for IGNORE INDEX FOR ORDER BY across covering-index rewrite + later ordered-read rewrite.

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

LGTM after reviewing this from the hint-binding, access-path selection, and unhappy-path angles.

  • correctness: the planner now records scan-scope hint ownership, validates hinted names up front, and filters regular-index candidates consistently for USE/FORCE/IGNORE, which closes the empty-result and missing-index gaps from the issue
  • path control: the new owner-tracking/inheritance keeps JOIN / covering-scan rewrites attached to the original hinted table scan instead of losing hint intent after the first rewrite
  • unhappy path coverage: the new tests cover missing hinted indexes, empty USE, invalid hint combinations, composite secondary indexes, join/order/group scopes, and metadata-failure branches

I did not find a remaining correctness or hang issue in the latest diff, so approving.

@mergify

mergify Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-13 10:58 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Preparing checks
  • ⏳ Merge

@mergify

mergify Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-13 10:58 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-07-13 11:01 UTC · at 5dfd2816204c264eb040912b61899b19dba0e5ab

This pull request spent 3 minutes 8 seconds in the queue, with no time running CI.

Waiting for
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-skipped = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-success = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
All conditions
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-skipped = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-success = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • github-review-decision = APPROVED [🛡 GitHub branch protection]

Reason

Pull request #25573 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

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/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants