Skip to content

fix: preserve composite index access for joins - #32832

Merged
XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-32460-main-c21ca95a
Nov 17, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-32460-main-c21ca95a

Conversation

@gouhongshen

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 #32460.

What this PR does / why we need it:

Root causes

  • Join-index selection required equality for every declared secondary-index key part. The reported VALUES join supplies the valid contiguous leading user_id/run_id prefix of idx_control(user_id, run_id, event_type, event_idx), so it received no bounded index-join candidate and scanned the base table despite FORCE INDEX.
  • When the forced event table was the right/build input, applyIndicesForJoins returned through generic hinted-index backfill before bounded composite-prefix admission. The resulting idx_control scan had no user_id/run_id runtime-filter probe.
  • The initial orientation repair swapped every eligible right-only forced secondary-index join before proving bounded access was constructible. With non-leading equality, generic FORCE INDEX access remained but cardinality-selected orientation was lost, potentially making the large fact input the hash-build side.
  • Candidate admission and costing also needed complete-declared-key priority and matched-prefix NDV accounting so partial-prefix support could not displace a selective complete key or make a broad probe appear selective.

Changes

  • Admit nonempty contiguous leading composite prefixes only with exact materializable runtime filters. Evaluate complete declared keys first, preserve generic forced-index fallback and residual predicates, and retain atomic metadata-error handling.
  • Extract shared candidate discovery and exact-filter construction for orientation preflight and the actual rewrite. Reorientation now occurs only after a bounded candidate is constructible; preflight copies ON expressions and uses placeholder tags so rejection has no planner side effects.
  • Run eligible forced-join normalization before final statistics/hash/shuffle planning and mark reoriented joins with statement-local state. Generic fallback retains the optimizer-selected build/probe orientation when bounded admission is unavailable.
  • Add planner regressions for the exact VALUES lookup, build-side forced access, serialized two-key MatchPrefix bounds, non-leading and partial-unique rejection, complete-key priority and fallback, one-million-versus-ten-row cardinality orientation, primary hints, and metadata-error atomicity.
  • Add the distributed SQL case for empty/populated plans, scalar and UNION ALL equivalence, residual and range predicates, duplicate/NULL/unmatched keys, IGNORE INDEX, and teardown.

Issue-to-test proof for #32460

  • TestForceIndexJoinUsesLeadingCompositePrefix failed on unchanged base 7fcd9bdff8433d72983f0399fd5c10dcab91a00a because idx_control was absent. It now requires the explicit index scan, retained filtered base scan, matching build/probe tags, MatchPrefix, RUNTIME_FILTER_KEY_SERIAL_FULL_V1, two varchar probe components, and a two-argument serial_full build expression.
  • TestForceIndexJoinUsesLeadingCompositePrefixFromBuildSide failed before the build-side repair with zero runtime-filter probes. It now proves that the forced build-side form receives the same bounded access.
  • TestForceIndexJoinGenericFallbackPreservesCardinalityBuildSide and the strengthened TestForceIndexJoinBuildSideRetainsGenericFallback failed on the unchanged orientation candidate. After the gate, the same commands pass: the one-million-row input stays on probe, the ten-row input stays physical child 1/hash build, generic idx_control access remains, and non-leading equality has no prefix probe.
  • The eleven-test focused selection also proves complete-key priority, unbounded complete-key fallback, partial-unique safety, primary-hint exclusion, and preflight/rewrite metadata-error atomicity. The complete pkg/sql/plan package passes.
  • On the preceding service revision, independent output inspection required actual MatchPrefix and two-component serial_full plan lines and verified populated rows (0,11),(1,11),(2,7), residual counts, boundaries, duplicate/NULL/unmatched keys, UNION ALL, and IGNORE INDEX. Two normal mo-tester comparisons each passed 14/14. The SQL/result fixture hashes are unchanged in this revision; current orientation behavior is covered by the focused and package planner tests.

Validation

Original-base proof (expected exit 1: no idx_control scan):

.agents/skills/mo-dev/scripts/mo-cgo-test -o /tmp/issue-32460-main-go/plan-baseline.test -v -count=1 -timeout=180s -run '^TestForceIndexJoinUsesLeadingCompositePrefix$' ./pkg/sql/plan

Build-side pre-repair proof (expected exit 1: zero runtime-filter probes):

.agents/skills/mo-dev/scripts/mo-cgo-test -o /tmp/issue-32460-main-review-go/plan-buildside.test -v -count=1 -timeout=180s -run '^TestForceIndexJoinUsesLeadingCompositePrefixFromBuildSide$' ./pkg/sql/plan

Orientation proof (exit 1 before the gate on both probe-side assertions; the identical command exited 0 after the gate):

.agents/skills/mo-dev/scripts/mo-cgo-test -o /tmp/issue-32460-main-review2-go/plan-cardinality.test -v -count=1 -timeout=180s -run '^(TestForceIndexJoinGenericFallbackPreservesCardinalityBuildSide|TestForceIndexJoinBuildSideRetainsGenericFallback)$' ./pkg/sql/plan

Final focused proof (exit 0; all eleven top-level tests and subtests passed, package 2.095s):

