Skip to content

Revert "perf(plan): reuse eligible multi-reference CTEs (#26331)" - #26418

Merged
mergify[bot] merged 5 commits into
matrixorigin:mainfrom
VioletQwQ-0:codex/revert-pr-26331
Jul 30, 2026
Merged

mergify[bot] merged 5 commits into
matrixorigin:mainfrom
VioletQwQ-0:codex/revert-pr-26331

Conversation

@VioletQwQ-0

@VioletQwQ-0 VioletQwQ-0 commented Jul 30, 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:

Reverts #26331.

issue #26131 remains open because this rollback restores the previous inline multi-reference CTE behavior.

What this PR does / why we need it:

This reverts squash commit 538a45b37d7162182734c943c59bbbd723da3508 from #26331 at the author request.

The revert removes the multi-reference CTE reuse planner, query-scoped materialized source and spill path, and the associated compile/Dispatch/Merge wiring as one complete unit. Multi-reference CTEs return to independent inline planning, and sink scans use the existing streaming pipeline path.

The revert is synchronized with current upstream/main (2c9608dd45960126ff3a91fc3bbb0addd53d816d). Later overlapping fixes #26357 and #26393 remain present.

There is no SQL syntax, protobuf, public API, or persistence-format change.

Validation

Exact head fb1b16f27b9665203197678f385815755b143b06:

  • previous head d693b810af44d4d9f0ae22ad3b18d68e78e6dc06: all major functional CI jobs passed; Matrixone Utils Coverage alone failed because modified-line coverage was exactly 75%, while the gate requires strictly greater than 75%
  • added TestDispatchResetSkipsInvalidRemoteReceiversAndNotifiesHealthyPeer, covering both nil receiver and nil error-channel guards while proving that a later healthy receiver still receives the original terminal error
  • focused coverprofile: pkg/sql/colexec/dispatch/types.go:227.33,234.14 changed from count 0 in CI to count 1 locally
  • exact-head remote Coverage: PASS with 4/4 modified lines covered (pr_coverage=1.0, threshold >0.75)
  • affected planner/compile/dispatch/merge package tests: PASS
  • complete affected owning packages under -race -count=1: PASS
  • focused dispatch terminal and compile sink-scan tests under -race -count=20: PASS
  • affected package build and vet: PASS
  • git diff --check: PASS
  • exact reverse/overlap audit: only later fix(plan): preserve ROLLUP source order keys #26357 and fix(dedupjoin): preserve parallel finalize failures #26393 edits remain relative to the perf(plan): reuse eligible multi-reference CTEs #26331 parent
  • schema-v4 preflight: PASS review=PASS validation=PASS

BVT: the previous exact-head Proxy multi-CN and pessimistic BVT jobs passed. This follow-up adds package-internal unit coverage only and does not change product behavior.

QA decision

QA required: yes. This rolls back optimizer selection and the materialized fan-out execution path. CI and environment-level analytical-query regression coverage should complete before merge.

@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

Choose a reason for hiding this comment

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

Deep review completed at fb1b16f. I verified this as a complete rollback rather than only reviewing the final diff:

  • the nine product files untouched after #26331 match the pre-#26331 parent byte-for-byte;
  • the surviving query_builder.go and operator.go deltas have identical stable patch IDs to the later #26357 and #26393 fixes, respectively;
  • all materialized-source planner/compile/operator symbols, package files, and the reopened #26131 regression test are removed with no stale references;
  • the added dispatch cleanup test covers the retained nil receiver/error-channel guard without changing rollback behavior.

Local normal tests passed for dispatch, merge, and plan. The only full compile-package failures were the three known Parquet fanout tests already reproducible on clean main; rollback-focused dispatch/compile/CTE tests pass under -race. CI is green and the exact head is mergeable.

@mergify mergify Bot added the queued label Jul 30, 2026
@mergify

mergify Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-30 05:23 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-07-30 06:04 UTC · at 014830f442d8409c041f52541c0f8ba157696af8 · squash

This pull request spent 41 minutes 20 seconds in the queue, including 37 minutes 48 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • 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 / 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 CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86

@mergify
mergify Bot merged commit 9c0f901 into matrixorigin:main Jul 30, 2026
25 of 26 checks passed
@mergify mergify Bot removed the queued label Jul 30, 2026
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Jul 30, 2026
XuPeng-SH pushed a commit that referenced this pull request Jul 30, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue #26131

## What this PR does / why we need it:

PR #26331 introduced conservative reuse for eligible multi-reference
CTEs: one bounded, query-scoped materialized producer with independent
`SINK_SCAN` readers. It was fully reverted by #26418 after TPCH Q15
failed on multi-CN execution.

This PR now explicitly restores the final reviewed #26331 implementation
and adds the targeted multi-CN correction as a separate second commit.
The restored code includes the post-review ownership and spill fixes
from #26331; it is not the earlier version that shared caller-owned
batches or failed when runtime cardinality exceeded the planner
estimate.

The multi-CN failure occurred when the materialized producer contained
several shuffle buckets per CN. `compileSinkNode` attached every bucket
directly below the coordinator merge, so a submitted remote bucket
retained local dispatch registrations targeting sibling buckets outside
its `RemoteRun` tree. The standalone remote-pipeline guard rejected that
tree.

The second commit reuses `groupShuffleBucketsByCNIfNeeded` before the
coordinator merge. Same-CN buckets now form one independently executable
remote producer tree, while process-local materialized state remains on
the coordinator. Single-CN, non-shuffle, single-reader, rejected CTE,
and ordinary streaming sink paths remain unchanged.

Conflict resolution was rebuilt on latest `upstream/main` rather than
mechanically applying the old diff. It preserves later main changes,
including the Dispatch invalid-remote-receiver regression and
derived-table alias validation.

The final follow-up commit closes the remaining spill-resource gap:
materialized sources now share the existing statement/CN memory,
spill-disk, and file-descriptor budget; serialization and read buffers
are admitted before allocation, cumulative framed spill bytes are
admitted before file mutation, and each spill batch has a 64 MiB runtime
cap. Cleanup and prepared reuse release every reservation exactly once.

Validation evidence for final head
`f26b7629d74211ce916206d23af34bc1536d5ee0` and its unchanged parent
implementation:

- complete non-race tests for `pkg/sql/internal/materialized`,
`pkg/sql/colexec/dispatch`, `pkg/sql/colexec/merge`, `pkg/sql/compile`,
`pkg/sql/plan`, and `pkg/tests/issues`
- complete `-race -count=1` tests for the same packages
- focused lifecycle race tests (`-count=20`), multi-CN topology test
(`-count=100`), and planner CTE reuse tests (`-count=20`) before the
final conflict-free latest-main rebase
- production-package build and vet for all changed packages
- `git diff --check`
- final-head schema-v4 preflight: `PASS review=PASS validation=PENDING`
- final-head exact/over-boundary disk, FD, marshal/read memory,
shared-source, file-failure, cleanup, and generation-reuse tests

Parent head `d782957e37a5b46af957609b7edce03640c2379a` additionally
completed stock two-CN TPCH SF10 Q15 through both CN ports: supplier
`33987`, revenue `2296670.4443`
- row checks: lineitem `59,986,052`, supplier `100,000`, Q15 filter
`2,265,119`, grouped suppliers `100,000`
- `EXPLAIN`: one lineitem scan/group producer and two `Sink Scan`
consumers of the same source; the second feeds `MAX(total_revenue)`
- forced client cancellation at about 120 ms: no residual Q15 in `SHOW
PROCESSLIST`; immediate rerun returned the correct result

The parent-head local SF10 terminal is correctness and lifecycle
evidence, not a strict performance claim; unrelated host load was
present during the final exact-head run.

### BVT

N/A. Ordinary BVT data and optimizer statistics cannot deterministically
select and assert the affected combination of an eligible materialized
multi-reference CTE, multi-CN cross-CN shuffle, and multiple producer
buckets per CN.

Equivalent coverage:

- planner and issue tests assert eligibility/rejection guards, scoped
CTE identity, one producer, two independent consumers, and the Q15 shape
- materialized-source and Merge tests cover ownership,
buffered-data-before-error, cancellation, reuse, bounded transient
memory, cumulative spill/FD admission, exact cleanup, and generation
reuse under `-race`
- `TestMaterializedCTESinkGroupsShuffleBucketsByCN` constructs the
production-shaped two-CN/two-bucket topology through `compileSinkNode`
and asserts one standalone producer tree per CN
- the exact-head stock two-CN SF10 SQL terminal covers the real
planner/compiler/executor path

### QA Decision

- required: yes
- reason: this fixes a user-visible distributed SQL failure involving
remote pipeline rejection, cleanup, and cancellation/reuse behavior
- production entrypoint: TPCH SF10 Q15 using one shared materialized CTE
producer with multiple `SINK_SCAN` consumers on a stock two-CN
deployment
- automated terminal coverage: exact-head topology, ownership/lifecycle,
planner, and local stock two-CN SF10 evidence listed above
- remaining validation: verify multi-CN Q15 result and
cancellation/reuse in the QA deployment or nightly distributed
environment

No SQL syntax, public API, protobuf, wire, or persisted-format changes.

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…#26331)" (matrixorigin#26418)

This reverts squash commit `538a45b37d7162182734c943c59bbbd723da3508` from matrixorigin#26331 at the author request.

The revert removes the multi-reference CTE reuse planner, query-scoped materialized source and spill path, and the associated compile/Dispatch/Merge wiring as one complete unit. Multi-reference CTEs return to independent inline planning, and sink scans use the existing streaming pipeline path.

The revert is synchronized with current `upstream/main` (`2c9608dd45960126ff3a91fc3bbb0addd53d816d`). Later overlapping fixes matrixorigin#26357 and matrixorigin#26393 remain present.

There is no SQL syntax, protobuf, public API, or persistence-format change.

### Validation

Exact head `fb1b16f27b9665203197678f385815755b143b06`:

- previous head `d693b810af44d4d9f0ae22ad3b18d68e78e6dc06`: all major functional CI jobs passed; Matrixone Utils Coverage alone failed because modified-line coverage was exactly 75%, while the gate requires strictly greater than 75%
- added `TestDispatchResetSkipsInvalidRemoteReceiversAndNotifiesHealthyPeer`, covering both nil receiver and nil error-channel guards while proving that a later healthy receiver still receives the original terminal error
- focused coverprofile: `pkg/sql/colexec/dispatch/types.go:227.33,234.14` changed from count 0 in CI to count 1 locally
- exact-head remote Coverage: PASS with 4/4 modified lines covered (`pr_coverage=1.0`, threshold `>0.75`)
- affected planner/compile/dispatch/merge package tests: PASS
- complete affected owning packages under `-race -count=1`: PASS
- focused dispatch terminal and compile sink-scan tests under `-race -count=20`: PASS
- affected package build and vet: PASS
- `git diff --check`: PASS
- exact reverse/overlap audit: only later matrixorigin#26357 and matrixorigin#26393 edits remain relative to the matrixorigin#26331 parent
- schema-v4 preflight: `PASS review=PASS validation=PASS`

BVT: the previous exact-head Proxy multi-CN and pessimistic BVT jobs passed. This follow-up adds package-internal unit coverage only and does not change product behavior.

### QA decision

QA required: yes. This rolls back optimizer selection and the materialized fan-out execution path. CI and environment-level analytical-query regression coverage should complete before merge.

Approved by: @XuPeng-SH
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…gin#26426)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#26131

## What this PR does / why we need it:

