ClickHouse/src/Interpreters/ApplyWithAliasVisitor.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

57 lines
1.8 KiB
C++
Raw Normal View History

2020-09-29 16:32:41 +00:00
#include <Interpreters/ApplyWithAliasVisitor.h>
#include <Interpreters/misc.h>
#include <Parsers/ASTSelectQuery.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
2022-10-01 15:47:58 +00:00
#include <Common/checkStackSize.h>
2020-09-29 16:32:41 +00:00
namespace DB
{
2022-10-01 15:47:58 +00:00
2020-09-29 16:32:41 +00:00
void ApplyWithAliasVisitor::visit(ASTPtr & ast, const Data & data)
{
2022-10-01 15:47:58 +00:00
checkStackSize();
2020-09-29 16:32:41 +00:00
if (auto * node_select = ast->as<ASTSelectQuery>())
{
std::optional<Data> new_data;
if (auto with = node_select->with())
{
std::set<String> current_names;
for (auto & child : with->children)
{
2021-01-02 04:51:13 +00:00
visit(child, new_data ? *new_data : data);
2020-09-29 16:32:41 +00:00
if (auto * ast_with_alias = dynamic_cast<ASTWithAlias *>(child.get()))
{
if (!new_data)
new_data = data;
new_data->exprs[ast_with_alias->alias] = child;
current_names.insert(ast_with_alias->alias);
}
}
for (const auto & with_alias : data.exprs)
{
if (!current_names.contains(with_alias.first))
2020-09-29 16:32:41 +00:00
with->children.push_back(with_alias.second->clone());
}
}
else if (!data.exprs.empty())
{
auto with_expression_list = std::make_shared<ASTExpressionList>();
for (const auto & with_alias : data.exprs)
with_expression_list->children.push_back(with_alias.second->clone());
node_select->setExpression(ASTSelectQuery::Expression::WITH, std::move(with_expression_list));
}
for (auto & child : node_select->children)
{
if (child != node_select->with())
visit(child, new_data ? *new_data : data);
}
}
else
for (auto & child : ast->children)
visit(child, data);
}
}