mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-30 18:57:21 +00:00
Refactor parse_joins, pt.2: implicit/cross/natural joins
- reduce duplication in the handling of implicit/cross joins and make the flow of data slightly clearer by returning the `join` instead of pushing it and exiting early. (I wanted the block that currently returns `join` to return one of JoinOperator::* tags, so that `parse_table_factor` and the construction of the `Join` struct could happen after we've parsed the JOIN keywords, but that seems impossible.) - move the check for the NATURAL keyword into the block that deals with INNER/OUTER joins that support constraints (and thus can be preceded by "NATURAL") - add a check for NATURAL not followed by a known join type with a test - add more tests for NATURAL joins (we didn't have any), and fix whitespace bug in `to_string()` that was uncovered (we emitted an extra space: `foo NATURAL JOIN bar `)
This commit is contained in:
parent
665b9df729
commit
d0f2de06ed
3 changed files with 86 additions and 58 deletions
|
@ -1218,6 +1218,43 @@ fn parse_joins_using() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_natural_join() {
|
||||
fn natural_join(f: impl Fn(JoinConstraint) -> JoinOperator) -> Join {
|
||||
Join {
|
||||
relation: TableFactor::Table {
|
||||
name: SQLObjectName(vec!["t2".to_string()]),
|
||||
alias: None,
|
||||
args: vec![],
|
||||
with_hints: vec![],
|
||||
},
|
||||
join_operator: f(JoinConstraint::Natural),
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
verified_only_select("SELECT * FROM t1 NATURAL JOIN t2").joins,
|
||||
vec![natural_join(JoinOperator::Inner)]
|
||||
);
|
||||
assert_eq!(
|
||||
verified_only_select("SELECT * FROM t1 NATURAL LEFT JOIN t2").joins,
|
||||
vec![natural_join(JoinOperator::LeftOuter)]
|
||||
);
|
||||
assert_eq!(
|
||||
verified_only_select("SELECT * FROM t1 NATURAL RIGHT JOIN t2").joins,
|
||||
vec![natural_join(JoinOperator::RightOuter)]
|
||||
);
|
||||
assert_eq!(
|
||||
verified_only_select("SELECT * FROM t1 NATURAL FULL JOIN t2").joins,
|
||||
vec![natural_join(JoinOperator::FullOuter)]
|
||||
);
|
||||
|
||||
let sql = "SELECT * FROM t1 natural";
|
||||
assert_eq!(
|
||||
ParserError::ParserError("Expected a join type after NATURAL, found: EOF".to_string()),
|
||||
parse_sql_statements(sql).unwrap_err(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_complex_join() {
|
||||
let sql = "SELECT c1, c2 FROM t1, t4 JOIN t2 ON t2.c = t1.c LEFT JOIN t3 USING(q, c) WHERE t4.c = t1.c";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue