-
Notifications
You must be signed in to change notification settings - Fork 411
/
InterpreterDropQuery.cpp
235 lines (191 loc) · 8.21 KB
/
InterpreterDropQuery.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright 2022 PingCAP, Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <Common/FailPoint.h>
#include <Common/escapeForFileName.h>
#include <Common/typeid_cast.h>
#include <Databases/IDatabase.h>
#include <Encryption/FileProvider.h>
#include <Interpreters/Context.h>
#include <Interpreters/InterpreterDropQuery.h>
#include <Parsers/ASTDropQuery.h>
#include <Poco/File.h>
#include <Storages/IManageableStorage.h>
#include <Storages/IStorage.h>
#include <common/logger_useful.h>
#include <ext/scope_guard.h>
namespace DB
{
namespace ErrorCodes
{
extern const int TABLE_WAS_NOT_DROPPED;
extern const int DATABASE_NOT_EMPTY;
extern const int UNKNOWN_DATABASE;
extern const int READONLY;
extern const int FAIL_POINT_ERROR;
} // namespace ErrorCodes
namespace FailPoints
{
extern const char exception_between_drop_meta_and_data[];
}
InterpreterDropQuery::InterpreterDropQuery(const ASTPtr & query_ptr_, Context & context_)
: query_ptr(query_ptr_)
, context(context_)
{}
BlockIO InterpreterDropQuery::execute()
{
ASTDropQuery & drop = typeid_cast<ASTDropQuery &>(*query_ptr);
checkAccess(drop);
String path = context.getPath();
String current_database = context.getCurrentDatabase();
bool drop_database = drop.table.empty() && !drop.database.empty();
if (drop_database && drop.detach)
{
auto database = context.detachDatabase(drop.database);
database->shutdown();
return {};
}
/// Drop temporary table.
if (drop.database.empty() || drop.temporary)
{
StoragePtr table = (context.hasSessionContext() ? context.getSessionContext() : context).tryRemoveExternalTable(drop.table);
if (table)
{
if (drop.database.empty() && !drop.temporary)
{
LOG_WARNING(
(&Poco::Logger::get("InterpreterDropQuery")),
"It is recommended to use `DROP TEMPORARY TABLE` to delete temporary tables");
}
table->shutdown();
/// If table was already dropped by anyone, an exception will be thrown
auto table_lock = table->lockExclusively(context.getCurrentQueryId(), drop.lock_timeout);
/// Delete table data
table->drop();
table->is_dropped = true;
return {};
}
}
String database_name = drop.database.empty() ? current_database : drop.database;
String database_name_escaped = escapeForFileName(database_name);
auto database = context.tryGetDatabase(database_name);
if (!database && !drop.if_exists)
throw Exception("Database " + database_name + " doesn't exist", ErrorCodes::UNKNOWN_DATABASE);
std::vector<std::pair<StoragePtr, std::unique_ptr<DDLGuard>>> tables_to_drop;
if (!drop_database)
{
StoragePtr table;
if (drop.if_exists)
table = context.tryGetTable(database_name, drop.table);
else
table = context.getTable(database_name, drop.table);
if (table)
tables_to_drop.emplace_back(table,
context.getDDLGuard(
database_name,
drop.table,
"Table " + database_name + "." + drop.table + " is dropping or detaching right now"));
else
return {};
}
else
{
if (!database)
{
if (!drop.if_exists)
throw Exception("Database " + database_name + " doesn't exist", ErrorCodes::UNKNOWN_DATABASE);
return {};
}
for (auto iterator = database->getIterator(context); iterator->isValid(); iterator->next())
tables_to_drop.emplace_back(iterator->table(),
context.getDDLGuard(database_name,
iterator->name(),
"Table " + database_name + "." + iterator->name() + " is dropping or detaching right now"));
}
for (auto & table : tables_to_drop)
{
if (!drop.detach)
{
if (!table.first->checkTableCanBeDropped())
throw Exception(
"Table " + database_name + "." + table.first->getTableName() + " couldn't be dropped due to failed pre-drop check",
ErrorCodes::TABLE_WAS_NOT_DROPPED);
}
/// If table was already dropped by anyone, an exception will be thrown;
/// If can not acquire the drop lock on table within `drop.lock_timeout`,
/// an exception will be thrown;
auto table_lock = table.first->lockExclusively(context.getCurrentQueryId(), drop.lock_timeout);
table.first->shutdown();
String current_table_name = table.first->getTableName();
if (drop.detach)
{
/// Drop table from memory, don't touch data and metadata
database->detachTable(current_table_name);
}
else
{
/// Clear storage data first, and if tiflash crash in the middle of `clearData`,
/// this table can still be restored, and can call `clearData` again.
table.first->clearData();
/// Delete table metdata and table itself from memory
database->removeTable(context, current_table_name);
SCOPE_EXIT({
// Once the storage's metadata removed from disk, we should ensure that it is removed from TMTStorages
if (auto storage = std::dynamic_pointer_cast<IManageableStorage>(table.first); storage)
storage->removeFromTMTContext();
});
FAIL_POINT_TRIGGER_EXCEPTION(FailPoints::exception_between_drop_meta_and_data);
/// Delete table data
table.first->drop();
table.first->is_dropped = true;
/// If it is not virtual database like Dictionary then drop remaining data dir
const String database_data_path = database->getDataPath();
if (!database_data_path.empty())
{
String table_data_path
= database_data_path + (endsWith(database_data_path, "/") ? "" : "/") + escapeForFileName(current_table_name);
if (Poco::File(table_data_path).exists())
{
context.getFileProvider()->deleteDirectory(table_data_path, false, true);
}
}
}
}
if (drop_database)
{
/// Delete the database. The tables in it have already been deleted.
auto lock = context.getLock();
/// Someone could have time to delete the database before us.
context.assertDatabaseExists(database_name);
/// Someone could have time to create a table in the database to be deleted while we deleted the tables without the context lock.
if (!context.getDatabase(database_name)->empty(context))
throw Exception("New table appeared in database being dropped. Try dropping it again.", ErrorCodes::DATABASE_NOT_EMPTY);
/// Delete database information from the RAM
auto database = context.detachDatabase(database_name);
/// Delete the database and remove its data / meta directory if need.
database->drop(context);
}
return {};
}
void InterpreterDropQuery::checkAccess(const ASTDropQuery & drop)
{
const Settings & settings = context.getSettingsRef();
auto readonly = settings.readonly;
/// It's allowed to drop temporary tables.
if (!readonly || (drop.database.empty() && context.tryGetExternalTable(drop.table) && readonly >= 2))
{
return;
}
throw Exception("Cannot drop table in readonly mode", ErrorCodes::READONLY);
}
} // namespace DB