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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
#include <Parsers/ParserBackupQuery.h>
#include <Parsers/ASTBackupQuery.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTIdentifier_fwd.h>
#include <Parsers/ASTSetQuery.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/ExpressionListParsers.h>
#include <Parsers/ParserPartition.h>
#include <Parsers/ParserSetQuery.h>
#include <Parsers/parseDatabaseAndTableName.h>
#include <Common/assert_cast.h>
#include <boost/range/algorithm_ext/erase.hpp>
namespace DB
{
namespace
{
using Kind = ASTBackupQuery::Kind;
using Element = ASTBackupQuery::Element;
using ElementType = ASTBackupQuery::ElementType;
bool parsePartitions(IParser::Pos & pos, Expected & expected, std::optional<ASTs> & partitions)
{
if (!ParserKeyword{"PARTITION"}.ignore(pos, expected) && !ParserKeyword{"PARTITIONS"}.ignore(pos, expected))
return false;
ASTs result;
auto parse_list_element = [&]
{
ASTPtr ast;
if (!ParserPartition{}.parse(pos, ast, expected))
return false;
result.push_back(ast);
return true;
};
if (!ParserList::parseUtil(pos, expected, parse_list_element, false))
return false;
partitions = std::move(result);
return true;
}
bool parseExceptDatabases(IParser::Pos & pos, Expected & expected, std::set<String> & except_databases)
{
return IParserBase::wrapParseImpl(pos, [&]
{
if (!ParserKeyword{"EXCEPT DATABASE"}.ignore(pos, expected) && !ParserKeyword{"EXCEPT DATABASES"}.ignore(pos, expected))
return false;
std::set<String> result;
auto parse_list_element = [&]
{
ASTPtr ast;
if (!ParserIdentifier{}.parse(pos, ast, expected))
return false;
result.insert(getIdentifierName(ast));
return true;
};
if (!ParserList::parseUtil(pos, expected, parse_list_element, false))
return false;
except_databases = std::move(result);
return true;
});
}
bool parseExceptTables(IParser::Pos & pos, Expected & expected, const std::optional<String> & database_name, std::set<DatabaseAndTableName> & except_tables)
{
return IParserBase::wrapParseImpl(pos, [&]
{
if (!ParserKeyword{"EXCEPT TABLE"}.ignore(pos, expected) && !ParserKeyword{"EXCEPT TABLES"}.ignore(pos, expected))
return false;
std::set<DatabaseAndTableName> result;
auto parse_list_element = [&]
{
DatabaseAndTableName table_name;
if (database_name)
{
ASTPtr ast;
if (!ParserIdentifier{}.parse(pos, ast, expected))
return false;
table_name.first = *database_name;
table_name.second = getIdentifierName(ast);
}
else
{
if (!parseDatabaseAndTableName(pos, expected, table_name.first, table_name.second))
return false;
}
result.emplace(std::move(table_name));
return true;
};
if (!ParserList::parseUtil(pos, expected, parse_list_element, false))
return false;
except_tables = std::move(result);
return true;
});
}
bool parseElement(IParser::Pos & pos, Expected & expected, Element & element)
{
return IParserBase::wrapParseImpl(pos, [&]
{
if (ParserKeyword{"TABLE"}.ignore(pos, expected) || ParserKeyword{"DICTIONARY"}.ignore(pos, expected) ||
ParserKeyword{"VIEW"}.ignore(pos, expected))
{
element.type = ElementType::TABLE;
if (!parseDatabaseAndTableName(pos, expected, element.database_name, element.table_name))
return false;
element.new_database_name = element.database_name;
element.new_table_name = element.table_name;
if (ParserKeyword("AS").ignore(pos, expected))
{
if (!parseDatabaseAndTableName(pos, expected, element.new_database_name, element.new_table_name))
return false;
}
parsePartitions(pos, expected, element.partitions);
return true;
}
if (ParserKeyword{"TEMPORARY TABLE"}.ignore(pos, expected))
{
element.type = ElementType::TEMPORARY_TABLE;
ASTPtr ast;
if (!ParserIdentifier{}.parse(pos, ast, expected))
return false;
element.table_name = getIdentifierName(ast);
element.new_table_name = element.table_name;
if (ParserKeyword("AS").ignore(pos, expected))
{
ast = nullptr;
if (!ParserIdentifier{}.parse(pos, ast, expected))
return false;
element.new_table_name = getIdentifierName(ast);
}
return true;
}
if (ParserKeyword{"DATABASE"}.ignore(pos, expected))
{
element.type = ElementType::DATABASE;
ASTPtr ast;
if (!ParserIdentifier{}.parse(pos, ast, expected))
return false;
element.database_name = getIdentifierName(ast);
element.new_database_name = element.database_name;
if (ParserKeyword("AS").ignore(pos, expected))
{
ast = nullptr;
if (!ParserIdentifier{}.parse(pos, ast, expected))
return false;
element.new_database_name = getIdentifierName(ast);
}
parseExceptTables(pos, expected, element.database_name, element.except_tables);
return true;
}
if (ParserKeyword{"ALL"}.ignore(pos, expected))
{
element.type = ElementType::ALL;
parseExceptDatabases(pos, expected, element.except_databases);
parseExceptTables(pos, expected, {}, element.except_tables);
return true;
}
return false;
});
}
bool parseElements(IParser::Pos & pos, Expected & expected, std::vector<Element> & elements)
{
return IParserBase::wrapParseImpl(pos, [&]
{
std::vector<Element> result;
auto parse_element = [&]
{
Element element;
if (parseElement(pos, expected, element))
{
result.emplace_back(std::move(element));
return true;
}
return false;
};
if (!ParserList::parseUtil(pos, expected, parse_element, false))
return false;
elements = std::move(result);
return true;
});
}
bool parseBackupName(IParser::Pos & pos, Expected & expected, ASTPtr & backup_name)
{
if (!ParserIdentifierWithOptionalParameters{}.parse(pos, backup_name, expected))
return false;
backup_name->as<ASTFunction &>().kind = ASTFunction::Kind::BACKUP_NAME;
return true;
}
bool parseBaseBackupSetting(IParser::Pos & pos, Expected & expected, ASTPtr & base_backup_name)
{
return IParserBase::wrapParseImpl(pos, [&]
{
return ParserKeyword{"base_backup"}.ignore(pos, expected)
&& ParserToken(TokenType::Equals).ignore(pos, expected)
&& parseBackupName(pos, expected, base_backup_name);
});
}
bool parseClusterHostIDs(IParser::Pos & pos, Expected & expected, ASTPtr & cluster_host_ids)
{
return ParserArray{}.parse(pos, cluster_host_ids, expected);
}
bool parseClusterHostIDsSetting(IParser::Pos & pos, Expected & expected, ASTPtr & cluster_host_ids)
{
return IParserBase::wrapParseImpl(pos, [&]
{
return ParserKeyword{"cluster_host_ids"}.ignore(pos, expected)
&& ParserToken(TokenType::Equals).ignore(pos, expected)
&& parseClusterHostIDs(pos, expected, cluster_host_ids);
});
}
bool parseSettings(IParser::Pos & pos, Expected & expected, ASTPtr & settings, ASTPtr & base_backup_name, ASTPtr & cluster_host_ids)
{
return IParserBase::wrapParseImpl(pos, [&]
{
if (!ParserKeyword{"SETTINGS"}.ignore(pos, expected))
return false;
SettingsChanges settings_changes;
ASTPtr res_base_backup_name;
ASTPtr res_cluster_host_ids;
auto parse_setting = [&]
{
if (!res_base_backup_name && parseBaseBackupSetting(pos, expected, res_base_backup_name))
return true;
if (!res_cluster_host_ids && parseClusterHostIDsSetting(pos, expected, res_cluster_host_ids))
return true;
SettingChange setting;
if (ParserSetQuery::parseNameValuePair(setting, pos, expected))
{
settings_changes.push_back(std::move(setting));
return true;
}
return false;
};
if (!ParserList::parseUtil(pos, expected, parse_setting, false))
return false;
ASTPtr res_settings;
if (!settings_changes.empty())
{
auto settings_changes_ast = std::make_shared<ASTSetQuery>();
settings_changes_ast->changes = std::move(settings_changes);
settings_changes_ast->is_standalone = false;
res_settings = settings_changes_ast;
}
settings = std::move(res_settings);
base_backup_name = std::move(res_base_backup_name);
cluster_host_ids = std::move(res_cluster_host_ids);
return true;
});
}
bool parseSyncOrAsync(IParser::Pos & pos, Expected & expected, ASTPtr & settings)
{
bool async;
if (ParserKeyword{"ASYNC"}.ignore(pos, expected))
async = true;
else if (ParserKeyword{"SYNC"}.ignore(pos, expected))
async = false;
else
return false;
SettingsChanges changes;
if (settings)
{
changes = assert_cast<ASTSetQuery *>(settings.get())->changes;
}
boost::remove_erase_if(changes, [](const SettingChange & change) { return change.name == "async"; });
changes.emplace_back("async", async);
auto new_settings = std::make_shared<ASTSetQuery>();
new_settings->changes = std::move(changes);
new_settings->is_standalone = false;
settings = new_settings;
return true;
}
bool parseOnCluster(IParserBase::Pos & pos, Expected & expected, String & cluster)
{
return IParserBase::wrapParseImpl(pos, [&]
{
return ParserKeyword{"ON"}.ignore(pos, expected) && ASTQueryWithOnCluster::parse(pos, cluster, expected);
});
}
}
bool ParserBackupQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
Kind kind;
if (ParserKeyword{"BACKUP"}.ignore(pos, expected))
kind = Kind::BACKUP;
else if (ParserKeyword{"RESTORE"}.ignore(pos, expected))
kind = Kind::RESTORE;
else
return false;
std::vector<Element> elements;
if (!parseElements(pos, expected, elements))
return false;
String cluster;
parseOnCluster(pos, expected, cluster);
if (!ParserKeyword{(kind == Kind::BACKUP) ? "TO" : "FROM"}.ignore(pos, expected))
return false;
ASTPtr backup_name;
if (!parseBackupName(pos, expected, backup_name))
return false;
ASTPtr settings;
ASTPtr base_backup_name;
ASTPtr cluster_host_ids;
parseSettings(pos, expected, settings, base_backup_name, cluster_host_ids);
parseSyncOrAsync(pos, expected, settings);
auto query = std::make_shared<ASTBackupQuery>();
node = query;
query->kind = kind;
query->elements = std::move(elements);
query->cluster = std::move(cluster);
if (backup_name)
query->set(query->backup_name, backup_name);
query->settings = std::move(settings);
query->cluster_host_ids = std::move(cluster_host_ids);
if (base_backup_name)
query->set(query->base_backup_name, base_backup_name);
return true;
}
}
|