Skip to content

fix(plan): limit range merge to composite sort keys - #24417

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

mergify[bot] merged 2 commits into
matrixorigin:mainfrom
LeftHandCold:fix/pr24289-range-merge-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:

PR #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 #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 feat(plan): support prefix range PK filters on composite keys #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.

Validation

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

Only merge paired range predicates into in_range when the range column is part of the composite sort key. This keeps ordinary non-sort-key TPCH range filters on their original comparison operators while preserving the intended composite-key fast-filter optimization.

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings May 17, 2026 04:16
@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 →

@mergify mergify Bot added kind/bug Something isn't working kind/enhancement kind/test-ci labels May 17, 2026

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 narrows the paired-range pre-merge step in doMergeFiltersOnCompositeKey so that ordinary col > x AND col < y predicates on non-sort-key columns are no longer rewritten into in_range. The unconditional merge introduced in #24289 was widening the readutil/ObjectMeta fast-filter path for non-sort-key ranges, causing TPCH slowdowns and elevated RSS. The fix restores the optimization to its intended composite-key scope.

Changes:

  • Skip the paired-range merge entirely when the sort key has a single part.
  • Build a set of sort-key part column indexes and only merge paired ranges when the range column is one of them.
  • Add planner tests covering both the non-sort-key (kept as >/<) and composite sort-key (merged into in_range) cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pkg/sql/plan/expr_opt.go Moves the numParts == 1 early return ahead of the merge, builds a sortKeyPartCols set, and skips range pairs whose column is not a sort-key part.
pkg/sql/plan/expr_opt_test.go New unit tests verifying that non-sort-key range pairs are preserved while composite sort-key range pairs still merge into in_range.

Keep the matrixorigin#24417 planner scope fix for table-scan filter normalization, but restore paired range merging inside apply_indices for regular secondary-index access so BETWEEN-style filters still lower to prefix_between/prefix_in_range on index scans.

Co-authored-by: Copilot <[email protected]>
@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 10:11 UTC · Rule: main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-05-17 10:11 UTC · at e853c4764f4eb2ba112e631b57cba2acc480a3cc · squash

This pull request spent 28 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 a75c782 into matrixorigin:main May 17, 2026
23 of 24 checks passed
@mergify mergify Bot removed the queued label May 17, 2026
mergify Bot pushed a commit that referenced this pull request May 17, 2026
#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.

Approved by: @aunjgr
mergify Bot pushed a commit that referenced this pull request May 19, 2026
This PR brings the PR #24289 fix set to `4.0-dev`.

It includes:

1. the main fix already merged as #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.

Approved by: @aunjgr, @ouyuanning, @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
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
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
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
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
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
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.

5 participants