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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
#include <Parsers/MySQL/ASTAlterCommand.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/ExpressionListParsers.h>
#include <Parsers/MySQL/ASTDeclareOption.h>
#include <Parsers/MySQL/ASTDeclareTableOptions.h>
#include <Interpreters/StorageID.h>
namespace DB
{
namespace MySQLParser
{
ASTPtr ASTAlterCommand::clone() const
{
auto res = std::make_shared<ASTAlterCommand>(*this);
res->children.clear();
if (index_decl)
res->set(res->index_decl, index_decl->clone());
if (default_expression)
res->set(res->default_expression, default_expression->clone());
if (additional_columns)
res->set(res->additional_columns, additional_columns->clone());
if (order_by_columns)
res->set(res->order_by_columns, order_by_columns->clone());
if (properties)
res->set(res->properties, properties->clone());
return res;
}
static inline bool parseAddCommand(IParser::Pos & pos, ASTPtr & node, Expected & expected)
{
ASTPtr declare_index;
ASTPtr additional_columns;
ParserDeclareIndex index_p;
ParserDeclareColumn column_p;
auto alter_command = std::make_shared<ASTAlterCommand>();
if (index_p.parse(pos, declare_index, expected))
{
alter_command->type = ASTAlterCommand::ADD_INDEX;
alter_command->set(alter_command->index_decl, declare_index);
}
else
{
alter_command->type = ASTAlterCommand::ADD_COLUMN;
ParserKeyword("COLUMN").ignore(pos, expected);
if (ParserToken(TokenType::OpeningRoundBracket).ignore(pos, expected))
{
ParserList columns_p(std::make_unique<ParserDeclareColumn>(), std::make_unique<ParserToken>(TokenType::Comma));
if (!columns_p.parse(pos, additional_columns, expected))
return false;
if (!ParserToken(TokenType::ClosingRoundBracket).ignore(pos, expected))
return false;
}
else
{
ASTPtr declare_column;
if (!column_p.parse(pos, declare_column, expected))
return false;
additional_columns = std::make_shared<ASTExpressionList>();
additional_columns->children.emplace_back(declare_column);
if (ParserKeyword("FIRST").ignore(pos, expected))
alter_command->first = true;
else if (ParserKeyword("AFTER").ignore(pos, expected))
{
ASTPtr after_column;
ParserIdentifier identifier_p;
if (!identifier_p.parse(pos, after_column, expected))
return false;
alter_command->column_name = getIdentifierName(after_column);
}
}
alter_command->set(alter_command->additional_columns, additional_columns);
}
node = alter_command;
return true;
}
static inline bool parseDropCommand(IParser::Pos & pos, ASTPtr & node, Expected & expected)
{
ASTPtr name;
ParserIdentifier identifier_p;
auto alter_command = std::make_shared<ASTAlterCommand>();
if (ParserKeyword("PRIMARY KEY").ignore(pos, expected))
{
alter_command->index_type = "PRIMARY_KEY";
alter_command->type = ASTAlterCommand::DROP_INDEX;
}
else if (ParserKeyword("FOREIGN KEY").ignore(pos, expected))
{
if (!identifier_p.parse(pos, name, expected))
return false;
alter_command->index_type = "FOREIGN";
alter_command->type = ASTAlterCommand::DROP_INDEX;
alter_command->index_name = getIdentifierName(name);
}
else if (ParserKeyword("INDEX").ignore(pos, expected) || ParserKeyword("KEY").ignore(pos, expected))
{
if (!identifier_p.parse(pos, name, expected))
return false;
alter_command->index_type = "KEY";
alter_command->type = ASTAlterCommand::DROP_INDEX;
alter_command->index_name = getIdentifierName(name);
}
else if (ParserKeyword("CONSTRAINT").ignore(pos, expected) || ParserKeyword("CHECK").ignore(pos, expected))
{
if (!identifier_p.parse(pos, name, expected))
return false;
alter_command->type = ASTAlterCommand::DROP_CHECK;
alter_command->constraint_name = getIdentifierName(name);
}
else
{
ParserKeyword("COLUMN").ignore(pos, expected);
if (!identifier_p.parse(pos, name, expected))
return false;
alter_command->type = ASTAlterCommand::DROP_COLUMN;
alter_command->column_name = getIdentifierName(name);
}
node = alter_command;
return true;
}
static inline bool parseAlterCommand(IParser::Pos & pos, ASTPtr & node, Expected & expected)
{
ASTPtr name;
ParserIdentifier identifier_p;
auto alter_command = std::make_shared<ASTAlterCommand>();
if (ParserKeyword("INDEX").ignore(pos, expected))
{
/// ALTER INDEX index_name {VISIBLE | INVISIBLE}
if (!identifier_p.parse(pos, name, expected))
return false;
alter_command->index_visible = ParserKeyword("VISIBLE").ignore(pos, expected);
if (!alter_command->index_visible && !ParserKeyword("INVISIBLE").ignore(pos, expected))
return false;
alter_command->type = ASTAlterCommand::MODIFY_INDEX_VISIBLE;
alter_command->index_name = getIdentifierName(name);
}
else if (ParserKeyword("CHECK").ignore(pos, expected) || ParserKeyword("CONSTRAINT").ignore(pos, expected))
{
/// ALTER {CHECK | CONSTRAINT} symbol [NOT] ENFORCED
if (!identifier_p.parse(pos, name, expected))
return false;
alter_command->not_check_enforced = ParserKeyword("NOT").ignore(pos, expected);
if (!ParserKeyword("ENFORCED").ignore(pos, expected))
return false;
alter_command->type = ASTAlterCommand::MODIFY_CHECK;
alter_command->constraint_name = getIdentifierName(name);
}
else
{
/// ALTER [COLUMN] col_name {SET DEFAULT {literal | (expr)} | DROP DEFAULT}
ParserKeyword("COLUMN").ignore(pos, expected);
if (!identifier_p.parse(pos, name, expected))
return false;
if (ParserKeyword("DROP DEFAULT").ignore(pos, expected))
alter_command->type = ASTAlterCommand::DROP_COLUMN_DEFAULT;
else if (ParserKeyword("SET DEFAULT").ignore(pos, expected))
{
ASTPtr default_expression;
ParserExpression expression_p;
if (!expression_p.parse(pos, default_expression, expected))
return false;
alter_command->type = ASTAlterCommand::MODIFY_COLUMN_DEFAULT;
alter_command->set(alter_command->default_expression, default_expression);
}
else
return false;
alter_command->column_name = getIdentifierName(name);
}
node = alter_command;
return true;
}
static inline bool parseRenameCommand(IParser::Pos & pos, ASTPtr & node, Expected & expected)
{
ASTPtr old_name;
ASTPtr new_name;
ParserIdentifier identifier_p;
auto alter_command = std::make_shared<ASTAlterCommand>();
if (ParserKeyword("COLUMN").ignore(pos, expected))
{
if (!identifier_p.parse(pos, old_name, expected))
return false;
if (!ParserKeyword("TO").ignore(pos, expected))
return false;
if (!identifier_p.parse(pos, new_name, expected))
return false;
alter_command->type = ASTAlterCommand::RENAME_COLUMN;
alter_command->old_name = getIdentifierName(old_name);
alter_command->column_name = getIdentifierName(new_name);
}
else if (ParserKeyword("TO").ignore(pos, expected) || ParserKeyword("AS").ignore(pos, expected))
{
if (!ParserCompoundIdentifier(true).parse(pos, new_name, expected))
return false;
auto new_table_id = new_name->as<ASTTableIdentifier>()->getTableId();
alter_command->type = ASTAlterCommand::RENAME_TABLE;
alter_command->new_table_name = new_table_id.table_name;
alter_command->new_database_name = new_table_id.database_name;
}
else if (ParserKeyword("INDEX").ignore(pos, expected) || ParserKeyword("KEY").ignore(pos, expected))
{
if (!identifier_p.parse(pos, old_name, expected))
return false;
if (!ParserKeyword("TO").ignore(pos, expected))
return false;
if (!identifier_p.parse(pos, new_name, expected))
return false;
alter_command->type = ASTAlterCommand::RENAME_INDEX;
alter_command->old_name = getIdentifierName(old_name);
alter_command->index_name = getIdentifierName(new_name);
}
else
{
if (!ParserCompoundIdentifier(true).parse(pos, new_name, expected))
return false;
auto new_table_id = new_name->as<ASTTableIdentifier>()->getTableId();
alter_command->type = ASTAlterCommand::RENAME_TABLE;
alter_command->new_table_name = new_table_id.table_name;
alter_command->new_database_name = new_table_id.database_name;
}
node = alter_command;
return true;
}
static inline bool parseOtherCommand(IParser::Pos & pos, ASTPtr & node, Expected & expected)
{
auto alter_command = std::make_shared<ASTAlterCommand>();
if (ParserKeyword("ORDER BY").ignore(pos, expected))
{
/// ORDER BY col_name [, col_name] ...
ASTPtr columns;
ParserList columns_p(std::make_unique<ParserIdentifier>(), std::make_unique<ParserToken>(TokenType::Comma));
if (!columns_p.parse(pos, columns, expected))
return false;
alter_command->type = ASTAlterCommand::ORDER_BY;
alter_command->set(alter_command->order_by_columns, columns);
}
else
{
ParserDeclareOption options_p{
{
OptionDescribe("FORCE", "force", std::make_shared<ParserAlwaysTrue>()),
OptionDescribe("ALGORITHM", "algorithm", std::make_shared<ParserIdentifier>()),
OptionDescribe("WITH VALIDATION", "validation", std::make_shared<ParserAlwaysTrue>()),
OptionDescribe("WITHOUT VALIDATION", "validation", std::make_shared<ParserAlwaysFalse>()),
OptionDescribe("IMPORT TABLESPACE", "import_tablespace", std::make_shared<ParserAlwaysTrue>()),
OptionDescribe("DISCARD TABLESPACE", "import_tablespace", std::make_shared<ParserAlwaysFalse>()),
OptionDescribe("ENABLE KEYS", "enable_keys", std::make_shared<ParserAlwaysTrue>()),
OptionDescribe("DISABLE KEYS", "enable_keys", std::make_shared<ParserAlwaysFalse>()),
/// TODO: with collate
OptionDescribe("CONVERT TO CHARACTER SET", "charset", std::make_shared<ParserCharsetOrCollateName>()),
OptionDescribe("CHARACTER SET", "charset", std::make_shared<ParserCharsetOrCollateName>()),
OptionDescribe("DEFAULT CHARACTER SET", "charset", std::make_shared<ParserCharsetOrCollateName>()),
OptionDescribe("COMMENT", "", std::make_shared<ParserIdentifier>()),
OptionDescribe("LOCK", "lock", std::make_shared<ParserIdentifier>())
}
};
ASTPtr properties_options;
ParserDeclareTableOptions table_options_p;
if (!options_p.parse(pos, properties_options, expected) && !table_options_p.parse(pos, properties_options, expected))
return false;
alter_command->type = ASTAlterCommand::MODIFY_PROPERTIES;
alter_command->set(alter_command->properties, properties_options);
}
node = alter_command;
return true;
}
static inline bool parseModifyCommand(IParser::Pos & pos, ASTPtr & node, Expected & expected, bool exists_old_column_name = false)
{
ASTPtr old_column_name;
auto alter_command = std::make_shared<ASTAlterCommand>();
ParserKeyword("COLUMN").ignore(pos, expected);
if (exists_old_column_name && !ParserIdentifier().parse(pos, old_column_name, expected))
return false;
ASTPtr additional_column;
if (!ParserDeclareColumn().parse(pos, additional_column, expected))
return false;
if (ParserKeyword("FIRST").ignore(pos, expected))
alter_command->first = true;
else if (ParserKeyword("AFTER").ignore(pos, expected))
{
ASTPtr after_column;
ParserIdentifier identifier_p;
if (!identifier_p.parse(pos, after_column, expected))
return false;
alter_command->column_name = getIdentifierName(after_column);
}
node = alter_command;
alter_command->type = ASTAlterCommand::MODIFY_COLUMN;
alter_command->set(alter_command->additional_columns, std::make_shared<ASTExpressionList>());
alter_command->additional_columns->children.emplace_back(additional_column);
if (exists_old_column_name)
alter_command->old_name = getIdentifierName(old_column_name);
return true;
}
bool ParserAlterCommand::parseImpl(IParser::Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword k_add("ADD");
ParserKeyword k_drop("DROP");
ParserKeyword k_alter("ALTER");
ParserKeyword k_rename("RENAME");
ParserKeyword k_modify("MODIFY");
ParserKeyword k_change("CHANGE");
if (k_add.ignore(pos, expected))
return parseAddCommand(pos, node, expected);
else if (k_drop.ignore(pos, expected))
return parseDropCommand(pos, node, expected);
else if (k_alter.ignore(pos, expected))
return parseAlterCommand(pos, node, expected);
else if (k_rename.ignore(pos, expected))
return parseRenameCommand(pos, node, expected);
else if (k_modify.ignore(pos, expected))
return parseModifyCommand(pos, node, expected);
else if (k_change.ignore(pos, expected))
return parseModifyCommand(pos, node, expected, true);
else
return parseOtherCommand(pos, node, expected);
}
}
}
|