Skip to content

Fix logical error in HashJoin::getNonJoinedBlocks due to overlapping columns - #98286

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-join-overlapping-columns-85622
Feb 28, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-join-overlapping-columns-85622

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Feb 28, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Fix a logical error exception in HashJoin::getNonJoinedBlocks caused by left-table column identifiers appearing in the right plan's header
  • In edge cases (found by AST fuzzer), AddedColumns deduplicates overlapping columns but getNonJoinedBlocks expects the arithmetic sum, causing Unexpected number of columns in result sample block
  • Add an ExpressionStep projection in buildJoinQueryPlan to remove non-key right columns overlapping with left column names before the join algorithm is created

Closes #85622
Closes #86528

Test plan

  • Added test 03993_join_no_overlapping_columns_85622 reproducing the issue
  • Verify test passes in CI (Debug/ASan builds where the assertion fires)
  • Run AST fuzzer suite to confirm no regressions

Changelog category (leave one):

  • CI Fix or Improvement

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 left-table column identifiers overlap with non-key right-side columns.

🤖 Generated with Claude Code

…g columns

In an edge case found by AST fuzzer, a left-table column identifier
(e.g., `__table1.key`) ends up in the right plan's header. When
`HashJoin` splits the right header into keys vs non-keys, this left-side
column lands in `sample_block_with_columns_to_add`. Then `AddedColumns`
deduplicates it (skips right columns matching left column names), but
`getNonJoinedBlocks` expects the arithmetic sum of left + right key +
columns_to_add to equal `result_sample_block` columns, causing:

    Unexpected number of columns in result sample block

The fix adds an `ExpressionStep` projection in `buildJoinQueryPlan` that
removes non-key right columns whose names overlap with left column names,
before the join algorithm is created.

Closes #85622
Closes #86528

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Feb 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cbd7a9c]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Feb 28, 2026
@alexey-milovidov alexey-milovidov self-assigned this Feb 28, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Good.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Feb 28, 2026
Merged via the queue into master with commit a86131b Feb 28, 2026
144 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-join-overlapping-columns-85622 branch February 28, 2026 16:06
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Feb 28, 2026
robot-clickhouse added a commit that referenced this pull request Feb 28, 2026
robot-clickhouse added a commit that referenced this pull request Feb 28, 2026
robot-clickhouse added a commit that referenced this pull request Feb 28, 2026
robot-clickhouse added a commit that referenced this pull request Feb 28, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 28, 2026
@alexey-milovidov alexey-milovidov removed the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Feb 28, 2026
alexey-milovidov added a commit that referenced this pull request Mar 1, 2026
…s-85622

Fix logical error in HashJoin::getNonJoinedBlocks due to overlapping columns
alexey-milovidov added a commit that referenced this pull request Mar 19, 2026
…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]>
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @alexey-milovidov — the changelog category for this PR might need a look.

Current: CI Fix or Improvement
Suggested: Bug Fix (user-visible misbehavior in an official stable release)

Why: The PR fixes a logical error in HashJoin::getNonJoinedBlocks that occurs when there are overlapping column names between left and right tables. This is a user-visible bug fix, not a CI improvement.

Could you verify this is correct? Ignore if the current category is intentional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logical error: 'Unexpected number of columns in result sample block [CI crash] HashJoin operation failed during block processing

4 participants