Skip to content

Commit 05a2921

Browse files
committed
1 parent 33303b2 commit 05a2921

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

src/ast/query.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,11 @@ pub enum TableFactor {
225225
subquery: Box<Query>,
226226
alias: Option<TableAlias>,
227227
},
228-
/// Represents a parenthesized join expression, such as
229-
/// `(foo <JOIN> bar [ <JOIN> baz ... ])`.
230-
/// The inner `TableWithJoins` can have no joins only if its
231-
/// `relation` is itself a `TableFactor::NestedJoin`.
228+
/// Represents a parenthesized table factor. The SQL spec only allows a
229+
/// join expression (`(foo <JOIN> bar [ <JOIN> baz ... ])`) to be nested,
230+
/// possibly several times, but the parser also accepts the non-standard
231+
/// nesting of bare tables (`table_with_joins.joins.is_empty()`), so the
232+
/// name `NestedJoin` is a bit of misnomer.
232233
NestedJoin(Box<TableWithJoins>),
233234
}
234235

src/parser.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -1771,7 +1771,6 @@ impl Parser {
17711771
// ^ ^ ^ ^
17721772
// | | | |
17731773
// | | | |
1774-
// | | | |
17751774
// | | | (4) belongs to a SetExpr::Query inside the subquery
17761775
// | | (3) starts a derived table (subquery)
17771776
// | (2) starts a nested join
@@ -1784,23 +1783,20 @@ impl Parser {
17841783
// case (3), and the next token would be `NATURAL`.
17851784
Ok(table_factor) => Ok(table_factor),
17861785
Err(_) => {
1787-
// The '(' we've recently consumed does not start a derived
1788-
// table. For valid input this can happen either when the
1789-
// token following the paren can't start a query (e.g. `foo`
1790-
// in `FROM (foo NATURAL JOIN bar)`, or when the '(' we've
1791-
// consumed is followed by another '(' that starts a
1792-
// derived table, like (3), or another nested join (2).
1793-
//
1794-
// Ignore the error and back up to where we were before.
1795-
// Either we'll be able to parse a valid nested join, or
1796-
// we won't, and we'll return that error instead.
1797-
//
1798-
// Even the SQL spec prohibits derived tables and bare
1799-
// tables from appearing alone in parentheses, we allowed it
1800-
// as some Db's allowed that (snowflake as example)
1786+
// A parsing error from `parse_derived_table_factor` indicates that
1787+
// the '(' we've recently consumed does not start a derived table
1788+
// (cases 1, 2, or 4). Ignore the error and back up to where we
1789+
// were before - right after the opening '('.
18011790
self.index = index;
1791+
1792+
// Inside the parentheses we expect to find a table factor
1793+
// followed by some joins or another level of nesting.
18021794
let table_and_joins = self.parse_table_and_joins()?;
18031795
self.expect_token(&Token::RParen)?;
1796+
// The SQL spec prohibits derived and bare tables from appearing
1797+
// alone in parentheses. We don't enforce this as some databases
1798+
// (e.g. Snowflake) allow such syntax.
1799+
18041800
Ok(TableFactor::NestedJoin(Box::new(table_and_joins)))
18051801
}
18061802
}

0 commit comments

Comments
 (0)