ClickHouse/src/Interpreters/ApplyWithSubqueryVisitor.cpp

109 lines
3.7 KiB
C++
Raw Normal View History

2020-09-12 17:00:04 +00:00
#include <Interpreters/ApplyWithSubqueryVisitor.h>
#include <Interpreters/IdentifierSemantic.h>
#include <Interpreters/StorageID.h>
#include <Interpreters/misc.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTSelectQuery.h>
2021-01-02 04:51:13 +00:00
#include <Parsers/ASTSubquery.h>
2020-09-12 17:00:04 +00:00
#include <Parsers/ASTTablesInSelectQuery.h>
#include <Parsers/ASTWithElement.h>
namespace DB
{
void ApplyWithSubqueryVisitor::visit(ASTPtr & ast, const Data & data)
{
if (auto * node_select = ast->as<ASTSelectQuery>())
2021-04-05 06:36:33 +00:00
visit(*node_select, data);
2020-09-29 16:32:41 +00:00
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);
}
2020-09-12 17:00:04 +00:00
}
2021-04-05 06:36:33 +00:00
void ApplyWithSubqueryVisitor::visit(ASTSelectQuery & ast, const Data & data)
{
std::optional<Data> new_data;
if (auto with = ast.with())
{
for (auto & child : with->children)
{
visit(child, new_data ? *new_data : data);
if (auto * ast_with_elem = child->as<ASTWithElement>())
{
if (!new_data)
new_data = data;
new_data->subqueries[ast_with_elem->name] = ast_with_elem->subquery;
}
}
}
for (auto & child : ast.children)
{
if (child != ast.with())
visit(child, new_data ? *new_data : data);
}
}
void ApplyWithSubqueryVisitor::visit(ASTSelectWithUnionQuery & ast, const Data & data)
{
for (auto & child : ast.children)
visit(child, data);
}
2020-09-12 17:00:04 +00:00
void ApplyWithSubqueryVisitor::visit(ASTTableExpression & table, const Data & data)
{
if (table.database_and_table_name)
{
2020-10-26 15:49:00 +00:00
auto table_id = table.database_and_table_name->as<ASTTableIdentifier>()->getTableId();
2020-09-12 17:00:04 +00:00
if (table_id.database_name.empty())
{
auto subquery_it = data.subqueries.find(table_id.table_name);
if (subquery_it != data.subqueries.end())
{
2021-01-02 04:51:13 +00:00
auto old_alias = table.database_and_table_name->tryGetAlias();
2020-09-12 17:00:04 +00:00
table.children.clear();
table.database_and_table_name.reset();
table.subquery = subquery_it->second->clone();
2021-01-02 04:51:13 +00:00
table.subquery->as<ASTSubquery &>().cte_name = table_id.table_name;
if (!old_alias.empty())
table.subquery->setAlias(old_alias);
2020-09-12 17:00:04 +00:00
table.children.emplace_back(table.subquery);
}
}
}
}
void ApplyWithSubqueryVisitor::visit(ASTFunction & func, const Data & data)
{
2021-05-31 15:00:04 +00:00
/// Special CTE case, where the right argument of IN is alias (ASTIdentifier) from WITH clause.
2020-09-12 17:00:04 +00:00
if (checkFunctionIsInOrGlobalInOperator(func))
{
auto & ast = func.arguments->children.at(1);
2021-05-31 15:00:04 +00:00
if (const auto * identifier = ast->as<ASTIdentifier>())
2020-09-12 17:00:04 +00:00
{
2021-05-31 15:00:04 +00:00
if (identifier->isShort())
2020-09-12 17:00:04 +00:00
{
/// Clang-tidy is wrong on this line, because `func.arguments->children.at(1)` gets replaced before last use of `name`.
auto name = identifier->shortName(); // NOLINT
2021-05-31 15:00:04 +00:00
auto subquery_it = data.subqueries.find(name);
2020-09-12 17:00:04 +00:00
if (subquery_it != data.subqueries.end())
{
2021-01-02 04:51:13 +00:00
auto old_alias = func.arguments->children[1]->tryGetAlias();
2020-09-12 17:00:04 +00:00
func.arguments->children[1] = subquery_it->second->clone();
2021-05-31 15:00:04 +00:00
func.arguments->children[1]->as<ASTSubquery &>().cte_name = name;
2021-01-02 04:51:13 +00:00
if (!old_alias.empty())
func.arguments->children[1]->setAlias(old_alias);
2020-09-12 17:00:04 +00:00
}
}
}
}
}
}