This commit is contained in:
Yarik Briukhovetskyi 2024-11-20 13:04:04 +01:00 committed by GitHub
parent ed4d955df6
commit bbeb3f61e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 2 deletions

View File

@ -114,17 +114,33 @@ void QueryAliasesMatcher<T>::visit(const ASTSubquery & const_subquery, const AST
subquery.prefer_alias_to_column_name = true; subquery.prefer_alias_to_column_name = true;
} }
template <typename T>
bool QueryAliasesMatcher<T>::checkIfNamesAreSame(Data & data, const ASTPtr & ast)
{
String name = ast->getColumnName();
for (const auto & obj : data)
{
if (obj.second->getColumnName() == name)
return true;
}
return false;
}
template <typename T> template <typename T>
void QueryAliasesMatcher<T>::visitOther(const ASTPtr & ast, Data & data) void QueryAliasesMatcher<T>::visitOther(const ASTPtr & ast, Data & data)
{ {
auto & aliases = data; auto & aliases = data;
String alias = ast->tryGetAlias(); String alias = ast->tryGetAlias();
if (!alias.empty()) if (!alias.empty())
{ {
if (aliases.contains(alias) && ast->getTreeHash(/*ignore_aliases=*/ true) != aliases[alias]->getTreeHash(/*ignore_aliases=*/ true)) if (aliases.contains(alias) && ast->getTreeHash(/*ignore_aliases=*/ true) != aliases[alias]->getTreeHash(/*ignore_aliases=*/ true))
{
if (checkIfNamesAreSame(aliases, ast))
throw Exception(wrongAliasMessage(ast, aliases[alias], alias), ErrorCodes::MULTIPLE_EXPRESSIONS_FOR_ALIAS); throw Exception(wrongAliasMessage(ast, aliases[alias], alias), ErrorCodes::MULTIPLE_EXPRESSIONS_FOR_ALIAS);
}
aliases[alias] = ast; aliases[ast->getColumnNameWithoutAlias()] = ast;
} }
/** QueryAliasesVisitor is executed before ExecuteScalarSubqueriesVisitor. /** QueryAliasesVisitor is executed before ExecuteScalarSubqueriesVisitor.

View File

@ -38,6 +38,7 @@ private:
static void visit(const ASTSubquery & subquery, const ASTPtr & ast, Data & data); static void visit(const ASTSubquery & subquery, const ASTPtr & ast, Data & data);
static void visit(const ASTArrayJoin &, const ASTPtr & ast, Data & data); static void visit(const ASTArrayJoin &, const ASTPtr & ast, Data & data);
static void visitOther(const ASTPtr & ast, Data & data); static void visitOther(const ASTPtr & ast, Data & data);
static bool checkIfNamesAreSame(Data & data, const ASTPtr & ast);
}; };
/// Visits AST nodes and collect their aliases in one map (with links to source nodes). /// Visits AST nodes and collect their aliases in one map (with links to source nodes).