2018-10-24 15:31:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-11-01 14:49:37 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2020-09-09 09:58:59 +00:00
|
|
|
#include <Parsers/ASTLiteral.h>
|
2018-10-24 15:31:07 +00:00
|
|
|
#include <Parsers/ASTQueryWithTableAndOutput.h>
|
|
|
|
#include <Parsers/ASTRenameQuery.h>
|
2018-11-01 14:49:37 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
|
|
|
#include <Parsers/ASTSubquery.h>
|
|
|
|
#include <Parsers/ASTSelectWithUnionQuery.h>
|
2021-11-01 13:19:31 +00:00
|
|
|
#include <Parsers/ASTSelectIntersectExceptQuery.h>
|
2018-11-01 14:49:37 +00:00
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
2018-12-06 10:45:54 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2021-10-01 19:35:17 +00:00
|
|
|
#include <Parsers/ASTCreateQuery.h>
|
2018-11-01 17:07:20 +00:00
|
|
|
#include <Parsers/DumpASTNode.h>
|
2021-09-13 02:09:29 +00:00
|
|
|
#include <Parsers/ASTAlterQuery.h>
|
2019-01-16 12:58:27 +00:00
|
|
|
#include <Interpreters/DatabaseAndTableWithAlias.h>
|
2019-01-25 15:42:24 +00:00
|
|
|
#include <Interpreters/IdentifierSemantic.h>
|
2021-10-01 19:35:17 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/ExternalDictionariesLoader.h>
|
|
|
|
#include <Interpreters/misc.h>
|
2022-01-13 17:23:44 +00:00
|
|
|
#include <set>
|
2018-10-24 15:31:07 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-11-06 13:28:22 +00:00
|
|
|
/// Visitors consist of functions with unified interface 'void visit(Casted & x, ASTPtr & y)', there x is y, successfully casted to Casted.
|
2020-01-11 09:50:41 +00:00
|
|
|
/// Both types and function could have const specifiers. The second argument is used by visitor to replaces AST node (y) if needed.
|
2018-11-01 14:49:37 +00:00
|
|
|
|
|
|
|
/// Visits AST nodes, add default database to tables if not set. There's different logic for DDLs and selects.
|
2018-10-24 15:31:07 +00:00
|
|
|
class AddDefaultDatabaseVisitor
|
|
|
|
{
|
|
|
|
public:
|
2020-10-24 18:46:10 +00:00
|
|
|
explicit AddDefaultDatabaseVisitor(
|
2021-10-01 19:35:17 +00:00
|
|
|
ContextPtr context_,
|
|
|
|
const String & database_name_,
|
2022-01-14 08:16:00 +00:00
|
|
|
bool only_replace_current_database_function_ = false,
|
|
|
|
bool only_replace_in_join_ = false)
|
2021-10-01 19:35:17 +00:00
|
|
|
: context(context_)
|
|
|
|
, database_name(database_name_)
|
2020-10-24 18:46:10 +00:00
|
|
|
, only_replace_current_database_function(only_replace_current_database_function_)
|
2022-01-14 08:16:00 +00:00
|
|
|
, only_replace_in_join(only_replace_in_join_)
|
2022-01-13 17:23:44 +00:00
|
|
|
{
|
|
|
|
if (!context->isGlobalContext())
|
|
|
|
{
|
|
|
|
for (const auto & [table_name, _ /* storage */] : context->getExternalTables())
|
|
|
|
{
|
|
|
|
external_tables.insert(table_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-24 15:31:07 +00:00
|
|
|
|
2018-11-01 14:49:37 +00:00
|
|
|
void visitDDL(ASTPtr & ast) const
|
2018-10-24 15:31:07 +00:00
|
|
|
{
|
2018-11-01 14:49:37 +00:00
|
|
|
visitDDLChildren(ast);
|
2018-10-24 15:31:07 +00:00
|
|
|
|
2021-09-13 02:09:29 +00:00
|
|
|
if (!tryVisitDynamicCast<ASTAlterQuery>(ast) &&
|
|
|
|
!tryVisitDynamicCast<ASTQueryWithTableAndOutput>(ast) &&
|
2020-09-09 09:58:59 +00:00
|
|
|
!tryVisitDynamicCast<ASTRenameQuery>(ast) &&
|
|
|
|
!tryVisitDynamicCast<ASTFunction>(ast))
|
2018-10-24 15:31:07 +00:00
|
|
|
{}
|
|
|
|
}
|
|
|
|
|
2018-11-01 17:07:20 +00:00
|
|
|
void visit(ASTPtr & ast) const
|
|
|
|
{
|
|
|
|
if (!tryVisit<ASTSelectQuery>(ast) &&
|
2018-12-06 10:45:54 +00:00
|
|
|
!tryVisit<ASTSelectWithUnionQuery>(ast) &&
|
|
|
|
!tryVisit<ASTFunction>(ast))
|
|
|
|
visitChildren(*ast);
|
2018-11-01 17:07:20 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 14:49:37 +00:00
|
|
|
void visit(ASTSelectQuery & select) const
|
|
|
|
{
|
|
|
|
ASTPtr unused;
|
|
|
|
visit(select, unused);
|
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ASTSelectWithUnionQuery & select) const
|
|
|
|
{
|
|
|
|
ASTPtr unused;
|
|
|
|
visit(select, unused);
|
|
|
|
}
|
|
|
|
|
2021-10-01 19:35:17 +00:00
|
|
|
void visit(ASTColumns & columns) const
|
|
|
|
{
|
|
|
|
for (auto & child : columns.children)
|
|
|
|
visit(child);
|
|
|
|
}
|
|
|
|
|
2018-10-24 15:31:07 +00:00
|
|
|
private:
|
2021-10-01 19:35:17 +00:00
|
|
|
|
|
|
|
ContextPtr context;
|
|
|
|
|
2018-11-01 14:49:37 +00:00
|
|
|
const String database_name;
|
2022-01-13 17:23:44 +00:00
|
|
|
std::set<String> external_tables;
|
2021-10-01 19:35:17 +00:00
|
|
|
|
2020-09-09 09:58:59 +00:00
|
|
|
bool only_replace_current_database_function = false;
|
2022-01-14 08:16:00 +00:00
|
|
|
bool only_replace_in_join = false;
|
2018-11-01 14:49:37 +00:00
|
|
|
|
|
|
|
void visit(ASTSelectWithUnionQuery & select, ASTPtr &) const
|
|
|
|
{
|
|
|
|
for (auto & child : select.list_of_selects->children)
|
2021-11-01 13:19:31 +00:00
|
|
|
{
|
|
|
|
if (child->as<ASTSelectQuery>())
|
|
|
|
tryVisit<ASTSelectQuery>(child);
|
|
|
|
else if (child->as<ASTSelectIntersectExceptQuery>())
|
|
|
|
tryVisit<ASTSelectIntersectExceptQuery>(child);
|
|
|
|
}
|
2018-11-01 14:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ASTSelectQuery & select, ASTPtr &) const
|
|
|
|
{
|
2019-04-09 14:22:35 +00:00
|
|
|
if (select.tables())
|
|
|
|
tryVisit<ASTTablesInSelectQuery>(select.refTables());
|
2018-11-01 17:07:20 +00:00
|
|
|
|
2018-12-06 10:45:54 +00:00
|
|
|
visitChildren(select);
|
2018-11-01 14:49:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 13:19:31 +00:00
|
|
|
void visit(ASTSelectIntersectExceptQuery & select, ASTPtr &) const
|
|
|
|
{
|
2021-11-09 08:16:18 +00:00
|
|
|
for (auto & child : select.getListOfSelects())
|
2021-11-01 13:19:31 +00:00
|
|
|
{
|
|
|
|
if (child->as<ASTSelectQuery>())
|
|
|
|
tryVisit<ASTSelectQuery>(child);
|
|
|
|
else if (child->as<ASTSelectIntersectExceptQuery>())
|
|
|
|
tryVisit<ASTSelectIntersectExceptQuery>(child);
|
|
|
|
else if (child->as<ASTSelectWithUnionQuery>())
|
|
|
|
tryVisit<ASTSelectWithUnionQuery>(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 14:49:37 +00:00
|
|
|
void visit(ASTTablesInSelectQuery & tables, ASTPtr &) const
|
|
|
|
{
|
|
|
|
for (auto & child : tables.children)
|
|
|
|
tryVisit<ASTTablesInSelectQueryElement>(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ASTTablesInSelectQueryElement & tables_element, ASTPtr &) const
|
|
|
|
{
|
2022-01-14 08:16:00 +00:00
|
|
|
if (only_replace_in_join && !tables_element.table_join)
|
|
|
|
return;
|
|
|
|
|
2018-11-01 14:49:37 +00:00
|
|
|
if (tables_element.table_expression)
|
|
|
|
tryVisit<ASTTableExpression>(tables_element.table_expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ASTTableExpression & table_expression, ASTPtr &) const
|
|
|
|
{
|
|
|
|
if (table_expression.database_and_table_name)
|
2020-10-26 15:49:00 +00:00
|
|
|
tryVisit<ASTTableIdentifier>(table_expression.database_and_table_name);
|
2018-11-01 14:49:37 +00:00
|
|
|
else if (table_expression.subquery)
|
|
|
|
tryVisit<ASTSubquery>(table_expression.subquery);
|
|
|
|
}
|
2018-10-24 15:31:07 +00:00
|
|
|
|
2020-10-26 15:49:00 +00:00
|
|
|
void visit(const ASTTableIdentifier & identifier, ASTPtr & ast) const
|
2018-10-24 15:31:07 +00:00
|
|
|
{
|
2022-01-13 17:23:44 +00:00
|
|
|
/// 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;
|
2018-10-24 15:31:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 14:49:37 +00:00
|
|
|
void visit(ASTSubquery & subquery, ASTPtr &) const
|
2018-10-24 15:31:07 +00:00
|
|
|
{
|
2018-11-01 14:49:37 +00:00
|
|
|
tryVisit<ASTSelectWithUnionQuery>(subquery.children[0]);
|
|
|
|
}
|
|
|
|
|
2018-12-06 10:45:54 +00:00
|
|
|
void visit(ASTFunction & function, ASTPtr &) const
|
2018-11-01 17:07:20 +00:00
|
|
|
{
|
2021-10-01 19:35:17 +00:00
|
|
|
bool is_operator_in = functionIsInOrGlobalInOperator(function.name);
|
|
|
|
bool is_dict_get = functionIsDictGet(function.name);
|
2018-12-06 10:45:54 +00:00
|
|
|
|
|
|
|
for (auto & child : function.children)
|
|
|
|
{
|
|
|
|
if (child.get() == function.arguments.get())
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < child->children.size(); ++i)
|
|
|
|
{
|
2021-10-01 19:35:17 +00:00
|
|
|
if (is_dict_get && i == 0)
|
|
|
|
{
|
|
|
|
if (auto * identifier = child->children[i]->as<ASTIdentifier>())
|
|
|
|
{
|
2021-10-14 19:12:06 +00:00
|
|
|
/// Identifier already qualified
|
2021-10-01 19:35:17 +00:00
|
|
|
if (identifier->compound())
|
|
|
|
continue;
|
|
|
|
|
2021-10-14 19:12:06 +00:00
|
|
|
auto qualified_dictionary_name = context->getExternalDictionariesLoader().qualifyDictionaryNameWithDatabase(identifier->name(), context);
|
|
|
|
child->children[i] = std::make_shared<ASTIdentifier>(qualified_dictionary_name.getParts());
|
2021-10-01 19:35:17 +00:00
|
|
|
}
|
|
|
|
else if (auto * literal = child->children[i]->as<ASTLiteral>())
|
|
|
|
{
|
|
|
|
auto & literal_value = literal->value;
|
|
|
|
|
|
|
|
if (literal_value.getType() != Field::Types::String)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto dictionary_name = literal_value.get<String>();
|
2021-10-14 19:12:06 +00:00
|
|
|
auto qualified_dictionary_name = context->getExternalDictionariesLoader().qualifyDictionaryNameWithDatabase(dictionary_name, context);
|
|
|
|
literal_value = qualified_dictionary_name.getFullName();
|
2021-10-01 19:35:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (is_operator_in && i == 1)
|
2018-12-06 10:45:54 +00:00
|
|
|
{
|
2021-04-14 15:35:52 +00:00
|
|
|
/// XXX: for some unknown reason this place assumes that argument can't be an alias,
|
|
|
|
/// like in the similar code in `MarkTableIdentifierVisitor`.
|
|
|
|
if (auto * identifier = child->children[i]->as<ASTIdentifier>())
|
2021-09-30 15:56:55 +00:00
|
|
|
{
|
2021-09-30 15:59:49 +00:00
|
|
|
/// If identifier is broken then we can do nothing and get an exception
|
2021-09-30 15:56:55 +00:00
|
|
|
auto maybe_table_identifier = identifier->createTable();
|
|
|
|
if (maybe_table_identifier)
|
|
|
|
child->children[i] = maybe_table_identifier;
|
|
|
|
}
|
2021-04-14 15:35:52 +00:00
|
|
|
|
2018-12-06 10:45:54 +00:00
|
|
|
/// Second argument of the "in" function (or similar) may be a table name or a subselect.
|
|
|
|
/// Rewrite the table name or descend into subselect.
|
2020-10-26 15:49:00 +00:00
|
|
|
if (!tryVisit<ASTTableIdentifier>(child->children[i]))
|
2018-12-06 10:45:54 +00:00
|
|
|
visit(child->children[i]);
|
|
|
|
}
|
|
|
|
else
|
2021-10-01 19:35:17 +00:00
|
|
|
{
|
2018-12-06 10:45:54 +00:00
|
|
|
visit(child->children[i]);
|
2021-10-01 19:35:17 +00:00
|
|
|
}
|
2018-12-06 10:45:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2021-10-01 19:35:17 +00:00
|
|
|
{
|
2018-12-06 10:45:54 +00:00
|
|
|
visit(child);
|
2021-10-01 19:35:17 +00:00
|
|
|
}
|
2018-12-06 10:45:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitChildren(IAST & ast) const
|
|
|
|
{
|
|
|
|
for (auto & child : ast.children)
|
2018-11-01 17:07:20 +00:00
|
|
|
visit(child);
|
|
|
|
}
|
|
|
|
|
2018-11-01 14:49:37 +00:00
|
|
|
template <typename T>
|
|
|
|
bool tryVisit(ASTPtr & ast) const
|
|
|
|
{
|
|
|
|
if (T * t = typeid_cast<T *>(ast.get()))
|
|
|
|
{
|
|
|
|
visit(*t, ast);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void visitDDL(ASTQueryWithTableAndOutput & node, ASTPtr &) const
|
|
|
|
{
|
2020-09-09 09:58:59 +00:00
|
|
|
if (only_replace_current_database_function)
|
|
|
|
return;
|
|
|
|
|
2021-11-11 13:28:18 +00:00
|
|
|
if (!node.database)
|
2021-09-06 22:13:54 +00:00
|
|
|
node.setDatabase(database_name);
|
2018-11-01 14:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void visitDDL(ASTRenameQuery & node, ASTPtr &) const
|
|
|
|
{
|
2020-09-09 09:58:59 +00:00
|
|
|
if (only_replace_current_database_function)
|
|
|
|
return;
|
|
|
|
|
2018-11-01 14:49:37 +00:00
|
|
|
for (ASTRenameQuery::Element & elem : node.elements)
|
2018-10-24 15:31:07 +00:00
|
|
|
{
|
|
|
|
if (elem.from.database.empty())
|
2018-11-01 14:49:37 +00:00
|
|
|
elem.from.database = database_name;
|
2018-10-24 15:31:07 +00:00
|
|
|
if (elem.to.database.empty())
|
2018-11-01 14:49:37 +00:00
|
|
|
elem.to.database = database_name;
|
2018-10-24 15:31:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 02:09:29 +00:00
|
|
|
void visitDDL(ASTAlterQuery & node, ASTPtr &) const
|
|
|
|
{
|
|
|
|
if (only_replace_current_database_function)
|
|
|
|
return;
|
|
|
|
|
2021-11-11 13:28:18 +00:00
|
|
|
if (!node.database)
|
2021-10-12 18:04:25 +00:00
|
|
|
node.setDatabase(database_name);
|
2021-09-13 02:09:29 +00:00
|
|
|
|
|
|
|
for (const auto & child : node.command_list->children)
|
|
|
|
{
|
|
|
|
auto * command_ast = child->as<ASTAlterCommand>();
|
|
|
|
if (command_ast->from_database.empty())
|
|
|
|
command_ast->from_database = database_name;
|
|
|
|
if (command_ast->to_database.empty())
|
|
|
|
command_ast->to_database = database_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 09:58:59 +00:00
|
|
|
void visitDDL(ASTFunction & function, ASTPtr & node) const
|
|
|
|
{
|
|
|
|
if (function.name == "currentDatabase")
|
|
|
|
{
|
|
|
|
node = std::make_shared<ASTLiteral>(database_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 14:49:37 +00:00
|
|
|
void visitDDLChildren(ASTPtr & ast) const
|
2018-10-24 15:31:07 +00:00
|
|
|
{
|
|
|
|
for (auto & child : ast->children)
|
2018-11-01 14:49:37 +00:00
|
|
|
visitDDL(child);
|
2018-10-24 15:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2018-11-01 14:49:37 +00:00
|
|
|
bool tryVisitDynamicCast(ASTPtr & ast) const
|
2018-10-24 15:31:07 +00:00
|
|
|
{
|
|
|
|
if (T * t = dynamic_cast<T *>(ast.get()))
|
|
|
|
{
|
2018-11-01 14:49:37 +00:00
|
|
|
visitDDL(*t, ast);
|
2018-10-24 15:31:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|