Skip to content

Commit 526a14d

Browse files
Merge pull request ClickHouse#76356 from ClickHouse/analyzer-rewrite-view-query
Analyzer: Rewrite SELECT query during VIEW creation
2 parents 184cc2e + 65c68c1 commit 526a14d

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

src/Interpreters/ApplyWithSubqueryVisitor.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <Parsers/ASTSubquery.h>
1212
#include <Parsers/ASTTablesInSelectQuery.h>
1313
#include <Parsers/ASTWithElement.h>
14+
#include <Parsers/ASTLiteral.h>
1415
#include <Common/checkStackSize.h>
1516

1617

@@ -62,11 +63,16 @@ void ApplyWithSubqueryVisitor::visit(ASTSelectQuery & ast, const Data & data)
6263
for (auto & child : with->children)
6364
{
6465
visit(child, new_data ? *new_data : data);
65-
if (auto * ast_with_elem = child->as<ASTWithElement>())
66+
auto * ast_with_elem = child->as<ASTWithElement>();
67+
auto * ast_literal = child->as<ASTLiteral>();
68+
if (ast_with_elem || ast_literal)
6669
{
6770
if (!new_data)
6871
new_data = data;
69-
new_data->subqueries[ast_with_elem->name] = ast_with_elem->subquery;
72+
if (ast_with_elem)
73+
new_data->subqueries[ast_with_elem->name] = ast_with_elem->subquery;
74+
else
75+
new_data->literals[ast_literal->alias] = child;
7076
}
7177
}
7278
}
@@ -120,15 +126,27 @@ void ApplyWithSubqueryVisitor::visit(ASTFunction & func, const Data & data)
120126
{
121127
/// Clang-tidy is wrong on this line, because `func.arguments->children.at(1)` gets replaced before last use of `name`.
122128
auto name = identifier->shortName(); // NOLINT
129+
123130
auto subquery_it = data.subqueries.find(name);
124131
if (subquery_it != data.subqueries.end())
125132
{
126133
auto old_alias = func.arguments->children[1]->tryGetAlias();
127134
func.arguments->children[1] = subquery_it->second->clone();
128-
func.arguments->children[1]->as<ASTSubquery &>().cte_name = name;
135+
func.arguments->children[1]->as<ASTSubquery>()->cte_name = name;
129136
if (!old_alias.empty())
130137
func.arguments->children[1]->setAlias(old_alias);
131138
}
139+
else
140+
{
141+
auto literal_it = data.literals.find(name);
142+
if (literal_it != data.literals.end())
143+
{
144+
auto old_alias = func.arguments->children[1]->tryGetAlias();
145+
func.arguments->children[1] = literal_it->second->clone();
146+
if (!old_alias.empty())
147+
func.arguments->children[1]->setAlias(old_alias);
148+
}
149+
}
132150
}
133151
}
134152
}

src/Interpreters/ApplyWithSubqueryVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ApplyWithSubqueryVisitor
2121
struct Data
2222
{
2323
std::map<String, ASTPtr> subqueries;
24+
std::map<String, ASTPtr> literals;
2425
};
2526

2627
void visit(ASTPtr & ast) { visit(ast, {}); }

src/Interpreters/InterpreterCreateQuery.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ void InterpreterCreateQuery::validateMaterializedViewColumnsAndEngine(const ASTC
11121112
/// We should treat SELECT as an initial query in order to properly analyze it.
11131113
auto context = Context::createCopy(getContext());
11141114
context->setQueryKindInitial();
1115-
input_block = InterpreterSelectQueryAnalyzer::getSampleBlock(create.select->clone(), context, SelectQueryOptions{}.createView());
1115+
input_block = InterpreterSelectQueryAnalyzer::getSampleBlock(create.select->clone(), context, SelectQueryOptions{}.analyze().createView());
11161116
}
11171117
else
11181118
{

tests/queries/0_stateless/03359_analyzer_rewrite_view_query.reference

Whitespace-only changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CREATE TABLE mydestination
2+
(
3+
`object` String
4+
)
5+
ENGINE = MergeTree
6+
ORDER BY object;
7+
8+
CREATE MATERIALIZED VIEW myview TO mydestination
9+
AS WITH ('foo', 'bar') AS objects
10+
SELECT 'foo' AS object
11+
WHERE object IN (objects);
12+
13+
SELECT * FROM myview;

0 commit comments

Comments
 (0)