2023-08-22 16:24:41 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <Analyzer/createUniqueTableAliases.h>
|
|
|
|
#include <Analyzer/InDepthQueryTreeVisitor.h>
|
|
|
|
#include <Analyzer/IQueryTreeNode.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
class CreateUniqueTableAliasesVisitor : public InDepthQueryTreeVisitorWithContext<CreateUniqueTableAliasesVisitor>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Base = InDepthQueryTreeVisitorWithContext<CreateUniqueTableAliasesVisitor>;
|
|
|
|
|
|
|
|
explicit CreateUniqueTableAliasesVisitor(const ContextPtr & context)
|
|
|
|
: Base(context)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void enterImpl(QueryTreeNodePtr & node)
|
|
|
|
{
|
|
|
|
switch (node->getNodeType())
|
|
|
|
{
|
|
|
|
case QueryTreeNodeType::QUERY:
|
|
|
|
[[fallthrough]];
|
|
|
|
case QueryTreeNodeType::UNION:
|
2023-08-22 17:14:24 +00:00
|
|
|
{
|
2023-08-22 16:24:41 +00:00
|
|
|
if (getSubqueryDepth() == 1)
|
2023-08-22 17:14:24 +00:00
|
|
|
{
|
|
|
|
node->removeAlias();
|
2023-08-22 16:24:41 +00:00
|
|
|
break;
|
2023-08-22 17:14:24 +00:00
|
|
|
}
|
2023-08-22 16:24:41 +00:00
|
|
|
[[fallthrough]];
|
2023-08-22 17:14:24 +00:00
|
|
|
}
|
2023-08-22 16:24:41 +00:00
|
|
|
case QueryTreeNodeType::TABLE:
|
|
|
|
[[fallthrough]];
|
|
|
|
case QueryTreeNodeType::TABLE_FUNCTION:
|
|
|
|
{
|
|
|
|
auto & alias = table_expression_to_alias[node];
|
|
|
|
if (alias.empty())
|
|
|
|
{
|
|
|
|
alias = fmt::format("__table{}", table_expression_to_alias.size());
|
|
|
|
node->setAlias(alias);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
// We need to use raw pointer as a key, not a QueryTreeNodePtrWithHash.
|
|
|
|
std::unordered_map<QueryTreeNodePtr, String> table_expression_to_alias;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void createUniqueTableAliases(QueryTreeNodePtr & node, const ContextPtr & context)
|
|
|
|
{
|
|
|
|
CreateUniqueTableAliasesVisitor(context).visit(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|