Repository navigation
fix(plan): honor mysql index hints - #25573
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
[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
left a comment
There was a problem hiding this comment.
The previous ORDER BY-hint blocker still is not fully closed on the latest head.
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.
- 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
left a comment
There was a problem hiding this comment.
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.
Merge Queue Status
|
Merge Queue Status
This pull request spent 3 minutes 8 seconds in the queue, with no time running CI. Waiting for
All conditions
ReasonPull 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.
HintYou 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. Tick the box to put this pull request back in the merge queue (same as
|
What type of PR is this?
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