Repository navigation
Conversation
A materialized CTE named identically to a base table it reads inside its own body must bind that self-reference to the base table (recursive CTEs are not supported here). The self-reference guard in QueryAnalyzer checks the node stored in cte_name_to_query_node, but the materialized-CTE expression path inserted the TableNode placeholder/clone into ctes_in_resolve_process, which the guard never sees. When the CTE body is a set operation, a later branch then self-referenced the still unmaterialized CTE, producing a read with no DelayedPortsProcessor gate and a LOGICAL_ERROR (STID 2467-2c2d). Insert the cte_name_to_query_node node instead, matching the sibling resolveQueryJoinTreeNode path. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…e-self-name-set-operation
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-0:20260714-172500 |
|
cc @novikd @vdimir — could you review this? It fixes a materialized-CTE self-reference name-resolution bug (STID 2467-2c2d, still live on master): when a materialized CTE is named the same as a base table it reads and its body is a set operation, the |
|
Workflow [PR], commit [8a99f4e] Summary: ✅
AI ReviewSummaryThe change fixes materialized CTE self-binding in expression-level set operations. The earlier two-consumer finding is also fixed in the current code. Test Findings❌ Blockers
Missing context / blind spots
Final Verdict |
CI finish ledger — cd7050bCI fully finished on this HEAD (Finish Workflow + Mergeable Check pass; only CH Inc sync pending, which is exempt). CIDB shows ZERO FAIL/ERROR test rows at this commit.
Session id: cron:our-pr-ci-monitor:20260714-233000 |
The self-reference guard has three call sites: the expression path in
resolveExpressionNode and, in resolveQueryJoinTreeNode, the first-reference
branch (which creates the storage) and the reused-reference branch (which
resolves this clone's own copy of the body and reuses the existing storage).
Only the first two inserted the CTE into ctes_in_resolve_process.
The reused-reference branch resolves an unresolved body, so it needs the guard
too. Without it, a body reference such as `FROM test1` inside a CTE named
`test1` binds back to the CTE instead of the base table. Because the shared
storage is already initialized, that nested clone takes the same branch again
and recurses until TOO_DEEP_SUBQUERIES, so a query with two consumers of one
self-named materialized CTE failed outright:
WITH test1 AS MATERIALIZED ((SELECT i FROM test1) UNION ALL (SELECT i FROM test1))
SELECT count() FROM test1, test1 AS t2;
A set operation in the body is not required to reach the branch - two
consumers are enough. A single consumer never reaches it, which is why the
existing test did not cover this.
The scope walk that finds the cte_name_to_query_node entry was duplicated at
each site; it is now findCTENodeInScopes, so the three sites cannot drift
apart again.
Reported by the AI reviewer on ClickHouse#110428.
The two-consumer counts are compared against a non-materialized CTE, so on their own they would also pass if the CTE were inlined instead of materialized. Assert the `MaterializingCTE` plan step is present first; with `enable_materialized_cte = 0` that assertion yields 0, so it discriminates. Also call the failure an exception rather than a crash - it is a thrown `LOGICAL_ERROR`, handled normally in release builds - and wrap the diagnostic names in inline code blocks.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 42/43 (97.67%) · Uncovered code |
CI finish ledger - 630aa25Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260729-120000 |
The same invariant is broken at two more sites, and the four sites cannot be split across two PRsThis PR fixes the materialized-CTE self-reference guard at The invariant. Four carrier classes, measured on four debug binaries. Each arm asserts
The last row is the one that matters: five independent shapes of it (JOIN of two shadowed subqueries; one plain reference joined with a shadowed one, both orders; two scalar subqueries; a comma join plus a shadowed Why it cannot be two PRs. The complete fix is the four sites keyed uniformly on the map node: Two options, both of which touch this PR, so the call is yours:
I have a validated 31-case matrix (carriers, non-materialized oracles, seven controls, a materialization assertion, and a check that Everything above is behind Session id: cron:clickhouse-plan-slot-1:20260805-071502 |
…e-self-name-set-operation Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units20 translation units recompiled, 209 s compile time in total, 20 of them have a recent master baseline. |
Related: #110176
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes two errors when a materialized CTE is named identically to a base table it reads inside its own body: a
LOGICAL_ERROR(Reading from materialized CTE before its materialization completed - DelayedPortsProcessor gate is missing) when the body is a set operation, e.g.WITH test1 AS MATERIALIZED ((SELECT i FROM test1) EXCEPT ALL (SELECT i FROM test1)) SELECT 4 AS i WHERE i IN (test1), andTOO_DEEP_SUBQUERIESwhen the CTE is referenced twice, e.g.WITH test1 AS MATERIALIZED (SELECT i FROM test1) SELECT count() FROM test1, test1 AS t2.Description
Found by the AST fuzzer (STID 2467-2c2d), still live on master. A distinct, non-
EXPLAIN ANALYZEmanifestation of theDelayedPortsProcessor gate is missingsite.A materialized CTE named the same as a base table it reads inside its own body must bind that self-reference to the base table (recursive CTEs are not supported here), exactly like a non-materialized CTE. The guard in
tryResolveIdentifierFromCTEtests the node stored inscope.cte_name_to_query_nodeagainstctes_in_resolve_process, so only that node works as a guard.The guard has three call sites, and two were passing the wrong node or none at all:
The expression path in
resolveExpressionNodeinserted the freshly clonedTableNodeplaceholder, which the guard never looks up. With a set-operation body the guard did not fire on the second branch, soFROM test1there bound to the still-unmaterialized CTE and the plan read itsStorageMemorywith noDelayedPortsProcessorgate, tripping the fail-fast inReadFromMemoryStorageStep.The reused-reference branch of
resolveQueryJoinTreeNode(taken when another reference already created the storage) resolves this clone's own unresolved copy of the body but inserted nothing. A body reference bound back to the CTE, re-entered the same branch and recursed untilTOO_DEEP_SUBQUERIES. Reaching it needs two consumers, not a set operation, which is why the first version of the test did not cover it; found by the AI reviewer on this PR.Both now insert the
cte_name_to_query_nodenode. The scope walk that finds it was duplicated per site and is nowfindCTENodeInScopes, so the three sites cannot drift apart again.Verified against the non-materialized CTE as an oracle, plus a plan assertion that the CTE really is materialized. Genuine recursive CTEs are still rejected. No performance impact: this only changes which node a set insert/erase uses during name resolution.
Workflow [PR]
Sync PR [sync-upstream/pr/110428]