| 254 | |
| 255 | |
| 256 | ASTPtr tryParseQuery( |
| 257 | IParser & parser, |
| 258 | const char * & _out_query_end, /* also query begin as input parameter */ |
| 259 | const char * all_queries_end, |
| 260 | std::string & out_error_message, |
| 261 | bool hilite, |
| 262 | const std::string & query_description, |
| 263 | bool allow_multi_statements, |
| 264 | size_t max_query_size, |
| 265 | size_t max_parser_depth, |
| 266 | size_t max_parser_backtracks, |
| 267 | bool skip_insignificant) |
| 268 | { |
| 269 | const char * query_begin = _out_query_end; |
| 270 | Tokens tokens(query_begin, all_queries_end, max_query_size, skip_insignificant); |
| 271 | /// NOTE: consider use UInt32 for max_parser_depth setting. |
| 272 | IParser::Pos token_iterator(tokens, static_cast<uint32_t>(max_parser_depth), static_cast<uint32_t>(max_parser_backtracks)); |
| 273 | |
| 274 | if (token_iterator->isEnd() |
| 275 | || token_iterator->type == TokenType::Semicolon) |
| 276 | { |
| 277 | out_error_message = "Empty query"; |
| 278 | // Token iterator skips over comments, so we'll get this error for queries |
| 279 | // like this: |
| 280 | // " |
| 281 | // -- just a comment |
| 282 | // ; |
| 283 | //" |
| 284 | // Advance the position, so that we can use this parser for stream parsing |
| 285 | // even in presence of such queries. |
| 286 | _out_query_end = token_iterator->begin; |
| 287 | return nullptr; |
| 288 | } |
| 289 | |
| 290 | Expected expected; |
| 291 | |
| 292 | /** A shortcut - if Lexer found invalid tokens, fail early without full parsing. |
| 293 | * But there are certain cases when invalid tokens are permitted: |
| 294 | * 1. INSERT queries can have arbitrary data after the FORMAT clause, that is parsed by a different parser. |
| 295 | * 2. It can also be the case when there are multiple queries separated by semicolons, and the first queries are ok |
| 296 | * while subsequent queries have syntax errors. |
| 297 | * |
| 298 | * This shortcut is needed to avoid complex backtracking in case of obviously erroneous queries. |
| 299 | */ |
| 300 | IParser::Pos lookahead(token_iterator); |
| 301 | if (!ParserKeyword(Keyword::INSERT_INTO).ignore(lookahead)) |
| 302 | { |
| 303 | while (lookahead->type != TokenType::Semicolon && lookahead->type != TokenType::EndOfStream) |
| 304 | { |
| 305 | if (lookahead->isError()) |
| 306 | { |
| 307 | out_error_message = getLexicalErrorMessage(query_begin, all_queries_end, *lookahead, hilite, query_description); |
| 308 | // Advance the position for further processing of possible test hint. |
| 309 | _out_query_end = token_iterator.max().end; |
| 310 | return nullptr; |
| 311 | } |
| 312 | |
| 313 | ++lookahead; |