Skip to content

Build the empty JOIN side of the constant-false short-circuit from its child's header - #126204

Merged
george-larionov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-short-circuit-false-join-empty-side-header
Oct 20, 2026
Merged

george-larionov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-short-circuit-false-join-empty-side-header

Conversation

@groeneai

@groeneai groeneai commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #126117
Closes: #126542
Related: #122128
Related: #126542

Changelog category (leave one):

  • Improvement

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

A JOIN whose ON condition has a constant-false conjunct now works with the experimental parallel_replicas_plan_based setting when the join uses grace_hash, spills to disk, or runs with join_use_nulls = 1, instead of terminating the server or failing with NOT_FOUND_COLUMN_IN_BLOCK.

Description

Stress test (amd_debug) aborted the server on 04768_parallel_replicas_right_join_null_conjunct with the randomized join_algorithm = 'grace_hash' (report; Assertion 'px != 0' failed in TemporaryBlockStreamHolder). Minimal query, with parallel_replicas_plan_based = 1:

SELECT count() FROM tb_l AS l RIGHT JOIN tb_r AS r ON l.b = r.b AND CAST(NULL AS Nullable(UInt8))
SETTINGS join_algorithm = 'grace_hash', max_bytes_before_external_join = 1000000000;

Join runtime filters rebuild the join's input headers with type-only columns (JoinStepLogical::preCalculateKeys). Plan-based parallel replicas optimize the local plan again after that, so the constant-false short-circuit (#110234) runs on such a join and builds its empty ReadNothing source from the join's input header: a source whose output columns have no column object. grace_hash (directly, or when a hash join spills) dereferences one in TemporaryBlockStreamHolder; with join_use_nulls = 1 the query fails with Not found column __table1.b: in block __table1.b String nullptr. #122128 guarded one consumer of the same header (materializeBlockInplace).

The empty source now takes the output header of the subtree it replaces, which is what the physical join is built from. Plans that are not optimized twice are unchanged: there the two headers are equal. The same code is on 26.8 and 26.9.

The new test runs the RIGHT JOIN with hash, a spilling parallel_hash, grace_hash and join_use_nulls = 1; on master the spilling and grace_hash queries abort the server and the join_use_nulls one throws. 20/20 randomized runs abort without the fix, 50/50 pass with it.

Independent of #124134, which changes the same function for WITH TOTALS.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/126204]

Version info

  • Merged into: 26.11.1.258-master (included in 26.11 and later)
  • Backported to: 26.10.2.39, 26.9.23.14, 26.8.32.16

…s child's header

Join runtime filters rebuild a JoinStepLogical's input headers with type-only
columns (preCalculateKeys). Plan-based parallel replicas optimize the local
plan again after that, so tryShortCircuitConstantFalseJoin ran on such a join
and built its ReadNothingStep from join->getInputHeaders()[side]: a source step
whose output header has null column objects. GraceHashJoin (directly, or after
SpillingHashJoin switches to it) dereferences them in
TemporaryBlockStreamHolder, and join_use_nulls = 1 fails with
NOT_FOUND_COLUMN_IN_BLOCK.

Take the header from the replaced subtree's own output, which is what the
physical join is built from. In plans optimized only once the two are equal.

Closes ClickHouse#126117.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding; bounded: 1 full pass + 1 fix round + 1 delta recheck).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test checks only results, so it could pass if the runtime filter is declined for the 2-row probe (join_runtime_filter_min_probe_rows) or the spill does not happen; asks for a pin plus plan/ProfileEvents assertions DISAGREE With the fix reverted, 20 of 20 randomized runs of the test killed the server; 19 of them drew join_runtime_filter_min_probe_rows between 234 and 9372, so the filter is not declined in this plan (without runtime filters the query does not abort). The spill line forces the switch with max_bytes_before_external_join = 1, and grace_hash spills from the first block. Each of the three checked lines fails on master on its own.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding. Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-review-slot-8:20261016-042600

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The grace_hash RIGHT JOIN from the description, with plan-based parallel replicas and join runtime filters, aborts a debug server 10/10 on master (px != 0 in TemporaryBlockStreamHolder, same stack as the stress job).
b Root cause explained? preCalculateKeys makes the join's input headers type-only; the plan-based parallel replicas local plan is optimized again, the constant-false short-circuit builds ReadNothing from that header, and the grace join dereferences the missing column.
c Fix matches root cause? Yes. The empty source takes its replaced subtree's output header, at the step that published the bad header; no consumer guard.
d Test intent preserved / new tests added? New test: hash control, spilling parallel_hash, grace_hash, join_use_nulls = 1. Two randomized settings that bypass the local plan or reorder the join are clamped with a Random settings limits line.
e Both directions demonstrated? Master: 20/20 randomized runs abort the server; each checked query fails on its own. Fix: 50/50 randomized runs pass, 20/20 also with the clamps removed.
f Fix is general across code paths? One line covers both join sides and every algorithm (kind x algorithm matrix equals the non-parallel result). No other step publishes a join's input header as its own output header.
g Fix generalizes across inputs (params/datatypes/wrappers)? RIGHT/LEFT/INNER/FULL x hash/parallel_hash/grace_hash/partial_merge/auto, spill on and off, join_use_nulls 0/1: all equal the result without parallel replicas.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or serialization change; plans that are not optimized twice get an equal header.
i Invariants and contracts preserved? A source step's output header now carries a column for every column; the join's own input headers are untouched. 92 related stateless tests give identical results with and without the fix.

