Skip to content

fix(plan): make paired range index selection deterministic - #24418

Merged
mergify[bot] merged 2 commits into
matrixorigin:mainfrom
LeftHandCold:fix/pr24289-followup-main
May 17, 2026
Merged

mergify[bot] merged 2 commits into
matrixorigin:mainfrom
LeftHandCold:fix/pr24289-followup-main

Conversation

@LeftHandCold

@LeftHandCold LeftHandCold commented May 17, 2026 •

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:

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.FilterList order, which matches the old pre-paired-range behavior more closely.

Added a unit test covering the multi-paired-range case.

Validation

source /Users/shenjiangwei/Work/code/matrixone/setup_env.sh && go test ./pkg/sql/plan ./pkg/vm/engine/readutil -count=1

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]>
Copilot AI review requested due to automatic review settings May 17, 2026 10:25
@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 →

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 colLowerBounds map iteration with an ordered scan over node.FilterList, returning the first column that has both a lower and upper bound.
  • Add a unit test TestGetIndexForNonEquiCond_PrefersFirstPairedRangeByFilterOrder covering 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 aunjgr 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.

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 in FilterList order. ✅
  • Upper-bound-first ordering (e.g. col <= X AND col >= Y) is handled correctly by min(). ✅
  • in / between / in_range / or filters are unaffected because classifyRangeBound returns nil for them and the new block is skipped. ✅
  • Non-paired single range filters fall through to the existing fn != nil path 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.

@mergify mergify Bot added the queued label May 17, 2026
@mergify

mergify Bot commented May 17, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-05-17 18:44 UTC · Rule: main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-05-17 18:44 UTC · at c1e8ea481d90795e66d70ceaf7f4354d2c2aa0a3 · squash

This pull request spent 24 seconds in the queue, including 3 seconds running CI.

Required conditions to merge
  • #approved-reviews-by >= 1 [🛡 GitHub branch protection]
  • #changes-requested-reviews-by = 0 [🛡 GitHub branch protection]
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-decision = APPROVED [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • 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)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • 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)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • 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)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Ubuntu/x86
    • check-neutral = Matrixone CI / SCA Test on Ubuntu/x86
    • check-skipped = Matrixone CI / SCA Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • 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)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • 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)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • 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)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage

@mergify
mergify Bot merged commit 9aa6717 into matrixorigin:main May 17, 2026
52 of 55 checks passed
@mergify mergify Bot removed the queued label May 17, 2026
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…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
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…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
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…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

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/enhancement 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