Repository navigation
Fix LOGICAL_ERROR "Column identifier is already registered" in ALTER UPDATE with EXISTS - #106025
Conversation
…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
|
cc @CurtizJ @novikd — could you review this? Fix for chronic AST-fuzzer error "Column identifier is already registered" (STID |
|
Pre-PR validation gate (session a) Deterministic repro? Yes. Minimal command (debug build): b) Root cause explained? Yes. In c) Fix matches root cause? Yes. Re-running d) Test intent preserved? / New test added? Yes. New regression test e) Demonstrated in both directions? Yes.
f) Fix is general, not a narrow patch? Yes. The fix targets
Walked the callers of |
|
Workflow [PR], commit [0c72215] Summary: ✅ AI ReviewSummaryThis PR is now a test-only change: it adds Final Verdict
|
The fix re-runs `createUniqueAliasesIfNecessary` on detached subquery trees inside the new analyzer's `Planner` path. The old analyzer takes a different code path (`TreeRewriter::collectUsedColumns`) and rejects the AST-fuzzer query shape (`SELECT DISTINCT *, currentDatabase() > database`) with `UNKNOWN_IDENTIFIER` before any planner step runs, so the bug cannot manifest there and the test cannot meaningfully run on the old analyzer. CI failure on the old-analyzer variant: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106025&sha=0d93b2f6bd9ece61ca2aef4f2c2a153f41860377&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20old%20analyzer%2C%20s3%20storage%2C%20DatabaseReplicated%2C%20WasmEdge%2C%20parallel%29 Also rewords the test header's `LOGICAL_ERROR` reference to "the exception", per the project rule that logical errors do not crash the server in release builds.
|
Pushed Why this is correct, not a band-aid:
For reference, the other three failing checks on the current commit are not caused by this PR:
Pre-PR validation gate for this follow-up commit: (a) Deterministic repro of the original failure: confirmed by the CI log linked above; query 9 ( |
|
@groeneai, why does the bugfix validation check fail to reproduce it? |
|
@alexey-milovidov The test does reproduce the bug on The reason that did not surface as "bug reproduced" is a 30-hour timing gap. PR #105643 widened I will merge |
…mutations-column-identifier
…h 04300_deltalake_ffi_panic_guard from master
|
Should we allow correlated subqueries in mutations at all? |
|
@novikd Good question, and I want to be precise about what this PR does and does not do. This PR does not add support for correlated subqueries in mutations. It fixes a server crash ( ALTER TABLE t UPDATE v = 0 WHERE exists((SELECT * FROM system.one)) SETTINGS mutations_sync = 2;
ALTER TABLE t UPDATE v = 1 WHERE NOT exists((SELECT * FROM system.numbers WHERE number > 1000000 LIMIT 1)) SETTINGS mutations_sync = 2;The analyzer rewrites If the team's preference is that such subqueries (correlated or not) should not be allowed in mutations at all, that is a separate decision. I am happy to instead implement a clean analyzer-level rejection that produces a proper user error rather than a crash. Either direction removes the abort. I just want to confirm which one you want before I touch it. @KochetovNicolai Understood on the temporary block. Could you confirm what unblocks this: fix the crash as written here, or reject these queries with a clean error in the analyzer? I will follow whichever you and @novikd prefer. |
|
@groeneai, please deprecate correlated subqueries in mutations. |
Per @KochetovNicolai and @novikd review on PR ClickHouse#106025: correlated subqueries in `ALTER UPDATE` / `ALTER DELETE` are deprecated. Non-correlated `EXISTS` continues to work via the existing `createUniqueAliasesIfNecessary` fix in `buildSubqueryPlansForSetsAndAdd`. A new in-depth visitor walks the resolved mutation expression right after `QueryAnalyzer::resolve` and throws `NOT_IMPLEMENTED` on any `QueryNode` / `UnionNode` that has correlated columns. This produces a clean user error instead of letting the query reach the planner where it would previously raise the `Logical error: 'Column identifier ... is already registered'` exception (and abort the server in debug / sanitizer builds). Test extends `04303_stid_4697_4326_mutation_exists_column_identifier` with three correlated cases that must error with `NOT_IMPLEMENTED`: correlated `EXISTS` over `system.one`-style subquery, correlated `EXISTS` over `numbers(10)`, and correlated `EXISTS` inside `ALTER DELETE`. Existing non-correlated `EXISTS` and `NOT EXISTS` cases are kept and continue to pass. PR-relatedness: pivot of PR ClickHouse#106025 in response to the team's preferred direction (deprecate vs. fix-the-crash). The IN-function gate at `src/Analyzer/Resolve/resolveFunction.cpp:1139` already covers correlated `IN` arguments globally; this change adds the parallel mutation-scope gate for `EXISTS` and any other correlated subquery shape that survives analyzer rewrites.
|
@KochetovNicolai @novikd Pivoted in 6a7a646. Behavior:
Test
Existing non-correlated Re the carve-out you mentioned ("Technically, we can allow correlated subqueries within scalar subqueries or |
Pre-PR validation gate
Session id: cron:clickhouse-ci-task-worker:20260604-135400 |
|
@novikd I built and tested both halves of this before touching the branch, and the result changes the picture: 1. #106414 is a non-correlated crash fix, not correlated-subquery support. Its only code change is one That query is not correlated (the inner 2. Correlated subqueries in mutations are already rejected on master, independent of #106414. 3. Consequence: my analyzer-scope gate (the visitor at :184) is redundant with that Given that, the revert as written is not what we want. The coherent options:
Reverting #106414's |
…ions Per @novikd's review (ClickHouse#106025 "remove the code added in ClickHouse#106414 (keep the test)") and the unresolved thread asking to reuse `traverseQueryTree` instead of a new visitor. Both halves of this PR's earlier code are now redundant with `master`: * The non-correlated `EXISTS` crash-fix (`createUniqueAliasesIfNecessary` on the detached subquery) landed independently on `master` via ClickHouse#106414 (`MutationsInterpreter.cpp`). * Correlated subqueries in mutations are already rejected with `NOT_IMPLEMENTED` on `master` by `correlated_subtrees.assertEmpty("in mutation filter"/"in mutation update")` in `MutationsInterpreter::prepareMutationStages`. This PR's `ValidateNoCorrelatedSubqueryVisitor` therefore added no behavior over `master`; `Bugfix validation` confirms the regression test `04303_stid_4697_4326_mutation_exists_column_identifier` already passes on the unfixed base, so every case it covers is handled by the existing `master` code. Restore `MutationsInterpreter.cpp` to `master` and keep only the regression test. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Reduced this PR to test-only and merged fresh What changed
Verification (master-behavior binary,
Output matches the One open decision for a human, @alexey-milovidov / @novikd: I did not revert #106414 itself. As demonstrated earlier (and reproduced here), #106414 is the non-correlated |
|
CI finished on HEAD The 3 remaining reds are all unrelated flakes — this PR adds only a stateless SQL test (no C++/Keeper changes):
@groeneai, could you confirm/track the |
|
@alexey-milovidov Confirmed and tracked. Root cause (stdout was correct on that run; only stderr tripped the harness): at Fix: #107438 ("Do not log benign ENOTCONN on socket shutdown during ZooKeeper finalize") downgrades exactly this log to TRACE at both CIDB (30d, No separate PR needed here. #106025 is good to merge once the flaky reds are re-run/overridden. |
CI finish ledger — a22dc54CI fully finished (Finish Workflow: success). This PR is test-only (ALTER UPDATE column-identifier regression), APPROVED by @ alexey-milovidov + @ KochetovNicolai. Every failure has an owner; none is PR-caused.
Session id: cron:our-pr-ci-monitor:20260630-163000 |
CI finish ledger — a22dc54Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
This PR's diff is test-only ( Session id: cron:our-pr-ci-monitor:20260701-043000 |
…el merge TSan race fix, STID 1003-358c)
CI finish ledger — 75ede47CI fully finished on head
Session id: cron:our-pr-ci-monitor:20260708-173000 |
LLVM Coverage Report
Changed lines: No C/C++ source files changed — skipping uncovered code analysis. Newly covered by added/modified tests: 117 line(s), 9 function(s) across 43 file(s) · Details Top files
|
CI finish ledger — 0c72215Clean CI. The master-merge that picked up #109389 (uniqExact two-level parallel-merge TSan fix) resolved the prior head's sole failure. No failures on this head; nothing PR-caused.
Prior head 75ede47 carried Stress test (arm_tsan) STID 1003-358c (UniqExactSet::merge TSan data race, chronic trunk, not PR-caused), owned by #109389 (ours, merged 2026-07-07). Merging master to pick it up cleared it — 0 recurrences on this head. Session id: cron:our-pr-ci-monitor:20260709-000000 |
Backport #106025 to 26.6: Fix LOGICAL_ERROR "Column identifier is already registered" in ALTER UPDATE with EXISTS
Per @KochetovNicolai and @novikd review on PR ClickHouse#106025: correlated subqueries in `ALTER UPDATE` / `ALTER DELETE` are deprecated. Non-correlated `EXISTS` continues to work via the existing `createUniqueAliasesIfNecessary` fix in `buildSubqueryPlansForSetsAndAdd`. A new in-depth visitor walks the resolved mutation expression right after `QueryAnalyzer::resolve` and throws `NOT_IMPLEMENTED` on any `QueryNode` / `UnionNode` that has correlated columns. This produces a clean user error instead of letting the query reach the planner where it would previously raise the `Logical error: 'Column identifier ... is already registered'` exception (and abort the server in debug / sanitizer builds). Test extends `04303_stid_4697_4326_mutation_exists_column_identifier` with three correlated cases that must error with `NOT_IMPLEMENTED`: correlated `EXISTS` over `system.one`-style subquery, correlated `EXISTS` over `numbers(10)`, and correlated `EXISTS` inside `ALTER DELETE`. Existing non-correlated `EXISTS` and `NOT EXISTS` cases are kept and continue to pass. PR-relatedness: pivot of PR ClickHouse#106025 in response to the team's preferred direction (deprecate vs. fix-the-crash). The IN-function gate at `src/Analyzer/Resolve/resolveFunction.cpp:1139` already covers correlated `IN` arguments globally; this change adds the parallel mutation-scope gate for `EXISTS` and any other correlated subquery shape that survives analyzer rewrites.
…ions Per @novikd's review (ClickHouse#106025 "remove the code added in ClickHouse#106414 (keep the test)") and the unresolved thread asking to reuse `traverseQueryTree` instead of a new visitor. Both halves of this PR's earlier code are now redundant with `master`: * The non-correlated `EXISTS` crash-fix (`createUniqueAliasesIfNecessary` on the detached subquery) landed independently on `master` via ClickHouse#106414 (`MutationsInterpreter.cpp`). * Correlated subqueries in mutations are already rejected with `NOT_IMPLEMENTED` on `master` by `correlated_subtrees.assertEmpty("in mutation filter"/"in mutation update")` in `MutationsInterpreter::prepareMutationStages`. This PR's `ValidateNoCorrelatedSubqueryVisitor` therefore added no behavior over `master`; `Bugfix validation` confirms the regression test `04303_stid_4697_4326_mutation_exists_column_identifier` already passes on the unfixed base, so every case it covers is handled by the existing `master` code. Restore `MutationsInterpreter.cpp` to `master` and keep only the regression test. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…-mutations-column-identifier Fix LOGICAL_ERROR "Column identifier is already registered" in ALTER UPDATE with EXISTS
Adds a regression test for the chronic AST-fuzzer
LOGICAL_ERROR"Column identifier ... is already registered" (STID4697-4326) on the mutations +EXISTSpath, and for the rejection of correlated subqueries in mutations.The two code fixes this test guards have since landed on
masterindependently, so this PR is now test-only (per @novikd's review: "remove the code added in #106414, keep the test"; correlated subqueries should not be allowed in mutations):EXISTSover a real source table no longer aborts the server. ThecreateUniqueAliasesIfNecessarycall on each detached subquery inMutationsInterpreter(added by Fix logical error "Column identifier ... is already registered" in mutations #106414) gives the inner source nodes unique__tableNaliases, sobuildColumnIdentifierno longer registers the same bare column name twice.ALTER UPDATE/ALTER DELETEare rejected with a cleanNOT_IMPLEMENTEDbycorrelated_subtrees.assertEmpty("in mutation filter"/"in mutation update")inMutationsInterpreter::prepareMutationStages.The earlier
ValidateNoCorrelatedSubqueryVisitorgate that this PR carried added no behavior overmaster(every case in the test is already handled by the two pieces above —Bugfix validationconfirms the test passes on the unfixed base), so it was removed andMutationsInterpreter.cppis restored tomaster.The test
04303_stid_4697_4326_mutation_exists_column_identifiercovers:EXISTSoversystem.oneandNOT EXISTSoversystem.numbers(must run, no crash);DynamicshapeUPDATE dynamic = NULL WHERE exists((SELECT DISTINCT *, currentDatabase() > database));UPDATE/DELETEwithEXISTSreferencing an outer column) that must error withNOT_IMPLEMENTED;id IN (... JOIN ...)shape with two subquery-wrapped derived tables that both projectid.It is tagged
no-old-analyzer: the bug class lives in the new analyzer'sEXISTSrewrite; the old analyzer rejects these shapes with a different error before the planner runs.Closes: #106649
Related: #106414
CI 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
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Add a regression test for
LOGICAL_ERROR"Column identifier is already registered" on theALTER UPDATE/ALTER DELETE+EXISTSpath and forNOT_IMPLEMENTEDrejection of correlated subqueries in mutations. The fixes themselves are already onmaster.Documentation entry for user-facing changes
Version info
26.7.1.732(included in26.7and later)26.6.2.58