Skip to content

Fix Block structure mismatch exception in MaterializingCTETransform - #100573

Merged
alexey-milovidov merged 11 commits into
masterfrom
fix-materialized-cte-totals-crash
Apr 12, 2026
Merged

alexey-milovidov merged 11 commits into
masterfrom
fix-materialized-cte-totals-crash

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 24, 2026 •

Copy link
Copy Markdown
Member

addMaterializingCTETransform was not calling dropTotalsAndExtremes before transforming the pipeline, unlike the similar addCreatingSetsTransform. When a CTE with WITH TOTALS was materialized and referenced multiple times, the totals ports survived with the original header while the main output became empty. When uniting multiple CTE pipelines, uniteTotals tried to connect these non-empty totals ports to a ConcatProcessor created with the empty common header, causing a Block structure mismatch.

Found in: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100404&sha=4e239671fdc3523725369f5ad6028b4a52c43b45&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29
#100404

Changelog category (leave one):

  • Not For Changelog

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix Block structure mismatch exception when using materialized CTEs with WITH TOTALS referenced multiple times.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.848
  • Backported to: 26.3.9.2

`addMaterializingCTETransform` was not calling `dropTotalsAndExtremes`
before transforming the pipeline, unlike the similar
`addCreatingSetsTransform`. When a CTE with `WITH TOTALS` was
materialized and referenced multiple times, the totals ports survived
with the original header while the main output became empty. When
uniting multiple CTE pipelines, `uniteTotals` tried to connect these
non-empty totals ports to a `ConcatProcessor` created with the empty
common header, causing a Block structure mismatch.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100404&sha=4e239671fdc3523725369f5ad6028b4a52c43b45&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [41b7fe2]

Summary: ✅


AI Review

Summary

This PR fixes a real pipeline inconsistency in addMaterializingCTETransform by dropping totals/extremes before materializing CTE output, matching existing behavior in addCreatingSetsTransform. The added stateless test reproduces the previous Block structure mismatch exception and validates the fix path. I did not find correctness, safety, performance, or compatibility issues in the patch.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 24, 2026
The test needs `enable_materialized_cte = 1` (off by default) and
two distinct CTEs each referenced twice, so that `MaterializingCTEsStep`
enters the `unitePipelines` code path where the totals mismatch occurs.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@novikd novikd self-assigned this Mar 25, 2026

@novikd novikd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe we want to backport it to 26.3, and then the PR category should be "Not for the changelog"?
The rest looks good to me.

@alexey-milovidov alexey-milovidov added v26.3-must-backport pr-not-for-changelog This PR should not be mentioned in the changelog and removed pr-bugfix Pull request with bugfix, not backported by default labels Mar 25, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@clickhouse-gh

clickhouse-gh Bot commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.00% +0.00%
Functions 90.90% 90.90% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 100.00% (5/5) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov merged commit 57dfebc into master Apr 12, 2026
163 of 164 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-materialized-cte-totals-crash branch April 12, 2026 15:40
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 12, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Apr 12, 2026
robot-clickhouse added a commit that referenced this pull request Apr 12, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Apr 12, 2026
clickhouse-gh Bot added a commit that referenced this pull request Apr 12, 2026
Backport #100573 to 26.3: Fix Block structure mismatch exception in MaterializingCTETransform
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo v26.3-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants