Skip to content

Commit 95bf236

Browse files
Backport #83036 to 25.6: Read only required VIEW columns on the shards
1 parent 108491e commit 95bf236

3 files changed

Lines changed: 53 additions & 3 deletions

File tree

src/Analyzer/QueryTreePassManager.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,12 @@ void QueryTreePassManager::run(QueryTreeNodePtr query_tree_node)
193193

194194
void QueryTreePassManager::runOnlyResolve(QueryTreeNodePtr query_tree_node)
195195
{
196-
// Run only QueryAnalysisPass and GroupingFunctionsResolvePass passes.
197-
run(query_tree_node, 3);
196+
// Run only query tree passes that doesn't affect output header:
197+
// 1. QueryAnalysisPass
198+
// 2. GroupingFunctionsResolvePass
199+
// 3. AutoFinalOnQueryPass
200+
// 4. RemoveUnusedProjectionColumnsPass
201+
run(query_tree_node, 4);
198202
}
199203

200204
void QueryTreePassManager::run(QueryTreeNodePtr query_tree_node, size_t up_to_pass_index)
@@ -252,8 +256,10 @@ void addQueryTreePasses(QueryTreePassManager & manager, bool only_analyze)
252256
manager.addPass(std::make_unique<QueryAnalysisPass>(only_analyze));
253257
manager.addPass(std::make_unique<GroupingFunctionsResolvePass>());
254258
manager.addPass(std::make_unique<AutoFinalOnQueryPass>());
255-
259+
/// This pass should be run for the secondary queries
260+
/// to ensure that the only required columns are read from VIEWs on the shards.
256261
manager.addPass(std::make_unique<RemoveUnusedProjectionColumnsPass>());
262+
257263
manager.addPass(std::make_unique<FunctionToSubcolumnsPass>());
258264

259265
manager.addPass(std::make_unique<ConvertLogicalExpressionToCNFPass>());
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
0
2+
['default.test.i1','default.test_view.i1']
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
CREATE TABLE test
2+
(
3+
`i1` Int64,
4+
`i2` Int64,
5+
`i3` Int64,
6+
`i4` Int64,
7+
`i5` Int64,
8+
`i6` Int64,
9+
`i7` Int64,
10+
`i8` Int64,
11+
`i9` Int64,
12+
`i10` Int64
13+
)
14+
ENGINE = MergeTree
15+
ORDER BY tuple()
16+
SETTINGS index_granularity = 8192;
17+
18+
CREATE VIEW test_view
19+
AS SELECT *
20+
FROM test;
21+
22+
SET prefer_localhost_replica = 0;
23+
SET serialize_query_plan = 0;
24+
25+
SELECT max(i1)
26+
FROM remote('localhost', currentDatabase(), test_view)
27+
SETTINGS log_comment = 'THIS IS A COMMENT TO MARK THE INITIAL QUERY';
28+
29+
SYSTEM FLUSH LOGS;
30+
31+
SELECT columns
32+
FROM system.query_log
33+
WHERE
34+
initial_query_id = (
35+
SELECT query_id
36+
FROM system.query_log
37+
WHERE
38+
current_database = currentDatabase()
39+
AND log_comment = 'THIS IS A COMMENT TO MARK THE INITIAL QUERY'
40+
LIMIT 1)
41+
AND type = 'QueryFinish'
42+
AND NOT is_initial_query;

0 commit comments

Comments
 (0)