Skip to content

fix(plan): bound serialized regular-index range scans - #32503

Merged
XuPeng-SH merged 6 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-32209-main-40a7082c
Nov 11, 2026
Merged

XuPeng-SH merged 6 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-32209-main-40a7082c

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Related to #32209.

What this PR does / why we need it:

Root causes

  • Composite regular-index matching stopped after an equality prefix, leaving a usable range on the next key part as a decoded residual instead of a serialized cursor bound.
  • Group/order FORCE INDEX processing could select the hinted covering fallback before ordinary range planning, causing bounded aggregates such as COUNT(DISTINCT a) to scan the whole covering index.
  • The hinted serialized-range path skipped the existing filter-stability guard. It could eagerly evaluate a throwing IN bound that the decoded filter order would short-circuit, as exposed by UT Coverage run 42903327705.
  • Automatic composite-range selection had the same guard gap. With KEY ix(a,b), an empty a=99 prefix and a throwing b suffix selected prefix_in_range and raised error 20203 instead of retaining the equality-prefix scan.
  • Two correctness guards were required with composite cursor bounds: hidden multi-part unique indexes omit keys containing NULL, and serialized VARCHAR/VARBINARY cursor endpoints can include escaped-NUL peers that do not satisfy the SQL equality.
  • Coverage run 42916129155 merged all producer profiles but measured only 95/130 changed lines (73.08%). The composite suffix tests covered BETWEEN and open comparison pairs but lacked the reachable closed-pair spelling >= plus <=.

Changes

  • Match a usable range immediately after a composite equality prefix and build prefix_between/prefix_in_range, or full-key between/in_range, serialized bounds. Retain decoded predicates as exact row-side residuals and preserve ordered-LIMIT safeguards.
  • Route eligible non-equality hinted covering matches through ordinary regular-index range matching and building, including forced bounded COUNT(DISTINCT) queries. Require stable filter evaluation before using that serialized access; throwing bounds retain decoded filters, while literal bounds retain prefix_in.
  • Apply regularIndexFiltersHaveStableEvaluation before automatic composite suffix selection. Unstable filters retain equality-prefix scans, while stable literal suffixes retain serialized prefix_in_range bounds.
  • Reject a composite-range unique index as a row source when any key part is nullable and unconstrained; retain exact VARCHAR/VARBINARY equality rechecks for prefix-ambiguous values.
  • Add focused planner regressions and composite_index_range BVT coverage for indexed-versus-unindexed results, scan work, boundaries, duplicates, NULLs, unique completeness, byte strings, decimals, and fallback behavior. Include unique and non-unique closed comparison pairs, asserting both serialized bounds and exact residual operators.

Issue-to-test proof

  • Composite suffix range: the focused planner test failed at the assigned base with prefix_eq; it passes with serialized bounds and exact residuals. SQL returns SUM(k)=34 on indexed and unindexed tables, with 2 blocks/4 rows for the selective range and 0/0 for the empty range.
  • Forced aggregate: the focused planner test failed at the assigned base with decoded BETWEEN; it passes with a bounded lookup. Exact COUNT(DISTINCT a) plans both use idx_a, return 2/3 for bounded/full queries and their unindexed controls, and reduce bounded inputRows from 12 to 9.
  • Scoped filter error domains: with the guard reverted, the new order/group planner cases incorrectly use prefix_in for throwing bounds; with the guard, they retain decoded IN, and literal controls retain prefix_in. The exact CI SQL case passes in both memory and persisted modes, and the clean-base memory control also passes.
  • Automatic filter error domains: the unhinted throwing-suffix planner regression failed before this repair with prefix_in_range; it now retains prefix_eq, and its literal control retains prefix_in_range. The exact unhinted SQL BVT returns no rows without error, while the stable control returns (9,y).
  • Default range join: the base-passing retention control remains correct. Indexed and unindexed small joins return 4, each affected index scan reads 2 blocks/3 rows, and the independent medium fixture returns 1000 with bounded scans.
  • Nullable unique completeness: pre-repair covering SQL returned only (93,99); repaired indexed and unindexed queries return (91,92,93,94,99), or (93,99) with c IS NOT NULL. All four independent covering SELECT b ... ORDER BY b cases passed.
  • Byte-string equality: default and forced VARCHAR/VARBINARY b>90 queries return only (HEX 61,91), while b>=90 returns (61,90),(61,91), matching unindexed controls and excluding escaped-NUL peers.
  • The final independent black-box challenge passed all 12 scripts with no gaps, including the exact forced-DISTINCT work comparison, covering nullable-unique cases, invalid inputs, rollback, reconnects, and concurrent CN reads.

Validation

Composite and forced-aggregate base red/green tests:

.agents/skills/mo-dev/scripts/mo-cgo-test -v -run '^TestRegularIndexCompositeSuffixRange$' -count=1 -timeout=10m -o /tmp/issue-32209-validation/plan-base.test ./pkg/sql/plan
.agents/skills/mo-dev/scripts/mo-cgo-test -v -run '^TestRegularIndexRangeAccessControls$' -count=1 -timeout=15m -o /tmp/issue-32209-validation/plan-leading.test ./pkg/sql/plan

Nullable-unique and byte-string correctness red/green:

.agents/skills/mo-dev/scripts/mo-cgo-test -v -run '^(TestRegularIndexUniqueSuffixRangeRequiresCompleteRowSource|TestRegularIndexCompositeSuffixRangeBounds)$' -count=1 -timeout=10m -o /tmp/issue-32209-review-validation/plan.test ./pkg/sql/plan

Scoped error-domain planner regression, all four order/group throwing/literal cases passed in 2.065s:

.agents/skills/mo-dev/scripts/mo-cgo-test -o /tmp/issue-32209-ci-repair/plan-post-fix.test -v -count=1 -timeout=10m -run '^TestScopedForceIndexPreservesFilterEvaluationDomain$' ./pkg/sql/plan

Exact public SQL regression, memory and persisted modes passed in 16.423s:

.agents/skills/mo-dev/scripts/mo-cgo-test -o /tmp/issue-32209-ci-repair/sqlintegration-post-fix.test -v -count=1 -timeout=15m -run '^TestLikePrefixIndex$/^(memory|persisted)$/^alternative_preserves_error_domains$' ./pkg/tests/sqlintegration

Automatic suffix error-domain regression, with the throwing case failing before the guard and both throwing/literal cases passing afterward:

.agents/skills/mo-dev/scripts/mo-cgo-test -run '^TestRegularIndexCompositeSuffixRangePreservesFilterEvaluationDomain$' -count=1 -timeout=180s -o /tmp/issue-32209-plan-review.test ./pkg/sql/plan

Coverage-gate regression: both new closed comparison-pair cases passed. Replaying the CI parser with the failed run's profile and the new focused profile passes the strict gate at 98/130 changed lines (75.38%):

.agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=120s -run '^TestRegularIndexCompositeSuffixRangeBounds$' -coverprofile=/tmp/issue-32209-coverage-repair/focused-cover.out -o /tmp/issue-32209-coverage-repair/plan-focused.test ./pkg/sql/plan

Complete owning package after the automatic-path repair, passed in 226.105s:

.agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=20m -o /tmp/issue-32209-plan-review.test ./pkg/sql/plan

BVT normal comparison mode, two runs with 111/111 statements passing each time, with database teardown verified after both runs (ART is the isolated runtime directory and WT is the worktree). Generated results were reviewed and contain only the new empty-result and stable-literal cases:

java -Xms1024M -Xmx1024M -cp "$(cat classpath.txt)" -Dconf.yml=$ART/runtime/mo-tester/mo.yml -Drun.yml=$ART/runtime/mo-tester/run.yml io.mo.Tester "path=$WT/test/distributed/cases/optimizer/composite_index_range.sql" method=run rate=100

Whole-binary build and incremental static checks passed. The latest repair also passed gofmt, vet, and lint with no diagnostics:

make -j4 build
GOWORK=off go vet -mod=readonly ./pkg/sql/plan
GOWORK=off golangci-lint run -c .golangci.yml --new-from-rev 56ad29d8a5ebcd7359febb3e5c5575b2fce8eee6 ./pkg/sql/plan

Residual risks

  • The original 30M-row, three-CN latency benchmark was not rerun. Proof uses deterministic planner oracles, exact SQL results, and scan-work counters; no wall-clock improvement is promised for every table size or unselective range.
  • Broader repository suites remain for CI.
  • The default join retains serial_extract comparison and intentional <opaque> literal redaction. This PR does not change those behaviors or restrict legitimate join output.

Match a suffix range after a composite equality prefix, preserve exact residuals and nullable-unique completeness, and route hinted range aggregates through regular-index range planning.

