Repository navigation
fix(plan): make paired range index selection deterministic - #24418
Conversation
Choose among multiple paired-range index candidates by filter order instead of Go map iteration so EXPLAIN and plan selection stay stable. Co-authored-by: Copilot <[email protected]>
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? |
There was a problem hiding this comment.
Pull request overview
Follow-up to #24417 that removes nondeterminism in getIndexForNonEquiCond's paired-range index selection. Previously, when multiple indexed columns each had a paired lower/upper bound, the chosen index depended on Go map iteration order. This PR replaces the map iteration with iteration over node.FilterList, so selection follows filter order deterministically.
Changes:
- Replace
colLowerBoundsmap iteration with an ordered scan overnode.FilterList, returning the first column that has both a lower and upper bound. - Add a unit test
TestGetIndexForNonEquiCond_PrefersFirstPairedRangeByFilterOrdercovering the multi-paired-range case.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| pkg/sql/plan/apply_indices.go | Iterate node.FilterList instead of colLowerBounds map to make paired range index pick deterministic. |
| pkg/sql/plan/apply_indices_test.go | New test asserting that the first paired range in filter order wins (idx_quantity over idx_price). |
Integrate paired-range detection into the normal filter-order scan so earlier IN/BETWEEN/single-range candidates keep their previous priority while paired ranges remain deterministic. Co-authored-by: Copilot <[email protected]>
aunjgr
left a comment
There was a problem hiding this comment.
Review
The fix correctly eliminates Go map-iteration nondeterminism from getIndexForNonEquiCond.
What the old code did wrong
When multiple index columns each had a complete paired range (e.g. quantity BETWEEN … AND price BETWEEN …), the old dedicated map-iteration loop over colLowerBounds could pick either column depending on Go's random map traversal order — producing inconsistent plan/EXPLAIN output across runs.
Why the new code is correct
Folding the paired-range check into the second FilterList loop (which already iterates in a fixed order) is the right approach:
int32(i) == min(lowerIdx, upperIdx)fires exactly once per pair, at the first bound encountered inFilterListorder. ✅- Upper-bound-first ordering (e.g.
col <= X AND col >= Y) is handled correctly bymin(). ✅ in/between/in_range/orfilters are unaffected becauseclassifyRangeBoundreturnsnilfor them and the new block is skipped. ✅- Non-paired single range filters fall through to the existing
fn != nilpath unchanged. ✅
Minor note
classifyRangeBound(fn) is now called twice for each >=/<=/>/< filter (once in first pass, once in second pass). Negligible for query planning, but worth noting.
Test coverage
Tests cover the primary regression case (multi-paired-range determinism) and the early-non-paired-preempts-later-paired case. One untested but provably correct case: upper-bound-first paired range (col <= X AND col >= Y) — min(lowerIdx, upperIdx) handles it correctly.
LGTM. No correctness issues found.
Merge Queue Status
This pull request spent 24 seconds in the queue, including 3 seconds running CI. Required conditions to merge
|
…gin#24418) matrixorigin#24417 fixed the root cause of PR matrixorigin#24289 by limiting paired range rewrite on table-scan filters to composite sort-key parts only. This follow-up keeps that behavior unchanged, but removes a small remaining source of nondeterminism in `getIndexForNonEquiCond`: when multiple indexed columns each form a paired range, the previous implementation could pick a candidate based on Go map iteration order. That did not imply incorrect results, but it could make plan selection and EXPLAIN output drift. This PR makes the choice deterministic by honoring `node.FilterList` order, which matches the old pre-paired-range behavior more closely. Added a unit test covering the multi-paired-range case. Approved by: @aunjgr
…gin#24418) matrixorigin#24417 fixed the root cause of PR matrixorigin#24289 by limiting paired range rewrite on table-scan filters to composite sort-key parts only. This follow-up keeps that behavior unchanged, but removes a small remaining source of nondeterminism in `getIndexForNonEquiCond`: when multiple indexed columns each form a paired range, the previous implementation could pick a candidate based on Go map iteration order. That did not imply incorrect results, but it could make plan selection and EXPLAIN output drift. This PR makes the choice deterministic by honoring `node.FilterList` order, which matches the old pre-paired-range behavior more closely. Added a unit test covering the multi-paired-range case. Approved by: @aunjgr
…gin#24418) matrixorigin#24417 fixed the root cause of PR matrixorigin#24289 by limiting paired range rewrite on table-scan filters to composite sort-key parts only. This follow-up keeps that behavior unchanged, but removes a small remaining source of nondeterminism in `getIndexForNonEquiCond`: when multiple indexed columns each form a paired range, the previous implementation could pick a candidate based on Go map iteration order. That did not imply incorrect results, but it could make plan selection and EXPLAIN output drift. This PR makes the choice deterministic by honoring `node.FilterList` order, which matches the old pre-paired-range behavior more closely. Added a unit test covering the multi-paired-range case. Approved by: @aunjgr
What type of PR is this?
Which issue(s) this PR fixes:
Ref #24348
What this PR does / why we need it:
#24417 fixed the root cause of PR #24289 by limiting paired range rewrite on table-scan filters to composite sort-key parts only.
This follow-up keeps that behavior unchanged, but removes a small remaining source of nondeterminism in
getIndexForNonEquiCond: when multiple indexed columns each form a paired range, the previous implementation could pick a candidate based on Go map iteration order.That did not imply incorrect results, but it could make plan selection and EXPLAIN output drift. This PR makes the choice deterministic by honoring
node.FilterListorder, which matches the old pre-paired-range behavior more closely.Added a unit test covering the multi-paired-range case.
Validation