1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#pragma once
#include <Interpreters/IInterpreter.h>
#include <Parsers/ASTRenameQuery.h>
namespace DB
{
class AccessRightsElements;
class DDLGuard;
/// To avoid deadlocks, we must acquire locks for tables in same order in any different RENAMEs.
struct UniqueTableName
{
String database_name;
String table_name;
UniqueTableName(const String & database_name_, const String & table_name_)
: database_name(database_name_), table_name(table_name_) {}
bool operator< (const UniqueTableName & rhs) const
{
return std::tie(database_name, table_name) < std::tie(rhs.database_name, rhs.table_name);
}
};
struct RenameDescription
{
RenameDescription(const ASTRenameQuery::Element & elem, const String & current_database) :
from_database_name(!elem.from.database ? current_database : elem.from.getDatabase()),
from_table_name(elem.from.getTable()),
to_database_name(!elem.to.database ? current_database : elem.to.getDatabase()),
to_table_name(elem.to.getTable()),
if_exists(elem.if_exists)
{}
String from_database_name;
String from_table_name;
String to_database_name;
String to_table_name;
bool if_exists;
};
using RenameDescriptions = std::vector<RenameDescription>;
using TableGuards = std::map<UniqueTableName, std::unique_ptr<DDLGuard>>;
/** Rename one table
* or rename many tables at once.
*/
class InterpreterRenameQuery : public IInterpreter, WithContext
{
public:
InterpreterRenameQuery(const ASTPtr & query_ptr_, ContextPtr context_);
BlockIO execute() override;
void extendQueryLogElemImpl(QueryLogElement & elem, const ASTPtr & ast, ContextPtr) const override;
bool renamedInsteadOfExchange() const { return renamed_instead_of_exchange; }
private:
BlockIO executeToTables(const ASTRenameQuery & rename, const RenameDescriptions & descriptions, TableGuards & ddl_guards);
BlockIO executeToDatabase(const ASTRenameQuery & rename, const RenameDescriptions & descriptions);
enum class RenameType
{
RenameTable,
RenameDatabase
};
AccessRightsElements getRequiredAccess(RenameType type) const;
ASTPtr query_ptr;
bool renamed_instead_of_exchange{false};
};
}
|