Related to matrixorigin#32209.
Apply the regular-index stability guard before serialized range access for
scoped FORCE INDEX hints. Throwing bounds must retain decoded filter
short-circuiting; literal bounds continue to use serialized ranges.

Add order/group regressions for throwing and literal bounds.
@gouhongshen

Copy link
Copy Markdown
Contributor Author

@XuPeng-SH Fixed in 9103f5188a.

Automatic composite suffix matching now calls regularIndexFiltersHaveStableEvaluation before adding the serialized suffix. The unhinted planner regression retains prefix_eq for the throwing regexp_replace suffix, while its stable literal control retains prefix_in_range. The new regression failed before the guard with prefix_in_range and passes afterward.

The exact unhinted SQL case is now in composite_index_range: the empty-prefix throwing query returns no rows without error, and the literal control returns (9,y). Normal mo-tester comparison passed twice with 111/111 statements, with database teardown verified after both runs. The full ./pkg/sql/plan package, make -j4 build, vet, and lint also passed.

@XuPeng-SH

XuPeng-SH commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 9103f5188a418fbf4863d948ee32734519641617

Review completed. Read the assessment.

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

mo-bot review · mo-bot-local · review

Commit: 9103f5188a418fbf4863d948ee32734519641617

Assessment: approve

The change coherently reuses existing index matching, normalization and scan builders. Combined bounds reduce suffix-range scans while exact residuals and stability/completeness guards preserve SQL semantics. The prior error-domain finding is resolved; no actionable findings remain.

Model: gpt-6.1-sol / max

Session: 01a1a004-9884-72c0-ad39-f096c4209c4a · slot 1 · generation 0

Validation:

  • All 11 selected planner tests passed. Independent frontend SQL probes passed memory/persisted cases and two same-instance cycles with teardown verified.

  • The same composite query read 4 blocks/7 rows at the merge-base and 2 blocks/4 rows at HEAD, with identical results. Empty-range and forced-aggregate work controls passed.

  • Reviewed all four changed files, six discussion entries and linked issue context. Native inputs, formatting, source fingerprints, receipts and logs are reconciled in receipt-reconciliation.json.

  • Reused frozen-head CI successes for build, owning UT, SCA, coverage and active BVT.

Limitations:

  • Local SQL probes used one CN. Broad CI evidence comes from the supplied status snapshot and source selection; raw CI logs were unavailable.

  • The original 30M-row, three-CN latency benchmark was not rerun; no wall-clock improvement is asserted.

@gouhongshen

Copy link
Copy Markdown
Contributor Author

@Mergifyio queue

@mergify

mergify Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

❌ Left the queue · Matrixone CI execution / UT Test on Ubuntu/x86 fails with this pull request

2 h 6 min in the queue, with no time running CI.

So far

  • ✅ Entered the queue Wed 11 Nov, 04:02 · rule main · triggered by @gouhongshen with the @mergifyio queue command
  • ❌ Checks failed · in place
  • 🚫 Left the queue 06:08 · 752e1b8

The failure is in this pull request. It failed tested alone on top of main.

Reason

The merge conditions cannot be satisfied due to failing checks

  • CI Required

Failing checks:

To get back in the queue

  • Push a fix: the pull request is queued again once its queue conditions match.
  • If the failure is a flake, tick the box below to queue it again as it is.

Merge conditions not met when it left the queue

  • any of · required by GitHub branch protection:
    • check-neutral = CI Required
    • check-skipped = CI Required
    • check-success = CI Required

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request
2 merge conditions met
  • #review-threads-unresolved = 0 · required by GitHub branch protection
  • github-review-approved · required by GitHub branch protection

Times in UTC · dashboard · how the merge queue works

@mergify mergify Bot added the queued label Nov 11, 2026
@gouhongshen

Copy link
Copy Markdown
Contributor Author

The Ubuntu/x86 UT failure is tracked in existing issue #32424. Run 42956686144 attempt 1, job 130313413109, failed pkg/tests/upgrade.TestViewDescriptionSubscription after 2.42m; CI Required only propagated that failure. The PR changes regular-index planner code/tests and optimizer BVT files, both planner race shards passed, and the failing upgrade-test sources and UT runner are unchanged from the base.

This branch was successfully deployed

No deployments
ci — 752e1b8d Deployed Nov 11, 2026 by mergify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dequeued kind/bug Something isn't working size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants