MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / parse_table_factor_inner

Method parse_table_factor_inner

src/sql-parser/src/parser.rs:8402–8503  ·  view source on GitHub ↗
(&mut self)

Source from the content-addressed store, hash-verified

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

Callers 1

parse_table_factorMethod · 0.80

Calls 15

parse_keywordMethod · 0.80
consume_tokenMethod · 0.80
parse_keywordsMethod · 0.80
parse_rows_fromMethod · 0.80
parse_raw_nameMethod · 0.80
expect_tokenMethod · 0.80
parse_optional_argsMethod · 0.80
parse_table_and_joinsMethod · 0.80
expectedMethod · 0.80
peek_posMethod · 0.80

Tested by

no test coverage detected