Skip to content

Fix exception in GlobalPlannerContext::createColumnIdentifier for UNION ALL queries - #101104

Merged
alexey-milovidov merged 23 commits into
masterfrom
fix/planner-column-identifier-union-all
Apr 28, 2026
Merged

alexey-milovidov merged 23 commits into
masterfrom
fix/planner-column-identifier-union-all

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

This PR adds a regression test for an exception that used to be raised by GlobalPlannerContext::createColumnIdentifier for some UNION ALL queries:

Column identifier m is already registered

In UNION ALL queries, all branches share the same GlobalPlannerContext. When two branches referenced a table expression with no alias and the same column name, createColumnIdentifier would throw Column identifier is already registered. This was originally found by the AST Fuzzer.

The underlying issue is already fixed on master by:

So this PR keeps only the regression test (no source code changes) as a guard against future regressions.

Original report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=101023&sha=c1dde15b4607b68f7bbecd414d3d65fe2dc8e088&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29 (#101023)

Changelog category (leave one):

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

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.145

…NION ALL queries

In UNION ALL queries, all branches share the same `GlobalPlannerContext`.
When both branches reference a table expression with no alias and the same
column name, `createColumnIdentifier` would throw "Column identifier is
already registered". This was triggered by the AST fuzzer.

Fix by disambiguating with a numeric suffix instead of throwing.

Found in: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=101023&sha=c1dde15b4607b68f7bbecd414d3d65fe2dc8e088&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29

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

clickhouse-gh Bot commented Mar 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a6dd601]

Summary: ✅


AI Review

Summary

This PR adds a focused stateless regression test for a previously fixed planner exception in UNION ALL handling (Column identifier is already registered). The change is narrow, test-only, and consistent with the stated goal of guarding against recurrence.

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 ✅
No large/binary files ✅
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Mar 29, 2026
alexey-milovidov and others added 2 commits March 30, 2026 02:37
…rContext`

The previous approach of disambiguating with a numeric suffix (`_0`, `_1`, ...) broke
the column identifier mapping — the second UNION ALL branch's `TableExpressionData`
would store identifier `m_0` while the execution layer expected `m`, causing missing rows.

The correct fix is simpler: just return the existing identifier on collision. Each UNION
ALL branch has its own `PlannerContext` with separate `TableExpressionData`, so they can
safely share the same column identifier string.

Also add `ORDER BY` to the test to make output deterministic.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Comment thread src/Planner/PlannerContext.cpp Resolved
alexey-milovidov and others added 6 commits March 30, 2026 08:09
…nstead of reusing

Instead of silently reusing an existing identifier on collision in
`GlobalPlannerContext::createColumnIdentifier`, append a numeric suffix
(e.g. `_0`, `_1`) to maintain uniqueness. This prevents hiding logical
errors while still supporting UNION ALL branches that share a
`GlobalPlannerContext`.

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

Copy link
Copy Markdown
Member Author

Does not reproduce anymore. Let's try to keep the test only.

The bug in `GlobalPlannerContext::createColumnIdentifier` no longer
reproduces on current master, so revert the planner code changes and
keep only the regression test.

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

Copy link
Copy Markdown
Member Author

The flaky check failure is fixed in #102148, let's update the branch.

alexey-milovidov and others added 5 commits April 19, 2026 05:07
The revert was premature — CI (arm_asan_ubsan targeted, and multiple
flaky-check shards) still reproduces "Column identifier m is already
registered" from the regression test added in this PR.

Restore the disambiguation with a numeric suffix. On collision, append
`_0`, `_1`, ... to keep identifiers unique across `UNION ALL` branches
that share a `GlobalPlannerContext`.

Reference CI run:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=101104&sha=09c1177f0af08116d6c26b2486d4d0eb622dc329&name_0=PR&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20targeted%29

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@alexey-milovidov alexey-milovidov self-assigned this Apr 26, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

The bug does not reproduce by the provided test. Was it fixed by any other previous PRs?
If the bug still exists, we must improve the test.
If it is already fixed, remove the code changes, but keep the test just in case.

…identifier-union-all

# Conflicts:
#	src/Planner/PlannerContext.cpp
@@ -0,0 +1,11 @@
-- Reproducer for a bug where UNION ALL branches sharing the same GlobalPlannerContext

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor typo in the test comment: branches sharing reads as ungrammatical here.

Suggested wording:
-- Reproducer for a bug where UNION ALL branches that share the same GlobalPlannerContext

@clickhouse-gh

clickhouse-gh Bot commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.00% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 36.36% (4/11) · Uncovered code

Full report · Diff report

alexey-milovidov and others added 4 commits April 27, 2026 13:02
The original "Column identifier is already registered" exception is fixed
by other PRs that landed on master:

- #101048 ("Fix exception 'Column identifier is already registered' in planner")
- #101051 ("Fix Column identifier is already registered with additional_result_filter in UNION/EXCEPT")

Per the author's review comment on this PR, drop the disambiguation
workaround in `GlobalPlannerContext::createColumnIdentifier` and keep
only the regression test from this PR as a guard.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Reword the comment per review feedback and note that the underlying bug
was fixed by other PRs (#101048, #101051), so this test remains as a
regression guard rather than as a reproducer.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Apr 28, 2026
Merged via the queue into master with commit 143a107 Apr 28, 2026
165 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix/planner-column-identifier-union-all branch April 28, 2026 13:48
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 28, 2026
github-merge-queue Bot pushed a commit that referenced this pull request Jul 9, 2026
…UPDATE with EXISTS

When the mutations analyzer rewrites `exists((subq))` to
`1 IN (SELECT 1 FROM (subq) LIMIT 1)` (the `only_analyze` path), the
resulting subquery tree can reach `buildSubqueryPlansForSetsAndAdd`
with inner source nodes that have no `__tableN` aliases. The planner
then registers `dummy` (or any bare column name) once via
`prepareBuildQueryPlanForTableExpression` for the wrapping `QueryNode`
and again via `CollectSourceColumnsVisitor` for the inner `TableNode`,
producing the LOGICAL_ERROR.

Re-running `createUniqueAliasesIfNecessary` on the detached subquery
tree before planning guarantees every table expression has a unique
alias, so the two registrations produce distinct identifiers like
`__table1.dummy` and `__table2.dummy`.

Adds a regression test that reproduces the original AST-fuzzer query
(STID 4697-4326) from issue #104877. The prior fix family
(#100770, #101048, #101051, #101104) addressed the same error class
for UNION ALL / additional_result_filter SELECT paths but did not
cover the mutations + EXISTS pattern.

Issue: #104877
STID: 4697-4326
Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=3120048c80b01b65ef75858d34e17b669505de6a&name_0=MasterCI&name_1=AST%20fuzzer%20%28arm_asan_ubsan%29
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…UPDATE with EXISTS

When the mutations analyzer rewrites `exists((subq))` to
`1 IN (SELECT 1 FROM (subq) LIMIT 1)` (the `only_analyze` path), the
resulting subquery tree can reach `buildSubqueryPlansForSetsAndAdd`
with inner source nodes that have no `__tableN` aliases. The planner
then registers `dummy` (or any bare column name) once via
`prepareBuildQueryPlanForTableExpression` for the wrapping `QueryNode`
and again via `CollectSourceColumnsVisitor` for the inner `TableNode`,
producing the LOGICAL_ERROR.

Re-running `createUniqueAliasesIfNecessary` on the detached subquery
tree before planning guarantees every table expression has a unique
alias, so the two registrations produce distinct identifiers like
`__table1.dummy` and `__table2.dummy`.

Adds a regression test that reproduces the original AST-fuzzer query
(STID 4697-4326) from issue ClickHouse#104877. The prior fix family
(ClickHouse#100770, ClickHouse#101048, ClickHouse#101051, ClickHouse#101104) addressed the same error class
for UNION ALL / additional_result_filter SELECT paths but did not
cover the mutations + EXISTS pattern.

Issue: ClickHouse#104877
STID: 4697-4326
Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=3120048c80b01b65ef75858d34e17b669505de6a&name_0=MasterCI&name_1=AST%20fuzzer%20%28arm_asan_ubsan%29
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