Skip to content

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

Closed

Description

@qoega

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:

Block structure mismatch in UnionStep stream: different columns:
equals(b, t2.b) UInt8 UInt8(size = 0)
equals(b, t2.b) UInt8 Const(size = 0, UInt8(size = 1))

Minimal repro

No tables needed (clickhouse local):

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);

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.

Activity

  1. qoega commented on Jun 10, 2026

    @qoega
    MemberAuthor

    @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.

  2. added
    fuzz
    sqlancer
    on Jun 10, 2026
  3. groeneai commented on Jun 10, 2026

    @groeneai
    Collaborator

    @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 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.

  4. added 6 commits that reference this issue on Jun 10, 2026
    d2dc86c
    7e27bb3
    27f00a1
    4493fad
    ffd0383
    cfe8828
  5. clickgapai commented on Jun 18, 2026

    @clickgapai
    Contributor

    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 source-build bisect converged to Fix flaky test_keeper_max_append_byte_size under sanitizers #98945, but it is not the introducer (see above) — not attributing it.

    Affects

    • Reproduces on: master, 26.5, 26.4, 26.3
    • Does NOT reproduce on: 25.8

    Suggested 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-optimizer
    Severity: P2


    ClickGap · Finding: h_issue_106956

    Triage metadata

  6. qoega commented on Jun 19, 2026

    @qoega
    MemberAuthor

    Closing — fixed by #107041 (merged 2026-06-18). Thanks @groeneai.

  7. clickgapai commented on Jun 19, 2026

    @clickgapai
    Contributor

    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);
    

    ▶ Run on ClickHouse Fiddle

    Bisect

    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.

    Component: comp-query-optimizer
    Severity: P2

    cc introducing PR author: @alexey-milovidov


    ClickGap · Finding: h_issue_106956

    Triage metadata

  8. added 2 commits that reference this issue on Jul 8, 2026
    6131023
    be3d44f
  9. removed
    sqlancer
    on Jul 13, 2026
  10. added a commit that references this issue on Sep 11, 2026
    c9aa79c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fuzz

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions