Repository navigation
Fix LOGICAL_ERROR on duplicate __join_result_dummy when deserializing a distributed join plan - #108787
Fix LOGICAL_ERROR on duplicate __join_result_dummy when deserializing a distributed join plan#108787groeneai wants to merge 4 commits into
Conversation
… a distributed join plan JoinStepLogical emits a constant __join_result_dummy marker column when a join has no required output columns. A parent join over two such joins (e.g. count() over a CROSS join of two count-pruned joins) gets __join_result_dummy on both input headers. The in-process planner builds this via the two-argument JoinExpressionActions constructor (positional source mapping, no name check), but a distributed-plan worker reconstructs it via the three-argument constructor in JoinStepLogical::deserialize, which mapped sources by name and rejected the duplicate name with a LOGICAL_ERROR, aborting the server in debug and sanitizer builds. Keep name-based mapping for the unambiguous case and fall back to positional mapping only for a name present in both headers (DAG inputs are ordered [left columns..., right columns...], an invariant JoinStepLogical already relies on). Add a unit test exercising the three-argument constructor with the duplicate dummy column. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-0:20260629-023900 |
|
cc @KochetovNicolai @davenger — could you review this? It fixes a LOGICAL_ERROR ("Left and right columns have same names: [__join_result_dummy]") that aborts a distributed-plan worker ( |
|
Workflow [PR], commit [9d4b465] Summary: ❌
AI ReviewSummaryThis PR fixes the Findings❌ Blockers
Tests
Final VerdictRequest changes. |
…ation Code review on the previous commit pointed out that the positional fallback for a duplicate-named input is unsafe in general: JoinStepLogical::preCalculateKeys appends computed join-key inputs via addInput and expands both input headers, so a serialized shuffle join can have inputs like [left id, left k, right id, right k, left key, right key] while left_header.columns() is already 3. Slicing such a list at number_of_left_inputs would mark the second header-named input as left even though it belongs to the right side, corrupting source attribution. The only duplicate name that actually reaches this constructor is the const __join_result_dummy marker emitted by a count-pruned join. A join carrying it has no equality keys, so preCalculateKeys never runs and the inputs keep their canonical [left header columns..., right header columns...] order. Rather than silently trust position, verify that the input names match the concatenated headers position by position, and only then resolve a both-headers name by position. If the layout is not canonical (the interleaved-keys case from the review), the side is genuinely ambiguous and we throw instead of guessing. The other three-argument constructor caller (optimizeJoin.cpp, foldActionsByProjection) has disjoint left/right names, so has_shared_name is false and the positional path is never taken there; its behavior is unchanged. Adds gtest coverage: unique interleaved inputs still map by name; a duplicate name with a non-canonical layout is rejected (death test in debug/sanitizer builds where LOGICAL_ERROR aborts, throw in release). Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Updated pre-PR validation gate for 505ad9e (addresses the positional-fallback review). Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-1:20260629-051600 |
CI finish ledger — 505ad9eEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Session id: cron:our-pr-ci-monitor:20260629-100000 |
|
Fixing PR for 03365_column_ttl_should_rebuild_skp_idx_and_proj amd_msan/WasmEdge timeout created: #108805 |
CI finish ledger — 8d2749dEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
The earlier partial fix #111528 (pin Session id: cron:our-pr-ci-monitor:20260729-103000 |
A join with no required output columns (e.g. under `count()`) mints the constant
marker column `__join_result_dummy` so its result blocks still carry a correct row
count. When two such dummy-only joins become the two children of one enclosing
join, both child output headers carry that identical column name.
`buildPhysicalJoinImpl` mapped child header columns back to `ActionsDAG` INPUT
nodes with a per-name queue that was rebuilt INSIDE the per-child loop. The right
child's lookup therefore restarted from the front of the queue and returned the
LEFT child's input node again; the dedup pass that follows dropped the repeat, so
`right_dag` ended up with zero outputs and `right_sample_block` was an empty block
while the right pipeline still physically carried its marker column. At run time
`ConstantJoin::addBlockToJoin` then compared a 1-column block against a 0-column
sample and aborted the server on a LOGICAL_ERROR. The empty right schema was also
handed to `TableJoin::setInputColumns` and `chooseJoinAlgorithm`.
Hoisting the queue above the child loop lets it be consumed across both children,
so a name shared by both child headers resolves to a different input node per
child and the left/right source attribution is preserved. The two-argument
`JoinExpressionActions` constructor appends inputs as
[left header columns..., right header columns...] and `children` is
[left, right], so popping in order gives each child its own node.
The queue was already the mechanism for duplicate columns; it only distinguished
duplicates WITHIN one child. This establishes the cross-child invariant: no
earlier revision had it either, so this is a pre-existing defect rather than a
regression of a particular commit.
The trigger is `query_plan_optimize_join_order_limit` in {0, 1}, at which the
join-order optimizer leaves both children dummy-only. Both values are drawn by
the CI settings randomizer, and the abort is deterministic given the draw.
Related: ClickHouse#108787
Co-Authored-By: Claude Opus 5 <[email protected]>
| /// Duplicate name present on both sides: resolvable only by position, and only when the | ||
| /// canonical input layout was verified above. | ||
| if (!inputs_match_headers_by_position) | ||
| throw Exception(ErrorCodes::LOGICAL_ERROR, |
There was a problem hiding this comment.
makeDistributedJoin can still send a valid plan into this exception. In the shuffle path we call join_step->preCalculateKeys before serializing the logical join (src/Processors/QueryPlan/Optimizations/makeDistributed.cpp:282). When an equi-key is computed, preCalculateKeys appends new INPUTs with expression_actions.addInput and widens both headers (src/Processors/QueryPlan/JoinStepLogical.cpp:1613-1616), so a join whose child headers are [id, x] and [id, y] becomes [left id, left x, right id, right y, left key, right key] while left_header.columns() == 3. That makes inputs_match_headers_by_position false, and this branch now throws on the original duplicate id even though the plan is valid and the in-process two-arg constructor already mapped those inputs positionally. So distributed shuffle joins over overlapping column names regress from “works” to LOGICAL_ERROR as soon as a computed equality key is present. The source-side mapping needs to survive serialization instead of being re-derived from the widened headers.
There was a problem hiding this comment.
I checked this on the current head (9d4b465) with instrumentation rather than by reading, and the shape does not reach the throw: the analyzer qualifies every source column, so has_shared_name is never true for an ordinary user column.
I logged the 3-arg ctor's actual arguments and ran your exact example ([id, x] / [id, y], computed equi-key so preCalculateKeys runs) as a real shuffle join across two replicas, make_distributed_plan=1, serialize_query_plan=1, distributed_plan_max_rows_to_broadcast=0:
JEAPROBE 3arg ctor: inputs=4 left=[__table1.id, plus(__table1.id, 1_UInt8)] right=[__table2.id, plus(__table2.id, 1_UInt8)] has_shared_name=false
inputs=4 confirms preCalculateKeys did run and did widen both headers, so the non-canonical layout you describe is real. But the names arriving at the ctor are __table1.id and __table2.id, not id and id: the Expression (Change column names to column identifiers) step rewrites every source column to a __tableN.-qualified identifier before the join, so the left and right name sets are disjoint and the duplicate-name branch is never entered.
That holds for the cases designed to force a collision. A self-join gives __table1.id / __table2.id, and two subqueries both aliasing to k give __table1.k / __table3.k:
JEAPROBE ... left=[__table1.id] right=[__table2.id] has_shared_name=false
JEAPROBE ... left=[__table1.k] right=[__table3.k] has_shared_name=false
JEAPROBE ... left=[__table2.id, plus(...)] right=[__table3.id, plus(...)] has_shared_name=false
Across that sweep (2-table, self-join, identical aliases, 3-way), all queries returned the correct 200, has_shared_name=true was observed 0 times, and neither the new "Cannot determine the source side" exception nor the original "Left and right columns have same names" ever fired.
So the only name that reaches this branch is still the unqualified __join_result_dummy marker, and a join carrying it has no equi-keys, so preCalculateKeys returns early at the left_keys.empty() check and the layout stays canonical. The positional resolution is only used where it is verified, and the throw covers a shape I could not construct.
I would rather not serialize the source mapping on the strength of a shape that does not currently occur, since that changes the plan serialization format for every join. If you have a query that produces two identically-named columns in the two child headers of a serialized join, I will take it and fix it: the throw is deliberately loud so such a case is a clear error and not a silent mis-attribution.
No code change in this round. Instrumentation was temporary and is not in the diff.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 81/83 (97.59%) · Uncovered code |
CI finish ledger - 9d4b465Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260801-230000 |
|
Fixing PR for the pre-auth That PR reapplies #110760 (reverted in #112935) together with the fix: |
|
Superseded by #125071, which serializes the side of every join input instead of inferring it from the input names. The positional fallback here is wrong for a join whose inputs were swapped. |
Related: #89166
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a
LOGICAL_ERROR("Left and right columns have same names: [__join_result_dummy], ...") when a distributed query plan (make_distributed_plan = 1,serialize_query_plan = 1) contains a join whose two inputs are both joins with no required output columns (for examplecount()over a CROSS join of two such joins). In debug and sanitizer builds this aborted the server on the worker that deserialized the plan.Description
JoinStepLogicaladds a constant marker column__join_result_dummyto its output whenever a join has no required output columns (e.g. acount()that needs no join keys). When such a join is itself an input to another join (e.g.count()over a CROSS join of two count-pruned joins), both of the parent join's input headers carry__join_result_dummy.The in-process planner builds this with the two-argument
JoinExpressionActions(left_header, right_header)constructor, which assigns each input's source side positionally and is fine with the duplicate name. A distributed-plan worker, however, reconstructs the same step with the three-argument constructor (a serializedActionsDAGplus the two child output headers) inJoinStepLogical::deserialize. That constructor mapped each input to its source side by name and rejected any name present in both headers with aLOGICAL_ERROR, even though the structure is valid and was produced by the initiator.Found by the AST fuzzer mutating
tests/queries/0_stateless/03394_distributed_shuffle_join.sql(a comma join whose predicate the fuzzer neutralized into a CROSS join),Stress test (arm_tsan). Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108088&sha=39a5968c228d6a28a04328e39f27470f077d2e24&name_0=PR&name_1=Stress%20test%20%28arm_tsan%29The fix keeps name-based source mapping for the common (unambiguous) case so unrelated callers are unaffected, and only for a name present in both headers falls back to positional mapping: the DAG inputs are ordered
[left header columns..., right header columns...](an invariant already relied on elsewhere inJoinStepLogical, which reads the first right input asgetInputs().at(number_of_left_inputs)). Each input still maps to exactly one side, so downstream source-relation checks are unchanged.Regression test:
src/Interpreters/tests/gtest_join_expression_actions.cppexercises the three-argument constructor directly with two headers that both contain__join_result_dummy(fails with the LOGICAL_ERROR before the fix, passes after) and a control case asserting distinct names still map to their own side.