PR matrixorigin#26331 introduced conservative reuse for eligible multi-reference
CTEs: one bounded, query-scoped materialized producer with independent
`SINK_SCAN` readers. It was fully reverted by matrixorigin#26418 after TPCH Q15
failed on multi-CN execution.

This PR now explicitly restores the final reviewed matrixorigin#26331 implementation
and adds the targeted multi-CN correction as a separate second commit.
The restored code includes the post-review ownership and spill fixes
from matrixorigin#26331; it is not the earlier version that shared caller-owned
batches or failed when runtime cardinality exceeded the planner
estimate.

The multi-CN failure occurred when the materialized producer contained
several shuffle buckets per CN. `compileSinkNode` attached every bucket
directly below the coordinator merge, so a submitted remote bucket
retained local dispatch registrations targeting sibling buckets outside
its `RemoteRun` tree. The standalone remote-pipeline guard rejected that
tree.

The second commit reuses `groupShuffleBucketsByCNIfNeeded` before the
coordinator merge. Same-CN buckets now form one independently executable
remote producer tree, while process-local materialized state remains on
the coordinator. Single-CN, non-shuffle, single-reader, rejected CTE,
and ordinary streaming sink paths remain unchanged.

Conflict resolution was rebuilt on latest `upstream/main` rather than
mechanically applying the old diff. It preserves later main changes,
including the Dispatch invalid-remote-receiver regression and
derived-table alias validation.

The final follow-up commit closes the remaining spill-resource gap:
materialized sources now share the existing statement/CN memory,
spill-disk, and file-descriptor budget; serialization and read buffers
are admitted before allocation, cumulative framed spill bytes are
admitted before file mutation, and each spill batch has a 64 MiB runtime
cap. Cleanup and prepared reuse release every reservation exactly once.

Validation evidence for final head
`f26b7629d74211ce916206d23af34bc1536d5ee0` and its unchanged parent
implementation:

- complete non-race tests for `pkg/sql/internal/materialized`,
`pkg/sql/colexec/dispatch`, `pkg/sql/colexec/merge`, `pkg/sql/compile`,
`pkg/sql/plan`, and `pkg/tests/issues`
- complete `-race -count=1` tests for the same packages
- focused lifecycle race tests (`-count=20`), multi-CN topology test
(`-count=100`), and planner CTE reuse tests (`-count=20`) before the
final conflict-free latest-main rebase
- production-package build and vet for all changed packages
- `git diff --check`
- final-head schema-v4 preflight: `PASS review=PASS validation=PENDING`
- final-head exact/over-boundary disk, FD, marshal/read memory,
shared-source, file-failure, cleanup, and generation-reuse tests

Parent head `d782957e37a5b46af957609b7edce03640c2379a` additionally
completed stock two-CN TPCH SF10 Q15 through both CN ports: supplier
`33987`, revenue `2296670.4443`
- row checks: lineitem `59,986,052`, supplier `100,000`, Q15 filter
`2,265,119`, grouped suppliers `100,000`
- `EXPLAIN`: one lineitem scan/group producer and two `Sink Scan`
consumers of the same source; the second feeds `MAX(total_revenue)`
- forced client cancellation at about 120 ms: no residual Q15 in `SHOW
PROCESSLIST`; immediate rerun returned the correct result

The parent-head local SF10 terminal is correctness and lifecycle
evidence, not a strict performance claim; unrelated host load was
present during the final exact-head run.

### BVT

N/A. Ordinary BVT data and optimizer statistics cannot deterministically
select and assert the affected combination of an eligible materialized
multi-reference CTE, multi-CN cross-CN shuffle, and multiple producer
buckets per CN.

Equivalent coverage:

- planner and issue tests assert eligibility/rejection guards, scoped
CTE identity, one producer, two independent consumers, and the Q15 shape
- materialized-source and Merge tests cover ownership,
buffered-data-before-error, cancellation, reuse, bounded transient
memory, cumulative spill/FD admission, exact cleanup, and generation
reuse under `-race`
- `TestMaterializedCTESinkGroupsShuffleBucketsByCN` constructs the
production-shaped two-CN/two-bucket topology through `compileSinkNode`
and asserts one standalone producer tree per CN
- the exact-head stock two-CN SF10 SQL terminal covers the real
planner/compiler/executor path

### QA Decision

- required: yes
- reason: this fixes a user-visible distributed SQL failure involving
remote pipeline rejection, cleanup, and cancellation/reuse behavior
- production entrypoint: TPCH SF10 Q15 using one shared materialized CTE
producer with multiple `SINK_SCAN` consumers on a stock two-CN
deployment
- automated terminal coverage: exact-head topology, ownership/lifecycle,
planner, and local stock two-CN SF10 evidence listed above
- remaining validation: verify multi-CN Q15 result and
cancellation/reuse in the QA deployment or nightly distributed
environment

No SQL syntax, public API, protobuf, wire, or persisted-format changes.

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…#26331)" (matrixorigin#26418)

This reverts squash commit `538a45b37d7162182734c943c59bbbd723da3508` from matrixorigin#26331 at the author request.

The revert removes the multi-reference CTE reuse planner, query-scoped materialized source and spill path, and the associated compile/Dispatch/Merge wiring as one complete unit. Multi-reference CTEs return to independent inline planning, and sink scans use the existing streaming pipeline path.

The revert is synchronized with current `upstream/main` (`2c9608dd45960126ff3a91fc3bbb0addd53d816d`). Later overlapping fixes matrixorigin#26357 and matrixorigin#26393 remain present.

There is no SQL syntax, protobuf, public API, or persistence-format change.

### Validation

Exact head `fb1b16f27b9665203197678f385815755b143b06`:

- previous head `d693b810af44d4d9f0ae22ad3b18d68e78e6dc06`: all major functional CI jobs passed; Matrixone Utils Coverage alone failed because modified-line coverage was exactly 75%, while the gate requires strictly greater than 75%
- added `TestDispatchResetSkipsInvalidRemoteReceiversAndNotifiesHealthyPeer`, covering both nil receiver and nil error-channel guards while proving that a later healthy receiver still receives the original terminal error
- focused coverprofile: `pkg/sql/colexec/dispatch/types.go:227.33,234.14` changed from count 0 in CI to count 1 locally
- exact-head remote Coverage: PASS with 4/4 modified lines covered (`pr_coverage=1.0`, threshold `>0.75`)
- affected planner/compile/dispatch/merge package tests: PASS
- complete affected owning packages under `-race -count=1`: PASS
- focused dispatch terminal and compile sink-scan tests under `-race -count=20`: PASS
- affected package build and vet: PASS
- `git diff --check`: PASS
- exact reverse/overlap audit: only later matrixorigin#26357 and matrixorigin#26393 edits remain relative to the matrixorigin#26331 parent
- schema-v4 preflight: `PASS review=PASS validation=PASS`

BVT: the previous exact-head Proxy multi-CN and pessimistic BVT jobs passed. This follow-up adds package-internal unit coverage only and does not change product behavior.

### QA decision

QA required: yes. This rolls back optimizer selection and the materialized fan-out execution path. CI and environment-level analytical-query regression coverage should complete before merge.

Approved by: @XuPeng-SH
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…gin#26426)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#26131

## What this PR does / why we need it:

PR matrixorigin#26331 introduced conservative reuse for eligible multi-reference
CTEs: one bounded, query-scoped materialized producer with independent
`SINK_SCAN` readers. It was fully reverted by matrixorigin#26418 after TPCH Q15
failed on multi-CN execution.

This PR now explicitly restores the final reviewed matrixorigin#26331 implementation
and adds the targeted multi-CN correction as a separate second commit.
The restored code includes the post-review ownership and spill fixes
from matrixorigin#26331; it is not the earlier version that shared caller-owned
batches or failed when runtime cardinality exceeded the planner
estimate.

