Repository navigation
Reject correlated recursive CTE instead of aborting with a logical error - #109863
alexey-milovidov merged 3 commits into
Conversation
A recursive CTE is planned as a fixed-point over a temporary table, so it has no per-call-site substitution mechanism and cannot be correlated to an outer scope. When an identifier in a recursive term leaked to an enclosing recursive CTE column (allow_experimental_correlated_subqueries=1), resolution bound it as a correlated column whose source is not part of the subquery, producing a dangling source node during planning and aborting with 'query tree node does not have valid source node' (LOGICAL_ERROR). Detect the correlated recursive query during resolution and throw a clean UNSUPPORTED_METHOD error, matching the existing 'Materialized CTE cannot be correlated' guard. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-1:20260709-043700 |
|
cc @novikd @vdimir — could you review? Rejects a correlated recursive CTE (allow_experimental_correlated_subqueries=1) with a clean UNSUPPORTED_METHOD error instead of aborting with a dangling-source-node LOGICAL_ERROR, mirroring the existing Materialized-CTE-cannot-be-correlated guard. @ novikd owns the correlated-subqueries path. |
|
Workflow [PR], commit [db35f01] Summary: ✅
AI ReviewSummaryThis PR hardens recursive CTE resolution by rejecting correlated seed terms as well as correlated recursive members before planning, so unsupported recursive correlations now fail with Final Verdict
|
The prior guard only rejected a correlated recursive member. A recursive CTE whose seed (non-recursive) term is correlated to an outer scope still passed: Planner::buildPlanForUnionNode lowers the whole union to ReadFromRecursiveCTEStep and RecursiveCTESource::buildStepExecutor re-runs the seed with only the temporary CTE table injected, so there is no mechanism to supply the outer-scope binding. That left the same unsupported contract in place and aborted with a LOGICAL_ERROR. Check isCorrelated() on the resolved non-recursive query as well and throw the same UNSUPPORTED_METHOD error. Add a seed-only-correlated regression test. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Good catch. Fixed in b679774. Added the same rejection for a correlated seed term: after resolving the non-recursive query (queries_nodes[0]) in resolveUnion, check its isCorrelated() and throw the same UNSUPPORTED_METHOD ("Recursive CTE '...' cannot be correlated"). This covers both a correlated non_recursive_query directly and, transitively, a correlated union seed. Verified locally on the PR binary (debug):
Extended tests/queries/0_stateless/04401_recursive_cte_correlated_subquery.sql with a seed-only-correlated case asserting |
CI finish ledger — b679774Every 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
Neither is PR-caused: PR #109863 touches only src/Analyzer/Resolve/QueryAnalyzer.cpp and one stateless test; both failures are unrelated chronic trunk/flaky signatures. Session id: cron:our-pr-ci-monitor:20260709-160000 |
|
Fixing PR for the |
CI finish ledger — b679774Every failure below has an owner. The original AST fuzzer STID 2945-38b2 (which this PR fixes) did not recur on this head. The 3 CANCELLED coverage/binary/integration shards are not failures.
Session id: cron:our-pr-ci-monitor:20260709-183000 |
|
@groeneai, all CI fixes are merged, please update the branch. |
…ive-cte-correlated-subquery
|
Branch updated to db35f01 (merged current master), CI rerunning. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 20/24 (83.33%) · Uncovered code |
CI finish ledger — db35f01CI fully finished on the updated branch (merged current master per @alexey-milovidov). All checks green: 156 SUCCESS + 18 SKIPPED, Finish Workflow + Mergeable Check + CH Inc sync all pass. No failures. Session id: cron:our-pr-ci-monitor:20260712-183000 |
|
Hi @groeneai @alexey-milovidov — while reviewing this PR I found the following:
Could you review the test PR and add the |
…sive-cte-correlated-subquery Reject correlated recursive CTE instead of aborting with a logical error
Related: #80564
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a
LOGICAL_ERROR(query tree node does not have valid source node) when a recursive CTE resolved an identifier from an outer scope as a correlated column (withallow_experimental_correlated_subqueries = 1). Such queries now return a clearUNSUPPORTED_METHODerror.Description
A recursive CTE is planned as a fixed-point over a temporary table, so it has no per-call-site substitution mechanism and cannot be correlated to an outer scope. When an identifier in the recursive term leaked to an enclosing recursive CTE's column (this happens, for example, when the base term is a scalar subquery whose projection column is not named the same as the recursive-term identifier), resolution bound it as a correlated column whose source is not part of the subquery. That produced a
ColumnNodewith a dangling source node, which later aborted during planning inColumnNode::getColumnSource()(CollectSourceColumnsVisitor,Planner/CollectTableExpressionData.cpp).The fix detects the correlated recursive query during resolution and throws a clean
UNSUPPORTED_METHODerror, mirroring the existing "Materialized CTE cannot be correlated" guard.Found by the AST fuzzer.
AST fuzzer (amd_msan)Minimal reproducer (requires
enable_analyzer = 1,allow_experimental_correlated_subqueries = 1):Version info
26.7.1.835(included in26.7and later)