Skip to content

Commit dfa0cfc

Browse files
committed
Require that nested joins always have one join
The SQL specification prohibits constructions like SELECT * FROM a NATURAL JOIN (b) where b sits alone inside parentheses. Parentheses in a FROM entry always introduce either a derived table or a join.
1 parent 5724cf3 commit dfa0cfc

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/sqlparser.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,16 @@ impl Parser {
17081708
// error instead.
17091709
self.index = index;
17101710
let table_and_joins = self.parse_table_and_joins()?;
1711+
match table_and_joins.relation {
1712+
TableFactor::Table { .. } | TableFactor::Derived { .. }
1713+
if table_and_joins.joins.is_empty() =>
1714+
{
1715+
// The SQL spec prohibits derived tables and bare
1716+
// tables from appearing alone in parentheses.
1717+
self.expected("joined table", self.peek_token())?
1718+
}
1719+
_ => (),
1720+
}
17111721
self.expect_token(&Token::RParen)?;
17121722
Ok(TableFactor::NestedJoin(Box::new(table_and_joins)))
17131723
}

tests/sqlparser_common.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,12 @@ fn parse_join_nesting() {
17311731
from.joins,
17321732
vec![join(nest!(nest!(nest!(table("b"), table("c")))))]
17331733
);
1734+
1735+
let res = parse_sql_statements("SELECT * FROM (a NATURAL JOIN (b))");
1736+
assert_eq!(
1737+
ParserError::ParserError("Expected joined table, found: )".to_string()),
1738+
res.unwrap_err()
1739+
);
17341740
}
17351741

17361742
#[test]
@@ -1862,7 +1868,13 @@ fn parse_derived_tables() {
18621868
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
18631869
}],
18641870
}))
1865-
)
1871+
);
1872+
1873+
let res = parse_sql_statements("SELECT * FROM ((SELECT 1) AS t)");
1874+
assert_eq!(
1875+
ParserError::ParserError("Expected joined table, found: )".to_string()),
1876+
res.unwrap_err()
1877+
);
18661878
}
18671879

18681880
#[test]

0 commit comments

Comments
 (0)