1#pragma once
2
3#include <list>
4
5#include <Parsers/IParserBase.h>
6#include <Parsers/CommonParsers.h>
7
8
9namespace DB
10{
11
12/** Consequent pairs of rows: the operator and the corresponding function. For example, "+" -> "plus".
13 * The parsing order of the operators is significant.
14 */
15using Operators_t = const char **;
16
17
18/** List of elements separated by something. */
19class ParserList : public IParserBase
20{
21public:
22 ParserList(ParserPtr && elem_parser_, ParserPtr && separator_parser_, bool allow_empty_ = true, char result_separator_ = ',')
23 : elem_parser(std::move(elem_parser_))
24 , separator_parser(std::move(separator_parser_))
25 , allow_empty(allow_empty_)
26 , result_separator(result_separator_)
27 {
28 }
29protected:
30 const char * getName() const { return "list of elements"; }
31 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
32private:
33 ParserPtr elem_parser;
34 ParserPtr separator_parser;
35 bool allow_empty;
36 char result_separator;
37};
38
39
40/** An expression with an infix binary left-associative operator.
41 * For example, a + b - c + d.
42 */
43class ParserLeftAssociativeBinaryOperatorList : public IParserBase
44{
45private:
46 Operators_t operators;
47 ParserPtr first_elem_parser;
48 ParserPtr remaining_elem_parser;
49
50public:
51 /** `operators_` - allowed operators and their corresponding functions
52 */
53 ParserLeftAssociativeBinaryOperatorList(Operators_t operators_, ParserPtr && first_elem_parser_)
54 : operators(operators_), first_elem_parser(std::move(first_elem_parser_))
55 {
56 }
57
58 ParserLeftAssociativeBinaryOperatorList(Operators_t operators_, ParserPtr && first_elem_parser_,
59 ParserPtr && remaining_elem_parser_)
60 : operators(operators_), first_elem_parser(std::move(first_elem_parser_)),
61 remaining_elem_parser(std::move(remaining_elem_parser_))
62 {
63 }
64
65protected:
66 const char * getName() const { return "list, delimited by binary operators"; }
67
68 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
69};
70
71
72/** Expression with an infix operator of arbitrary arity.
73 * For example, a AND b AND c AND d.
74 */
75class ParserVariableArityOperatorList : public IParserBase
76{
77private:
78 const char * infix;
79 const char * function_name;
80 ParserPtr elem_parser;
81
82public:
83 ParserVariableArityOperatorList(const char * infix_, const char * function_, ParserPtr && elem_parser_)
84 : infix(infix_), function_name(function_), elem_parser(std::move(elem_parser_))
85 {
86 }
87
88protected:
89 const char * getName() const { return "list, delimited by operator of variable arity"; }
90
91 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
92};
93
94
95/** An expression with a prefix unary operator.
96 * Example, NOT x.
97 */
98class ParserPrefixUnaryOperatorExpression : public IParserBase
99{
100private:
101 Operators_t operators;
102 ParserPtr elem_parser;
103
104public:
105 /** `operators_` - allowed operators and their corresponding functions
106 */
107 ParserPrefixUnaryOperatorExpression(Operators_t operators_, ParserPtr && elem_parser_)
108 : operators(operators_), elem_parser(std::move(elem_parser_))
109 {
110 }
111
112protected:
113 const char * getName() const { return "expression with prefix unary operator"; }
114 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
115};
116
117
118class ParserArrayElementExpression : public IParserBase
119{
120private:
121 static const char * operators[];
122
123protected:
124 const char * getName() const { return "array element expression"; }
125
126 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
127};
128
129
130class ParserTupleElementExpression : public IParserBase
131{
132private:
133 static const char * operators[];
134
135protected:
136 const char * getName() const { return "tuple element expression"; }
137
138 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
139};
140
141
142class ParserUnaryMinusExpression : public IParserBase
143{
144private:
145 static const char * operators[];
146 ParserPrefixUnaryOperatorExpression operator_parser {operators, std::make_unique<ParserTupleElementExpression>()};
147
148protected:
149 const char * getName() const { return "unary minus expression"; }
150
151 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
152};
153
154
155class ParserMultiplicativeExpression : public IParserBase
156{
157private:
158 static const char * operators[];
159 ParserLeftAssociativeBinaryOperatorList operator_parser {operators, std::make_unique<ParserUnaryMinusExpression>()};
160
161protected:
162 const char * getName() const { return "multiplicative expression"; }
163
164 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
165 {
166 return operator_parser.parse(pos, node, expected);
167 }
168};
169
170
171/// Optional conversion to INTERVAL data type. Example: "INTERVAL x SECOND" parsed as "toIntervalSecond(x)".
172class ParserIntervalOperatorExpression : public IParserBase
173{
174protected:
175 ParserMultiplicativeExpression next_parser;
176
177 const char * getName() const { return "INTERVAL operator expression"; }
178 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
179};
180
181
182class ParserAdditiveExpression : public IParserBase
183{
184private:
185 static const char * operators[];
186 ParserLeftAssociativeBinaryOperatorList operator_parser {operators, std::make_unique<ParserIntervalOperatorExpression>()};
187
188protected:
189 const char * getName() const { return "additive expression"; }
190
191 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
192 {
193 return operator_parser.parse(pos, node, expected);
194 }
195};
196
197
198class ParserConcatExpression : public IParserBase
199{
200 ParserVariableArityOperatorList operator_parser {"||", "concat", std::make_unique<ParserAdditiveExpression>()};
201
202protected:
203 const char * getName() const { return "string concatenation expression"; }
204
205 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
206 {
207 return operator_parser.parse(pos, node, expected);
208 }
209};
210
211
212class ParserBetweenExpression : public IParserBase
213{
214private:
215 ParserConcatExpression elem_parser;
216
217protected:
218 const char * getName() const { return "BETWEEN expression"; }
219
220 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
221};
222
223
224class ParserComparisonExpression : public IParserBase
225{
226private:
227 static const char * operators[];
228 ParserLeftAssociativeBinaryOperatorList operator_parser {operators, std::make_unique<ParserBetweenExpression>()};
229
230protected:
231 const char * getName() const { return "comparison expression"; }
232
233 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
234 {
235 return operator_parser.parse(pos, node, expected);
236 }
237};
238
239
240/** Parser for nullity checking with IS (NOT) NULL.
241 */
242class ParserNullityChecking : public IParserBase
243{
244private:
245 ParserComparisonExpression elem_parser;
246
247protected:
248 const char * getName() const override { return "nullity checking"; }
249 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
250};
251
252
253class ParserLogicalNotExpression : public IParserBase
254{
255private:
256 static const char * operators[];
257 ParserPrefixUnaryOperatorExpression operator_parser {operators, std::make_unique<ParserNullityChecking>()};
258
259protected:
260 const char * getName() const { return "logical-NOT expression"; }
261
262 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
263 {
264 return operator_parser.parse(pos, node, expected);
265 }
266};
267
268
269class ParserLogicalAndExpression : public IParserBase
270{
271private:
272 ParserVariableArityOperatorList operator_parser {"AND", "and", std::make_unique<ParserLogicalNotExpression>()};
273
274protected:
275 const char * getName() const { return "logical-AND expression"; }
276
277 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
278 {
279 return operator_parser.parse(pos, node, expected);
280 }
281};
282
283
284class ParserLogicalOrExpression : public IParserBase
285{
286private:
287 ParserVariableArityOperatorList operator_parser {"OR", "or", std::make_unique<ParserLogicalAndExpression>()};
288
289protected:
290 const char * getName() const { return "logical-OR expression"; }
291
292 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
293 {
294 return operator_parser.parse(pos, node, expected);
295 }
296};
297
298
299/** An expression with ternary operator.
300 * For example, a = 1 ? b + 1 : c * 2.
301 */
302class ParserTernaryOperatorExpression : public IParserBase
303{
304private:
305 ParserLogicalOrExpression elem_parser;
306
307protected:
308 const char * getName() const { return "expression with ternary operator"; }
309
310 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
311};
312
313
314class ParserLambdaExpression : public IParserBase
315{
316private:
317 ParserTernaryOperatorExpression elem_parser;
318
319protected:
320 const char * getName() const { return "lambda expression"; }
321
322 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
323};
324
325
326using ParserExpression = ParserLambdaExpression;
327
328
329class ParserExpressionWithOptionalAlias : public IParserBase
330{
331public:
332 ParserExpressionWithOptionalAlias(bool allow_alias_without_as_keyword);
333protected:
334 ParserPtr impl;
335
336 const char * getName() const { return "expression with optional alias"; }
337
338 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
339 {
340 return impl->parse(pos, node, expected);
341 }
342};
343
344
345/** A comma-separated list of expressions, probably empty. */
346class ParserExpressionList : public IParserBase
347{
348public:
349 ParserExpressionList(bool allow_alias_without_as_keyword_)
350 : allow_alias_without_as_keyword(allow_alias_without_as_keyword_) {}
351
352protected:
353 bool allow_alias_without_as_keyword;
354
355 const char * getName() const { return "list of expressions"; }
356 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
357};
358
359
360class ParserNotEmptyExpressionList : public IParserBase
361{
362public:
363 ParserNotEmptyExpressionList(bool allow_alias_without_as_keyword)
364 : nested_parser(allow_alias_without_as_keyword) {}
365private:
366 ParserExpressionList nested_parser;
367protected:
368 const char * getName() const { return "not empty list of expressions"; }
369 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
370};
371
372
373class ParserOrderByExpressionList : public IParserBase
374{
375protected:
376 const char * getName() const { return "order by expression"; }
377 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
378};
379
380
381/// Parser for key-value pair, where value can be list of pairs.
382class ParserKeyValuePair : public IParserBase
383{
384protected:
385 const char * getName() const override { return "key-value pair"; }
386 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
387};
388
389
390/// Parser for list of key-value pairs.
391class ParserKeyValuePairsList : public IParserBase
392{
393protected:
394 const char * getName() const override { return "list of pairs"; }
395 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
396};
397
398
399class ParserTTLExpressionList : public IParserBase
400{
401protected:
402 const char * getName() const { return "ttl expression"; }
403 bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
404};
405
406}
407