(&mut self)
| 8400 | } |
| 8401 | |
| 8402 | fn parse_table_factor_inner(&mut self) -> Result<TableFactor<Raw>, ParserError> { |
| 8403 | if self.parse_keyword(LATERAL) { |
| 8404 | // LATERAL must always be followed by a subquery or table function. |
| 8405 | if self.consume_token(&Token::LParen) { |
| 8406 | return self.parse_derived_table_factor(Lateral); |
| 8407 | } else if self.parse_keywords(&[ROWS, FROM]) { |
| 8408 | return self.parse_rows_from(); |
| 8409 | } else { |
| 8410 | let name = self.parse_raw_name()?; |
| 8411 | self.expect_token(&Token::LParen)?; |
| 8412 | let args = self.parse_optional_args(false)?; |
| 8413 | let (with_ordinality, alias) = self.parse_table_function_suffix()?; |
| 8414 | return Ok(TableFactor::Function { |
| 8415 | function: Function { |
| 8416 | name, |
| 8417 | args, |
| 8418 | filter: None, |
| 8419 | over: None, |
| 8420 | distinct: false, |
| 8421 | }, |
| 8422 | alias, |
| 8423 | with_ordinality, |
| 8424 | }); |
| 8425 | } |
| 8426 | } |
| 8427 | |
| 8428 | if self.consume_token(&Token::LParen) { |
| 8429 | // A left paren introduces either a derived table (i.e., a subquery) |
| 8430 | // or a nested join. It's nearly impossible to determine ahead of |
| 8431 | // time which it is... so we just try to parse both. |
| 8432 | // |
| 8433 | // Here's an example that demonstrates the complexity: |
| 8434 | // /-------------------------------------------------------\ |
| 8435 | // | /-----------------------------------\ | |
| 8436 | // SELECT * FROM ( ( ( (SELECT 1) UNION (SELECT 2) ) AS t1 NATURAL JOIN t2 ) ) |
| 8437 | // ^ ^ ^ ^ |
| 8438 | // | | | | |
| 8439 | // | | | | |
| 8440 | // | | | (4) belongs to a SetExpr::Query inside the subquery |
| 8441 | // | | (3) starts a derived table (subquery) |
| 8442 | // | (2) starts a nested join |
| 8443 | // (1) an additional set of parens around a nested join |
| 8444 | // |
| 8445 | |
| 8446 | // Check if the recently consumed '(' started a derived table, in |
| 8447 | // which case we've parsed the subquery, followed by the closing |
| 8448 | // ')', and the alias of the derived table. In the example above |
| 8449 | // this is case (3), and the next token would be `NATURAL`. |
| 8450 | maybe!(self.maybe_parse(|parser| parser.parse_derived_table_factor(NotLateral))); |
| 8451 | |
| 8452 | // The '(' we've recently consumed does not start a derived table. |
| 8453 | // For valid input this can happen either when the token following |
| 8454 | // the paren can't start a query (e.g. `foo` in `FROM (foo NATURAL |
| 8455 | // JOIN bar)`, or when the '(' we've consumed is followed by another |
| 8456 | // '(' that starts a derived table, like (3), or another nested join |
| 8457 | // (2). |
| 8458 | // |
| 8459 | // Ignore the error and back up to where we were before. Either |
no test coverage detected