Fix AddDefaultDatabaseVisitor for temporary tables

This will fix rewriting of distributed queries with GLOBAL IN

v2: get external tables only for non-global context

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
This commit is contained in:
Azat Khuzhin 2022-01-13 20:23:44 +03:00
parent 0a9b1ee803
commit 249bdce453

View File

@ -19,6 +19,7 @@
#include <Interpreters/Context.h>
#include <Interpreters/ExternalDictionariesLoader.h>
#include <Interpreters/misc.h>
#include <set>
namespace DB
{
@ -37,7 +38,15 @@ public:
: context(context_)
, database_name(database_name_)
, only_replace_current_database_function(only_replace_current_database_function_)
{}
{
if (!context->isGlobalContext())
{
for (const auto & [table_name, _ /* storage */] : context->getExternalTables())
{
external_tables.insert(table_name);
}
}
}
void visitDDL(ASTPtr & ast) const
{
@ -81,6 +90,7 @@ private:
ContextPtr context;
const String database_name;
std::set<String> external_tables;
bool only_replace_current_database_function = false;
@ -138,13 +148,17 @@ private:
void visit(const ASTTableIdentifier & identifier, ASTPtr & ast) const
{
if (!identifier.compound())
{
auto qualified_identifier = std::make_shared<ASTTableIdentifier>(database_name, identifier.name());
if (!identifier.alias.empty())
qualified_identifier->setAlias(identifier.alias);
ast = qualified_identifier;
}
/// Already has database.
if (identifier.compound())
return;
/// There is temporary table with such name, should not be rewritten.
if (external_tables.count(identifier.shortName()))
return;
auto qualified_identifier = std::make_shared<ASTTableIdentifier>(database_name, identifier.name());
if (!identifier.alias.empty())
qualified_identifier->setAlias(identifier.alias);
ast = qualified_identifier;
}
void visit(ASTSubquery & subquery, ASTPtr &) const