1 | #include <Common/typeid_cast.h> |
2 | #include <Parsers/ASTFunction.h> |
3 | #include <Parsers/ASTIdentifier.h> |
4 | #include <Parsers/ASTIndexDeclaration.h> |
5 | #include <Parsers/ASTExpressionList.h> |
6 | #include <Parsers/ASTCreateQuery.h> |
7 | #include <Parsers/ASTSetQuery.h> |
8 | #include <Parsers/ASTSelectWithUnionQuery.h> |
9 | #include <Parsers/ExpressionListParsers.h> |
10 | #include <Parsers/ParserCreateQuery.h> |
11 | #include <Parsers/ParserSelectWithUnionQuery.h> |
12 | #include <Parsers/ParserSetQuery.h> |
13 | #include <Parsers/ASTConstraintDeclaration.h> |
14 | #include <Parsers/ParserDictionary.h> |
15 | #include <Parsers/ParserDictionaryAttributeDeclaration.h> |
16 | |
17 | |
18 | namespace DB |
19 | { |
20 | |
21 | bool ParserNestedTable::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
22 | { |
23 | ParserToken open(TokenType::OpeningRoundBracket); |
24 | ParserToken close(TokenType::ClosingRoundBracket); |
25 | ParserIdentifier name_p; |
26 | ParserNameTypePairList columns_p; |
27 | |
28 | ASTPtr name; |
29 | ASTPtr columns; |
30 | |
31 | /// For now `name == 'Nested'`, probably alternative nested data structures will appear |
32 | if (!name_p.parse(pos, name, expected)) |
33 | return false; |
34 | |
35 | if (!open.ignore(pos)) |
36 | return false; |
37 | |
38 | if (!columns_p.parse(pos, columns, expected)) |
39 | return false; |
40 | |
41 | if (!close.ignore(pos)) |
42 | return false; |
43 | |
44 | auto func = std::make_shared<ASTFunction>(); |
45 | tryGetIdentifierNameInto(name, func->name); |
46 | func->arguments = columns; |
47 | func->children.push_back(columns); |
48 | node = func; |
49 | |
50 | return true; |
51 | } |
52 | |
53 | |
54 | bool ParserIdentifierWithParameters::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
55 | { |
56 | ParserFunction function_or_array; |
57 | if (function_or_array.parse(pos, node, expected)) |
58 | return true; |
59 | |
60 | ParserNestedTable nested; |
61 | if (nested.parse(pos, node, expected)) |
62 | return true; |
63 | |
64 | return false; |
65 | } |
66 | |
67 | bool ParserNameTypePairList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
68 | { |
69 | return ParserList(std::make_unique<ParserNameTypePair>(), std::make_unique<ParserToken>(TokenType::Comma), false) |
70 | .parse(pos, node, expected); |
71 | } |
72 | |
73 | bool ParserColumnDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
74 | { |
75 | return ParserList(std::make_unique<ParserColumnDeclaration>(), std::make_unique<ParserToken>(TokenType::Comma), false) |
76 | .parse(pos, node, expected); |
77 | } |
78 | |
79 | bool ParserNameList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
80 | { |
81 | return ParserList(std::make_unique<ParserCompoundIdentifier>(), std::make_unique<ParserToken>(TokenType::Comma), false) |
82 | .parse(pos, node, expected); |
83 | } |
84 | |
85 | bool ParserIndexDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
86 | { |
87 | ParserKeyword s_type("TYPE" ); |
88 | ParserKeyword s_granularity("GRANULARITY" ); |
89 | |
90 | ParserIdentifier name_p; |
91 | ParserIdentifierWithOptionalParameters ident_with_optional_params_p; |
92 | ParserExpression expression_p; |
93 | ParserUnsignedInteger granularity_p; |
94 | |
95 | ASTPtr name; |
96 | ASTPtr expr; |
97 | ASTPtr type; |
98 | ASTPtr granularity; |
99 | |
100 | if (!name_p.parse(pos, name, expected)) |
101 | return false; |
102 | |
103 | if (!expression_p.parse(pos, expr, expected)) |
104 | return false; |
105 | |
106 | if (!s_type.ignore(pos, expected)) |
107 | return false; |
108 | |
109 | if (!ident_with_optional_params_p.parse(pos, type, expected)) |
110 | return false; |
111 | |
112 | if (!s_granularity.ignore(pos, expected)) |
113 | return false; |
114 | |
115 | if (!granularity_p.parse(pos, granularity, expected)) |
116 | return false; |
117 | |
118 | auto index = std::make_shared<ASTIndexDeclaration>(); |
119 | index->name = name->as<ASTIdentifier &>().name; |
120 | index->granularity = granularity->as<ASTLiteral &>().value.get<UInt64>(); |
121 | index->set(index->expr, expr); |
122 | index->set(index->type, type); |
123 | node = index; |
124 | |
125 | return true; |
126 | } |
127 | |
128 | bool ParserConstraintDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
129 | { |
130 | ParserKeyword s_check("CHECK" ); |
131 | |
132 | ParserIdentifier name_p; |
133 | ParserLogicalOrExpression expression_p; |
134 | |
135 | ASTPtr name; |
136 | ASTPtr expr; |
137 | |
138 | if (!name_p.parse(pos, name, expected)) |
139 | return false; |
140 | |
141 | if (!s_check.ignore(pos, expected)) |
142 | return false; |
143 | |
144 | if (!expression_p.parse(pos, expr, expected)) |
145 | return false; |
146 | |
147 | auto constraint = std::make_shared<ASTConstraintDeclaration>(); |
148 | constraint->name = name->as<ASTIdentifier &>().name; |
149 | constraint->set(constraint->expr, expr); |
150 | node = constraint; |
151 | |
152 | return true; |
153 | } |
154 | |
155 | |
156 | bool ParserTablePropertyDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
157 | { |
158 | ParserKeyword s_index("INDEX" ); |
159 | ParserKeyword s_constraint("CONSTRAINT" ); |
160 | |
161 | ParserIndexDeclaration index_p; |
162 | ParserConstraintDeclaration constraint_p; |
163 | ParserColumnDeclaration column_p; |
164 | |
165 | ASTPtr new_node = nullptr; |
166 | |
167 | if (s_index.ignore(pos, expected)) |
168 | { |
169 | if (!index_p.parse(pos, new_node, expected)) |
170 | return false; |
171 | } |
172 | else if (s_constraint.ignore(pos, expected)) |
173 | { |
174 | if (!constraint_p.parse(pos, new_node, expected)) |
175 | return false; |
176 | } |
177 | else |
178 | { |
179 | if (!column_p.parse(pos, new_node, expected)) |
180 | return false; |
181 | } |
182 | |
183 | node = new_node; |
184 | return true; |
185 | } |
186 | |
187 | bool ParserIndexDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
188 | { |
189 | return ParserList(std::make_unique<ParserIndexDeclaration>(), std::make_unique<ParserToken>(TokenType::Comma), false) |
190 | .parse(pos, node, expected); |
191 | } |
192 | |
193 | bool ParserConstraintDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
194 | { |
195 | return ParserList(std::make_unique<ParserConstraintDeclaration>(), std::make_unique<ParserToken>(TokenType::Comma), false) |
196 | .parse(pos, node, expected); |
197 | } |
198 | |
199 | bool ParserTablePropertiesDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
200 | { |
201 | ASTPtr list; |
202 | if (!ParserList( |
203 | std::make_unique<ParserTablePropertyDeclaration>(), |
204 | std::make_unique<ParserToken>(TokenType::Comma), false) |
205 | .parse(pos, list, expected)) |
206 | return false; |
207 | |
208 | ASTPtr columns = std::make_shared<ASTExpressionList>(); |
209 | ASTPtr indices = std::make_shared<ASTExpressionList>(); |
210 | ASTPtr constraints = std::make_shared<ASTExpressionList>(); |
211 | |
212 | for (const auto & elem : list->children) |
213 | { |
214 | if (elem->as<ASTColumnDeclaration>()) |
215 | columns->children.push_back(elem); |
216 | else if (elem->as<ASTIndexDeclaration>()) |
217 | indices->children.push_back(elem); |
218 | else if (elem->as<ASTConstraintDeclaration>()) |
219 | constraints->children.push_back(elem); |
220 | else |
221 | return false; |
222 | } |
223 | |
224 | auto res = std::make_shared<ASTColumns>(); |
225 | |
226 | if (!columns->children.empty()) |
227 | res->set(res->columns, columns); |
228 | if (!indices->children.empty()) |
229 | res->set(res->indices, indices); |
230 | if (!constraints->children.empty()) |
231 | res->set(res->constraints, constraints); |
232 | |
233 | node = res; |
234 | |
235 | return true; |
236 | } |
237 | |
238 | |
239 | bool ParserStorage::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
240 | { |
241 | ParserKeyword s_engine("ENGINE" ); |
242 | ParserToken s_eq(TokenType::Equals); |
243 | ParserKeyword s_partition_by("PARTITION BY" ); |
244 | ParserKeyword s_primary_key("PRIMARY KEY" ); |
245 | ParserKeyword s_order_by("ORDER BY" ); |
246 | ParserKeyword s_sample_by("SAMPLE BY" ); |
247 | ParserKeyword s_ttl("TTL" ); |
248 | ParserKeyword s_settings("SETTINGS" ); |
249 | |
250 | ParserIdentifierWithOptionalParameters ident_with_optional_params_p; |
251 | ParserExpression expression_p; |
252 | ParserSetQuery settings_p(/* parse_only_internals_ = */ true); |
253 | ParserTTLExpressionList parser_ttl_list; |
254 | |
255 | ASTPtr engine; |
256 | ASTPtr partition_by; |
257 | ASTPtr primary_key; |
258 | ASTPtr order_by; |
259 | ASTPtr sample_by; |
260 | ASTPtr ttl_table; |
261 | ASTPtr settings; |
262 | |
263 | if (!s_engine.ignore(pos, expected)) |
264 | return false; |
265 | |
266 | s_eq.ignore(pos, expected); |
267 | |
268 | if (!ident_with_optional_params_p.parse(pos, engine, expected)) |
269 | return false; |
270 | |
271 | while (true) |
272 | { |
273 | if (!partition_by && s_partition_by.ignore(pos, expected)) |
274 | { |
275 | if (expression_p.parse(pos, partition_by, expected)) |
276 | continue; |
277 | else |
278 | return false; |
279 | } |
280 | |
281 | if (!primary_key && s_primary_key.ignore(pos, expected)) |
282 | { |
283 | if (expression_p.parse(pos, primary_key, expected)) |
284 | continue; |
285 | else |
286 | return false; |
287 | } |
288 | |
289 | if (!order_by && s_order_by.ignore(pos, expected)) |
290 | { |
291 | if (expression_p.parse(pos, order_by, expected)) |
292 | continue; |
293 | else |
294 | return false; |
295 | } |
296 | |
297 | if (!sample_by && s_sample_by.ignore(pos, expected)) |
298 | { |
299 | if (expression_p.parse(pos, sample_by, expected)) |
300 | continue; |
301 | else |
302 | return false; |
303 | } |
304 | |
305 | if (!ttl_table && s_ttl.ignore(pos, expected)) |
306 | { |
307 | if (parser_ttl_list.parse(pos, ttl_table, expected)) |
308 | continue; |
309 | else |
310 | return false; |
311 | } |
312 | |
313 | if (s_settings.ignore(pos, expected)) |
314 | { |
315 | if (!settings_p.parse(pos, settings, expected)) |
316 | return false; |
317 | } |
318 | |
319 | break; |
320 | } |
321 | |
322 | auto storage = std::make_shared<ASTStorage>(); |
323 | storage->set(storage->engine, engine); |
324 | storage->set(storage->partition_by, partition_by); |
325 | storage->set(storage->primary_key, primary_key); |
326 | storage->set(storage->order_by, order_by); |
327 | storage->set(storage->sample_by, sample_by); |
328 | storage->set(storage->ttl_table, ttl_table); |
329 | |
330 | storage->set(storage->settings, settings); |
331 | |
332 | node = storage; |
333 | return true; |
334 | } |
335 | |
336 | |
337 | bool ParserCreateTableQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
338 | { |
339 | ParserKeyword s_create("CREATE" ); |
340 | ParserKeyword s_temporary("TEMPORARY" ); |
341 | ParserKeyword s_attach("ATTACH" ); |
342 | ParserKeyword s_table("TABLE" ); |
343 | ParserKeyword s_if_not_exists("IF NOT EXISTS" ); |
344 | ParserKeyword s_as("AS" ); |
345 | ParserToken s_dot(TokenType::Dot); |
346 | ParserToken s_lparen(TokenType::OpeningRoundBracket); |
347 | ParserToken s_rparen(TokenType::ClosingRoundBracket); |
348 | ParserStorage storage_p; |
349 | ParserIdentifier name_p; |
350 | ParserTablePropertiesDeclarationList table_properties_p; |
351 | ParserSelectWithUnionQuery select_p; |
352 | ParserFunction table_function_p; |
353 | ParserNameList names_p; |
354 | |
355 | ASTPtr database; |
356 | ASTPtr table; |
357 | ASTPtr columns_list; |
358 | ASTPtr to_database; |
359 | ASTPtr to_table; |
360 | ASTPtr storage; |
361 | ASTPtr as_database; |
362 | ASTPtr as_table; |
363 | ASTPtr as_table_function; |
364 | ASTPtr select; |
365 | |
366 | String cluster_str; |
367 | bool attach = false; |
368 | bool if_not_exists = false; |
369 | bool is_temporary = false; |
370 | |
371 | if (!s_create.ignore(pos, expected)) |
372 | { |
373 | if (s_attach.ignore(pos, expected)) |
374 | attach = true; |
375 | else |
376 | return false; |
377 | } |
378 | |
379 | if (s_temporary.ignore(pos, expected)) |
380 | { |
381 | is_temporary = true; |
382 | } |
383 | if (!s_table.ignore(pos, expected)) |
384 | return false; |
385 | |
386 | if (s_if_not_exists.ignore(pos, expected)) |
387 | if_not_exists = true; |
388 | |
389 | if (!name_p.parse(pos, table, expected)) |
390 | return false; |
391 | |
392 | if (s_dot.ignore(pos, expected)) |
393 | { |
394 | database = table; |
395 | if (!name_p.parse(pos, table, expected)) |
396 | return false; |
397 | } |
398 | |
399 | if (ParserKeyword{"ON" }.ignore(pos, expected)) |
400 | { |
401 | if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected)) |
402 | return false; |
403 | } |
404 | |
405 | // Shortcut for ATTACH a previously detached table |
406 | if (attach && (!pos.isValid() || pos.get().type == TokenType::Semicolon)) |
407 | { |
408 | auto query = std::make_shared<ASTCreateQuery>(); |
409 | node = query; |
410 | |
411 | query->attach = attach; |
412 | query->if_not_exists = if_not_exists; |
413 | query->cluster = cluster_str; |
414 | |
415 | tryGetIdentifierNameInto(database, query->database); |
416 | tryGetIdentifierNameInto(table, query->table); |
417 | |
418 | return true; |
419 | } |
420 | |
421 | /// List of columns. |
422 | if (s_lparen.ignore(pos, expected)) |
423 | { |
424 | if (!table_properties_p.parse(pos, columns_list, expected)) |
425 | return false; |
426 | |
427 | if (!s_rparen.ignore(pos, expected)) |
428 | return false; |
429 | |
430 | if (!storage_p.parse(pos, storage, expected) && !is_temporary) |
431 | return false; |
432 | } |
433 | else |
434 | { |
435 | storage_p.parse(pos, storage, expected); |
436 | |
437 | if (!s_as.ignore(pos, expected)) |
438 | return false; |
439 | |
440 | if (!select_p.parse(pos, select, expected)) /// AS SELECT ... |
441 | { |
442 | if (!table_function_p.parse(pos, as_table_function, expected)) |
443 | { |
444 | /// AS [db.]table |
445 | if (!name_p.parse(pos, as_table, expected)) |
446 | return false; |
447 | |
448 | if (s_dot.ignore(pos, expected)) |
449 | { |
450 | as_database = as_table; |
451 | if (!name_p.parse(pos, as_table, expected)) |
452 | return false; |
453 | } |
454 | |
455 | /// Optional - ENGINE can be specified. |
456 | if (!storage) |
457 | storage_p.parse(pos, storage, expected); |
458 | } |
459 | } |
460 | } |
461 | |
462 | |
463 | auto query = std::make_shared<ASTCreateQuery>(); |
464 | node = query; |
465 | |
466 | if (as_table_function) |
467 | query->as_table_function = as_table_function; |
468 | |
469 | query->attach = attach; |
470 | query->if_not_exists = if_not_exists; |
471 | query->temporary = is_temporary; |
472 | |
473 | tryGetIdentifierNameInto(database, query->database); |
474 | tryGetIdentifierNameInto(table, query->table); |
475 | query->cluster = cluster_str; |
476 | |
477 | tryGetIdentifierNameInto(to_database, query->to_database); |
478 | tryGetIdentifierNameInto(to_table, query->to_table); |
479 | |
480 | query->set(query->columns_list, columns_list); |
481 | query->set(query->storage, storage); |
482 | |
483 | tryGetIdentifierNameInto(as_database, query->as_database); |
484 | tryGetIdentifierNameInto(as_table, query->as_table); |
485 | query->set(query->select, select); |
486 | |
487 | return true; |
488 | } |
489 | |
490 | bool ParserCreateLiveViewQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
491 | { |
492 | ParserKeyword s_create("CREATE" ); |
493 | ParserKeyword s_temporary("TEMPORARY" ); |
494 | ParserKeyword s_attach("ATTACH" ); |
495 | ParserKeyword s_if_not_exists("IF NOT EXISTS" ); |
496 | ParserKeyword s_as("AS" ); |
497 | ParserKeyword s_view("VIEW" ); |
498 | ParserKeyword s_live("LIVE" ); |
499 | ParserToken s_dot(TokenType::Dot); |
500 | ParserToken s_lparen(TokenType::OpeningRoundBracket); |
501 | ParserToken s_rparen(TokenType::ClosingRoundBracket); |
502 | ParserStorage storage_p; |
503 | ParserIdentifier name_p; |
504 | ParserTablePropertiesDeclarationList table_properties_p; |
505 | ParserSelectWithUnionQuery select_p; |
506 | |
507 | ASTPtr database; |
508 | ASTPtr table; |
509 | ASTPtr columns_list; |
510 | ASTPtr to_database; |
511 | ASTPtr to_table; |
512 | ASTPtr storage; |
513 | ASTPtr as_database; |
514 | ASTPtr as_table; |
515 | ASTPtr select; |
516 | |
517 | String cluster_str; |
518 | bool attach = false; |
519 | bool if_not_exists = false; |
520 | bool is_temporary = false; |
521 | |
522 | if (!s_create.ignore(pos, expected)) |
523 | { |
524 | if (s_attach.ignore(pos, expected)) |
525 | attach = true; |
526 | else |
527 | return false; |
528 | } |
529 | |
530 | if (s_temporary.ignore(pos, expected)) |
531 | { |
532 | is_temporary = true; |
533 | } |
534 | |
535 | if (!s_live.ignore(pos, expected)) |
536 | return false; |
537 | |
538 | if (!s_view.ignore(pos, expected)) |
539 | return false; |
540 | |
541 | if (s_if_not_exists.ignore(pos, expected)) |
542 | if_not_exists = true; |
543 | |
544 | if (!name_p.parse(pos, table, expected)) |
545 | return false; |
546 | |
547 | if (s_dot.ignore(pos, expected)) |
548 | { |
549 | database = table; |
550 | if (!name_p.parse(pos, table, expected)) |
551 | return false; |
552 | } |
553 | |
554 | if (ParserKeyword{"ON" }.ignore(pos, expected)) |
555 | { |
556 | if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected)) |
557 | return false; |
558 | } |
559 | |
560 | // TO [db.]table |
561 | if (ParserKeyword{"TO" }.ignore(pos, expected)) |
562 | { |
563 | if (!name_p.parse(pos, to_table, expected)) |
564 | return false; |
565 | |
566 | if (s_dot.ignore(pos, expected)) |
567 | { |
568 | to_database = to_table; |
569 | if (!name_p.parse(pos, to_table, expected)) |
570 | return false; |
571 | } |
572 | } |
573 | |
574 | /// Optional - a list of columns can be specified. It must fully comply with SELECT. |
575 | if (s_lparen.ignore(pos, expected)) |
576 | { |
577 | if (!table_properties_p.parse(pos, columns_list, expected)) |
578 | return false; |
579 | |
580 | if (!s_rparen.ignore(pos, expected)) |
581 | return false; |
582 | } |
583 | |
584 | /// AS SELECT ... |
585 | if (!s_as.ignore(pos, expected)) |
586 | return false; |
587 | |
588 | if (!select_p.parse(pos, select, expected)) |
589 | return false; |
590 | |
591 | |
592 | auto query = std::make_shared<ASTCreateQuery>(); |
593 | node = query; |
594 | |
595 | query->attach = attach; |
596 | query->if_not_exists = if_not_exists; |
597 | query->is_live_view = true; |
598 | query->temporary = is_temporary; |
599 | |
600 | tryGetIdentifierNameInto(database, query->database); |
601 | tryGetIdentifierNameInto(table, query->table); |
602 | query->cluster = cluster_str; |
603 | |
604 | tryGetIdentifierNameInto(to_database, query->to_database); |
605 | tryGetIdentifierNameInto(to_table, query->to_table); |
606 | |
607 | query->set(query->columns_list, columns_list); |
608 | |
609 | tryGetIdentifierNameInto(as_database, query->as_database); |
610 | tryGetIdentifierNameInto(as_table, query->as_table); |
611 | query->set(query->select, select); |
612 | |
613 | return true; |
614 | } |
615 | |
616 | bool ParserCreateDatabaseQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
617 | { |
618 | ParserKeyword s_create("CREATE" ); |
619 | ParserKeyword s_attach("ATTACH" ); |
620 | ParserKeyword s_database("DATABASE" ); |
621 | ParserKeyword s_if_not_exists("IF NOT EXISTS" ); |
622 | ParserStorage storage_p; |
623 | ParserIdentifier name_p; |
624 | |
625 | ASTPtr database; |
626 | ASTPtr storage; |
627 | |
628 | String cluster_str; |
629 | bool attach = false; |
630 | bool if_not_exists = false; |
631 | |
632 | if (!s_create.ignore(pos, expected)) |
633 | { |
634 | if (s_attach.ignore(pos, expected)) |
635 | attach = true; |
636 | else |
637 | return false; |
638 | } |
639 | |
640 | if (!s_database.ignore(pos, expected)) |
641 | return false; |
642 | |
643 | if (s_if_not_exists.ignore(pos, expected)) |
644 | if_not_exists = true; |
645 | |
646 | if (!name_p.parse(pos, database, expected)) |
647 | return false; |
648 | |
649 | if (ParserKeyword{"ON" }.ignore(pos, expected)) |
650 | { |
651 | if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected)) |
652 | return false; |
653 | } |
654 | |
655 | storage_p.parse(pos, storage, expected); |
656 | |
657 | |
658 | auto query = std::make_shared<ASTCreateQuery>(); |
659 | node = query; |
660 | |
661 | query->attach = attach; |
662 | query->if_not_exists = if_not_exists; |
663 | |
664 | tryGetIdentifierNameInto(database, query->database); |
665 | query->cluster = cluster_str; |
666 | |
667 | query->set(query->storage, storage); |
668 | |
669 | return true; |
670 | } |
671 | |
672 | bool ParserCreateViewQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
673 | { |
674 | ParserKeyword s_create("CREATE" ); |
675 | ParserKeyword s_temporary("TEMPORARY" ); |
676 | ParserKeyword s_attach("ATTACH" ); |
677 | ParserKeyword s_if_not_exists("IF NOT EXISTS" ); |
678 | ParserKeyword s_as("AS" ); |
679 | ParserKeyword s_view("VIEW" ); |
680 | ParserKeyword s_materialized("MATERIALIZED" ); |
681 | ParserKeyword s_populate("POPULATE" ); |
682 | ParserKeyword s_or_replace("OR REPLACE" ); |
683 | ParserToken s_dot(TokenType::Dot); |
684 | ParserToken s_lparen(TokenType::OpeningRoundBracket); |
685 | ParserToken s_rparen(TokenType::ClosingRoundBracket); |
686 | ParserStorage storage_p; |
687 | ParserIdentifier name_p; |
688 | ParserTablePropertiesDeclarationList table_properties_p; |
689 | ParserSelectWithUnionQuery select_p; |
690 | ParserNameList names_p; |
691 | |
692 | ASTPtr database; |
693 | ASTPtr table; |
694 | ASTPtr columns_list; |
695 | ASTPtr to_database; |
696 | ASTPtr to_table; |
697 | ASTPtr storage; |
698 | ASTPtr as_database; |
699 | ASTPtr as_table; |
700 | ASTPtr select; |
701 | |
702 | String cluster_str; |
703 | bool attach = false; |
704 | bool if_not_exists = false; |
705 | bool is_view = false; |
706 | bool is_materialized_view = false; |
707 | bool is_populate = false; |
708 | bool replace_view = false; |
709 | |
710 | if (!s_create.ignore(pos, expected)) |
711 | { |
712 | if (s_attach.ignore(pos, expected)) |
713 | attach = true; |
714 | else |
715 | return false; |
716 | } |
717 | |
718 | /// VIEW or MATERIALIZED VIEW |
719 | if (s_or_replace.ignore(pos, expected)) |
720 | { |
721 | replace_view = true; |
722 | } |
723 | |
724 | if (!replace_view && s_materialized.ignore(pos, expected)) |
725 | { |
726 | is_materialized_view = true; |
727 | } |
728 | else |
729 | is_view = true; |
730 | |
731 | if (!s_view.ignore(pos, expected)) |
732 | return false; |
733 | |
734 | if (!replace_view && s_if_not_exists.ignore(pos, expected)) |
735 | if_not_exists = true; |
736 | |
737 | if (!name_p.parse(pos, table, expected)) |
738 | return false; |
739 | |
740 | if (s_dot.ignore(pos, expected)) |
741 | { |
742 | database = table; |
743 | if (!name_p.parse(pos, table, expected)) |
744 | return false; |
745 | } |
746 | |
747 | if (ParserKeyword{"ON" }.ignore(pos, expected)) |
748 | { |
749 | if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected)) |
750 | return false; |
751 | } |
752 | |
753 | // TO [db.]table |
754 | if (ParserKeyword{"TO" }.ignore(pos, expected)) |
755 | { |
756 | if (!name_p.parse(pos, to_table, expected)) |
757 | return false; |
758 | |
759 | if (s_dot.ignore(pos, expected)) |
760 | { |
761 | to_database = to_table; |
762 | if (!name_p.parse(pos, to_table, expected)) |
763 | return false; |
764 | } |
765 | } |
766 | |
767 | /// Optional - a list of columns can be specified. It must fully comply with SELECT. |
768 | if (s_lparen.ignore(pos, expected)) |
769 | { |
770 | if (!table_properties_p.parse(pos, columns_list, expected)) |
771 | return false; |
772 | |
773 | if (!s_rparen.ignore(pos, expected)) |
774 | return false; |
775 | } |
776 | |
777 | if (is_materialized_view && !to_table) |
778 | { |
779 | /// Internal ENGINE for MATERIALIZED VIEW must be specified. |
780 | if (!storage_p.parse(pos, storage, expected)) |
781 | return false; |
782 | |
783 | if (s_populate.ignore(pos, expected)) |
784 | is_populate = true; |
785 | } |
786 | |
787 | /// AS SELECT ... |
788 | if (!s_as.ignore(pos, expected)) |
789 | return false; |
790 | |
791 | if (!select_p.parse(pos, select, expected)) |
792 | return false; |
793 | |
794 | |
795 | auto query = std::make_shared<ASTCreateQuery>(); |
796 | node = query; |
797 | |
798 | query->attach = attach; |
799 | query->if_not_exists = if_not_exists; |
800 | query->is_view = is_view; |
801 | query->is_materialized_view = is_materialized_view; |
802 | query->is_populate = is_populate; |
803 | query->replace_view = replace_view; |
804 | |
805 | tryGetIdentifierNameInto(database, query->database); |
806 | tryGetIdentifierNameInto(table, query->table); |
807 | query->cluster = cluster_str; |
808 | |
809 | tryGetIdentifierNameInto(to_database, query->to_database); |
810 | tryGetIdentifierNameInto(to_table, query->to_table); |
811 | |
812 | query->set(query->columns_list, columns_list); |
813 | query->set(query->storage, storage); |
814 | |
815 | tryGetIdentifierNameInto(as_database, query->as_database); |
816 | tryGetIdentifierNameInto(as_table, query->as_table); |
817 | query->set(query->select, select); |
818 | |
819 | return true; |
820 | |
821 | } |
822 | |
823 | bool ParserCreateDictionaryQuery::parseImpl(IParser::Pos & pos, ASTPtr & node, Expected & expected) |
824 | { |
825 | ParserKeyword s_create("CREATE" ); |
826 | ParserKeyword s_attach("ATTACH" ); |
827 | ParserKeyword s_dictionary("DICTIONARY" ); |
828 | ParserKeyword s_if_not_exists("IF NOT EXISTS" ); |
829 | ParserKeyword s_on("ON" ); |
830 | ParserIdentifier name_p; |
831 | ParserToken s_left_paren(TokenType::OpeningRoundBracket); |
832 | ParserToken s_right_paren(TokenType::ClosingRoundBracket); |
833 | ParserToken s_dot(TokenType::Dot); |
834 | ParserDictionaryAttributeDeclarationList attributes_p; |
835 | ParserDictionary dictionary_p; |
836 | |
837 | |
838 | bool if_not_exists = false; |
839 | |
840 | ASTPtr database; |
841 | ASTPtr name; |
842 | ASTPtr attributes; |
843 | ASTPtr dictionary; |
844 | String cluster_str; |
845 | |
846 | bool attach = false; |
847 | if (!s_create.ignore(pos, expected)) |
848 | { |
849 | if (s_attach.ignore(pos, expected)) |
850 | attach = true; |
851 | else |
852 | return false; |
853 | } |
854 | |
855 | if (!s_dictionary.ignore(pos, expected)) |
856 | return false; |
857 | |
858 | if (s_if_not_exists.ignore(pos, expected)) |
859 | if_not_exists = true; |
860 | |
861 | if (!name_p.parse(pos, name, expected)) |
862 | return false; |
863 | |
864 | if (s_dot.ignore(pos)) |
865 | { |
866 | database = name; |
867 | if (!name_p.parse(pos, name, expected)) |
868 | return false; |
869 | } |
870 | |
871 | if (s_on.ignore(pos, expected)) |
872 | { |
873 | if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected)) |
874 | return false; |
875 | } |
876 | |
877 | if (!attach) |
878 | { |
879 | if (!s_left_paren.ignore(pos, expected)) |
880 | return false; |
881 | |
882 | if (!attributes_p.parse(pos, attributes, expected)) |
883 | return false; |
884 | |
885 | if (!s_right_paren.ignore(pos, expected)) |
886 | return false; |
887 | |
888 | if (!dictionary_p.parse(pos, dictionary, expected)) |
889 | return false; |
890 | } |
891 | |
892 | auto query = std::make_shared<ASTCreateQuery>(); |
893 | node = query; |
894 | query->is_dictionary = true; |
895 | query->attach = attach; |
896 | |
897 | if (database) |
898 | query->database = typeid_cast<const ASTIdentifier &>(*database).name; |
899 | |
900 | query->table = typeid_cast<const ASTIdentifier &>(*name).name; |
901 | |
902 | query->if_not_exists = if_not_exists; |
903 | query->set(query->dictionary_attributes_list, attributes); |
904 | query->set(query->dictionary, dictionary); |
905 | query->cluster = cluster_str; |
906 | |
907 | return true; |
908 | } |
909 | |
910 | |
911 | bool ParserCreateQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
912 | { |
913 | ParserCreateTableQuery table_p; |
914 | ParserCreateDatabaseQuery database_p; |
915 | ParserCreateViewQuery view_p; |
916 | ParserCreateDictionaryQuery dictionary_p; |
917 | ParserCreateLiveViewQuery live_view_p; |
918 | |
919 | return table_p.parse(pos, node, expected) |
920 | || database_p.parse(pos, node, expected) |
921 | || view_p.parse(pos, node, expected) |
922 | || dictionary_p.parse(pos, node, expected) |
923 | || live_view_p.parse(pos, node, expected); |
924 | } |
925 | |
926 | } |
927 | |