Repository navigation
Fix views and mutations reading a table instead of an alias declared inside a WITH expression - #123774
Fix views and mutations reading a table instead of an alias declared inside a WITH expression#123774groeneai wants to merge 6 commits into
WITH expression#123774Conversation
…ryVisitor `ApplyWithSubqueryVisitor` registered as a substitutable literal only the alias of a direct `WITH` child, so in `WITH tuple([7] AS nested) AS wrapper` it registered `wrapper` but not `nested`. A nested `SELECT` using `7 IN nested` then kept the identifier, and the full `AddDefaultDatabaseVisitor` traversal, which treats every name left in place as a table, qualified it as `<db>.nested`. A view, a materialized view, `MODIFY QUERY` and `ALTER TABLE ... UPDATE` with that shape read a same-named table instead of the alias, or failed with `UNKNOWN_IDENTIFIER` (reproduced on 26.8.2.7 and 26.3.33.24). The narrow pass that repairs a definition after SQL UDF expansion already takes the aliases at any depth of a `WITH` expression, not inside a lambda or a subquery, like the analyzer. A `Replicated` database writes that repaired text into the DDL log and every replica processes it again through the full pipeline, which re-qualified the alias. This made `05237_with_expression_alias_scope_descendant` fail on every run under `--replicated-database`. Register every such alias right after the expression is visited, as the direct child's alias was before, so it is inlined (`7 IN ([7] AS nested)`) the way a top-level alias has been since ClickHouse#100042. The rule now lives in one helper, `ApplyWithSubqueryVisitor::forEachWithExpressionAlias`, which the narrow pass also calls. The test gets back the mutation arms removed in the ClickHouse#114908 rework, each on its own row, and a hand-written view arm. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…y are visited An alias inherited from an enclosing `WITH` was substituted even into a nested select that declares the same alias itself, where the analyzer resolves the local one. A non-CTE `WITH` child was visited as a whole before its aliases were registered, so a later part of the same expression did not see one. Test arms: a materialized view and `MODIFY QUERY`, a later part of the same `WITH` expression, and nested selects that shadow a descendant or a top-level alias. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…TH` alias A nested select's `ARRAY JOIN` and `JOIN ... ON` aliases were not counted as its own, so an inherited alias still replaced them. A lambda body got the inherited aliases unchanged, so one was substituted over a lambda parameter or a body alias of the same name. Add a stack check to the recursive alias walk. Test arms: `ARRAY JOIN`, `JOIN ... ON`, a lambda parameter and a lambda-body alias that bind the same name. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The analyzer puts the aliases inside a table function's arguments into the select scope, so `ApplyWithSubqueryVisitor` now counts them among a nested select's own aliases, and `AddDefaultDatabaseVisitor::collectAliases` descends into those arguments (not into the table alias) so the dropped name stays bare. Both must change together, otherwise the name is either inlined or qualified as a table. 05237 gains a table function shadow arm. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The analyzer stops collecting aliases at a lambda, also inside a table function argument, but `collectAliases` walked into it there. A lambda-body alias then left an unrelated table name bare in a stored view, so the view read that name from the reader's current database. Take the argument aliases with `forEachExpressionAlias`, as the visitor does. Adds a test arm read through `USE system`, and moves the own-alias scan above the comment that describes the `scope_data` block. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop a comment that restates its call and shorten the visitWithExpression comment to one line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 9 findings over 5 rounds).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261003-221100 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261003-204800 |
|
Workflow [PR], commit [83a4678] AI ReviewSummaryThis PR fixes a real class of persisted-query alias-resolution bugs around descendant aliases inside Findings
Tests
Final VerdictChanges required. |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
CREATE VIEW,CREATE MATERIALIZED VIEW,ALTER TABLE ... MODIFY QUERYandALTER TABLE ... UPDATEtreating an alias declared inside aWITHexpression (for exampleWITH tuple([7] AS s) AS w) as a table name when a nestedSELECTuses it on the right side ofIN: the statement failed withUNKNOWN_IDENTIFIER, or read a table with the same name instead of the alias.Description
On 26.8 and 26.3,
CREATE VIEW v AS WITH tuple([7] AS s) AS w SELECT (SELECT toUInt8(7 IN s)) AS rstoressas<db>.s. Without a tablesit fails withUNKNOWN_IDENTIFIER; with one, the view returns 0 while the same query run directly returns 1. A materialized view,MODIFY QUERYandALTER TABLE ... UPDATEbehave the same.On master this also fails
05237_with_expression_alias_scope_descendanton every run under aReplicateddatabase (failing job): replay reprocesses the definition the DDL log keeps after SQL UDF expansion.Root cause:
ApplyWithSubqueryVisitorregisters only the alias of a directWITHchild (w, nots), and the fullAddDefaultDatabaseVisitortraversal then qualifies every name left in place. The narrow pass after SQL UDF expansion (#114908) already follows the analyzer.Change:
ApplyWithSubqueryVisitorregisters every alias aWITHexpression declares at any depth, except inside a lambda or a subquery, as soon as its node is visited, and inlines it (7 IN ([7] AS s)), as it has inlined a top-level alias since #100042. A name a nestedSELECTor a lambda declares itself wins, also for a top-level alias. The narrow pass uses the same helper.AddDefaultDatabaseVisitornow also counts an alias inside a table function argument as the select's own, as the analyzer does, also withoutWITH. As for a top-level alias, a use before the declaration is not inlined, andCREATE VIEW/MODIFY QUERYexpand without a context, soenable_global_with_statement = 0in a nestedSELECTdoes not stop the inlining there.Validation: 05237 gets back the mutation arms, plus view, materialized view,
MODIFY QUERY, same-expression and shadowing arms; master fails 8 of them. 50/50 randomized runs, 10/10 under--replicated-database, no new failures among 595 neighbour tests.Overlaps textually with open #120778, #122362 and #120089.
Workflow [PR]
Sync PR [sync-upstream/pr/123774]