Skip to content

Fix exception "Column identifier is already registered" in planner - #101048

Merged
alexey-milovidov merged 19 commits into
masterfrom
fix-planner-duplicate-column-identifier
Apr 26, 2026
Merged

alexey-milovidov merged 19 commits into
masterfrom
fix-planner-duplicate-column-identifier

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

During AST fuzzer stress testing, prepareBuildQueryPlanForTableExpression could attempt to register the same column identifier twice when the same table expression was processed multiple times with an empty column list. Use createColumnIdentifierOrGet to avoid the duplicate registration, and guard addColumn with a hasColumn check.

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 "Column identifier is already registered" in the planner that could occur when the same table expression was processed multiple times.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

During AST fuzzer stress testing, `prepareBuildQueryPlanForTableExpression`
could attempt to register the same column identifier twice when the same
table expression was processed multiple times with an empty column list.
Use `createColumnIdentifierOrGet` to avoid the duplicate registration,
and guard `addColumn` with a `hasColumn` check.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Comment thread src/Planner/PlannerContext.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Mar 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [309b75d]

Summary: ✅


AI Review

Summary

This PR fixes a planner LOGICAL_ERROR (Column identifier is already registered) by reusing an already-registered identifier in repeated table-expression planning, and it normalizes identifier formatting for aliased sources. I did not find additional blocker/major issues in the current PR head.

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

Comment thread src/Planner/PlannerContext.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the pr-ci label Mar 28, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Add a test.

alexey-milovidov and others added 2 commits March 29, 2026 01:13
Apply proper quoting with `backQuoteIfNeed` when constructing column
identifiers from source alias and column name, in both
`createColumnIdentifier` and `createColumnIdentifierOrGet`.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The `backQuoteIfNeed` quoting applied to column identifiers in
`createColumnIdentifier` and `createColumnIdentifierOrGet` breaks
column lookup throughout the planner, because these identifiers are
used as internal keys (not SQL identifiers). Names containing dots
(e.g. `nested.key`, `u2.uid`) get backtick-quoted, causing
`NOT_FOUND_COLUMN_IN_BLOCK` exceptions and EXPLAIN output mismatches.

Revert to the original unquoted identifier construction, which is
consistent with how the rest of the planner resolves columns.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Comment thread src/Planner/PlannerContext.cpp Resolved
The planner could throw a LOGICAL_ERROR "Column identifier is already
registered" when `prepareBuildQueryPlanForTableExpression` processed a
table expression multiple times with an empty selected-column list.
This was found by the AST fuzzer during stress testing.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
alexey-milovidov and others added 6 commits March 29, 2026 05:57
Use `backQuoteIfNeed` for both alias and column name when building
column identifiers in `createColumnIdentifier` and `createColumnIdentifierOrGet`
to avoid collisions for quoted names containing dots.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The `backQuoteIfNeed` change in `createColumnIdentifier` causes EXPLAIN
output to backtick-quote column identifiers containing dots, parens, or
numeric names (e.g. `__table1.json.a` → `__table1.\`json.a\``).

Update reference files for affected tests:
- 03275_subcolumns_in_primary_key
- 03302_analyzer_distributed_filter_push_down
- 03375_l2_distance_transposed_partial_reads_pass
- 03405_merge_filter_into_join
- 03445_subcolumns_prewhere_pushdown
- 03786_outer_to_semi_or_anti

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Extract the shared identifier-building logic from `createColumnIdentifier`
and `createColumnIdentifierOrGet` into a static `buildColumnIdentifier`
helper, as suggested in review.

Fix the `03375_l2_distance_transposed_partial_reads_pass` reference file
where the EXPLAIN output truncation point for `cosineDistanceTransposed`
was off by 8 characters (due to backtick-quoted column names making the
line longer).

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

The EXPLAIN output truncation point for the \`L2DistanceTransposed\`
function result name was off — the backtick-quoted column identifiers
make the line longer, so the fixed-width truncation cuts deeper into
the result name.

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

The `backQuoteIfNeed` change in `buildColumnIdentifier` causes EXPLAIN
output to backtick-quote column identifiers containing dots
(e.g. `__table1.json.a` → `__table1.\`json.a\``). This test was missed
in the earlier reference update.

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 added pr-bugfix Pull request with bugfix, not backported by default and removed pr-ci labels Apr 25, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

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

Changed lines: 100.00% (17/17) · 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.

A little bit questionable, but at least the change with quoting is good.

@alexey-milovidov alexey-milovidov self-assigned this Apr 26, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Apr 26, 2026
Merged via the queue into master with commit 6647043 Apr 26, 2026
165 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-planner-duplicate-column-identifier branch April 26, 2026 10:53
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 26, 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: GlobalPlannerContext::createColumnIdentifier and prepareBuildQueryPlanForTableExpression — core code present in all supported branches.

Why: Fix in GlobalPlannerContext::createColumnIdentifier and prepareBuildQueryPlanForTableExpression; src/Planner/PlannerContext.cpp was added in 2022 (commit 44ada63, 'Added Planner'), predating all supported release branches. The exception was reachable in stable releases via AST-fuzzer-style queries (e.g. count_distinct_optimization with QUALIFY referencing the same table expression twice).

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.

3 participants