Skip to content

fix(plan): preserve prepared regular-index range performance - #27122

Merged
XuPeng-SH merged 3 commits into
mainfrom
fix/regular-index-covering-dominance
Aug 13, 2026
Merged

XuPeng-SH merged 3 commits into
mainfrom
fix/regular-index-covering-dominance

Conversation

@XuPeng-SH

Copy link
Copy Markdown
Contributor

What changed

  • Separate physical index-key serialization from SQL comparison-bound serialization.
    Physical non-unique composite keys continue to use serial_full, while lookup bounds use NULL-propagating serial.
  • Make prefix_in ignore NULL needles and compact only owned, non-NULL prefixes.
  • Keep only residual predicates that are still required for encoded row-side NULL ordering or byte-string prefix ambiguity.
  • Include stable index-only output/decode work in the lower bound of prepared-range cost uncertainty.
  • Propagate expression-binding failures instead of silently accepting incomplete rewrites.

Root cause

The previous fix used physical stored-key serialization (serial_full) for SQL comparison operands. Prepared NULL bounds therefore needed decoded residual predicates to recover SQL three-valued semantics. For OR-ed random ranges this duplicated parameter folding and serialized extraction work on every execution, and the cost model also undercounted stable downstream work when evaluating an uncertain prepared range.

The fix models the two contracts independently: stored keys must preserve NULL components, but comparison bounds must reject NULL. serial and serial_full are byte-identical for non-NULL components, so using serial for bounds preserves index compatibility without row-by-row NULL guards.

Compatibility and regression coverage

  • Non-unique index storage and join runtime-filter materialization still use serial_full.
  • Nullable strict upper bounds and ambiguous varchar/varbinary prefixes retain exact decoded residuals.
  • Broad literal ranges still reject the index; uncertain prepared ranges keep the index when cost intervals overlap.
  • Covering scans remain covering, while non-covering prepared ranges still select an index join.
  • Prepared equality, IN, BETWEEN, mixed OR, update, delete, and replace paths are covered.

Validation

  • mo-cgo-test -count=1 ./pkg/sql/plan/function ./pkg/sql/plan
  • Focused -race coverage for all changed planner and function cases
  • CGo-configured go vet ./pkg/sql/plan ./pkg/sql/plan/function
  • Scoped molint and golangci-lint: clean
  • a55, 2M rows, 100 threads, 4 GiB memory cache:
    • intermediate decoded-guard implementation: 22,906.80 TPS, p95 24.83 ms
    • systemic serialization fix: 28,260.18 TPS, p95 19.65 ms
    • +23.37% TPS and -20.86% p95, with zero errors
  • a55 CPU profile: EvalFoldExpr 16.25% -> 10.77%, tryFoldParameter 11.71% -> 7.73%
  • EXPLAIN ANALYZE: narrow prepared ranges use the covering index; broad covering ranges use the base table scan

The historical CI comparison used 10M rows and 1000 threads, so its absolute TPS is intentionally not compared with the a55 2M/100-thread validation.

Fixes #26971

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

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deep review: no blocking findings at ce661a3884e9cb9901b68eed9f5abfb6223c6bed.

I checked the change as four separate contracts:

  • Physical non-unique regular-index keys and join runtime-filter materialization still use serial_full; only SQL comparison operands move to NULL-propagating serial. Non-NULL encodings remain byte-identical.
  • NULL truth-table closure is preserved: NULL equality/range bounds reject through serial; prefix_in drops NULL needles; nullable stored keys retain the decoded residual only for the strict-upper-bound case that physical NULL ordering can over-admit. Byte-string prefix ambiguity still retains an exact residual.
  • prefix_in now owns cloned non-NULL bytes before compaction. Its cached state is per executor, and ResetForNextQuery recreates the function op, so prepared executions cannot reuse an earlier needle set.
  • The prepared-range uncertainty lower bound now charges stable index-only output/decode work without changing the backfill lower bound; broad known ranges can still reject the index, while all-parameter covering and non-covering ranges retain an eligible candidate.

Validation on the exact head:

  • git diff --check
  • CGo-configured go build and go vet for ./pkg/sql/plan/function ./pkg/sql/plan
  • Full uncached tests for both packages
  • Focused -race tests covering serialization compatibility, NULL/prefix semantics, residual selection, and prepared cost crossovers

The latest CI run has no failures at review time; Ubuntu UT, SCA, coverage, BVT, Iceberg, and CodeQL C/C++ are still in progress.

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

@aunjgr @aptend @LeftHandCold 麻烦帮忙 review 一下当前 PR,重点关注 prepared regular-index range 的 NULL 语义、prefix lookup residual,以及成本模型边界。当前 head: ce661a3884e9cb9901b68eed9f5abfb6223c6bed。谢谢!

@XuPeng-SH
XuPeng-SH merged commit 600db02 into main Aug 13, 2026
23 of 28 checks passed
@XuPeng-SH
XuPeng-SH deleted the fix/regular-index-covering-dominance branch August 13, 2026 14:15
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…rigin#27122)

## What changed

- Separate physical index-key serialization from SQL comparison-bound
serialization.
Physical non-unique composite keys continue to use `serial_full`, while
lookup bounds use NULL-propagating `serial`.
- Make `prefix_in` ignore NULL needles and compact only owned, non-NULL
prefixes.
- Keep only residual predicates that are still required for encoded
row-side NULL ordering or byte-string prefix ambiguity.
- Include stable index-only output/decode work in the lower bound of
prepared-range cost uncertainty.
- Propagate expression-binding failures instead of silently accepting
incomplete rewrites.

## Root cause

The previous fix used physical stored-key serialization (`serial_full`)
for SQL comparison operands. Prepared NULL bounds therefore needed
decoded residual predicates to recover SQL three-valued semantics. For
OR-ed random ranges this duplicated parameter folding and serialized
extraction work on every execution, and the cost model also undercounted
stable downstream work when evaluating an uncertain prepared range.

The fix models the two contracts independently: stored keys must
preserve NULL components, but comparison bounds must reject NULL.
`serial` and `serial_full` are byte-identical for non-NULL components,
so using `serial` for bounds preserves index compatibility without
row-by-row NULL guards.

## Compatibility and regression coverage

- Non-unique index storage and join runtime-filter materialization still
use `serial_full`.
- Nullable strict upper bounds and ambiguous varchar/varbinary prefixes
retain exact decoded residuals.
- Broad literal ranges still reject the index; uncertain prepared ranges
keep the index when cost intervals overlap.
- Covering scans remain covering, while non-covering prepared ranges
still select an index join.
- Prepared equality, IN, BETWEEN, mixed OR, update, delete, and replace
paths are covered.

## Validation

- `mo-cgo-test -count=1 ./pkg/sql/plan/function ./pkg/sql/plan`
- Focused `-race` coverage for all changed planner and function cases
- CGo-configured `go vet ./pkg/sql/plan ./pkg/sql/plan/function`
- Scoped molint and golangci-lint: clean
- a55, 2M rows, 100 threads, 4 GiB memory cache:
- intermediate decoded-guard implementation: 22,906.80 TPS, p95 24.83 ms
  - systemic serialization fix: 28,260.18 TPS, p95 19.65 ms
  - +23.37% TPS and -20.86% p95, with zero errors
- a55 CPU profile: `EvalFoldExpr` 16.25% -> 10.77%, `tryFoldParameter`
11.71% -> 7.73%
- `EXPLAIN ANALYZE`: narrow prepared ranges use the covering index;
broad covering ranges use the base table scan

The historical CI comparison used 10M rows and 1000 threads, so its
absolute TPS is intentionally not compared with the a55 2M/100-thread
validation.

Fixes matrixorigin#26971
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…rigin#27122)

## What changed

- Separate physical index-key serialization from SQL comparison-bound
serialization.
Physical non-unique composite keys continue to use `serial_full`, while
lookup bounds use NULL-propagating `serial`.
- Make `prefix_in` ignore NULL needles and compact only owned, non-NULL
prefixes.
- Keep only residual predicates that are still required for encoded
row-side NULL ordering or byte-string prefix ambiguity.
- Include stable index-only output/decode work in the lower bound of
prepared-range cost uncertainty.
- Propagate expression-binding failures instead of silently accepting
incomplete rewrites.

## Root cause

The previous fix used physical stored-key serialization (`serial_full`)
for SQL comparison operands. Prepared NULL bounds therefore needed
decoded residual predicates to recover SQL three-valued semantics. For
OR-ed random ranges this duplicated parameter folding and serialized
extraction work on every execution, and the cost model also undercounted
stable downstream work when evaluating an uncertain prepared range.

The fix models the two contracts independently: stored keys must
preserve NULL components, but comparison bounds must reject NULL.
`serial` and `serial_full` are byte-identical for non-NULL components,
so using `serial` for bounds preserves index compatibility without
row-by-row NULL guards.

