forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathASTDropQuery.cpp
More file actions
344 lines (293 loc) · 13.1 KB
/
Copy pathASTDropQuery.cpp
File metadata and controls
344 lines (293 loc) · 13.1 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <Parsers/ASTDropQuery.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTLiteral.h>
#include <Common/quoteString.h>
#include <IO/Operators.h>
#include <Parsers/ASTJSONHelpers.h>
#include <Parsers/ASTJSONReadHelpers.h>
namespace DB
{
namespace ErrorCodes
{
extern const int SYNTAX_ERROR;
extern const int BAD_ARGUMENTS;
}
String ASTDropQuery::getID(char delim) const
{
if (kind == ASTDropQuery::Kind::Drop)
return "DropQuery" + (delim + getDatabase()) + delim + getTable();
if (kind == ASTDropQuery::Kind::Detach)
return "DetachQuery" + (delim + getDatabase()) + delim + getTable();
if (kind == ASTDropQuery::Kind::Truncate)
return "TruncateQuery" + (delim + getDatabase()) + delim + getTable();
throw Exception(ErrorCodes::SYNTAX_ERROR, "Not supported kind of drop query.");
}
ASTPtr ASTDropQuery::clone() const
{
auto res = make_intrusive<ASTDropQuery>(*this);
res->children.clear();
cloneTableOptions(*res);
if (database_and_tables)
{
res->database_and_tables = database_and_tables->clone();
res->children.push_back(res->database_and_tables);
}
cloneOutputOptions(*res);
return res;
}
void ASTDropQuery::writeJSON(WriteBuffer & out) const
{
JSONObjectWriter w(out, "DropQuery");
w.writeString("database", getDatabase());
w.writeString("table", getTable());
if (!cluster.empty())
w.writeString("cluster", cluster);
const char * kind_str = "Drop";
if (kind == Kind::Detach)
kind_str = "Detach";
else if (kind == Kind::Truncate)
kind_str = "Truncate";
w.writeString("kind", std::string_view(kind_str));
w.writeBool("if_exists", if_exists);
w.writeBool("if_empty", if_empty);
w.writeBool("no_ddl_lock", no_ddl_lock);
w.writeBool("has_all", has_all);
w.writeBool("has_tables", has_tables);
if (!like.empty())
w.writeString("like", like);
w.writeBool("not_like", not_like);
w.writeBool("case_insensitive_like", case_insensitive_like);
w.writeBool("is_dictionary", is_dictionary);
w.writeBool("is_view", is_view);
w.writeBool("sync", sync);
w.writeBool("permanently", permanently);
/// `TEMPORARY` is part of the formatted DDL (`DROP TEMPORARY TABLE ...`) and selects a
/// different target object class, so it must survive the round-trip.
if (isTemporary())
w.writeBool("is_temporary", true);
w.writeChild("database_and_tables", database_and_tables);
/// Serialize the database/table identifier ASTs as well so that parameterized
/// names like `{tbl:Identifier}` survive the round-trip. The plain `database`/`table`
/// strings above lose them (they stringify to empty), so we restore from these on read.
w.writeChild("database_ast", database);
w.writeChild("table_ast", table);
writeOutputOptionsJSON(w);
}
void ASTDropQuery::readJSON(const Poco::JSON::Object & json)
{
JSONObjectReader r(json);
/// Restore the full identifier ASTs first, falling back to the plain string names only when the
/// AST key is absent. `writeJSON` emits both forms, so reading AST-first (like `ASTDropIndexQuery`/
/// `ASTAlterQuery`) avoids leaving a stale extra `children` entry from `setDatabase`/`setTable` that
/// no longer matches the `database`/`table` member. The AST form also preserves parameterized names
/// like `{tbl:Identifier}` that the string form cannot represent. These slots are parser-produced
/// identifiers; `getDatabase`/`getTable` read them via `tryGetIdentifierNameInto`, so reject other
/// node types here.
if (auto database_child = r.readIdentifierChild("database_ast"))
{
database = database_child;
children.push_back(database);
}
else
{
String db = r.getString("database");
if (!db.empty())
setDatabase(db);
}
if (auto table_child = r.readIdentifierChild("table_ast"))
{
table = table_child;
children.push_back(table);
}
else
{
String tbl = r.getString("table");
if (!tbl.empty())
setTable(tbl);
}
cluster = r.getString("cluster");
String kind_str = r.getString("kind");
if (kind_str == "Drop")
kind = Kind::Drop;
else if (kind_str == "Detach")
kind = Kind::Detach;
else if (kind_str == "Truncate")
kind = Kind::Truncate;
else
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Unknown DropQuery kind: '{}'", kind_str);
if_exists = r.getBool("if_exists");
if_empty = r.getBool("if_empty");
no_ddl_lock = r.getBool("no_ddl_lock");
has_all = r.getBool("has_all");
has_tables = r.getBool("has_tables");
like = r.getString("like");
not_like = r.getBool("not_like");
case_insensitive_like = r.getBool("case_insensitive_like");
is_dictionary = r.getBool("is_dictionary");
is_view = r.getBool("is_view");
sync = r.getBool("sync");
permanently = r.getBool("permanently");
if (r.getBool("is_temporary"))
setIsTemporary(true);
/// `database_and_tables` is parser-owned as an `ASTExpressionList` of `ASTTableIdentifier`s.
/// `formatQueryImpl` downcasts it with `as<ASTExpressionList &>()` and casts each entry to
/// `ASTTableIdentifier`, so reject any other shape from malformed `clickhouse_json` here.
auto child = r.readChildOfType<ASTExpressionList>("database_and_tables");
if (child)
{
for (const auto & entry : child->children)
if (!entry || !entry->as<ASTTableIdentifier>())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Each entry of 'database_and_tables' must be a table identifier during AST JSON deserialization");
database_and_tables = child;
children.push_back(database_and_tables);
}
/// `formatQueryImpl` unconditionally dereferences `table` in the single-table branch.
/// Require at least one valid target so we cannot construct an AST that crashes on formatting.
if (!table && !database && !database_and_tables)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "`DropQuery` must specify at least one of 'database', 'table', or 'database_and_tables' during AST JSON deserialization");
/// `has_all`/`has_tables` are produced by the parser only for `TRUNCATE [ALL] TABLES FROM <db>`,
/// whose shape is `kind == Truncate`, a single `database` target, and no `table`/`database_and_tables`.
/// Any other combination is parser-impossible and would make the formatted SQL disagree with the
/// executed operation (e.g. `kind == Drop` with `has_tables` formats as `DROP TABLES FROM db` while
/// `InterpreterDropQuery` runs `DROP DATABASE`). Reject it.
if (has_all || has_tables)
{
if (kind != Kind::Truncate)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "'has_all'/'has_tables' are only valid for TRUNCATE during AST JSON deserialization");
if (!has_tables)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "'has_all' requires 'has_tables' (TRUNCATE ALL TABLES FROM) during AST JSON deserialization");
if (!database || table || database_and_tables)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "TRUNCATE TABLES FROM requires a single 'database' target during AST JSON deserialization");
}
/// `TEMPORARY` is parsed only in the table-target branch (`DROP|DETACH|TRUNCATE TEMPORARY
/// [TABLE|VIEW|DICTIONARY] ...`), never for a `DATABASE` or `TABLES FROM` target. A database-only
/// AST with `is_temporary` formats as parser-impossible `DROP TEMPORARY DATABASE ...` while
/// `InterpreterDropQuery` still dispatches on `database` and drops the database. Reject it.
if (isTemporary() && !table && !database_and_tables)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "'is_temporary' requires a table target ('table' or 'database_and_tables') during AST JSON deserialization");
/// `PERMANENTLY` is parsed only for `DETACH`; on `DROP`/`TRUNCATE` it would format as
/// parser-impossible `... PERMANENTLY` SQL that execution ignores. Reject it.
if (permanently && kind != Kind::Detach)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "'permanently' is only valid for DETACH during AST JSON deserialization");
/// The LIKE filter (`like`/`not_like`/`case_insensitive_like`) is parsed only in the
/// `TRUNCATE [ALL] TABLES FROM <db>` branch. `formatQueryImpl` prints it unconditionally while
/// `InterpreterDropQuery` consults it only when `kind == Truncate && has_tables`, so on any other
/// shape the formatted SQL is parser-impossible and the filter is silently ignored. Reject it,
/// including the orphaned modifier flags without a pattern.
if ((!like.empty() || not_like || case_insensitive_like) && !has_tables)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "'like', 'not_like' and 'case_insensitive_like' are only valid for TRUNCATE [ALL] TABLES FROM during AST JSON deserialization");
if (like.empty() && (not_like || case_insensitive_like))
throw Exception(ErrorCodes::BAD_ARGUMENTS, "'not_like'/'case_insensitive_like' require a non-empty 'like' pattern during AST JSON deserialization");
/// A database-only target (no `table`, no `database_and_tables`) is formatted and executed
/// as `DROP DATABASE`, ignoring the `is_view`/`is_dictionary` flags. Such a combination
/// cannot be produced by the parser and, left unchecked, would let a JSON that claims to
/// name a view or dictionary silently execute as `DROP DATABASE`. Reject it.
if (!table && !database_and_tables && (is_view || is_dictionary))
throw Exception(ErrorCodes::BAD_ARGUMENTS, "`DropQuery` with 'is_view' or 'is_dictionary' set must specify a table target ('table' or 'database_and_tables') during AST JSON deserialization");
readOutputOptionsJSON(r);
}
void ASTDropQuery::formatQueryImpl(WriteBuffer & ostr, const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
{
if (kind == ASTDropQuery::Kind::Drop)
ostr << "DROP ";
else if (kind == ASTDropQuery::Kind::Detach)
ostr << "DETACH ";
else if (kind == ASTDropQuery::Kind::Truncate)
ostr << "TRUNCATE ";
else
throw Exception(ErrorCodes::SYNTAX_ERROR, "Not supported kind of drop query.");
if (isTemporary())
ostr << "TEMPORARY ";
if (has_all)
ostr << "ALL ";
if (has_tables)
ostr << "TABLES FROM ";
else if (!table && !database_and_tables && database)
ostr << "DATABASE ";
else if (is_dictionary)
ostr << "DICTIONARY ";
else if (is_view)
ostr << "VIEW ";
else
ostr << "TABLE ";
if (if_exists)
ostr << "IF EXISTS ";
if (if_empty)
ostr << "IF EMPTY ";
if (!table && !database_and_tables && database)
{
database->format(ostr, settings, state, frame);
}
else if (database_and_tables)
{
auto & list = database_and_tables->as<ASTExpressionList &>();
for (auto it = list.children.begin(); it != list.children.end(); ++it)
{
if (it != list.children.begin())
ostr << ", ";
auto identifier = dynamic_pointer_cast<ASTTableIdentifier>(*it);
if (!identifier)
throw Exception(ErrorCodes::SYNTAX_ERROR, "Unexpected type for list of table names.");
if (auto db = identifier->getDatabase())
{
db->format(ostr, settings, state, frame);
ostr << '.';
}
auto tb = identifier->getTable();
chassert(tb);
tb->format(ostr, settings, state, frame);
}
}
else
{
if (database)
{
database->format(ostr, settings, state, frame);
ostr << '.';
}
chassert(table);
table->format(ostr, settings, state, frame);
}
if (!like.empty())
{
ostr
<< (not_like ? " NOT" : "")
<< (case_insensitive_like ? " ILIKE " : " LIKE ")
<< quoteString(like);
}
formatOnCluster(ostr, settings);
if (permanently)
ostr << " PERMANENTLY";
if (sync)
ostr << " SYNC";
}
ASTs ASTDropQuery::getRewrittenASTsOfSingleTable(ASTPtr self) const
{
ASTs res;
if (database_and_tables == nullptr)
{
res.push_back(self);
return res;
}
auto & list = database_and_tables->as<ASTExpressionList &>();
for (const auto & child : list.children)
{
auto cloned = clone();
auto & query = cloned->as<ASTDropQuery &>();
query.database_and_tables = nullptr;
query.children.clear();
auto database_and_table = dynamic_pointer_cast<ASTTableIdentifier>(child);
if (!database_and_table)
throw Exception(ErrorCodes::SYNTAX_ERROR, "Unexpected type for list of table names.");
query.database = database_and_table->getDatabase();
query.table = database_and_table->getTable();
if (query.database)
query.children.push_back(query.database);
if (query.table)
query.children.push_back(query.table);
res.push_back(cloned);
}
return res;
}
}