Repository navigation
Fix filter push-down changing column constness after partial AND extraction - #99915
Conversation
…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]>
|
I absolutely don't like this change. The fix works but feels like a band-aid — computing |
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
left a comment
There was a problem hiding this comment.
Now looks better.
LLVM Coverage Report
PR changed lines: PR changed-lines coverage: 100.00% (9/9, 0 noise lines excluded) |
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_downflag only captures the case where ALL conjunctions are pushed down, missing this "runtime folding" case. When theFilterStepheader is recomputed, the filter column becomes const, but parent steps still hold the old non-const header, causing "Block structure mismatch" whenmergeExpressionslater uses the stale header.Fix: after modifying the DAG, recompute the header and wrap the remaining predicate in
materializeif 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):
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
Version info
26.3.1.854