## Compatibility and regression coverage

- Non-unique index storage and join runtime-filter materialization still
use `serial_full`.
- Nullable strict upper bounds and ambiguous varchar/varbinary prefixes
retain exact decoded residuals.
- Broad literal ranges still reject the index; uncertain prepared ranges
keep the index when cost intervals overlap.
- Covering scans remain covering, while non-covering prepared ranges
still select an index join.
- Prepared equality, IN, BETWEEN, mixed OR, update, delete, and replace
paths are covered.

## Validation

- `mo-cgo-test -count=1 ./pkg/sql/plan/function ./pkg/sql/plan`
- Focused `-race` coverage for all changed planner and function cases
- CGo-configured `go vet ./pkg/sql/plan ./pkg/sql/plan/function`
- Scoped molint and golangci-lint: clean
- a55, 2M rows, 100 threads, 4 GiB memory cache:
- intermediate decoded-guard implementation: 22,906.80 TPS, p95 24.83 ms
  - systemic serialization fix: 28,260.18 TPS, p95 19.65 ms
  - +23.37% TPS and -20.86% p95, with zero errors
- a55 CPU profile: `EvalFoldExpr` 16.25% -> 10.77%, `tryFoldParameter`
11.71% -> 7.73%
- `EXPLAIN ANALYZE`: narrow prepared ranges use the covering index;
broad covering ranges use the base table scan

The historical CI comparison used 10M rows and 1000 threads, so its
absolute TPS is intentionally not compared with the a55 2M/100-thread
validation.

Fixes matrixorigin#26971
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…rigin#27122)

## What changed

- Separate physical index-key serialization from SQL comparison-bound
serialization.
Physical non-unique composite keys continue to use `serial_full`, while
lookup bounds use NULL-propagating `serial`.
- Make `prefix_in` ignore NULL needles and compact only owned, non-NULL
prefixes.
- Keep only residual predicates that are still required for encoded
row-side NULL ordering or byte-string prefix ambiguity.
- Include stable index-only output/decode work in the lower bound of
prepared-range cost uncertainty.
- Propagate expression-binding failures instead of silently accepting
incomplete rewrites.

## Root cause

The previous fix used physical stored-key serialization (`serial_full`)
for SQL comparison operands. Prepared NULL bounds therefore needed
decoded residual predicates to recover SQL three-valued semantics. For
OR-ed random ranges this duplicated parameter folding and serialized
extraction work on every execution, and the cost model also undercounted
stable downstream work when evaluating an uncertain prepared range.

The fix models the two contracts independently: stored keys must
preserve NULL components, but comparison bounds must reject NULL.
`serial` and `serial_full` are byte-identical for non-NULL components,
so using `serial` for bounds preserves index compatibility without
row-by-row NULL guards.

## Compatibility and regression coverage

- Non-unique index storage and join runtime-filter materialization still
use `serial_full`.
- Nullable strict upper bounds and ambiguous varchar/varbinary prefixes
retain exact decoded residuals.
- Broad literal ranges still reject the index; uncertain prepared ranges
keep the index when cost intervals overlap.
- Covering scans remain covering, while non-covering prepared ranges
still select an index join.
- Prepared equality, IN, BETWEEN, mixed OR, update, delete, and replace
paths are covered.

## Validation

- `mo-cgo-test -count=1 ./pkg/sql/plan/function ./pkg/sql/plan`
- Focused `-race` coverage for all changed planner and function cases
- CGo-configured `go vet ./pkg/sql/plan ./pkg/sql/plan/function`
- Scoped molint and golangci-lint: clean
- a55, 2M rows, 100 threads, 4 GiB memory cache:
- intermediate decoded-guard implementation: 22,906.80 TPS, p95 24.83 ms
  - systemic serialization fix: 28,260.18 TPS, p95 19.65 ms
  - +23.37% TPS and -20.86% p95, with zero errors
- a55 CPU profile: `EvalFoldExpr` 16.25% -> 10.77%, `tryFoldParameter`
11.71% -> 7.73%
- `EXPLAIN ANALYZE`: narrow prepared ranges use the covering index;
broad covering ranges use the base table scan

The historical CI comparison used 10M rows and 1000 threads, so its
absolute TPS is intentionally not compared with the a55 2M/100-thread
validation.

Fixes matrixorigin#26971

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

size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: sysbench random_ranges_1000 TPS 暴跌 97%,二分定位到 PR #26850 (cost encoded regular index access)

2 participants