ClickHouse/src/Interpreters/ColumnAliasesVisitor.cpp

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

95 lines
3.3 KiB
C++
Raw Normal View History

#include <Interpreters/ColumnAliasesVisitor.h>
#include <Interpreters/IdentifierSemantic.h>
2020-12-12 16:42:15 +00:00
#include <Interpreters/RequiredSourceColumnsVisitor.h>
#include <Interpreters/addTypeConversionToAST.h>
#include <Parsers/ASTTablesInSelectQuery.h>
#include <Parsers/ASTSelectQuery.h>
#include <Parsers/ASTSubquery.h>
#include <Parsers/ASTAlterQuery.h>
#include <Parsers/ASTInsertQuery.h>
#include <Parsers/ASTIdentifier.h>
2020-12-12 16:42:15 +00:00
#include <Parsers/ASTFunction.h>
namespace DB
{
bool ColumnAliasesMatcher::needChildVisit(const ASTPtr & node, const ASTPtr &, const Data & data)
{
if (data.excluded_nodes.contains(node.get()))
return false;
2020-12-12 16:42:15 +00:00
if (const auto * f = node->as<ASTFunction>())
{
2021-01-14 08:26:04 +00:00
/// "lambda" visits children itself.
2020-12-12 16:42:15 +00:00
if (f->name == "lambda")
return false;
}
return !(node->as<ASTTableExpression>()
|| node->as<ASTSubquery>()
2021-05-21 17:01:21 +00:00
|| node->as<ASTArrayJoin>());
}
void ColumnAliasesMatcher::visit(ASTPtr & ast, Data & data)
{
2021-05-21 17:01:21 +00:00
if (auto * func = ast->as<ASTFunction>())
visit(*func, ast, data);
else if (auto * ident = ast->as<ASTIdentifier>())
visit(*ident, ast, data);
2020-12-12 16:42:15 +00:00
}
void ColumnAliasesMatcher::visit(ASTFunction & node, ASTPtr & /*ast*/, Data & data)
{
/// Do not add formal parameters of the lambda expression
if (node.name == "lambda")
{
Names local_aliases;
2020-12-15 08:35:19 +00:00
auto names_from_lambda = RequiredSourceColumnsMatcher::extractNamesFromLambda(node);
for (const auto & name : names_from_lambda)
{
2020-12-12 16:42:15 +00:00
if (data.private_aliases.insert(name).second)
{
2020-12-12 16:42:15 +00:00
local_aliases.push_back(name);
}
2020-12-15 08:35:19 +00:00
}
2020-12-12 16:42:15 +00:00
/// visit child with masked local aliases
Visitor(data).visit(node.arguments->children[1]);
for (const auto & name : local_aliases)
data.private_aliases.erase(name);
}
}
void ColumnAliasesMatcher::visit(ASTIdentifier & node, ASTPtr & ast, Data & data)
{
if (auto column_name = IdentifierSemantic::getColumnName(node))
{
if (data.array_join_result_columns.contains(*column_name) || data.array_join_source_columns.contains(*column_name)
|| data.private_aliases.contains(*column_name) || !data.columns.has(*column_name))
2020-12-12 16:42:15 +00:00
return;
const auto & col = data.columns.get(*column_name);
if (col.default_desc.kind == ColumnDefaultKind::Alias)
{
2021-05-21 17:01:21 +00:00
auto alias = node.tryGetAlias();
auto alias_expr = col.default_desc.expression->clone();
auto original_column = alias_expr->getColumnName();
// If expanded alias is used in array join, avoid expansion, otherwise the column will be mis-array joined
if (data.array_join_result_columns.contains(original_column) || data.array_join_source_columns.contains(original_column))
2021-05-21 17:01:21 +00:00
return;
2021-06-12 14:41:50 +00:00
ast = addTypeConversionToAST(std::move(alias_expr), col.type->getName(), data.columns.getAll(), data.context);
2021-06-15 10:37:22 +00:00
// We need to set back the original column name, or else the process of naming resolution will complain.
2021-05-21 17:01:21 +00:00
if (!alias.empty())
ast->setAlias(alias);
else
ast->setAlias(*column_name);
2021-07-18 15:27:19 +00:00
data.changed = true;
2020-12-15 08:35:19 +00:00
// revisit ast to track recursive alias columns
2020-12-12 16:42:15 +00:00
Visitor(data).visit(ast);
}
}
}
2020-12-12 16:42:15 +00:00
}