Skip to content

Commit ba389ae

Browse files
Backport ClickHouse#91827 to 25.8: Fix LOGICAL_ERROR in RecursiveCTEChunkGenerator
1 parent a43affd commit ba389ae

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/Processors/Sources/RecursiveCTESource.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace ErrorCodes
3838
namespace
3939
{
4040

41-
std::vector<TableNode *> collectTableNodesWithStorage(const StoragePtr & storage, IQueryTreeNode * root)
41+
std::vector<TableNode *> collectTableNodesWithTemporaryTableName(const std::string & temporary_table_name, IQueryTreeNode * root)
4242
{
4343
std::vector<TableNode *> result;
4444

@@ -51,7 +51,7 @@ std::vector<TableNode *> collectTableNodesWithStorage(const StoragePtr & storage
5151
nodes_to_process.pop_back();
5252

5353
auto * table_node = subtree_node->as<TableNode>();
54-
if (table_node && table_node->getStorageID() == storage->getStorageID())
54+
if (table_node && table_node->getTemporaryTableName() == temporary_table_name)
5555
result.push_back(table_node);
5656

5757
for (auto & child : subtree_node->getChildren())
@@ -77,7 +77,9 @@ class RecursiveCTEChunkGenerator
7777
chassert(recursive_cte_union_node_typed.hasRecursiveCTETable());
7878

7979
auto & recursive_cte_table = recursive_cte_union_node_typed.getRecursiveCTETable();
80-
recursive_table_nodes = collectTableNodesWithStorage(recursive_cte_table->storage, recursive_cte_union_node.get());
80+
81+
const auto & cte_name = recursive_cte_union_node_typed.getCTEName();
82+
recursive_table_nodes = collectTableNodesWithTemporaryTableName(cte_name, recursive_cte_union_node.get());
8183
if (recursive_table_nodes.empty())
8284
throw Exception(ErrorCodes::LOGICAL_ERROR, "UNION query {} is not recursive", recursive_cte_union_node->formatASTForErrorMessage());
8385

tests/queries/0_stateless/03755_nested_recursive_cte.reference

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
DROP TABLE IF EXISTS t0;
2+
CREATE TABLE t0 (x Int32) ENGINE = Memory;
3+
INSERT INTO t0 VALUES (1);
4+
5+
-- The original problematic query pattern - inner CTE references outer CTE
6+
-- Using count() to get deterministic output regardless of how many rows are produced before hitting the limit
7+
SET max_recursive_cte_evaluation_depth = 5;
8+
SET enable_analyzer = 1;
9+
10+
SELECT count() > 0 FROM (
11+
WITH RECURSIVE q AS (
12+
SELECT 1 FROM t0 UNION ALL
13+
(WITH RECURSIVE x AS
14+
(SELECT 1 FROM t0 UNION ALL
15+
(SELECT 1 FROM q WHERE FALSE UNION ALL
16+
SELECT 1 FROM x WHERE FALSE))
17+
SELECT 1 FROM x))
18+
SELECT 1 FROM q
19+
); -- { serverError TOO_DEEP_RECURSION }
20+
21+
DROP TABLE t0;

0 commit comments

Comments
 (0)