Skip to content

Commit 54da75a

Browse files
committed
Go into more detail about handling the parens in parse_table_factor
1 parent 57b510a commit 54da75a

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/sqlparser.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,21 +1694,34 @@ impl Parser {
16941694
// time which it is... so we just try to parse both.
16951695
//
16961696
// Here's an example that demonstrates the complexity:
1697-
//
1698-
// SELECT * FROM ( ( (SELECT 1) UNION (SELECT 2) ) AS t1 NATURAL JOIN t2 )
1699-
// ^ ^ ^
1700-
// | | |
1701-
// | | belongs to the subquery
1702-
// | starts a derived table (subquery)
1703-
// starts a nested join
1697+
// /-------------------------------------------------------\
1698+
// | /-----------------------------------\ |
1699+
// SELECT * FROM ( ( ( (SELECT 1) UNION (SELECT 2) ) AS t1 NATURAL JOIN t2 ) )
1700+
// ^ ^ ^ ^
1701+
// | | | |
1702+
// | | | |
1703+
// | | | (4) belongs to a SQLSetExpr::Query inside the subquery
1704+
// | | (3) starts a derived table (subquery)
1705+
// | (2) starts a nested join
1706+
// (1) an additional set of parens around a nested join
17041707
//
17051708
match self.parse_derived_table_factor(NotLateral) {
1709+
// The recently consumed '(' started a derived table, and we've
1710+
// parsed the subquery, followed by the closing ')', and the
1711+
// alias of the derived table. In the example above this is
1712+
// case (3), and the next token would be `NATURAL`.
17061713
Ok(table_factor) => Ok(table_factor),
17071714
Err(_) => {
1708-
// It wasn't a derived table. Ignore the error and back up
1709-
// to where we were before. Either we'll be able to parse a
1710-
// valid nested join, or we won't, and we'll return that
1711-
// error instead.
1715+
// The '(' we've recently consumed does not start a derived
1716+
// table. For valid input this can happen either when the
1717+
// token following the paren can't start a query (e.g. `foo`
1718+
// in `FROM (foo NATURAL JOIN bar)`, or when the '(' we've
1719+
// consumed is followed by another '(' that starts a
1720+
// derived table, like (3), or another nested join (2).
1721+
//
1722+
// Ignore the error and back up to where we were before.
1723+
// Either we'll be able to parse a valid nested join, or
1724+
// we won't, and we'll return that error instead.
17121725
self.index = index;
17131726
let table_and_joins = self.parse_table_and_joins()?;
17141727
match table_and_joins.relation {

0 commit comments

Comments
 (0)