| 1 | #pragma once | 
|---|
| 2 |  | 
|---|
| 3 | #include <list> | 
|---|
| 4 |  | 
|---|
| 5 | #include <Parsers/IParserBase.h> | 
|---|
| 6 | #include <Parsers/CommonParsers.h> | 
|---|
| 7 |  | 
|---|
| 8 |  | 
|---|
| 9 | namespace 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 | */ | 
|---|
| 15 | using Operators_t = const char **; | 
|---|
| 16 |  | 
|---|
| 17 |  | 
|---|
| 18 | /** List of elements separated by something. */ | 
|---|
| 19 | class ParserList : public IParserBase | 
|---|
| 20 | { | 
|---|
| 21 | public: | 
|---|
| 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 | } | 
|---|
| 29 | protected: | 
|---|
| 30 | const char * getName() const { return "list of elements"; } | 
|---|
| 31 | bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected); | 
|---|
| 32 | private: | 
|---|
| 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 | */ | 
|---|
| 43 | class ParserLeftAssociativeBinaryOperatorList : public IParserBase | 
|---|
| 44 | { | 
|---|
| 45 | private: | 
|---|
| 46 | Operators_t operators; | 
|---|
| 47 | ParserPtr first_elem_parser; | 
|---|
| 48 | ParserPtr remaining_elem_parser; | 
|---|
| 49 |  | 
|---|
| 50 | public: | 
|---|
| 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 |  | 
|---|
| 65 | protected: | 
|---|
| 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 | */ | 
|---|
| 75 | class ParserVariableArityOperatorList : public IParserBase | 
|---|
| 76 | { | 
|---|
| 77 | private: | 
|---|
| 78 | const char * infix; | 
|---|
| 79 | const char * function_name; | 
|---|
| 80 | ParserPtr elem_parser; | 
|---|
| 81 |  | 
|---|
| 82 | public: | 
|---|
| 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 |  | 
|---|
| 88 | protected: | 
|---|
| 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 | */ | 
|---|
| 98 | class ParserPrefixUnaryOperatorExpression : public IParserBase | 
|---|
| 99 | { | 
|---|
| 100 | private: | 
|---|
| 101 | Operators_t operators; | 
|---|
| 102 | ParserPtr elem_parser; | 
|---|
| 103 |  | 
|---|
| 104 | public: | 
|---|
| 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 |  | 
|---|
| 112 | protected: | 
|---|
| 113 | const char * getName() const { return "expression with prefix unary operator"; } | 
|---|
| 114 | bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected); | 
|---|
| 115 | }; | 
|---|
| 116 |  | 
|---|
| 117 |  | 
|---|
| 118 | class ParserArrayElementExpression : public IParserBase | 
|---|
| 119 | { | 
|---|
| 120 | private: | 
|---|
| 121 | static const char * operators[]; | 
|---|
| 122 |  | 
|---|
| 123 | protected: | 
|---|
| 124 | const char * getName() const { return "array element expression"; } | 
|---|
| 125 |  | 
|---|
| 126 | bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected); | 
|---|
| 127 | }; | 
|---|
| 128 |  | 
|---|
| 129 |  | 
|---|
| 130 | class ParserTupleElementExpression : public IParserBase | 
|---|
| 131 | { | 
|---|
| 132 | private: | 
|---|
| 133 | static const char * operators[]; | 
|---|
| 134 |  | 
|---|
| 135 | protected: | 
|---|
| 136 | const char * getName() const { return "tuple element expression"; } | 
|---|
| 137 |  | 
|---|
| 138 | bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected); | 
|---|
| 139 | }; | 
|---|
| 140 |  | 
|---|
| 141 |  | 
|---|
| 142 | class ParserUnaryMinusExpression : public IParserBase | 
|---|
| 143 | { | 
|---|
| 144 | private: | 
|---|
| 145 | static const char * operators[]; | 
|---|
| 146 | ParserPrefixUnaryOperatorExpression operator_parser {operators, std::make_unique<ParserTupleElementExpression>()}; | 
|---|
| 147 |  | 
|---|
| 148 | protected: | 
|---|
| 149 | const char * getName() const { return "unary minus expression"; } | 
|---|
| 150 |  | 
|---|
| 151 | bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected); | 
|---|
| 152 | }; | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 | class ParserMultiplicativeExpression : public IParserBase | 
|---|
| 156 | { | 
|---|
| 157 | private: | 
|---|
| 158 | static const char * operators[]; | 
|---|
| 159 | ParserLeftAssociativeBinaryOperatorList operator_parser {operators, std::make_unique<ParserUnaryMinusExpression>()}; | 
|---|
| 160 |  | 
|---|
| 161 | protected: | 
|---|
| 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)". | 
|---|
| 172 | class ParserIntervalOperatorExpression : public IParserBase | 
|---|
| 173 | { | 
|---|
| 174 | protected: | 
|---|
| 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 |  | 
|---|
| 182 | class ParserAdditiveExpression : public IParserBase | 
|---|
| 183 | { | 
|---|
| 184 | private: | 
|---|
| 185 | static const char * operators[]; | 
|---|
| 186 | ParserLeftAssociativeBinaryOperatorList operator_parser {operators, std::make_unique<ParserIntervalOperatorExpression>()}; | 
|---|
| 187 |  | 
|---|
| 188 | protected: | 
|---|
| 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 |  | 
|---|
| 198 | class ParserConcatExpression : public IParserBase | 
|---|
| 199 | { | 
|---|
| 200 | ParserVariableArityOperatorList operator_parser { "||", "concat", std::make_unique<ParserAdditiveExpression>()}; | 
|---|
| 201 |  | 
|---|
| 202 | protected: | 
|---|
| 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 |  | 
|---|
| 212 | class ParserBetweenExpression : public IParserBase | 
|---|
| 213 | { | 
|---|
| 214 | private: | 
|---|
| 215 | ParserConcatExpression elem_parser; | 
|---|
| 216 |  | 
|---|
| 217 | protected: | 
|---|
| 218 | const char * getName() const { return "BETWEEN expression"; } | 
|---|
| 219 |  | 
|---|
| 220 | bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected); | 
|---|
| 221 | }; | 
|---|
| 222 |  | 
|---|
| 223 |  | 
|---|
| 224 | class ParserComparisonExpression : public IParserBase | 
|---|
| 225 | { | 
|---|
| 226 | private: | 
|---|
| 227 | static const char * operators[]; | 
|---|
| 228 | ParserLeftAssociativeBinaryOperatorList operator_parser {operators, std::make_unique<ParserBetweenExpression>()}; | 
|---|
| 229 |  | 
|---|
| 230 | protected: | 
|---|
| 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 | */ | 
|---|
| 242 | class ParserNullityChecking : public IParserBase | 
|---|
| 243 | { | 
|---|
| 244 | private: | 
|---|
| 245 | ParserComparisonExpression elem_parser; | 
|---|
| 246 |  | 
|---|
| 247 | protected: | 
|---|
| 248 | const char * getName() const override { return "nullity checking"; } | 
|---|
| 249 | bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override; | 
|---|
| 250 | }; | 
|---|
| 251 |  | 
|---|
| 252 |  | 
|---|
| 253 | class ParserLogicalNotExpression : public IParserBase | 
|---|
| 254 | { | 
|---|
| 255 | private: | 
|---|
| 256 | static const char * operators[]; | 
|---|
| 257 | ParserPrefixUnaryOperatorExpression operator_parser {operators, std::make_unique<ParserNullityChecking>()}; | 
|---|
| 258 |  | 
|---|
| 259 | protected: | 
|---|
| 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 |  | 
|---|
| 269 | class ParserLogicalAndExpression : public IParserBase | 
|---|
| 270 | { | 
|---|
| 271 | private: | 
|---|
| 272 | ParserVariableArityOperatorList operator_parser { "AND", "and", std::make_unique<ParserLogicalNotExpression>()}; | 
|---|
| 273 |  | 
|---|
| 274 | protected: | 
|---|
| 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 |  | 
|---|
| 284 | class ParserLogicalOrExpression : public IParserBase | 
|---|
| 285 | { | 
|---|
| 286 | private: | 
|---|
| 287 | ParserVariableArityOperatorList operator_parser { "OR", "or", std::make_unique<ParserLogicalAndExpression>()}; | 
|---|
| 288 |  | 
|---|
| 289 | protected: | 
|---|
| 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 | */ | 
|---|
| 302 | class ParserTernaryOperatorExpression : public IParserBase | 
|---|
| 303 | { | 
|---|
| 304 | private: | 
|---|
| 305 | ParserLogicalOrExpression elem_parser; | 
|---|
| 306 |  | 
|---|
| 307 | protected: | 
|---|
| 308 | const char * getName() const { return "expression with ternary operator"; } | 
|---|
| 309 |  | 
|---|
| 310 | bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected); | 
|---|
| 311 | }; | 
|---|
| 312 |  | 
|---|
| 313 |  | 
|---|
| 314 | class ParserLambdaExpression : public IParserBase | 
|---|
| 315 | { | 
|---|
| 316 | private: | 
|---|
| 317 | ParserTernaryOperatorExpression elem_parser; | 
|---|
| 318 |  | 
|---|
| 319 | protected: | 
|---|
| 320 | const char * getName() const { return "lambda expression"; } | 
|---|
| 321 |  | 
|---|
| 322 | bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected); | 
|---|
| 323 | }; | 
|---|
| 324 |  | 
|---|
| 325 |  | 
|---|
| 326 | using ParserExpression = ParserLambdaExpression; | 
|---|
| 327 |  | 
|---|
| 328 |  | 
|---|
| 329 | class ParserExpressionWithOptionalAlias : public IParserBase | 
|---|
| 330 | { | 
|---|
| 331 | public: | 
|---|
| 332 | ParserExpressionWithOptionalAlias(bool allow_alias_without_as_keyword); | 
|---|
| 333 | protected: | 
|---|
| 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. */ | 
|---|
| 346 | class ParserExpressionList : public IParserBase | 
|---|
| 347 | { | 
|---|
| 348 | public: | 
|---|
| 349 | ParserExpressionList(bool allow_alias_without_as_keyword_) | 
|---|
| 350 | : allow_alias_without_as_keyword(allow_alias_without_as_keyword_) {} | 
|---|
| 351 |  | 
|---|
| 352 | protected: | 
|---|
| 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 |  | 
|---|
| 360 | class ParserNotEmptyExpressionList : public IParserBase | 
|---|
| 361 | { | 
|---|
| 362 | public: | 
|---|
| 363 | ParserNotEmptyExpressionList(bool allow_alias_without_as_keyword) | 
|---|
| 364 | : nested_parser(allow_alias_without_as_keyword) {} | 
|---|
| 365 | private: | 
|---|
| 366 | ParserExpressionList nested_parser; | 
|---|
| 367 | protected: | 
|---|
| 368 | const char * getName() const { return "not empty list of expressions"; } | 
|---|
| 369 | bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected); | 
|---|
| 370 | }; | 
|---|
| 371 |  | 
|---|
| 372 |  | 
|---|
| 373 | class ParserOrderByExpressionList : public IParserBase | 
|---|
| 374 | { | 
|---|
| 375 | protected: | 
|---|
| 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. | 
|---|
| 382 | class ParserKeyValuePair : public IParserBase | 
|---|
| 383 | { | 
|---|
| 384 | protected: | 
|---|
| 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. | 
|---|
| 391 | class ParserKeyValuePairsList : public IParserBase | 
|---|
| 392 | { | 
|---|
| 393 | protected: | 
|---|
| 394 | const char * getName() const override { return "list of pairs"; } | 
|---|
| 395 | bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override; | 
|---|
| 396 | }; | 
|---|
| 397 |  | 
|---|
| 398 |  | 
|---|
| 399 | class ParserTTLExpressionList : public IParserBase | 
|---|
| 400 | { | 
|---|
| 401 | protected: | 
|---|
| 402 | const char * getName() const { return "ttl expression"; } | 
|---|
| 403 | bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected); | 
|---|
| 404 | }; | 
|---|
| 405 |  | 
|---|
| 406 | } | 
|---|
| 407 |  | 
|---|