Skip to content

Fix exception in join order optimizer when column names overlap - #100401

Merged
alexey-milovidov merged 9 commits into
masterfrom
fix-join-optimizer-overlapping-names
Apr 7, 2026
Merged

alexey-milovidov merged 9 commits into
masterfrom
fix-join-optimizer-overlapping-names

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

Linked issues

Fix exception in join order optimizer when left and right join headers contain overlapping internal column names (e.g. both sides produce __table2). This can happen when scalar subquery results and join table aliases collide in complex fuzzed queries. The optimizer now skips reordering in this case, falling back to the unoptimized join which handles overlapping names correctly.

Closes #89166

AST fuzzer report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100398&sha=d69b29735a13a4e8b1c0b95f263a4d962a8943da&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29
#100398

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.593
  • Backported to: 26.3.14.27

The `JoinExpressionActions` constructor throws a LOGICAL_ERROR when left
and right join headers contain overlapping column names. This can happen
when scalar subquery results and join table aliases collide in complex
queries (e.g. both sides produce a column named `__table2`).

Skip the join order optimization in this case, falling back to the
unoptimized join which handles overlapping names correctly.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100398&sha=d69b29735a13a4e8b1c0b95f263a4d962a8943da&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29

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

clickhouse-gh Bot commented Mar 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f94e1ac]

Summary: ✅


AI Review

Summary

This PR fixes a real correctness issue in join-order optimization: when left/right headers have overlapping internal column names, reordering could throw a LOGICAL_ERROR. The patch adds a defensive overlap check and safely falls back to non-reordered join execution, plus a focused regression test reproducing the fuzzer case. I did not find correctness, safety, concurrency, performance, or ClickHouse-rule violations in the submitted diff.

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

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 23, 2026
@alexey-milovidov alexey-milovidov added pr-not-for-changelog This PR should not be mentioned in the changelog and removed pr-bugfix Pull request with bugfix, not backported by default labels Mar 23, 2026
alexey-milovidov and others added 7 commits March 23, 2026 01:34
The test needs the exact fuzzed query with the original table schema
(including LowCardinality types) to reproduce the internal column name
collision. Removed serverError expectation since LOGICAL_ERROR cannot
be used in tests (it aborts in debug builds).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The join order optimizer is a new-analyzer feature, and the fuzzed query
causes stack overflow in `ApplyWithSubqueryVisitor` with the old analyzer.

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

Copy link
Copy Markdown
Member Author

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@clickhouse-gh

clickhouse-gh Bot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.10% +0.10%
Functions 90.90% 90.90% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 78.95% (15/19) · Uncovered code

Full report · Diff report

@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.

Good.

@alexey-milovidov alexey-milovidov self-assigned this Apr 7, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Apr 7, 2026
Merged via the queue into master with commit 2b40b8d Apr 7, 2026
159 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-join-optimizer-overlapping-names branch April 7, 2026 14:46
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 7, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Apr 19, 2026
… names

The join order optimizer's `JoinExpressionActions` constructor enforces
`input_nodes.size() == left_header.columns() + right_header.columns()`.
When a child plan produces an output header with duplicate column names
(for example from `SELECT 1 AS x, 2 AS y, x, y QUALIFY 7`, or when a
correlated scalar subquery preserves pre-projection duplicates),
`foldActionsByProjection` deduplicates INPUT nodes by name in the folded
DAG, so the count invariant fails and the exception
`Input nodes size mismatch in dag` is raised inside
`optimizeJoinLogicalImpl` → `chooseJoinOrder`.

PR ClickHouse#100401 already skips reordering when left and right headers share
names (`Left and right columns have same names`). This change extends the
same early-exit to duplicates that appear *within* one side. Both are
situations that the reconstruction cannot represent, so skipping the
optimization preserves the original plan and avoids the exception.

Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=2892de442814d1e629f7ea1e25ed88d7c6441297&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_debug%29
STID: 4752-593c
@watson-ch

watson-ch Bot commented May 19, 2026

Copy link
Copy Markdown

Backport labels have been automatically added based on https://github.com/ClickHouse/clickhouse-core-incidents/issues/1740

Labels added:

  • v25.12-must-backport
  • v26.2-must-backport

These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels.

@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label May 19, 2026
robot-ch-test-poll2 added a commit that referenced this pull request May 19, 2026
Cherry pick #100401 to 26.2: Fix exception in join order optimizer when column names overlap
robot-clickhouse added a commit that referenced this pull request May 19, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label May 19, 2026
@fm4v fm4v removed the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jun 12, 2026
robot-clickhouse added a commit that referenced this pull request Jun 12, 2026
Cherry pick #100401 to 26.3: Fix exception in join order optimizer when column names overlap
robot-clickhouse added a commit that referenced this pull request Jun 12, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jun 12, 2026
alexey-milovidov added a commit that referenced this pull request Jun 12, 2026
Backport #100401 to 26.3: Fix exception in join order optimizer when column names overlap
@watson-ch watson-ch Bot added v26.6-must-backport and removed pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Nov 23, 2026
@watson-ch

watson-ch Bot commented Nov 23, 2026

Copy link
Copy Markdown

Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/1740

Labels added:

  • v26.6-must-backport

These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels.

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 23, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo v25.12-must-backport v26.2-must-backport v26.6-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logical Error: Left and right columns have same names

5 participants