Skip to content

Fix Column identifier is already registered with additional_result_filter in UNION/EXCEPT - #101051

Merged
alexey-milovidov merged 12 commits into
masterfrom
fix-column-identifier-additional-result-filter
Apr 25, 2026
Merged

alexey-milovidov merged 12 commits into
masterfrom
fix-column-identifier-additional-result-filter

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

When multiple UNION/EXCEPT branches share the same GlobalPlannerContext and each applies additional_result_filter, the addAdditionalFilterStepIfNeeded function creates a fake TableNode without an alias for each branch. Both branches try to register the same bare column identifier (e.g. a) in the shared context, causing the second to throw a LOGICAL_ERROR exception.

Fix by giving each fake table expression a unique alias so that column identifiers become unique across branches.

Fixes #99931

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100947&sha=d0c5ef2315e4d001a6d34cf2e54a9ee2b176a538&name_0=PR&name_1=Stress%20test%20%28amd_debug%29
#100947

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix LOGICAL_ERROR exception "Column identifier is already registered" when additional_result_filter setting is used with UNION or EXCEPT queries.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.81

…_filter` in UNION/EXCEPT

When multiple UNION branches share the same `GlobalPlannerContext` and each
applies `additional_result_filter`, the `addAdditionalFilterStepIfNeeded`
function creates a fake `TableNode` without an alias for each branch. Both
branches try to register the same bare column identifier (e.g. "a") in the
shared context, causing the second to throw a LOGICAL_ERROR.

Fix by giving each fake table expression a unique alias so that column
identifiers become unique across branches (e.g. "_additional_result_filter_0.a"
and "_additional_result_filter_1.a").

Fixes #99931
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100947&sha=d0c5ef2315e4d001a6d34cf2e54a9ee2b176a538&name_0=PR&name_1=Stress%20test%20%28amd_debug%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 [a8fc225]

Summary: ✅


AI Review

Summary

This PR fixes the additional_result_filter identifier collision in UNION/EXCEPT planning by assigning collision-resistant synthetic aliases in addAdditionalFilterStepIfNeeded. The code change is targeted and correct, but the current regression test does not cover the user-alias collision scenario that motivated the second commit.

Findings

⚠️ Majors

  • [tests/queries/0_stateless/04064_additional_result_filter_union_column_identifier.sql:9] The regression test validates regular UNION/EXCEPT usage but does not test the specific collision-resistant alias path (for example when user input already uses _additional_result_filter_0).
    Suggested fix: add a case with an explicit _additional_result_filter_0-style alias and verify additional_result_filter does not raise Column identifier ... is already registered exception.
Tests
  • ⚠️ Add a regression case that intentionally introduces a user alias colliding with _additional_result_filter_N to verify the new collision-avoidance loop in planner logic.
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: ⚠️ Request changes
  • Minimum required actions:
    • Add a regression test covering a deliberate _additional_result_filter_N alias collision case.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 29, 2026
Comment thread src/Planner/Planner.cpp Outdated
/// When multiple UNION branches share the same GlobalPlannerContext and each applies additional_result_filter,
/// the bare column names (e.g. "a") would collide. Give the fake table expression a unique alias
/// so that identifiers become unique (e.g. "_additional_result_filter_0.a").
auto unique_alias = "_additional_result_filter_" + std::to_string(planner_context->getGlobalPlannerContext()->nextUniqueId());

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.

⚠️ This alias is deterministic and can still collide with real query aliases. For example, if a user has a source alias _additional_result_filter_0 and a projected column a, GlobalPlannerContext::createColumnIdentifier can still throw Column identifier ... is already registered for _additional_result_filter_0.a.

Please make the synthetic alias collision-resistant against existing identifiers in the shared context, e.g. loop over generated aliases until alias + "." + projection_column does not exist in GlobalPlannerContext (or use a truly unique suffix that cannot appear in user aliases).

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

alexey-milovidov and others added 6 commits April 6, 2026 17:49
The synthetic alias `_additional_result_filter_N` used in
`addAdditionalFilterStepIfNeeded` was deterministic and could collide
with a real user alias. Now loop over candidate aliases, checking each
against existing column identifiers in `GlobalPlannerContext`, until
a collision-free one is found.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
CREATE TABLE t_04064 (x UInt64) ENGINE = MergeTree ORDER BY x;
INSERT INTO t_04064 VALUES (1), (2), (3);

SELECT * FROM (SELECT x AS a FROM t_04064)

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.

⚠️ This regression test covers UNION/EXCEPT with regular aliases, but it does not exercise the collision scenario fixed in the second commit (user alias like _additional_result_filter_0).

Please add a case where a branch explicitly uses _additional_result_filter_0 (or similar) as a table/query alias and verifies additional_result_filter works without Column identifier ... is already registered exception.

@clickhouse-gh

clickhouse-gh Bot commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.20% 76.30% +0.10%

Changed lines: 83.33% (15/18) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Apr 25, 2026
Merged via the queue into master with commit 1e20a7f Apr 25, 2026
165 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-column-identifier-additional-result-filter branch April 25, 2026 12:54
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 25, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this PR may need backporting to 26.3 (LTS), 26.2, 26.1, 25.8 (LTS), but no backport label was found.

Affected code: addAdditionalFilterStepIfNeeded in Planner.cpp and GlobalPlannerContext in PlannerContext.h — core code present in all supported branches.

Why: The affected function addAdditionalFilterStepIfNeeded was introduced in PR #48405 (merged 2023-04-12), which predates all supported branches. The bug causes a LOGICAL_ERROR exception on valid SQL using additional_result_filter with UNION/EXCEPT/INTERSECT, affecting all supported releases.

If this should be backported, consider adding pr-must-backport or a version-specific label (e.g. v26.3-must-backport). Ignore this if backporting is not applicable.

alexey-milovidov added a commit that referenced this pull request Apr 27, 2026
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]>
alexey-milovidov added a commit that referenced this pull request Apr 27, 2026
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]>
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-bugfix Pull request with bugfix, not backported by default 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: Column identifier A is already registered (STID: 4697-4326)

4 participants