Skip to content

fix(plan): backport PR24289 range merge fixes to 4.0-dev - #24419

Merged
mergify[bot] merged 5 commits into
matrixorigin:4.0-devfrom
LeftHandCold:fix/pr24289-followup-4dev
May 19, 2026
Merged

mergify[bot] merged 5 commits into
matrixorigin:4.0-devfrom
LeftHandCold:fix/pr24289-followup-4dev

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:

This PR brings the PR #24289 fix set to 4.0-dev.

It includes:

  1. the main fix already merged as fix(plan): limit range merge to composite sort keys #24417 on main, which limits paired range rewrite in expr_opt.go to composite sort-key parts only, while preserving the intended secondary-index paired-range lowering in apply_indices.go;
  2. a deterministic follow-up for getIndexForNonEquiCond, so when multiple indexed columns each form a paired range, index selection follows node.FilterList order instead of Go map iteration order.

Together these changes keep the root-cause fix for the TPCH slowdown / TPCH->TPCC OOM path, preserve the intended prefix_between / prefix_in_range behavior on regular index scans, and avoid plan / EXPLAIN drift from nondeterministic paired-range candidate selection.

Validation

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

LeftHandCold and others added 2 commits May 17, 2026 18:22
PR matrixorigin#24289 added prefix range PK filters for composite keys, but its paired range pre-merge step was applied before checking whether the range column belongs to the composite sort key. As a result, ordinary predicates like `col > x AND col < y` on non-sort-key columns could be rewritten into `in_range` during normal table-scan filter normalization.

That rewrite expands the new readutil/ObjectMeta fast-filter path beyond the intended composite-key optimization. In large TPCH scans, low-selectivity ordinary range filters can then spend extra time loading object metadata, building block lists, and shipping larger pipeline block-list messages. This matches the TPCH slowdown and higher post-TPCH RSS observed after matrixorigin#24289, which later leaves too little memory headroom for TPCC under the 55Gi limit.

This PR limits the paired range merge to the intended scope:

- skip this paired-range rewrite for single-part sort keys;
- only merge paired ranges when the range column is one of the composite sort-key parts;
- preserve matrixorigin#24289's intended optimization for cases such as `pk_a = ? AND pk_b > ? AND pk_b < ?`;
- keep ordinary non-sort-key ranges as their original comparison filters.

Added planner tests for both sides: non-sort-key ranges stay as `>`/`<`, while composite sort-key ranges still merge into `in_range`.

Approved by: @ouyuanning, @aunjgr
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

This PR backports planner fixes for paired range handling, limiting composite-key range merging and adding deterministic secondary-index paired-range selection.

Changes:

  • Restricts paired range merging in composite-key filter optimization to composite sort-key parts.
  • Adds secondary-index paired range detection/lowering via a new range condition type.
  • Adds unit tests for composite-key range merging and deterministic paired-range index choice.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
pkg/sql/plan/expr_opt.go Narrows paired range merging scope for composite sort keys.
pkg/sql/plan/expr_opt_test.go Adds tests for sort-key vs non-sort-key range merging.
pkg/sql/plan/apply_indices.go Adds paired range index selection and replacement logic.
pkg/sql/plan/apply_indices_test.go Adds tests for paired range index detection and replacement.

Comment thread pkg/sql/plan/apply_indices.go Resolved
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]>

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

Requesting changes due to two issues found in review:

  1. Planner selection risk: In pkg/sql/plan/apply_indices.go, the paired-range early return bypasses the single-range Outcnt guard (InFilterCardLimitNonPK). On large tables this can pick an expensive index join. Please evaluate node.Stats.Outcnt against InFilterCardLimitNonPK before returning paired-range or apply equivalent gating.

  2. Missing BVT coverage: Add BVT cases for:

    • composite PK / CLUSTER BY tables with range predicates on non-PK columns (e.g., PK(a,b); WHERE a=1 AND c>5 AND c<10) to prevent predicate loss;
    • secondary-index range + residual non-indexed filter (e.g., WHERE val BETWEEN x AND y AND flag=1) to ensure residual filters are applied.

Please address these and re-request review.

@LeftHandCold

Copy link
Copy Markdown
Collaborator Author

Pushed 3247f82dd6 to apply the same non-PK large-range stats guard to paired secondary-index range candidates, so they no longer bypass the existing InFilterCardLimitNonPK / selectivity cutoff used for single-range scans.

I kept this follow-up narrow on purpose: it adds a focused unit test for the paired-range guard, and does not expand into new distributed BVT cases. The existing expr_opt_test.go coverage already locks the non-sort-key composite-filter preservation case, and this patch does not change residual-filter application semantics.

@mergify

mergify Bot commented May 19, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-05-19 04:07 UTC · Rule: release-4.0
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-05-19 05:01 UTC · at bf5f6c95ece8ac1b230f34a2d9b04c9560326c00 · squash

This pull request spent 53 minutes 50 seconds in the queue, including 4 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 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
  • 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

@mergify
mergify Bot merged commit 0c638fb into matrixorigin:4.0-dev May 19, 2026
42 of 44 checks passed
@mergify mergify Bot removed the queued label May 19, 2026

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.

6 participants