1#include <Parsers/ASTRenameQuery.h>
2#include <Databases/IDatabase.h>
3#include <Interpreters/Context.h>
4#include <Interpreters/InterpreterRenameQuery.h>
5#include <Storages/IStorage.h>
6#include <Interpreters/DDLWorker.h>
7#include <Common/typeid_cast.h>
8
9
10namespace DB
11{
12
13
14InterpreterRenameQuery::InterpreterRenameQuery(const ASTPtr & query_ptr_, Context & context_)
15 : query_ptr(query_ptr_), context(context_)
16{
17}
18
19
20struct RenameDescription
21{
22 RenameDescription(const ASTRenameQuery::Element & elem, const String & current_database) :
23 from_database_name(elem.from.database.empty() ? current_database : elem.from.database),
24 from_table_name(elem.from.table),
25 to_database_name(elem.to.database.empty() ? current_database : elem.to.database),
26 to_table_name(elem.to.table)
27 {}
28
29 String from_database_name;
30 String from_table_name;
31
32 String to_database_name;
33 String to_table_name;
34};
35
36
37BlockIO InterpreterRenameQuery::execute()
38{
39 const auto & rename = query_ptr->as<ASTRenameQuery &>();
40
41 if (!rename.cluster.empty())
42 {
43 NameSet databases;
44 for (const auto & elem : rename.elements)
45 {
46 databases.emplace(elem.from.database);
47 databases.emplace(elem.to.database);
48 }
49
50 return executeDDLQueryOnCluster(query_ptr, context, std::move(databases));
51 }
52
53 String path = context.getPath();
54 String current_database = context.getCurrentDatabase();
55
56 /** In case of error while renaming, it is possible that only part of tables was renamed
57 * or we will be in inconsistent state. (It is worth to be fixed.)
58 */
59
60 std::vector<RenameDescription> descriptions;
61 descriptions.reserve(rename.elements.size());
62
63 /// To avoid deadlocks, we must acquire locks for tables in same order in any different RENAMES.
64 struct UniqueTableName
65 {
66 String database_name;
67 String table_name;
68
69 UniqueTableName(const String & database_name_, const String & table_name_)
70 : database_name(database_name_), table_name(table_name_) {}
71
72 bool operator< (const UniqueTableName & rhs) const
73 {
74 return std::tie(database_name, table_name) < std::tie(rhs.database_name, rhs.table_name);
75 }
76 };
77
78 /// Don't allow to drop tables (that we are renaming); don't allow to create tables in places where tables will be renamed.
79 std::map<UniqueTableName, std::unique_ptr<DDLGuard>> table_guards;
80
81 for (const auto & elem : rename.elements)
82 {
83 descriptions.emplace_back(elem, current_database);
84
85 UniqueTableName from(descriptions.back().from_database_name, descriptions.back().from_table_name);
86 UniqueTableName to(descriptions.back().to_database_name, descriptions.back().to_table_name);
87
88 table_guards[from];
89 table_guards[to];
90 }
91
92 /// Must do it in consistent order.
93 for (auto & table_guard : table_guards)
94 table_guard.second = context.getDDLGuard(table_guard.first.database_name, table_guard.first.table_name);
95
96 for (auto & elem : descriptions)
97 {
98 context.assertTableDoesntExist(elem.to_database_name, elem.to_table_name);
99 auto from_table = context.getTable(elem.from_database_name, elem.from_table_name);
100 auto from_table_lock = from_table->lockExclusively(context.getCurrentQueryId());
101
102 context.getDatabase(elem.from_database_name)->renameTable(
103 context,
104 elem.from_table_name,
105 *context.getDatabase(elem.to_database_name),
106 elem.to_table_name,
107 from_table_lock);
108 }
109
110 return {};
111}
112
113
114}
115