Repository navigation
Analyzer: throw CYCLIC_ALIASES instead of hanging when an ALIAS column reads itself - #129640
alexey-milovidov merged 4 commits into
Conversation
…expression is resolved An ALIAS column whose expression reads the column itself through a subcolumn, e.g. `c0 Nullable(IPv6) ALIAS c0.null`, made a SELECT of it run until the server was killed. On 26.10 such a column is accepted by CREATE and ALTER; older releases reach it through ATTACH and table function structures (format, remote). It was found by La Casa Del Dolor on master: a read through clusterAllReplicas of a refreshable materialized view with such a column OOM-killed the server. ALIAS expressions are resolved lazily (ClickHouse#123926): tryGetColumnNode erases the column from unresolved_alias_columns and calls the resolver, which sets the node's expression and resolves it against the same table data. Resolving `c0.null` calls tryGetSubcolumnInfo -> tryGetColumnNode("c0"), which no longer finds the column unresolved and returns the node that is being resolved; wrapExpressionNodeInSubcolumn then makes getSubcolumn(<c0 node>, 'null') part of that node's own expression. The query tree is cyclic, and validateTreeSize, an iterative DFS that memoizes a node only after its children, grows its stack without bound. Analysis is not cancellable, so max_execution_time, max_memory_usage, max_server_memory_usage and KILL QUERY do not stop it. A direct cycle (`c0 ALIAS c0`) overflows the stack in a recursive walker instead (TOO_DEEP_RECURSION). The resolver now pushes the column name to AnalysisTableExpressionData::alias_columns_in_resolution before building the expression and pops it on scope exit, and tryGetColumnNode throws CYCLIC_ALIASES with the chain when a requested column is in that list. The resolver captures the original data, which is also the alias resolution data of every re-entrant lookup, so the check also works when a copy of the data started the resolution (the JOIN USING projection alias path); that is why it precedes the unresolved_alias_columns branch. Every cycle (through subcolumns, other ALIAS columns, functions) comes back through this lookup, so tables, Merge, remote/cluster*, format, materialized views and stored definitions are all covered. The definition-time check for such columns is ClickHouse#124850. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n throws The CYCLIC_ALIASES check left the column half-resolved: tryGetColumnNode erased it from unresolved_alias_columns before resolving, and the resolver had already set the unresolved expression when the check threw. Constant if/multiIf folding catches errors of the dead branch, so in `SELECT if(0, c0, NULL), c0` the second reference got a node whose expression was the unresolved identifier `c0.null` and the query failed with UNSUPPORTED_METHOD instead of CYCLIC_ALIASES. Now the resolver resets the expression unless it reached its end, and the column is erased from unresolved_alias_columns only after the resolver returns, so a later reference resolves it again and fails the same way. The test gains the if/multiIf retries plus the format() and JOIN USING shapes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (click to expand)
Re-review of the fix: no findings. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261105-225800 |
|
cc @PedroTadim @novikd, could you review this and add the |
|
Workflow [PR], commit [0d4643e] Summary: ✅
AI ReviewSummaryReviewed #129640 at The new regression test passes in the fast-test report for this head. Focused local checks with the same head's binary also confirm the lambda and inner-alias retry cases succeed. Final Verdict✅ Approve — no new inline findings. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Amazing finding, thank you!
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes7 object files changed (+40.52 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units14 translation units recompiled, 65 s compile time in total, 14 of them have a recent master baseline. |
…olves an ALIAS column `tryGetSubcolumnInfo` and `tryResolveIdentifierAsNestedPrefix` called `tryGetColumnNode`, which resolves the expression of an ALIAS column, for every candidate before checking its type. When the candidate was an ALIAS column whose resolution was already in progress, the new cycle check threw `CYCLIC_ALIASES`, although the probe would have discarded the column. Valid definitions that read 7 / 2 / 5 before this PR and threw afterwards: `t UInt8 ALIAS t.x` in table `t` (table-qualified column), `a UInt8 ALIAS t.x, t UInt8 ALIAS a`, `n.a UInt64 ALIAS length(n)` next to `n.b Array(UInt8)` (Nested prefix), and `a String ALIAS toString(a.b.c)` next to a `a.b JSON` column. The last two are accepted by CREATE TABLE. The type of a column node is its declared type, so both probes now check it on the node from `getColumnNodeMap` and call `tryGetColumnNode` only for a column they return. A probe that does return the column still reaches the cycle check, so the real cycles keep throwing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Fixed in 8133b27: subcolumn and |
…lumn Since 8cdb747 a failed resolution of an ALIAS column leaves the column unresolved, and the next lookup resolves it again. State of the failed attempt survived into that retry in two places: - `resolveLambda` erased the lambda from `lambdas_in_resolve_process` only on success, so the retry met the structurally equal lambda and threw `Recursive lambda`. - The column's `IdentifierResolveScope` was reused with the aliases, the identifier lookups in progress and the identifier cache of the failed attempt, so an inner alias of the expression threw `MULTIPLE_EXPRESSIONS_FOR_ALIAS`. The failure is visible when the cycle goes through a branch of `if`/`multiIf` that is not taken, whose error the constant folding discards. For a definition loaded by ATTACH, `a Int32 ALIAS if(0, b, 1)` with `b Int32 ALIAS arraySum(arrayMap(z -> z + a, [2]))` reads `1 3` on master and threw `Recursive lambda` here; `d Int32 ALIAS q + ((x + c) AS q)` with `c Int32 ALIAS if(0, d, 1)` reads `1 16` on master and threw `MULTIPLE_EXPRESSIONS_FOR_ALIAS`. The lambda is now erased by `SCOPE_EXIT`, and a retry replaces the scope with a fresh one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@groeneai from my understanding, |
|
Agreed, it should fail. That check is my #124850: with it on top of this PR, this This PR covers tables that already have such a column. 26.10.2.103 accepts the I can move the |
ClickHouse#129640 added the analyzer read-side guard and created its cyclic tables with a full-definition ATTACH, which did not validate ALIAS columns. This PR screens a full ATTACH like CREATE, so those ATTACH statements now fail with CYCLIC_ALIASES and the reads of tables that can no longer be created are removed; the non-cycle guards (`t_ok`, `t_probe`) still attach and read the same values. The read-side guard remains for definitions stored by older versions, which a stateless test cannot create. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since ClickHouse#129640 a read that closes an ALIAS cycle fails with CYCLIC_ALIASES in the analyzer, including a cycle hidden in a SQL UDF of a table function structure, which used to run the server out of memory. Following UDF bodies at definition time therefore only moved that error earlier, and it had to approximate the analyzer's binding of UDF parameters: a parameter that receives a function composition (`negate | toString`) and shares its name with a registered UDF was expanded as that UDF, so a valid structure was rejected (master reads it). Drop the UDF expansion and keep the subquery descent; `CREATE TABLE` and a full `ATTACH` expand SQL UDFs before the check anyway. The UDF test now checks that reading such a column fails with CYCLIC_ALIASES, and that the composition case reads its value. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #124850
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a query that never finished and used memory until the server was killed when it read an
ALIAScolumn whose expression reads the column itself through a subcolumn, directly or through otherALIAScolumns (for examplec0 Nullable(IPv6) ALIAS c0.null). Such a read now fails withCYCLIC_ALIASES.Description
On 26.10.2.103,
CREATE TABLE t (x UInt8, c0 Nullable(IPv6) ALIAS c0.null) ENGINE = MergeTree ORDER BY x; SELECT c0 FROM truns until the server is killed:max_execution_time,max_memory_usageandmax_server_memory_usagedo not stop it. Older releases reach it throughATTACHor table function structures. Found by La Casa Del Dolor on master: OOM in dmesg, a read of such a column of a refreshable materialized view throughclusterAllReplicas.Root cause:
ALIASexpressions are resolved on first use (#123926). Whilec0.nullis resolved,tryGetColumnNode("c0")is called again and returns the node being resolved, which then becomes part of its own expression (getSubcolumn(c0, 'null')). The query tree is cyclic andvalidateTreeSizewalks it forever. A direct cycle (c0 ALIAS c0) overflows the stack instead.Change:
AnalysisTableExpressionDatakeeps the names of theALIAScolumns being resolved, andtryGetColumnNodethrowsCyclic aliases: `c0` -> `c1` -> `c0`when one of them is requested again. A failed resolution leaves the column unresolved, with its scope and lambda recursion guard unwound, so a later reference in the same query (afterif(0, c0, ...)drops the first error) resolves it again, and a real cycle fails again. Every cycle comes back through this lookup, so stored definitions,Merge,remote/cluster*,formatandJOIN USINGare covered. Subcolumn andNestedprefix lookups check the declared type before they resolve a candidate, so a probe that is not a reference (twhilet UInt8 ALIAS t.xreads the columnxof tablet) is not a cycle.#124850 rejects such definitions at
CREATE/ALTER/ATTACH; whichever PR lands second adapts theATTACHstatements of the new test.Validation:
05546_alias_column_reads_itselffails on master at its first read and passes 50/50 with randomized settings; the stateless tests withalias, subcolumn,Nested,DEFAULT, lambda or short-circuit in their names show no new failures.Workflow [PR]
Sync PR [sync-upstream/pr/129640]
Version info
26.11.1.1517-master(included in26.11and later)