Repository navigation
fix(plan): limit range merge to composite sort keys - #24417
Conversation
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]>
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
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 intoin_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]>
Merge Queue Status
This pull request spent 28 seconds in the queue, including 3 seconds running CI. Required conditions to merge
|
#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
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
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
…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
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
…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
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
…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:
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 < yon non-sort-key columns could be rewritten intoin_rangeduring 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:
pk_a = ? AND pk_b > ? AND pk_b < ?;Added planner tests for both sides: non-sort-key ranges stay as
>/<, while composite sort-key ranges still merge intoin_range.Validation