You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Block structure mismatch in UnionStep stream when UNION ALL branches differ only in WHERE and one branch's predicate constant-folds (server abort LOGICAL_ERROR) #106956
Logical error: Block structure mismatch in UnionStep stream — constant-folded filter pushdown makes UNION branch headers diverge (Const vs non-Const)
Summary
A UNION ALL whose branches differ only in the WHERE predicate throws a LOGICAL_ERROR exception (and aborts servers built with sanitizers /
debug assertions) when one branch's predicate constant-folds: the optimizer
pushes the folded filter (_CAST(0_UInt8, 'UInt8')) into both sides of the
branch's join, after which that branch's projection column becomes a Const
column while the sibling branch's stays non-const. UnionStep then rejects
the divergent headers:
SELECTt2.a, t1.b=t2.bFROM (SELECT1AS a, 2AS b) AS t1
INNER JOIN (SELECT1AS a, 2AS b) AS t2 ONt1.a=t2.aWHEREt1.b=t2.bUNION ALLSELECTt2.a, t1.b=t2.bFROM (SELECT1AS a, 2AS b) AS t1
INNER JOIN (SELECT1AS a, 2AS b) AS t2 ONt1.a=t2.aWHERE NOT (t1.b=t2.b);
Code: 49. DB::Exception: Block structure mismatch in UnionStep stream.
Variants with UNION DISTINCT, SELECT DISTINCT, and an explicit CAST(t1.b = t2.b, 'Nullable(UInt8)') projection fail identically.
Reproduced on 26.6.1.1 at both 76c2edca2fd and current master
(fbad3ea7aed merge), release build, default settings. On a debug or
sanitizer build the logical error aborts the server.
Plan context from the server log right before the exception:
QueryPlanOptimizations: Pushed down partial filter _CAST(0_UInt8, 'UInt8'_String) to the Left side of join
QueryPlanOptimizations: Pushed down partial filter _CAST(0_UInt8, 'UInt8'_String) to the Right side of join
<Fatal> Logical error: 'Block structure mismatch in UnionStep stream: ...'
Fix area
src/Processors/QueryPlan/Optimizations/ filter-pushdown through join for
constant-folded predicates, vs UnionStep::updateOutputHeader
(src/Processors/QueryPlan/UnionStep.cpp:28): either the pushdown must not
leave the branch projection constant when the union sibling headers are
non-const (materialize the column), or UnionStep header checking must
tolerate constness differences by materializing.
Original fuzzer hit (for reference)
2026-06-10 11:39:07, fuzz-lab cycle 9i instance 08 (ASan), crash snapshot tmp/fuzz_lab/runs/20260610-1020-58419a326d5/crashes/inst-08-20260610-114029/.
Triggered by the oracle's own TLP p / NOT p / p IS NULL UNION rewrite of a
fuzzed query from the CAST-to-Nullable join stateless test — any user query
of this shape hits it too, as the standalone repro shows.
@groeneai could you please reproduce and fix this one? Repro is in the issue body — deterministic on master 28f8e5aca66 (current PR #99980 head), default settings.
Root cause: the set-operation branches are optimized independently in the first plan pass. The NOT (t1.b = t2.b) branch's predicate constant-folds, and filter push-down through the join leaves that branch's projection column Const while the sibling stays full. The parent UnionStep's strict header check then rejects the Const-vs-full divergence and aborts.
The fix materializes constant columns on set-operation branches during the first pass before the strict header check, reusing the existing materializeConstantsForSetOperationBranches logic that already handles the same divergence for distributed plans. Confirmed on the standalone repro plus UNION DISTINCT / SELECT DISTINCT / CAST-to-Nullable / EXCEPT variants; regression test added.
Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.
TL;DR
Block structure mismatch in UnionStep stream when UNION ALL branches differ only in WHERE and one branch's predicate constant-folds (server abort LOGICAL_ERROR)
ClickGap verified: reproduced it with the script below on master; tested 5 version(s) — 4 affected, 1 clean (see Affects); bisected to a candidate range — a single introducing PR could not be pinned (see Bisect).
Reproduction
SELECT t2.a, t1.b = t2.b
FROM (SELECT 1 AS a, 2 AS b) AS t1
INNER JOIN (SELECT 1 AS a, 2 AS b) AS t2 ON t1.a = t2.a
WHERE t1.b = t2.b
UNION ALL
SELECT t2.a, t1.b = t2.b
FROM (SELECT 1 AS a, 2 AS b) AS t1
INNER JOIN (SELECT 1 AS a, 2 AS b) AS t2 ON t1.a = t2.a
WHERE NOT (t1.b = t2.b);
Bisect
The bisect boundary was rejected — PR only modifies a Keeper integration test file and cannot affect ClickHouse query execution logic for UNION ALL or block structure handling. — a coincidental boundary, not the cause; no introducing PR pinned.
The bisect boundary above narrows the search in comp-query-optimizer — treat the candidate range as the starting point; the single introducing PR is unconfirmed.
Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.
TL;DR
Block structure mismatch in UnionStep stream when UNION ALL branches differ only in WHERE and one branch's predicate constant-folds (server abort LOGICAL_ERROR)
ClickGap verified: reproduced it with the script below on master; tested 6 version(s) — 4 affected, 2 clean (see Affects); bisected the regression to a single PR (#98431).
Reproduction
SELECT t2.a, t1.b = t2.b
FROM (SELECT 1 AS a, 2 AS b) AS t1
INNER JOIN (SELECT 1 AS a, 2 AS b) AS t2 ON t1.a = t2.a
WHERE t1.b = t2.b
UNION ALL
SELECT t2.a, t1.b = t2.b
FROM (SELECT 1 AS a, 2 AS b) AS t1
INNER JOIN (SELECT 1 AS a, 2 AS b) AS t2 ON t1.a = t2.a
WHERE NOT (t1.b = t2.b);
First shipped in release line: 26.3 (also in 26.4, 26.5)
Affects
Reproduces on: master, 26.5, 26.4, 26.3
Does NOT reproduce on: 26.2, 26.1
Suggested next step
Review PR #98431 for the introducing change in comp-query-optimizer. If a forward-fix or revert is intended, the bisect data above narrows the search space.
Activity
qoega commentedon Jun 10, 2026
@groeneai could you please reproduce and fix this one? Repro is in the issue body — deterministic on master
28f8e5aca66(current PR #99980 head), default settings.groeneai commentedon Jun 10, 2026
@qoega reproduced and fixed in #107041.
Root cause: the set-operation branches are optimized independently in the first plan pass. The
NOT (t1.b = t2.b)branch's predicate constant-folds, and filter push-down through the join leaves that branch's projection columnConstwhile the sibling stays full. The parentUnionStep's strict header check then rejects the Const-vs-full divergence and aborts.The fix materializes constant columns on set-operation branches during the first pass before the strict header check, reusing the existing
materializeConstantsForSetOperationBrancheslogic that already handles the same divergence for distributed plans. Confirmed on the standalone repro plus UNION DISTINCT / SELECT DISTINCT / CAST-to-Nullable / EXCEPT variants; regression test added.Fix Block structure mismatch in UnionStep/IntersectOrExceptStep when …
Also tolerate sparse/full branch divergence in the plan-optimization …
Fix Block structure mismatch in UnionStep/IntersectOrExceptStep when …
Fix Block structure mismatch in UnionStep/IntersectOrExceptStep when …
Fix Block structure mismatch in UnionStep/IntersectOrExceptStep when …
Fix Block structure mismatch in UnionStep/IntersectOrExceptStep when …
clickgapai commentedon Jun 18, 2026
Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.
TL;DR
Block structure mismatch in
UnionStepstream whenUNION ALLbranches differ only inWHEREand one branch's predicate constant-folds (server abortLOGICAL_ERROR)ClickGap verified: reproduced it with the script below on
master; tested 5 version(s) — 4 affected, 1 clean (see Affects); bisected to a candidate range — a single introducing PR could not be pinned (see Bisect).Reproduction
Bisect
Affects
master, 26.5, 26.4, 26.325.8Suggested next step
The bisect boundary above narrows the search in
comp-query-optimizer— treat the candidate range as the starting point; the single introducing PR is unconfirmed.Component:
comp-query-optimizerSeverity:
P2ClickGap · Finding:
h_issue_106956Triage metadata
comp-query-optimizercomp-query-executioncomp-query-optimizer): @KochetovNicolai @nickitatqoega commentedon Jun 19, 2026
Closing — fixed by #107041 (merged 2026-06-18). Thanks @groeneai.
clickgapai commentedon Jun 19, 2026
Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.
TL;DR
Block structure mismatch in
UnionStepstream whenUNION ALLbranches differ only inWHEREand one branch's predicate constant-folds (server abortLOGICAL_ERROR)ClickGap verified: reproduced it with the script below on
master; tested 6 version(s) — 4 affected, 2 clean (see Affects); bisected the regression to a single PR (#98431).Reproduction
▶ Run on ClickHouse Fiddle
Bisect
efbc96bb883a, bisected on master, 10 steps, confidence: HIGH)26.3(also in 26.4, 26.5)Affects
master, 26.5, 26.4, 26.326.2, 26.1Suggested next step
Review PR #98431 for the introducing change in
comp-query-optimizer. If a forward-fix or revert is intended, the bisect data above narrows the search space.Component:
comp-query-optimizerSeverity:
P2cc introducing PR author: @alexey-milovidov
ClickGap · Finding:
h_issue_106956Triage metadata
comp-query-optimizercomp-query-executioncomp-query-optimizer): @KochetovNicolai @nickitatFix Block structure mismatch in UnionStep/IntersectOrExceptStep when …
Fix Block structure mismatch in UnionStep/IntersectOrExceptStep when …
Merge branch 'master' into fix-liftupunion-agg-state-type-mismatch
Fix Block structure mismatch in UnionStep/IntersectOrExceptStep when …