1#include <Parsers/IAST.h>
2#include <Parsers/ASTExpressionList.h>
3#include <Parsers/ASTFunction.h>
4#include <Parsers/parseIntervalKind.h>
5#include <Parsers/ExpressionElementParsers.h>
6#include <Parsers/ExpressionListParsers.h>
7#include <Parsers/ParserCreateQuery.h>
8#include <Parsers/ASTFunctionWithKeyValueArguments.h>
9
10#include <Common/typeid_cast.h>
11#include <Common/StringUtils/StringUtils.h>
12
13
14namespace DB
15{
16
17
18const char * ParserMultiplicativeExpression::operators[] =
19{
20 "*", "multiply",
21 "/", "divide",
22 "%", "modulo",
23 nullptr
24};
25
26const char * ParserUnaryMinusExpression::operators[] =
27{
28 "-", "negate",
29 nullptr
30};
31
32const char * ParserAdditiveExpression::operators[] =
33{
34 "+", "plus",
35 "-", "minus",
36 nullptr
37};
38
39const char * ParserComparisonExpression::operators[] =
40{
41 "==", "equals",
42 "!=", "notEquals",
43 "<>", "notEquals",
44 "<=", "lessOrEquals",
45 ">=", "greaterOrEquals",
46 "<", "less",
47 ">", "greater",
48 "=", "equals",
49 "LIKE", "like",
50 "NOT LIKE", "notLike",
51 "IN", "in",
52 "NOT IN", "notIn",
53 "GLOBAL IN", "globalIn",
54 "GLOBAL NOT IN", "globalNotIn",
55 nullptr
56};
57
58const char * ParserLogicalNotExpression::operators[] =
59{
60 "NOT", "not",
61 nullptr
62};
63
64const char * ParserArrayElementExpression::operators[] =
65{
66 "[", "arrayElement",
67 nullptr
68};
69
70const char * ParserTupleElementExpression::operators[] =
71{
72 ".", "tupleElement",
73 nullptr
74};
75
76
77
78bool ParserList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
79{
80 bool first = true;
81
82 auto list = std::make_shared<ASTExpressionList>(result_separator);
83 node = list;
84
85 while (true)
86 {
87 if (first)
88 {
89 ASTPtr elem;
90 if (!elem_parser->parse(pos, elem, expected))
91 break;
92
93 list->children.push_back(elem);
94 first = false;
95 }
96 else
97 {
98 auto prev_pos = pos;
99
100 if (!separator_parser->ignore(pos, expected))
101 break;
102
103 ASTPtr elem;
104 if (!elem_parser->parse(pos, elem, expected))
105 {
106 pos = prev_pos;
107 break;
108 }
109
110 list->children.push_back(elem);
111 }
112 }
113
114 if (!allow_empty && first)
115 return false;
116
117 return true;
118}
119
120
121static bool parseOperator(IParser::Pos & pos, const char * op, Expected & expected)
122{
123 if (isWordCharASCII(*op))
124 {
125 return ParserKeyword(op).ignore(pos, expected);
126 }
127 else
128 {
129 if (strlen(op) == pos->size() && 0 == memcmp(op, pos->begin, pos->size()))
130 {
131 ++pos;
132 return true;
133 }
134 return false;
135 }
136}
137
138
139bool ParserLeftAssociativeBinaryOperatorList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
140{
141 bool first = true;
142
143 auto current_depth = pos.depth;
144 while (1)
145 {
146 if (first)
147 {
148 ASTPtr elem;
149 if (!first_elem_parser->parse(pos, elem, expected))
150 return false;
151
152 node = elem;
153 first = false;
154 }
155 else
156 {
157 /// try to find any of the valid operators
158
159 const char ** it;
160 for (it = operators; *it; it += 2)
161 if (parseOperator(pos, *it, expected))
162 break;
163
164 if (!*it)
165 break;
166
167 /// the function corresponding to the operator
168 auto function = std::make_shared<ASTFunction>();
169
170 /// function arguments
171 auto exp_list = std::make_shared<ASTExpressionList>();
172
173 ASTPtr elem;
174 if (!(remaining_elem_parser ? remaining_elem_parser : first_elem_parser)->parse(pos, elem, expected))
175 return false;
176
177 /// the first argument of the function is the previous element, the second is the next one
178 function->name = it[1];
179 function->arguments = exp_list;
180 function->children.push_back(exp_list);
181
182 exp_list->children.push_back(node);
183 exp_list->children.push_back(elem);
184
185 /** special exception for the access operator to the element of the array `x[y]`, which
186 * contains the infix part '[' and the suffix ''] '(specified as' [')
187 */
188 if (0 == strcmp(it[0], "["))
189 {
190 if (pos->type != TokenType::ClosingSquareBracket)
191 return false;
192 ++pos;
193 }
194
195 /// Left associative operator chain is parsed as a tree: ((((1 + 1) + 1) + 1) + 1)...
196 /// We must account it's depth - otherwise we may end up with stack overflow later - on destruction of AST.
197 pos.increaseDepth();
198 node = function;
199 }
200 }
201
202 pos.depth = current_depth;
203 return true;
204}
205
206
207bool ParserVariableArityOperatorList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
208{
209 ASTPtr arguments;
210
211 if (!elem_parser->parse(pos, node, expected))
212 return false;
213
214 while (true)
215 {
216 if (!parseOperator(pos, infix, expected))
217 break;
218
219 if (!arguments)
220 {
221 node = makeASTFunction(function_name, node);
222 arguments = node->as<ASTFunction &>().arguments;
223 }
224
225 ASTPtr elem;
226 if (!elem_parser->parse(pos, elem, expected))
227 return false;
228
229 arguments->children.push_back(elem);
230 }
231
232 return true;
233}
234
235bool ParserBetweenExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
236{
237 /// For the expression (subject [NOT] BETWEEN left AND right)
238 /// create an AST the same as for (subject> = left AND subject <= right).
239
240 ParserKeyword s_not("NOT");
241 ParserKeyword s_between("BETWEEN");
242 ParserKeyword s_and("AND");
243
244 ASTPtr subject;
245 ASTPtr left;
246 ASTPtr right;
247
248 if (!elem_parser.parse(pos, subject, expected))
249 return false;
250
251 bool negative = s_not.ignore(pos, expected);
252
253 if (!s_between.ignore(pos, expected))
254 {
255 if (negative)
256 --pos;
257
258 /// No operator was parsed, just return element.
259 node = subject;
260 }
261 else
262 {
263 if (!elem_parser.parse(pos, left, expected))
264 return false;
265
266 if (!s_and.ignore(pos, expected))
267 return false;
268
269 if (!elem_parser.parse(pos, right, expected))
270 return false;
271
272 auto f_combined_expression = std::make_shared<ASTFunction>();
273 auto args_combined_expression = std::make_shared<ASTExpressionList>();
274
275 /// [NOT] BETWEEN left AND right
276 auto f_left_expr = std::make_shared<ASTFunction>();
277 auto args_left_expr = std::make_shared<ASTExpressionList>();
278
279 auto f_right_expr = std::make_shared<ASTFunction>();
280 auto args_right_expr = std::make_shared<ASTExpressionList>();
281
282 args_left_expr->children.emplace_back(subject);
283 args_left_expr->children.emplace_back(left);
284
285 args_right_expr->children.emplace_back(subject);
286 args_right_expr->children.emplace_back(right);
287
288 if (negative)
289 {
290 /// NOT BETWEEN
291 f_left_expr->name = "less";
292 f_right_expr->name = "greater";
293 f_combined_expression->name = "or";
294 }
295 else
296 {
297 /// BETWEEN
298 f_left_expr->name = "greaterOrEquals";
299 f_right_expr->name = "lessOrEquals";
300 f_combined_expression->name = "and";
301 }
302
303 f_left_expr->arguments = args_left_expr;
304 f_left_expr->children.emplace_back(f_left_expr->arguments);
305
306 f_right_expr->arguments = args_right_expr;
307 f_right_expr->children.emplace_back(f_right_expr->arguments);
308
309 args_combined_expression->children.emplace_back(f_left_expr);
310 args_combined_expression->children.emplace_back(f_right_expr);
311
312 f_combined_expression->arguments = args_combined_expression;
313 f_combined_expression->children.emplace_back(f_combined_expression->arguments);
314
315 node = f_combined_expression;
316 }
317
318 return true;
319}
320
321bool ParserTernaryOperatorExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
322{
323 ParserToken symbol1(TokenType::QuestionMark);
324 ParserToken symbol2(TokenType::Colon);
325
326 ASTPtr elem_cond;
327 ASTPtr elem_then;
328 ASTPtr elem_else;
329
330 if (!elem_parser.parse(pos, elem_cond, expected))
331 return false;
332
333 if (!symbol1.ignore(pos, expected))
334 node = elem_cond;
335 else
336 {
337 if (!elem_parser.parse(pos, elem_then, expected))
338 return false;
339
340 if (!symbol2.ignore(pos, expected))
341 return false;
342
343 if (!elem_parser.parse(pos, elem_else, expected))
344 return false;
345
346 /// the function corresponding to the operator
347 auto function = std::make_shared<ASTFunction>();
348
349 /// function arguments
350 auto exp_list = std::make_shared<ASTExpressionList>();
351
352 function->name = "if";
353 function->arguments = exp_list;
354 function->children.push_back(exp_list);
355
356 exp_list->children.push_back(elem_cond);
357 exp_list->children.push_back(elem_then);
358 exp_list->children.push_back(elem_else);
359
360 node = function;
361 }
362
363 return true;
364}
365
366
367bool ParserLambdaExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
368{
369 ParserToken arrow(TokenType::Arrow);
370 ParserToken open(TokenType::OpeningRoundBracket);
371 ParserToken close(TokenType::ClosingRoundBracket);
372
373 Pos begin = pos;
374
375 do
376 {
377 ASTPtr inner_arguments;
378 ASTPtr expression;
379
380 bool was_open = false;
381
382 if (open.ignore(pos, expected))
383 {
384 was_open = true;
385 }
386
387 if (!ParserList(std::make_unique<ParserIdentifier>(), std::make_unique<ParserToken>(TokenType::Comma)).parse(pos, inner_arguments, expected))
388 break;
389
390 if (was_open)
391 {
392 if (!close.ignore(pos, expected))
393 break;
394 }
395
396 if (!arrow.ignore(pos, expected))
397 break;
398
399 if (!elem_parser.parse(pos, expression, expected))
400 return false;
401
402 /// lambda(tuple(inner_arguments), expression)
403
404 auto lambda = std::make_shared<ASTFunction>();
405 node = lambda;
406 lambda->name = "lambda";
407
408 auto outer_arguments = std::make_shared<ASTExpressionList>();
409 lambda->arguments = outer_arguments;
410 lambda->children.push_back(lambda->arguments);
411
412 auto tuple = std::make_shared<ASTFunction>();
413 outer_arguments->children.push_back(tuple);
414 tuple->name = "tuple";
415 tuple->arguments = inner_arguments;
416 tuple->children.push_back(inner_arguments);
417
418 outer_arguments->children.push_back(expression);
419
420 return true;
421 }
422 while (false);
423
424 pos = begin;
425 return elem_parser.parse(pos, node, expected);
426}
427
428
429bool ParserPrefixUnaryOperatorExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
430{
431 /// try to find any of the valid operators
432 const char ** it;
433 for (it = operators; *it; it += 2)
434 {
435 if (parseOperator(pos, *it, expected))
436 break;
437 }
438
439 /// Let's parse chains of the form `NOT NOT x`. This is hack.
440 /** This is done, because among the unary operators there is only a minus and NOT.
441 * But for a minus the chain of unary operators does not need to be supported.
442 */
443 if (it[0] && 0 == strncmp(it[0], "NOT", 3))
444 {
445 /// Was there an even number of NOTs.
446 bool even = false;
447
448 const char ** jt;
449 while (true)
450 {
451 for (jt = operators; *jt; jt += 2)
452 if (parseOperator(pos, *jt, expected))
453 break;
454
455 if (!*jt)
456 break;
457
458 even = !even;
459 }
460
461 if (even)
462 it = jt; /// Zero the result of parsing the first NOT. It turns out, as if there is no `NOT` chain at all.
463 }
464
465 ASTPtr elem;
466 if (!elem_parser->parse(pos, elem, expected))
467 return false;
468
469 if (!*it)
470 node = elem;
471 else
472 {
473 /// the function corresponding to the operator
474 auto function = std::make_shared<ASTFunction>();
475
476 /// function arguments
477 auto exp_list = std::make_shared<ASTExpressionList>();
478
479 function->name = it[1];
480 function->arguments = exp_list;
481 function->children.push_back(exp_list);
482
483 exp_list->children.push_back(elem);
484
485 node = function;
486 }
487
488 return true;
489}
490
491
492bool ParserUnaryMinusExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
493{
494 /// As an exception, negative numbers should be parsed as literals, and not as an application of the operator.
495
496 if (pos->type == TokenType::Minus)
497 {
498 ParserLiteral lit_p;
499 Pos begin = pos;
500
501 if (lit_p.parse(pos, node, expected))
502 return true;
503
504 pos = begin;
505 }
506
507 return operator_parser.parse(pos, node, expected);
508}
509
510
511bool ParserArrayElementExpression::parseImpl(Pos & pos, ASTPtr & node, Expected &expected)
512{
513 return ParserLeftAssociativeBinaryOperatorList{
514 operators,
515 std::make_unique<ParserExpressionElement>(),
516 std::make_unique<ParserExpressionWithOptionalAlias>(false)
517 }.parse(pos, node, expected);
518}
519
520
521bool ParserTupleElementExpression::parseImpl(Pos & pos, ASTPtr & node, Expected &expected)
522{
523 return ParserLeftAssociativeBinaryOperatorList{
524 operators,
525 std::make_unique<ParserArrayElementExpression>(),
526 std::make_unique<ParserUnsignedInteger>()
527 }.parse(pos, node, expected);
528}
529
530
531ParserExpressionWithOptionalAlias::ParserExpressionWithOptionalAlias(bool allow_alias_without_as_keyword)
532 : impl(std::make_unique<ParserWithOptionalAlias>(std::make_unique<ParserExpression>(),
533 allow_alias_without_as_keyword))
534{
535}
536
537
538bool ParserExpressionList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
539{
540 return ParserList(
541 std::make_unique<ParserExpressionWithOptionalAlias>(allow_alias_without_as_keyword),
542 std::make_unique<ParserToken>(TokenType::Comma))
543 .parse(pos, node, expected);
544}
545
546
547bool ParserNotEmptyExpressionList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
548{
549 return nested_parser.parse(pos, node, expected) && !node->children.empty();
550}
551
552
553bool ParserOrderByExpressionList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
554{
555 return ParserList(std::make_unique<ParserOrderByElement>(), std::make_unique<ParserToken>(TokenType::Comma), false)
556 .parse(pos, node, expected);
557}
558
559
560bool ParserTTLExpressionList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
561{
562 return ParserList(std::make_unique<ParserTTLElement>(), std::make_unique<ParserToken>(TokenType::Comma), false)
563 .parse(pos, node, expected);
564}
565
566
567bool ParserNullityChecking::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
568{
569 ASTPtr node_comp;
570 if (!elem_parser.parse(pos, node_comp, expected))
571 return false;
572
573 ParserKeyword s_is{"IS"};
574 ParserKeyword s_not{"NOT"};
575 ParserKeyword s_null{"NULL"};
576
577 if (s_is.ignore(pos, expected))
578 {
579 bool is_not = false;
580 if (s_not.ignore(pos, expected))
581 is_not = true;
582
583 if (!s_null.ignore(pos, expected))
584 return false;
585
586 auto args = std::make_shared<ASTExpressionList>();
587 args->children.push_back(node_comp);
588
589 auto function = std::make_shared<ASTFunction>();
590 function->name = is_not ? "isNotNull" : "isNull";
591 function->arguments = args;
592 function->children.push_back(function->arguments);
593
594 node = function;
595 }
596 else
597 node = node_comp;
598
599 return true;
600}
601
602
603bool ParserIntervalOperatorExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
604{
605 /// If no INTERVAL keyword, go to nested parser.
606 if (!ParserKeyword("INTERVAL").ignore(pos, expected))
607 return next_parser.parse(pos, node, expected);
608
609 ASTPtr expr;
610 /// Any expression can be inside, because operator surrounds it.
611 if (!ParserExpressionWithOptionalAlias(false).parse(pos, expr, expected))
612 return false;
613
614 IntervalKind interval_kind;
615 if (!parseIntervalKind(pos, expected, interval_kind))
616 return false;
617
618 /// the function corresponding to the operator
619 auto function = std::make_shared<ASTFunction>();
620
621 /// function arguments
622 auto exp_list = std::make_shared<ASTExpressionList>();
623
624 /// the first argument of the function is the previous element, the second is the next one
625 function->name = interval_kind.toNameOfFunctionToIntervalDataType();
626 function->arguments = exp_list;
627 function->children.push_back(exp_list);
628
629 exp_list->children.push_back(expr);
630
631 node = function;
632 return true;
633}
634
635bool ParserKeyValuePair::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
636{
637 ParserIdentifier id_parser;
638 ParserLiteral literal_parser;
639
640 ASTPtr identifier;
641 ASTPtr value;
642 bool with_brackets = false;
643 if (!id_parser.parse(pos, identifier, expected))
644 return false;
645
646 /// If it's not literal or identifier, than it's possible list of pairs
647 if (!literal_parser.parse(pos, value, expected) && !id_parser.parse(pos, value, expected))
648 {
649 ParserKeyValuePairsList kv_pairs_list;
650 ParserToken open(TokenType::OpeningRoundBracket);
651 ParserToken close(TokenType::ClosingRoundBracket);
652
653 if (!open.ignore(pos))
654 return false;
655
656 if (!kv_pairs_list.parse(pos, value, expected))
657 return false;
658
659 if (!close.ignore(pos))
660 return false;
661
662 with_brackets = true;
663 }
664
665 auto pair = std::make_shared<ASTPair>(with_brackets);
666 pair->first = Poco::toLower(typeid_cast<ASTIdentifier &>(*identifier.get()).name);
667 pair->second = value;
668 node = pair;
669 return true;
670}
671
672bool ParserKeyValuePairsList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
673{
674 ParserList parser(std::make_unique<ParserKeyValuePair>(), std::make_unique<ParserNothing>(), true, 0);
675 return parser.parse(pos, node, expected);
676}
677
678}
679