Skip to content

Fix block structure mismatch in removeUnusedColumns optimization - #98431

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix-block-structure-mismatch-removeUnusedColumns
Mar 3, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix-block-structure-mismatch-removeUnusedColumns

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

Summary

  • Fix a debug-only assertion failure ("Block structure mismatch") in the removeUnusedColumns query plan optimization (STID: 0993-29ef)
  • In addDiscardingExpressionStepIfNeeded, when column names match between a parent's input header and a child's output header but column representations differ (e.g., ColumnConst vs materialized), the parent's input header was not updated, leaving it stale
  • This happens when JoinStepLogical::updateOutputHeader materializes its __join_result_dummy constant column after all join outputs are removed
  • The fix syncs the parent's input header with the child's actual output header when names match but structure differs

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=98373&sha=d179a2f6d23d2bd8c4328fc54902b73b8b1a6a13&name_0=PR&name_1=BuzzHouse%20%28amd_debug%29
#98373

Closes #92845

Changelog category (leave one):

  • CI Fix or Improvement

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

Fix block structure mismatch exception in removeUnusedColumns query plan optimization when JoinStepLogical materializes its dummy column.

🤖 Generated with Claude Code

Version info

  • Merged into: 26.3.1.343

In `addDiscardingExpressionStepIfNeeded`, when all column names between a
parent's input header and a child's output header match, the function
returned without updating the parent's input header. However, column
representations could differ (e.g., `ColumnConst` vs materialized) —
this happens when `JoinStepLogical::updateOutputHeader` materializes its
`__join_result_dummy` constant column. The parent's stored input header
remained stale, causing the `checkHeaders` debug assertion to fail with
"Block structure mismatch".

Now, when column names all match but the blocks differ structurally,
sync the parent's input header with the child's actual output header.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=98373&sha=d179a2f6d23d2bd8c4328fc54902b73b8b1a6a13&name_0=PR&name_1=BuzzHouse%20%28amd_debug%29

Closes #92845

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e70b58f]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 1, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Can we construct a test?

Exercise the `removeUnusedColumns` optimization with various JOIN patterns
where output columns are unused, triggering the `__join_result_dummy`
column path in `JoinStepLogical`. This is a regression test for the
block structure mismatch fixed in the previous commit.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
alexey-milovidov and others added 2 commits March 2, 2026 21:28
…mismatch

The previous test queries did not trigger the bug in debug builds.
The key ingredient is a nullable expression referencing columns from
both sides of the join (e.g., `and(t1.b = t2.b, NULL)`) combined with
a WHERE clause on the same cross-table expression and DISTINCT or
GROUP BY ALL. This causes `ExpressionStep::removeUnusedColumns` to
update its output header via `ActionsDAG::updateHeader`, where constant
folding changes a column from materialized to Const representation,
while the parent step's input header retains the stale representation.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logical error: Block structure mismatch in A stream: different columns: (STID: 0993-29ef)

2 participants