Skip to content

Fix duplicate columns in TableJoin::setUsedColumns causing exception in HashJoin::getNonJoinedBlocks - #100018

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix-duplicate-columns-in-join-getNonJoinedBlocks
Mar 23, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix-duplicate-columns-in-join-getNonJoinedBlocks

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 19, 2026 •

Copy link
Copy Markdown
Member

ActionsDAG::getRequiredColumnsNames can return duplicate column names when the DAG has multiple input nodes with the same name. When these duplicates are passed to TableJoin::setUsedColumns (from JoinStepLogical.cpp), the same column gets pushed to result_columns_from_left_table multiple times. This causes a column count mismatch in HashJoin::getNonJoinedBlocks where the assertion expects the arithmetic sum of left + right_keys + right_added columns to equal result_sample_block columns (which are deduplicated).

This is a follow-up to #98286 which fixed the same error via a different code path (PlannerJoinTree/buildJoinQueryPlan) but didn't cover the JoinStepLogical path.

Found by AST fuzzer: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=99936&sha=494ca1e7a6070de924219397dd6a93afb0c714a0&name_0=PR&name_1=AST%20fuzzer%20%28amd_msan%29
#99936

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

Fix logical error exception Unexpected number of columns in result sample block in HashJoin::getNonJoinedBlocks when duplicate column names appear in the join's required columns list.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

🤖 Generated with Claude Code

Version info

  • Merged into: 26.4.1.164

…n in `HashJoin::getNonJoinedBlocks`

`ActionsDAG::getRequiredColumnsNames` can return duplicate column names
when the DAG has multiple input nodes with the same name. When these
duplicates are passed to `TableJoin::setUsedColumns`, the same column
gets pushed to `result_columns_from_left_table` multiple times. This
causes a column count mismatch in `HashJoin::getNonJoinedBlocks` where
the assertion expects the arithmetic sum of left + right_keys +
right_added columns to equal `result_sample_block` columns (which are
deduplicated).

This is a follow-up to #98286 which fixed the same error via a
different code path (`PlannerJoinTree`), but did not cover the
`JoinStepLogical` path.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=99936&sha=494ca1e7a6070de924219397dd6a93afb0c714a0&name_0=PR&name_1=AST%20fuzzer%20%28amd_msan%29

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [08d5d10]

Summary: ✅


AI Review

Summary

This PR fixes two real issues: duplicate required column names in TableJoin::setUsedColumns that could trigger Unexpected number of columns in result sample block, and a data race on stop_read_return_partial_result in TCPHandler::processOrdinaryQuery. The fix in TableJoin now caps duplicate usage by the number of available input columns, and the TCPHandler change reads the cancel flag under callback_mutex. Added regression coverage for the Merge-table join reproducer looks appropriate. High-level verdict: good, targeted fix.

ClickHouse Rules

Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅

Final Verdict

  • Status: ✅ Approve

alexey-milovidov and others added 2 commits March 21, 2026 12:12
…the input

The previous fix unconditionally deduplicated all column names, which broke
queries with legitimate duplicate columns (e.g., `SELECT number, number FROM
numbers(5)` in a FULL JOIN). Instead, cap the number of times each column
is added to `result_columns_from_left_table` / `columns_added_by_join` to
the number of times it appears in the respective input column list.

This fixes the Merge table case (where `ActionsDAG::getRequiredColumnsNames`
returns spurious duplicates) while preserving correct behavior for queries
that intentionally select duplicate columns.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…yQuery`

`processCancel` writes `state.stop_read_return_partial_result` under
`callback_mutex` (when called from a pipeline executor thread via
`receiveClusterFunctionReadTaskResponse`), but `processOrdinaryQuery`
reads it at line 1441 outside the lock. Read the value into a local
variable under the existing `callback_mutex` lock to eliminate the race.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100018&sha=7c5081d08457af33ca97d7e5d51854ac8cd55c0f&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20parallel%2C%201%2F2%29

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 24.60% 24.60% +0.00%
Branches 76.40% 76.40% +0.00%

PR changed lines: PR changed-lines coverage: 100.00% (45/45, 0 noise lines excluded)
Diff coverage report
Uncovered code

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Okay.

@alexey-milovidov alexey-milovidov self-assigned this Mar 23, 2026
@alexey-milovidov
alexey-milovidov merged commit 1b0e7d9 into master Mar 23, 2026
148 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-duplicate-columns-in-join-getNonJoinedBlocks branch March 23, 2026 20:26
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants