Skip to content

Fix filter push-down changing column constness after partial AND extraction - #99915

Merged
alexey-milovidov merged 2 commits into
masterfrom
fix-filter-pushdown-const-mismatch
Mar 19, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
fix-filter-pushdown-const-mismatch

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

After partial push-down extracts some conjunctions from a HAVING filter (e.g., materialize(0) from (MAX(c0) + NULL) AND materialize(0)), the remaining expression may evaluate to a constant on 0-row header input (e.g., plus(X, NULL) folds to const NULL regardless of X).

The existing is_filter_const_after_push_down flag only captures the case where ALL conjunctions are pushed down, missing this "runtime folding" case. When the FilterStep header is recomputed, the filter column becomes const, but parent steps still hold the old non-const header, causing "Block structure mismatch" when mergeExpressions later uses the stale header.

Fix: after modifying the DAG, recompute the header and wrap the remaining predicate in materialize if it became const.

Found via AST fuzzer: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=99539&sha=4d694d72dadaddbf43066e6a7132370f5c379ab8&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29
#99539

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix "Block structure mismatch" exception in queries with HAVING clause where the filter expression contains both an aggregate wrapped in a NULL-producing function and materialize(0).

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.3.1.854

…action

After partial push-down extracts some conjunctions from a HAVING filter
(e.g., `materialize(0)` from `(MAX(c0) + NULL) AND materialize(0)`),
the remaining expression may evaluate to a constant on 0-row header
input (e.g., `plus(X, NULL)` folds to const NULL regardless of X).

The existing `is_filter_const_after_push_down` flag only captures the
case where ALL conjunctions are pushed down, missing this "runtime
folding" case. When the FilterStep header is recomputed, the filter
column becomes const, but parent steps still hold the old non-const
header, causing "Block structure mismatch" when `mergeExpressions`
later uses the stale header.

Fix: after modifying the DAG, recompute the header and wrap the
remaining predicate in `materialize` if it became const.

Found via AST fuzzer: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=99539&sha=4d694d72dadaddbf43066e6a7132370f5c379ab8&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29

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

Copy link
Copy Markdown
Member Author

I absolutely don't like this change. The fix works but feels like a band-aid — computing updateHeader just to detect a constness side-effect of the DAG modification is expensive and inelegant. The root issue is that is_filter_const_after_push_down from removeUnusedConjunctions doesn't capture "runtime folding" cases where a remaining expression evaluates to const despite having non-const DAG inputs (e.g., plus(X, NULL) → const NULL). A better approach might be to teach removeUnusedConjunctions (or the caller) to detect this properly at the DAG level, or to fix the stale header propagation in the optimization framework itself.

@clickhouse-gh

clickhouse-gh Bot commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [89d6f84]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 18, 2026
@alexey-milovidov
alexey-milovidov requested a review from novikd March 18, 2026 14:45
When an optimization modifies a grandchild step's output header (e.g.,
filter push-down changes a filter's DAG and output constness), the
intermediate child step's cached input header becomes stale. Later
optimizations on the parent node (like `mergeExpressions`) use this
stale header, causing "Block structure mismatch" in debug builds.

Fix: before applying optimizations to a node, refresh its input headers
from its children's actual output headers. This is a general fix that
handles any case of stale headers from child optimizations, not just
the specific filter push-down case.

The specific trigger was: `(MAX(c0) + NULL) AND materialize(0)` in
HAVING. After push-down extracts `materialize(0)`, the remaining
`plus(X, NULL)` folds to const NULL on 0-row header input, but the
parent Projection step still held a non-const header.

Found via AST fuzzer: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=99539&sha=4d694d72dadaddbf43066e6a7132370f5c379ab8&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29

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

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Now looks better.

@alexey-milovidov
alexey-milovidov marked this pull request as ready for review March 18, 2026 15:24
@clickhouse-gh

clickhouse-gh Bot commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.70% 83.80% +0.10%
Functions 23.90% 23.90% +0.00%
Branches 76.30% 76.30% +0.00%

PR changed lines: PR changed-lines coverage: 100.00% (9/9, 0 noise lines excluded)
Diff coverage report
Uncovered code

@alexey-milovidov alexey-milovidov self-assigned this Mar 19, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Mar 19, 2026
Merged via the queue into master with commit 062cc12 Mar 19, 2026
317 of 318 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-filter-pushdown-const-mismatch branch March 19, 2026 11:37
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 19, 2026
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.

2 participants