Skip to content

Commit 317fce8

Browse files
Backport ClickHouse#84026 to 25.6: Disable query condition cache for recursive CTEs
1 parent 6d0e416 commit 317fce8

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

src/Processors/Sources/RecursiveCTESource.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace DB
2626
namespace Setting
2727
{
2828
extern const SettingsUInt64 max_recursive_cte_evaluation_depth;
29+
extern const SettingsBool use_query_condition_cache;
2930
}
3031

3132
namespace ErrorCodes
@@ -176,6 +177,10 @@ class RecursiveCTEChunkGenerator
176177
recursive_subquery_settings[Setting::max_recursive_cte_evaluation_depth].value,
177178
recursive_cte_union_node->formatASTForErrorMessage());
178179

180+
/// Workaround for issue 84026: Usage of the query condition cache with recursive CTEs caused wrong results
181+
if (recursive_step > 0 && recursive_subquery_settings[Setting::use_query_condition_cache])
182+
recursive_query_context->setSetting("use_query_condition_cache", false);
183+
179184
auto & query_to_execute = recursive_step > 0 ? recursive_query : non_recursive_query;
180185
++recursive_step;
181186

src/Storages/MergeTree/MergeTreeSelectProcessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ ChunkAndProgress MergeTreeSelectProcessor::read()
196196
part_name,
197197
output->getHash(),
198198
reader_settings.query_condition_cache_store_conditions_as_plaintext
199-
? prewhere_info->prewhere_actions.getNames()[0]
199+
? output->result_name
200200
: "",
201201
task->getPrewhereUnmatchedMarks(),
202202
data_part->index_granularity->getMarksCount(),
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Prepare data
2+
-- First run
3+
uuid2
4+
uuid3
5+
uuid4
6+
-- Second run
7+
uuid2
8+
uuid3
9+
uuid4
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
-- Tags: no-parallel
2+
-- Tag no-parallel: Messes with internal cache
3+
4+
-- Test for issue #81506 (recursive CTEs return wrong results if the query condition cache is on)
5+
6+
SET allow_experimental_analyzer = 1; -- needed by recursive CTEs
7+
8+
-- Start from a clean query condition cache
9+
SYSTEM DROP QUERY CONDITION CACHE;
10+
11+
SELECT '-- Prepare data';
12+
13+
DROP TABLE IF EXISTS tab;
14+
CREATE TABLE tab
15+
(
16+
id String,
17+
parent String,
18+
)
19+
ENGINE = MergeTree
20+
ORDER BY tuple();
21+
22+
INSERT INTO tab (id, parent) VALUES
23+
('uuid1', 'uuid2'),
24+
('uuid3', 'uuid4'),
25+
('uuid4', 'uuid2'),
26+
('uuid2', 'empty'),
27+
('uuid5', 'uuid2'),
28+
('uuid6', 'uuid4');
29+
30+
SELECT '-- First run';
31+
32+
WITH RECURSIVE
33+
recursive AS (
34+
SELECT id FROM tab WHERE id = 'uuid3'
35+
UNION ALL
36+
SELECT parent AS id
37+
FROM tab
38+
WHERE tab.id IN recursive AND parent != 'empty'
39+
GROUP BY parent
40+
)
41+
SELECT *
42+
FROM recursive
43+
GROUP BY id
44+
ORDER BY id;
45+
46+
SELECT '-- Second run'; -- same query
47+
48+
WITH RECURSIVE
49+
recursive AS (
50+
SELECT id FROM tab WHERE id = 'uuid3'
51+
UNION ALL
52+
SELECT parent AS id
53+
FROM tab
54+
WHERE tab.id IN recursive AND parent != 'empty'
55+
GROUP BY parent
56+
)
57+
SELECT *
58+
FROM recursive
59+
GROUP BY id
60+
ORDER BY id;
61+
62+
DROP TABLE tab;

0 commit comments

Comments
 (0)