The multi-CN failure occurred when the materialized producer contained
several shuffle buckets per CN. `compileSinkNode` attached every bucket
directly below the coordinator merge, so a submitted remote bucket
retained local dispatch registrations targeting sibling buckets outside
its `RemoteRun` tree. The standalone remote-pipeline guard rejected that
tree.

The second commit reuses `groupShuffleBucketsByCNIfNeeded` before the
coordinator merge. Same-CN buckets now form one independently executable
remote producer tree, while process-local materialized state remains on
the coordinator. Single-CN, non-shuffle, single-reader, rejected CTE,
and ordinary streaming sink paths remain unchanged.

Conflict resolution was rebuilt on latest `upstream/main` rather than
mechanically applying the old diff. It preserves later main changes,
including the Dispatch invalid-remote-receiver regression and
derived-table alias validation.

The final follow-up commit closes the remaining spill-resource gap:
materialized sources now share the existing statement/CN memory,
spill-disk, and file-descriptor budget; serialization and read buffers
are admitted before allocation, cumulative framed spill bytes are
admitted before file mutation, and each spill batch has a 64 MiB runtime
cap. Cleanup and prepared reuse release every reservation exactly once.

Validation evidence for final head
`f26b7629d74211ce916206d23af34bc1536d5ee0` and its unchanged parent
implementation:

- complete non-race tests for `pkg/sql/internal/materialized`,
`pkg/sql/colexec/dispatch`, `pkg/sql/colexec/merge`, `pkg/sql/compile`,
`pkg/sql/plan`, and `pkg/tests/issues`
- complete `-race -count=1` tests for the same packages
- focused lifecycle race tests (`-count=20`), multi-CN topology test
(`-count=100`), and planner CTE reuse tests (`-count=20`) before the
final conflict-free latest-main rebase
- production-package build and vet for all changed packages
- `git diff --check`
- final-head schema-v4 preflight: `PASS review=PASS validation=PENDING`
- final-head exact/over-boundary disk, FD, marshal/read memory,
shared-source, file-failure, cleanup, and generation-reuse tests

Parent head `d782957e37a5b46af957609b7edce03640c2379a` additionally
completed stock two-CN TPCH SF10 Q15 through both CN ports: supplier
`33987`, revenue `2296670.4443`
- row checks: lineitem `59,986,052`, supplier `100,000`, Q15 filter
`2,265,119`, grouped suppliers `100,000`
- `EXPLAIN`: one lineitem scan/group producer and two `Sink Scan`
consumers of the same source; the second feeds `MAX(total_revenue)`
- forced client cancellation at about 120 ms: no residual Q15 in `SHOW
PROCESSLIST`; immediate rerun returned the correct result

The parent-head local SF10 terminal is correctness and lifecycle
evidence, not a strict performance claim; unrelated host load was
present during the final exact-head run.

### BVT

N/A. Ordinary BVT data and optimizer statistics cannot deterministically
select and assert the affected combination of an eligible materialized
multi-reference CTE, multi-CN cross-CN shuffle, and multiple producer
buckets per CN.

Equivalent coverage:

- planner and issue tests assert eligibility/rejection guards, scoped
CTE identity, one producer, two independent consumers, and the Q15 shape
- materialized-source and Merge tests cover ownership,
buffered-data-before-error, cancellation, reuse, bounded transient
memory, cumulative spill/FD admission, exact cleanup, and generation
reuse under `-race`
- `TestMaterializedCTESinkGroupsShuffleBucketsByCN` constructs the
production-shaped two-CN/two-bucket topology through `compileSinkNode`
and asserts one standalone producer tree per CN
- the exact-head stock two-CN SF10 SQL terminal covers the real
planner/compiler/executor path

### QA Decision

- required: yes
- reason: this fixes a user-visible distributed SQL failure involving
remote pipeline rejection, cleanup, and cancellation/reuse behavior
- production entrypoint: TPCH SF10 Q15 using one shared materialized CTE
producer with multiple `SINK_SCAN` consumers on a stock two-CN
deployment
- automated terminal coverage: exact-head topology, ownership/lifecycle,
planner, and local stock two-CN SF10 evidence listed above
- remaining validation: verify multi-CN Q15 result and
cancellation/reuse in the QA deployment or nightly distributed
environment

No SQL syntax, public API, protobuf, wire, or persisted-format changes.

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…#26331)" (matrixorigin#26418)

This reverts squash commit `538a45b37d7162182734c943c59bbbd723da3508` from matrixorigin#26331 at the author request.

The revert removes the multi-reference CTE reuse planner, query-scoped materialized source and spill path, and the associated compile/Dispatch/Merge wiring as one complete unit. Multi-reference CTEs return to independent inline planning, and sink scans use the existing streaming pipeline path.

The revert is synchronized with current `upstream/main` (`2c9608dd45960126ff3a91fc3bbb0addd53d816d`). Later overlapping fixes matrixorigin#26357 and matrixorigin#26393 remain present.

There is no SQL syntax, protobuf, public API, or persistence-format change.

### Validation

Exact head `fb1b16f27b9665203197678f385815755b143b06`:

- previous head `d693b810af44d4d9f0ae22ad3b18d68e78e6dc06`: all major functional CI jobs passed; Matrixone Utils Coverage alone failed because modified-line coverage was exactly 75%, while the gate requires strictly greater than 75%
- added `TestDispatchResetSkipsInvalidRemoteReceiversAndNotifiesHealthyPeer`, covering both nil receiver and nil error-channel guards while proving that a later healthy receiver still receives the original terminal error
- focused coverprofile: `pkg/sql/colexec/dispatch/types.go:227.33,234.14` changed from count 0 in CI to count 1 locally
- exact-head remote Coverage: PASS with 4/4 modified lines covered (`pr_coverage=1.0`, threshold `>0.75`)
- affected planner/compile/dispatch/merge package tests: PASS
- complete affected owning packages under `-race -count=1`: PASS
- focused dispatch terminal and compile sink-scan tests under `-race -count=20`: PASS
- affected package build and vet: PASS
- `git diff --check`: PASS
- exact reverse/overlap audit: only later matrixorigin#26357 and matrixorigin#26393 edits remain relative to the matrixorigin#26331 parent
- schema-v4 preflight: `PASS review=PASS validation=PASS`

BVT: the previous exact-head Proxy multi-CN and pessimistic BVT jobs passed. This follow-up adds package-internal unit coverage only and does not change product behavior.

### QA decision

QA required: yes. This rolls back optimizer selection and the materialized fan-out execution path. CI and environment-level analytical-query regression coverage should complete before merge.

Approved by: @XuPeng-SH
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…gin#26426)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#26131

## What this PR does / why we need it:

PR matrixorigin#26331 introduced conservative reuse for eligible multi-reference
CTEs: one bounded, query-scoped materialized producer with independent
`SINK_SCAN` readers. It was fully reverted by matrixorigin#26418 after TPCH Q15
failed on multi-CN execution.

This PR now explicitly restores the final reviewed matrixorigin#26331 implementation
and adds the targeted multi-CN correction as a separate second commit.
The restored code includes the post-review ownership and spill fixes
from matrixorigin#26331; it is not the earlier version that shared caller-owned
batches or failed when runtime cardinality exceeded the planner
estimate.

The multi-CN failure occurred when the materialized producer contained
several shuffle buckets per CN. `compileSinkNode` attached every bucket
directly below the coordinator merge, so a submitted remote bucket
retained local dispatch registrations targeting sibling buckets outside
its `RemoteRun` tree. The standalone remote-pipeline guard rejected that
tree.

The second commit reuses `groupShuffleBucketsByCNIfNeeded` before the
coordinator merge. Same-CN buckets now form one independently executable
remote producer tree, while process-local materialized state remains on
the coordinator. Single-CN, non-shuffle, single-reader, rejected CTE,
and ordinary streaming sink paths remain unchanged.