Session id: cron:clickhouse-impl-slot-7:20261016-010800

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @vdimir @devcrafter, could you review this? With plan-based parallel replicas the local plan is optimized a second time, after join runtime filters made the join's input headers type-only, so the constant-false JOIN short-circuit built its empty ReadNothing source from a header with no column objects and grace_hash (or a spilling hash join) dereferenced one; the source now takes the replaced subtree's output header.

@clickhouse-gh

clickhouse-gh Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [81a2a86]

Summary: ✅


AI Review

Summary

The PR builds ReadNothingStep from the replaced child's output header, preventing type-only logical join headers from reaching physical join execution under plan-based parallel replicas. The regression test covers grace_hash, spilling parallel_hash, and join_use_nulls = 1. No actionable findings.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... labels Oct 16, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 16, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 9df815b

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Unit tests (asan_ubsan, function_prop_fuzzer) / FunctionsStress.stress stuck thread, not from this PR (it changes only the constant-false JOIN short-circuit): Thread is stuck for 265.0s ... SELECT range(materialize(CAST(false AS Bool)), materialize(CAST(26511733 AS UInt32))), stuck in __asan_stack_malloc_1 under ColumnArray::compareAtImpl. The same range stall has failed on master and on many unrelated PRs since 2026-10-15 #126210 (mine, open)
54 dropped jobs (builds, stateless, integration, install, performance, stress, upgrade, compatibility, ClickBench, Docker images) not run: praktika dropped them because the first row failed, so this head has no results for them #126210 (the owner of the first row)
Mergeable Check, PR praktika rollups of the rows above #126210 (the owner of the first row)

Session id: cron:our-pr-ci-monitor:20261016-093007

@PedroTadim

Copy link
Copy Markdown
Member

@vdimir should be very easy to review

@PedroTadim PedroTadim self-assigned this Oct 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 81a2a869a with master 92d878bb4 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 7d062c274; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 8 s compile time in total, 7 of them have a recent master baseline.

Job report

@george-larionov

Copy link
Copy Markdown
Member

I can review it.

@george-larionov george-larionov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.

@PedroTadim should this be backported?

@george-larionov
george-larionov added this pull request to the merge queue Oct 19, 2026
Merged via the queue into ClickHouse:master with commit 4f27008 Oct 20, 2026
178 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 20, 2026
@PedroTadim

Copy link
Copy Markdown
Member

@groeneai can you confirm if this fix needs to be backported?

@groeneai

Copy link
Copy Markdown
Collaborator Author

Yes, to 26.8, 26.9 and 26.10. v26.8-must-backport covers exactly those three, because it expands to the named release and every newer active one. I can't set labels on this repo.

  • The fixed line came with the constant-false JOIN short-circuit (Short-circuit constant-false JOINs at the query-plan level #110234). shortCircuitConstantFalseJoin.cpp on 26.8, 26.9 and 26.10 is identical to master before this PR, so the cherry-pick is clean. 26.7 and 26.3 don't have the short-circuit.
  • The crash already hit Stress test runs on the 26.9 and 26.8 branches. It also fails Upgrade check (amd_release) on unrelated PRs (32 runs on 31 PRs since 2026-10-16, 4 of them after this merged), because that job runs the previous release, now 26.9.22.3, with its own test 04768 (latest). That stays red until a 26.9 release has this fix.
  • Users can only hit it with the experimental parallel_replicas_plan_based.

Also, #122128 (where test 04768 comes from) is not on 26.10: the branch was cut before it merged, and no 26.10 backport was created.

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 20, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 20, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 20, 2026
Backport #126204 to 26.10: Build the empty JOIN side of the constant-false short-circuit from its child's header
clickhouse-gh Bot added a commit that referenced this pull request Oct 20, 2026
Backport #126204 to 26.9: Build the empty JOIN side of the constant-false short-circuit from its child's header
PedroTadim added a commit that referenced this pull request Oct 20, 2026
Backport #126204 to 26.8: Build the empty JOIN side of the constant-false short-circuit from its child's header
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 22, 2026
…constant-false short-circuit from its child's header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.8-must-backport

Projects

None yet

7 participants