.agents/skills/mo-dev/scripts/mo-cgo-test -o /tmp/issue-32460-main-review2-go/plan-cardinality.test -v -count=1 -timeout=180s -run '^(TestForceIndexJoinUsesLeadingCompositePrefix|TestForceIndexJoinUsesLeadingCompositePrefixFromBuildSide|TestForceIndexJoinBuildSideRetainsGenericFallback|TestForceIndexJoinGenericFallbackPreservesCardinalityBuildSide|TestIndexJoinCompositePrefixSafetyBoundaries|TestIndexJoinPrefersCompleteKeyOverPartialPrefix|TestNormalizeForcedJoinProbeSideSkipsPrimary|TestForceIndexForJoinReturnsMetadataErrorsAtomically|TestRightForceIndexForJoinRollsBackSwapOnMetadataError|TestNormalizeForcedJoinProbeSidePreservesOrientationOnMetadataError|TestNormalizedForceIndexJoinReturnsFallbackMetadataErrorAtomically)$' ./pkg/sql/plan

Complete planner package (exit 0; ok github.com/matrixorigin/matrixone/pkg/sql/plan 162.211s):

.agents/skills/mo-dev/scripts/mo-cgo-test -o /tmp/issue-32460-main-review2-go/plan-cardinality.test -count=1 -timeout=20m ./pkg/sql/plan

Vet and incremental lint (both exit 0; vet empty, lint 0 issues):

go vet -mod=readonly ./pkg/sql/plan
golangci-lint run -c .golangci.yml --new-from-rev 7fcd9bdff8433d72983f0399fd5c10dcab91a00a ./pkg/sql/plan

Supporting service build on the preceding revision (exit 0; only duplicate-library linker warnings):

make -j8 build

Supporting distributed SQL proof on the preceding revision. genrs generated the inspected result; two isolated run configurations each reported total 14, success 14, failed 0, ignored 0, abnormal 0:

cd "$scratch"
java -Xms1024M -Xmx1024M -cp "$scratch:/Users/ghs-mo/MOWorkSpace/mo-tester/lib/*" -Dconf.yml="$scratch/mo.yml" -Drun.yml="$scratch/run.yml" io.mo.Tester

gofmt reports no files requiring formatting. The committed diff check reports only the two canonical mo-tester trailing spaces on Values Scan "*VALUES*" result lines; both normal comparisons accepted those generated results.

Residual risks

  • The deterministic plans and populated fixtures establish access shape and SQL semantics; they do not measure production latency, throughput, or scanned-row totals on long event histories.
  • The service BVT and full mo-service build were not rerun after the final orientation-only planner revision. Current-revision proof is the focused and complete planner package plus vet/lint; broader distributed optimizer suites and other CI checks remain for CI.

Allow VALUES-driven joins to use bounded composite-index prefixes while
preserving complete-key priority and exact runtime-filter requirements.
Gate forced-join reorientation on constructible bounded access so generic
FORCE INDEX fallback retains cardinality-selected build/probe orientation.

Add focused planner and distributed SQL regressions for bounded access,
fallback orientation, residual semantics, and metadata-error atomicity.

Related to matrixorigin#32460.
@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 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Queue requested — the merge queue status continues in this comment ↓.

@XuPeng-SH

XuPeng-SH commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

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

Commit: 6a2d5034ffc7fe362a88d9b781dc919a50ac0a55

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: 6a2d5034ffc7fe362a88d9b781dc919a50ac0a55

Assessment: approve

Shared candidate discovery and existing exact runtime-filter machinery provide a coherent fix. The statement-local marker preserves forced-index fallback after reorientation. No actionable correctness or design defects remain.

Model: gpt-6.1-sol / max

Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0

Validation:

  • Reviewed the complete seven-file delta, baseline admission rules, runtime-filter consumers, physical-planning order, hint fallback, metadata errors and test oracles. Saved review-plan.md and complete discussion-assessment.json.

  • Source/format audit passed: checks/1794881734479106937-2.json matches review-source-audit.log. Verified frozen inputs, clean source, patch/blob indexes, formatting, discussion coverage and native fingerprints. Two canonical mo-tester fixture trailing spaces remain.

  • Independent fixture oracle passed: checks/1794881498661882011-2.json matches fixture-oracle.log. SQLite evaluation verifies all four populated result tables and scalar UNION equivalence; this is not MatrixOne execution.

  • Reused completed UT, SCA, shared-build and multi-CN BVT successes in the frozen head's CI rollup. Author-reported focused/full planner tests and preceding service validation remain attributed evidence.

Limitations:

  • Local planner validation timed out during dependency downloads before tests ran: checks/1794881717738530256-2.json, exit 124, planner-focused.log. This provides no runtime result.

  • No local MatrixOne service BVT or production performance benchmark was run. Individual CI logs were unavailable; newer pending duplicate runs were not counted as passes.

@mergify mergify Bot added the queued label Nov 17, 2026
@mergify

mergify Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🚫 Left the queue · pull request dequeued

11 min in the queue, with no time running CI.

So far

  • ✅ Entered the queue Tue 17 Nov, 02:21 · rule main · triggered by rule Automatic queue on approval for main
  • ✅ Checks skipped · PR is already up-to-date
  • 🚫 Left the queue 02:31 · 6a2d503

Reason

Pull request #32832 has been dequeued

Mergify failed to merge the pull request. GitHub can't merge the pull request after 10 minutes of retrying.

Required status check "CI Required" is expected.

To get back in the queue

  • Push a change if one is needed: the pull request is queued again once its queue conditions match.
  • If no change is needed, tick the box below to queue it again as it is.

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

  • Requeue this pull request
3 merge conditions met
  • #review-threads-unresolved = 0 · required by GitHub branch protection
  • github-review-approved · required by GitHub branch protection
  • any of · required by GitHub branch protection:
    • check-success = CI Required
    • check-neutral = CI Required
    • check-skipped = CI Required

Times in UTC · dashboard · how the merge queue works

This branch was previously deployed

1 inactive deployment
ci — 6a2d5034 Deployed Nov 17, 2026 by gouhongshen
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