Conflict resolution was rebuilt on latest `upstream/main` rather than
mechanically applying the old diff. It preserves later main changes,
including the Dispatch invalid-remote-receiver regression and
derived-table alias validation.

The final follow-up commit closes the remaining spill-resource gap:
materialized sources now share the existing statement/CN memory,
spill-disk, and file-descriptor budget; serialization and read buffers
are admitted before allocation, cumulative framed spill bytes are
admitted before file mutation, and each spill batch has a 64 MiB runtime
cap. Cleanup and prepared reuse release every reservation exactly once.

Validation evidence for final head
`f26b7629d74211ce916206d23af34bc1536d5ee0` and its unchanged parent
implementation:

- complete non-race tests for `pkg/sql/internal/materialized`,
`pkg/sql/colexec/dispatch`, `pkg/sql/colexec/merge`, `pkg/sql/compile`,
`pkg/sql/plan`, and `pkg/tests/issues`
- complete `-race -count=1` tests for the same packages
- focused lifecycle race tests (`-count=20`), multi-CN topology test
(`-count=100`), and planner CTE reuse tests (`-count=20`) before the
final conflict-free latest-main rebase
- production-package build and vet for all changed packages
- `git diff --check`
- final-head schema-v4 preflight: `PASS review=PASS validation=PENDING`
- final-head exact/over-boundary disk, FD, marshal/read memory,
shared-source, file-failure, cleanup, and generation-reuse tests

Parent head `d782957e37a5b46af957609b7edce03640c2379a` additionally
completed stock two-CN TPCH SF10 Q15 through both CN ports: supplier
`33987`, revenue `2296670.4443`
- row checks: lineitem `59,986,052`, supplier `100,000`, Q15 filter
`2,265,119`, grouped suppliers `100,000`
- `EXPLAIN`: one lineitem scan/group producer and two `Sink Scan`
consumers of the same source; the second feeds `MAX(total_revenue)`
- forced client cancellation at about 120 ms: no residual Q15 in `SHOW
PROCESSLIST`; immediate rerun returned the correct result

The parent-head local SF10 terminal is correctness and lifecycle
evidence, not a strict performance claim; unrelated host load was
present during the final exact-head run.

### BVT

N/A. Ordinary BVT data and optimizer statistics cannot deterministically
select and assert the affected combination of an eligible materialized
multi-reference CTE, multi-CN cross-CN shuffle, and multiple producer
buckets per CN.

Equivalent coverage:

- planner and issue tests assert eligibility/rejection guards, scoped
CTE identity, one producer, two independent consumers, and the Q15 shape
- materialized-source and Merge tests cover ownership,
buffered-data-before-error, cancellation, reuse, bounded transient
memory, cumulative spill/FD admission, exact cleanup, and generation
reuse under `-race`
- `TestMaterializedCTESinkGroupsShuffleBucketsByCN` constructs the
production-shaped two-CN/two-bucket topology through `compileSinkNode`
and asserts one standalone producer tree per CN
- the exact-head stock two-CN SF10 SQL terminal covers the real
planner/compiler/executor path

### QA Decision

- required: yes
- reason: this fixes a user-visible distributed SQL failure involving
remote pipeline rejection, cleanup, and cancellation/reuse behavior
- production entrypoint: TPCH SF10 Q15 using one shared materialized CTE
producer with multiple `SINK_SCAN` consumers on a stock two-CN
deployment
- automated terminal coverage: exact-head topology, ownership/lifecycle,
planner, and local stock two-CN SF10 evidence listed above
- remaining validation: verify multi-CN Q15 result and
cancellation/reuse in the QA deployment or nightly distributed
environment

No SQL syntax, public API, protobuf, wire, or persisted-format changes.

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
@VioletQwQ-0
VioletQwQ-0 deleted the codex/revert-pr-26331 branch December 1, 2026 03:47

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 size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants