@@ -1694,21 +1694,34 @@ impl Parser {
1694
1694
// time which it is... so we just try to parse both.
1695
1695
//
1696
1696
// 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
1704
1707
//
1705
1708
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`.
1706
1713
Ok ( table_factor) => Ok ( table_factor) ,
1707
1714
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.
1712
1725
self . index = index;
1713
1726
let table_and_joins = self . parse_table_and_joins ( ) ?;
1714
1727
match table_and_joins. relation {
0 commit comments