Skip to content

Serialize the side of every join input in a distributed query plan - #125071

Open
groeneai wants to merge 3 commits into
ClickHouse:masterfrom
groeneai:fix-join-step-serialize-input-sides
Open

groeneai wants to merge 3 commits into
ClickHouse:masterfrom
groeneai:fix-join-step-serialize-input-sides

Conversation

@groeneai

@groeneai groeneai commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #124938
Related: #108787

Changelog category (leave one):

  • Improvement

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

A distributed query plan (make_distributed_plan) now carries the side of every input of a join, so a join over two joins that return no columns, such as SELECT count() FROM t1, t2, t3, t4 WHERE t1.b = t2.b AND t3.a = t4.a, no longer fails with Left and right columns have same names when the plan is deserialized.

Description

Failing CI job: AST fuzzer (amd_debug, targeted, old_compatibility) on #124938 (STID 4752-3ae2). No related open issue found.

A join whose two children are joins that output no columns has two inputs named __join_result_dummy, one per side. Every task of a distributed plan is serialized and deserialized, also with distributed_plan_execute_locally = 1. JoinStepLogical::serialize wrote the actions DAG without the side of each input, so deserialize inferred it from the input names, which is impossible for a name present in both headers: LOGICAL_ERROR: Left and right columns have same names: [__join_result_dummy], [__join_result_dummy]. The input order does not encode the side either: swapInputs (Cascades join commutativity) swaps the headers but keeps the DAG inputs, and in the failing plan input 0 is on the right side.

Join step version 1, written at plan version 20 (the current 26.10 value), adds one byte per DAG input with its side, and the receiver builds the mapping from it through a new JoinExpressionActions constructor. Version 0 streams keep the name inference.

Validation on a debug build: the new test aborts the master server at its first distributed statement in 20 of 20 randomized runs and passes on this branch in 50 of 50, with every distributed result equal to the same query without the distributed plan. The existing join step serialization and step version unit tests pass.

This supersedes #108787, which keeps the inference and assigns input 0 to the left side, wrong for a swapped join.


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

groeneai and others added 2 commits October 10, 2026 11:02
A join over two joins that output no columns has two inputs named
`__join_result_dummy`, one per side. `JoinStepLogical::serialize` wrote the
actions DAG without the side of each input, so `deserialize` inferred it from
the input names and failed with `Left and right columns have same names` for
a name present in both headers (STID 4752-3ae2, every `make_distributed_plan`
task is serialized). The DAG input order does not encode the side either:
`swapInputs` swaps the headers and keeps the inputs.

`Join` step version 1 (plan version 20) writes one byte per DAG input with its
side, and the receiver builds the mapping from it through a new
`JoinExpressionActions` constructor. Version 0 keeps the name inference.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the motivation clause from the DBMS_MIN_QUERY_PLAN_SERIALIZATION_VERSION_WITH_JOIN_INPUT_SIDES comment and the version comment at the Join step registration, which repeats the constant's name.

CI job: https://github.com/ClickHouse/ClickHouse/actions/runs/38013471256/job/114117867192
Reported in: ClickHouse#124938

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 10, 2026
@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) plus my own cold review (3 findings). No blocker or major findings; the nits below are noted, not blocking.

# Sev Finding Verdict Evidence / action
1 💡 Commit message omits the CI job URL (.claude/CLAUDE.md: include a supplied CI URL in the commit message) AGREE Fixed: the Trim comments commit message carries the job URL and #124938
2 💡 The test pins the obsolete query_plan_use_new_logical_join_step AGREE No-op pin (MAKE_OBSOLETE), the test oracle is unaffected; noted
3 💡 The 3-arg JoinExpressionActions constructor repeats the size and INPUT checks of the new overload AGREE Harmless; the move-assignment hand-off avoids a use-after-move on a delegating constructor; noted
4 💡 Join step version 1 reuses plan version 20 AGREE Correct while 26.10 is not branched (26.9 speaks 19); it moves to the next bump if the branch is cut before merge

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-11:20261010-110600

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. On a master debug server, SELECT count() FROM t1, t2, t3, t4 WHERE (t1.b = t2.b) AND (t3.a = t4.a) with make_distributed_plan = 1, distributed_plan_execute_locally = 1, distributed_plan_workers_num = 2, query_plan_optimize_join_order_limit = 0 or 1 aborts every time, with enable_cascades_optimizer 0 and 1. The CI fuzzer query on an empty table and a populated table abort the same way.
b Root cause explained? Two count()-only joins under one join give it two inputs named __join_result_dummy. JoinStepLogical::serialize did not write the side of each DAG input, so deserialize inferred it by name, which is impossible for a name in both headers. The DAG input order cannot stand in for it, because swapInputs swaps the headers and keeps the inputs.
c Fix matches root cause? Yes. The side is written where it was dropped (Join step version 1) and read back instead of inferred. No guard at the throw site.
d Test intent preserved / new tests added? New 05363_distributed_plan_join_dummy_only_both_children: four limit/Cascades combinations, a table joined on top of the dual-dummy join, and the empty-table fuzzer shape, each compared with the same query without the distributed plan, plus a query_log check that the plan ran in the local executor.
e Both directions demonstrated? Master snapshot: [ FAIL ], server abort with Left and right columns have same names in 20 of 20 randomized runs. This branch: [ OK ] in 50 of 50 randomized runs.
f Fix is general across code paths? JoinStepLogical::deserialize is the only place a join's side mapping crosses the wire. The other name-inferring constructors (optimizeJoin.cpp, AggregationPushdown.cpp) run in process on the headers they were built from. clone already copies the mapping.
g Fix generalizes across inputs (params/datatypes/wrappers)? The side byte is per DAG input, independent of the column type and of how many inputs share a name; swapped and unswapped joins, empty and non-empty tables, and limit 0/1 with Cascades on and off are covered.
h Backward compatible? (maintainer-approved exception only) Yes. A stream at plan version below 20 gets Join version 0, byte-identical to before, and version 0 is still read with name inference. No released version speaks plan version 20.
i Invariants and contracts preserved? Every DAG input keeps exactly one side (serialize throws LOGICAL_ERROR otherwise), the side byte must name a header that has the input (LOGICAL_ERROR), a byte above 1 is INCORRECT_DATA, and the 3-argument constructor keeps its checks and messages in the same order.

Session id: cron:clickhouse-impl-slot-5:20261010-083600

@clickhouse-gh

clickhouse-gh Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [589b1d2]

Summary: ✅


AI Review

Summary

Preserves each JoinStepLogical input's source side during distributed-plan serialization, fixing deserialization when both inputs contain __join_result_dummy. No actionable findings at PR head 589b1d2d2696ffc5cf58556ffd193f90906077e3. The added stateless regression passes in the current head's Fast test report.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... labels Oct 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 589b1d2d2 with master fd62c8e2e (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 5ba211c4d; 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

366 translation units recompiled, 3667 s compile time in total, 347 of them have a recent master baseline.

Translation units without a recent master baseline:

  • src/Interpreters/TableJoin.cpp: 8.4 s
  • src/Processors/Transforms/MergeJoinTransform.cpp: 8.0 s
  • src/Interpreters/JoinedTables.cpp: 7.4 s
  • src/Interpreters/JoinExpressionActions.cpp: 5.9 s
  • src/Processors/Transforms/JoiningTransform.cpp: 5.7 s
  • src/Interpreters/ConstantJoin.cpp: 5.3 s
  • src/Processors/QueryPlan/Profiling/Analysis/JoinStatsAnalyzer.cpp: 5.2 s

Job report

This branch has not been deployed

No deployments
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-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants