mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-21 15:12:02 +00:00
Fix CTE in view.
This commit is contained in:
parent
cf2931384b
commit
46c9affa83
@ -13,9 +13,22 @@ namespace DB
|
||||
void ApplyWithSubqueryVisitor::visit(ASTPtr & ast, const Data & data)
|
||||
{
|
||||
if (auto * node_select = ast->as<ASTSelectQuery>())
|
||||
visit(*node_select, data);
|
||||
else
|
||||
{
|
||||
for (auto & child : ast->children)
|
||||
visit(child, data);
|
||||
if (auto * node_func = ast->as<ASTFunction>())
|
||||
visit(*node_func, data);
|
||||
else if (auto * node_table = ast->as<ASTTableExpression>())
|
||||
visit(*node_table, data);
|
||||
}
|
||||
}
|
||||
|
||||
void ApplyWithSubqueryVisitor::visit(ASTSelectQuery & ast, const Data & data)
|
||||
{
|
||||
std::optional<Data> new_data;
|
||||
if (auto with = node_select->with())
|
||||
if (auto with = ast.with())
|
||||
{
|
||||
for (auto & child : with->children)
|
||||
{
|
||||
@ -29,21 +42,17 @@ void ApplyWithSubqueryVisitor::visit(ASTPtr & ast, const Data & data)
|
||||
}
|
||||
}
|
||||
|
||||
for (auto & child : node_select->children)
|
||||
for (auto & child : ast.children)
|
||||
{
|
||||
if (child != node_select->with())
|
||||
if (child != ast.with())
|
||||
visit(child, new_data ? *new_data : data);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (auto & child : ast->children)
|
||||
}
|
||||
|
||||
void ApplyWithSubqueryVisitor::visit(ASTSelectWithUnionQuery & ast, const Data & data)
|
||||
{
|
||||
for (auto & child : ast.children)
|
||||
visit(child, data);
|
||||
if (auto * node_func = ast->as<ASTFunction>())
|
||||
visit(*node_func, data);
|
||||
else if (auto * node_table = ast->as<ASTTableExpression>())
|
||||
visit(*node_table, data);
|
||||
}
|
||||
}
|
||||
|
||||
void ApplyWithSubqueryVisitor::visit(ASTTableExpression & table, const Data & data)
|
||||
|
@ -7,6 +7,8 @@
|
||||
namespace DB
|
||||
{
|
||||
class ASTFunction;
|
||||
class ASTSelectQuery;
|
||||
class ASTSelectWithUnionQuery;
|
||||
struct ASTTableExpression;
|
||||
|
||||
class ApplyWithSubqueryVisitor
|
||||
@ -18,9 +20,13 @@ public:
|
||||
};
|
||||
|
||||
static void visit(ASTPtr & ast) { visit(ast, {}); }
|
||||
static void visit(ASTSelectQuery & select) { visit(select, {}); }
|
||||
static void visit(ASTSelectWithUnionQuery & select) { visit(select, {}); }
|
||||
|
||||
private:
|
||||
static void visit(ASTPtr & ast, const Data & data);
|
||||
static void visit(ASTSelectQuery & ast, const Data & data);
|
||||
static void visit(ASTSelectWithUnionQuery & ast, const Data & data);
|
||||
static void visit(ASTTableExpression & table, const Data & data);
|
||||
static void visit(ASTFunction & func, const Data & data);
|
||||
};
|
||||
|
@ -59,6 +59,7 @@
|
||||
#include <Interpreters/QueryLog.h>
|
||||
#include <Interpreters/addTypeConversionToAST.h>
|
||||
#include <Interpreters/FunctionNameNormalizer.h>
|
||||
#include <Interpreters/ApplyWithSubqueryVisitor.h>
|
||||
|
||||
#include <TableFunctions/TableFunctionFactory.h>
|
||||
#include <common/logger_useful.h>
|
||||
@ -887,6 +888,8 @@ BlockIO InterpreterCreateQuery::createTable(ASTCreateQuery & create)
|
||||
|
||||
if (create.select && create.isView())
|
||||
{
|
||||
// Expand CTE before filling default database
|
||||
ApplyWithSubqueryVisitor().visit(*create.select);
|
||||
AddDefaultDatabaseVisitor visitor(current_database);
|
||||
visitor.visit(*create.select);
|
||||
}
|
||||
|
@ -1,3 +1,7 @@
|
||||
drop table if exists t;
|
||||
create table t engine = Memory as with cte as (select * from numbers(10)) select * from cte;
|
||||
drop table t;
|
||||
|
||||
drop table if exists view1;
|
||||
create view view1 as with t as (select number n from numbers(3)) select n from t;
|
||||
drop table view1;
|
||||
|
Loading…
Reference in New Issue
Block a user