Skip to content

Commit 80e8f16

Browse files
Merge pull request ClickHouse#78663 from ClickHouse/fix-analyzer-create-view-on-cluster-positional
Fix analyzer: resolve positional arguments in SELECT of CREATE VIEW ... ON CLUSTER
2 parents 6850041 + d329f2b commit 80e8f16

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

src/Interpreters/InterpreterCreateQuery.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,10 @@ void InterpreterCreateQuery::validateMaterializedViewColumnsAndEngine(const ASTC
11091109
{
11101110
if (getContext()->getSettingsRef()[Setting::allow_experimental_analyzer])
11111111
{
1112-
input_block = InterpreterSelectQueryAnalyzer::getSampleBlock(create.select->clone(), getContext());
1112+
/// We should treat SELECT as an initial query in order to properly analyze it.
1113+
auto context = Context::createCopy(getContext());
1114+
context->setQueryKindInitial();
1115+
input_block = InterpreterSelectQueryAnalyzer::getSampleBlock(create.select->clone(), context, SelectQueryOptions{}.createView());
11131116
}
11141117
else
11151118
{

src/Interpreters/InterpreterSelectQueryAnalyzer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ static QueryTreeNodePtr buildQueryTreeAndRunPasses(const ASTPtr & query,
158158
/// We should not apply any query tree level optimizations on shards
159159
/// because it can lead to a changed header.
160160
if (select_query_options.ignore_ast_optimizations
161+
|| select_query_options.is_create_view
161162
|| context->getClientInfo().query_kind == ClientInfo::QueryKind::SECONDARY_QUERY)
162163
query_tree_pass_manager.runOnlyResolve(query_tree);
163164
else

src/Interpreters/SelectQueryOptions.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct SelectQueryOptions
4545
bool is_create_parameterized_view = false;
4646
/// Bypass setting constraints for some internal queries such as projection ASTs.
4747
bool ignore_setting_constraints = false;
48+
bool is_create_view = false; /// this select is a part of CREATE [MATERIALIZED] VIEW query
4849

4950
/// Bypass access check for select query.
5051
/// This allows to skip double access check in some specific cases (e.g. insert into table with materialized view)
@@ -87,6 +88,12 @@ struct SelectQueryOptions
8788
return out;
8889
}
8990

91+
SelectQueryOptions & createView(bool value = true)
92+
{
93+
is_create_view = value;
94+
return *this;
95+
}
96+
9097
SelectQueryOptions createParameterizedView() const
9198
{
9299
SelectQueryOptions out = *this;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
d Date
22
f UInt64
3+
a String
4+
total UInt64

tests/queries/0_stateless/02006_test_positional_arguments_on_cluster.sql

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,40 @@ DESC t02006;
1717

1818
DROP TABLE IF EXISTS t02006 on cluster test_shard_localhost format Null;
1919
DROP TABLE IF EXISTS m02006 on cluster test_shard_localhost format Null;
20+
DROP TABLE IF EXISTS tt02006 on cluster test_shard_localhost format Null;
21+
22+
SET enable_analyzer=1;
23+
24+
CREATE TABLE t02006 ON CLUSTER test_shard_localhost
25+
(
26+
`a` String,
27+
`b` UInt32
28+
)
29+
ENGINE = ReplicatedMergeTree
30+
PRIMARY KEY a
31+
ORDER BY a
32+
format Null;
33+
34+
CREATE TABLE tt02006 ON CLUSTER test_shard_localhost
35+
(
36+
`a` String,
37+
`total` SimpleAggregateFunction(sum, UInt64)
38+
)
39+
ENGINE = ReplicatedAggregatingMergeTree
40+
ORDER BY a
41+
format Null;
42+
43+
CREATE MATERIALIZED VIEW m02006 ON CLUSTER test_shard_localhost TO tt02006
44+
AS SELECT
45+
a,
46+
sum(b) AS total
47+
FROM t02006
48+
GROUP BY 1
49+
ORDER BY 1 ASC
50+
format Null;
51+
52+
DESC m02006;
53+
54+
DROP TABLE IF EXISTS t02006 on cluster test_shard_localhost format Null;
55+
DROP TABLE IF EXISTS m02006 on cluster test_shard_localhost format Null;
56+
DROP TABLE IF EXISTS tt02006 on cluster test_shard_localhost format Null;

0 commit comments

Comments
 (0)