@@ -42,6 +42,12 @@ pub enum IsOptional {
42
42
}
43
43
use IsOptional :: * ;
44
44
45
+ pub enum IsLateral {
46
+ Lateral ,
47
+ NotLateral ,
48
+ }
49
+ use IsLateral :: * ;
50
+
45
51
impl From < TokenizerError > for ParserError {
46
52
fn from ( e : TokenizerError ) -> Self {
47
53
ParserError :: TokenizerError ( format ! ( "{:?}" , e) )
@@ -1670,30 +1676,42 @@ impl Parser {
1670
1676
1671
1677
/// A table name or a parenthesized subquery, followed by optional `[AS] alias`
1672
1678
pub fn parse_table_factor ( & mut self ) -> Result < TableFactor , ParserError > {
1673
- let lateral = self . parse_keyword ( "LATERAL" ) ;
1679
+ if self . parse_keyword ( "LATERAL" ) {
1680
+ // LATERAL must always be followed by a subquery.
1681
+ if !self . consume_token ( & Token :: LParen ) {
1682
+ self . expected ( "subquery after LATERAL" , self . peek_token ( ) ) ?;
1683
+ }
1684
+ return self . parse_derived_table_factor ( Lateral ) ;
1685
+ }
1686
+
1674
1687
if self . consume_token ( & Token :: LParen ) {
1675
- if self . parse_keyword ( "SELECT" )
1676
- || self . parse_keyword ( "WITH" )
1677
- || self . parse_keyword ( "VALUES" )
1678
- {
1679
- self . prev_token ( ) ;
1680
- let subquery = Box :: new ( self . parse_query ( ) ?) ;
1681
- self . expect_token ( & Token :: RParen ) ?;
1682
- let alias = self . parse_optional_table_alias ( keywords:: RESERVED_FOR_TABLE_ALIAS ) ?;
1683
- Ok ( TableFactor :: Derived {
1684
- lateral,
1685
- subquery,
1686
- alias,
1687
- } )
1688
- } else if lateral {
1689
- parser_err ! ( "Expected subquery after LATERAL, found nested join" . to_string( ) )
1690
- } else {
1691
- let table_reference = self . parse_table_and_joins ( ) ?;
1692
- self . expect_token ( & Token :: RParen ) ?;
1693
- Ok ( TableFactor :: NestedJoin ( Box :: new ( table_reference) ) )
1688
+ let index = self . index ;
1689
+ // A left paren introduces either a derived table (i.e., a subquery)
1690
+ // or a nested join. It's nearly impossible to determine ahead of
1691
+ // time which it is... so we just try to parse both.
1692
+ //
1693
+ // Here's an example that demonstrates the complexity:
1694
+ //
1695
+ // SELECT * FROM ( ( (SELECT 1) UNION (SELECT 2) ) AS t1 NATURAL JOIN t2 )
1696
+ // ^ ^ ^
1697
+ // | | |
1698
+ // | | belongs to the subquery
1699
+ // | starts a derived table (subquery)
1700
+ // starts a nested join
1701
+ //
1702
+ match self . parse_derived_table_factor ( NotLateral ) {
1703
+ Ok ( table_factor) => Ok ( table_factor) ,
1704
+ Err ( _) => {
1705
+ // It wasn't a derived table. Ignore the error and back up
1706
+ // to where we were before. Either we'll be able to parse a
1707
+ // valid nested join, or we won't, and we'll return that
1708
+ // error instead.
1709
+ self . index = index;
1710
+ let table_and_joins = self . parse_table_and_joins ( ) ?;
1711
+ self . expect_token ( & Token :: RParen ) ?;
1712
+ Ok ( TableFactor :: NestedJoin ( Box :: new ( table_and_joins) ) )
1713
+ }
1694
1714
}
1695
- } else if lateral {
1696
- self . expected ( "subquery after LATERAL" , self . peek_token ( ) )
1697
1715
} else {
1698
1716
let name = self . parse_object_name ( ) ?;
1699
1717
// Postgres, MSSQL: table-valued functions:
@@ -1723,6 +1741,23 @@ impl Parser {
1723
1741
}
1724
1742
}
1725
1743
1744
+ pub fn parse_derived_table_factor (
1745
+ & mut self ,
1746
+ lateral : IsLateral ,
1747
+ ) -> Result < TableFactor , ParserError > {
1748
+ let subquery = Box :: new ( self . parse_query ( ) ?) ;
1749
+ self . expect_token ( & Token :: RParen ) ?;
1750
+ let alias = self . parse_optional_table_alias ( keywords:: RESERVED_FOR_TABLE_ALIAS ) ?;
1751
+ Ok ( TableFactor :: Derived {
1752
+ lateral : match lateral {
1753
+ Lateral => true ,
1754
+ NotLateral => false ,
1755
+ } ,
1756
+ subquery,
1757
+ alias,
1758
+ } )
1759
+ }
1760
+
1726
1761
fn parse_join_constraint ( & mut self , natural : bool ) -> Result < JoinConstraint , ParserError > {
1727
1762
if natural {
1728
1763
Ok ( JoinConstraint :: Natural )
0 commit comments