2020-09-29 16:32:41 +00:00
|
|
|
#include <Interpreters/ApplyWithGlobalVisitor.h>
|
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
|
|
|
#include <Parsers/ASTSelectWithUnionQuery.h>
|
|
|
|
#include <Parsers/ASTWithAlias.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-01-04 03:12:47 +00:00
|
|
|
|
|
|
|
void ApplyWithGlobalVisitor::visit(ASTSelectQuery & select, const std::map<String, ASTPtr> & exprs, const ASTPtr & with_expression_list)
|
|
|
|
{
|
|
|
|
auto with = select.with();
|
|
|
|
if (with)
|
|
|
|
{
|
|
|
|
std::set<String> current_names;
|
2021-01-07 09:04:59 +00:00
|
|
|
for (const auto & child : with->children)
|
2021-01-04 03:12:47 +00:00
|
|
|
{
|
2021-01-07 09:04:59 +00:00
|
|
|
if (const auto * ast_with_alias = dynamic_cast<const ASTWithAlias *>(child.get()))
|
2021-01-04 03:12:47 +00:00
|
|
|
current_names.insert(ast_with_alias->alias);
|
|
|
|
}
|
2021-01-07 09:04:59 +00:00
|
|
|
for (const auto & with_alias : exprs)
|
2021-01-04 03:12:47 +00:00
|
|
|
{
|
|
|
|
if (!current_names.count(with_alias.first))
|
|
|
|
with->children.push_back(with_alias.second->clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
select.setExpression(ASTSelectQuery::Expression::WITH, with_expression_list->clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApplyWithGlobalVisitor::visit(
|
|
|
|
ASTSelectWithUnionQuery & selects, const std::map<String, ASTPtr> & exprs, const ASTPtr & with_expression_list)
|
|
|
|
{
|
|
|
|
for (auto & select : selects.list_of_selects->children)
|
|
|
|
{
|
|
|
|
if (ASTSelectWithUnionQuery * node_union = select->as<ASTSelectWithUnionQuery>())
|
|
|
|
{
|
|
|
|
visit(*node_union, exprs, with_expression_list);
|
|
|
|
}
|
|
|
|
else if (ASTSelectQuery * node_select = select->as<ASTSelectQuery>())
|
|
|
|
{
|
|
|
|
visit(*node_select, exprs, with_expression_list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 16:32:41 +00:00
|
|
|
void ApplyWithGlobalVisitor::visit(ASTPtr & ast)
|
|
|
|
{
|
|
|
|
if (ASTSelectWithUnionQuery * node_union = ast->as<ASTSelectWithUnionQuery>())
|
|
|
|
{
|
2021-01-04 03:12:47 +00:00
|
|
|
if (auto * first_select = node_union->list_of_selects->children[0]->as<ASTSelectQuery>())
|
2020-09-29 16:32:41 +00:00
|
|
|
{
|
2021-01-04 03:12:47 +00:00
|
|
|
ASTPtr with_expression_list = first_select->with();
|
|
|
|
if (with_expression_list)
|
2020-09-29 16:32:41 +00:00
|
|
|
{
|
2021-01-04 03:12:47 +00:00
|
|
|
std::map<String, ASTPtr> exprs;
|
|
|
|
for (auto & child : with_expression_list->children)
|
2020-09-29 16:32:41 +00:00
|
|
|
{
|
2021-01-04 03:12:47 +00:00
|
|
|
if (auto * ast_with_alias = dynamic_cast<ASTWithAlias *>(child.get()))
|
|
|
|
exprs[ast_with_alias->alias] = child;
|
|
|
|
}
|
|
|
|
for (auto it = node_union->list_of_selects->children.begin() + 1; it != node_union->list_of_selects->children.end(); ++it)
|
|
|
|
{
|
|
|
|
if (auto * union_child = (*it)->as<ASTSelectWithUnionQuery>())
|
|
|
|
visit(*union_child, exprs, with_expression_list);
|
|
|
|
else if (auto * select_child = (*it)->as<ASTSelectQuery>())
|
|
|
|
visit(*select_child, exprs, with_expression_list);
|
2020-09-29 16:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-04 05:01:19 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Other non-SELECT queries that contains SELECT children, such as EXPLAIN or INSERT
|
|
|
|
for (auto & child : ast->children)
|
|
|
|
visit(child);
|
|
|
|
}
|
2020-09-29 16:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|