Skip to content

Commit 252fdba

Browse files
authored
Allow plain JOIN without turning it into INNER (apache#1692)
1 parent 784605c commit 252fdba

File tree

8 files changed

+42
-16
lines changed

8 files changed

+42
-16
lines changed

src/ast/query.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2038,13 +2038,20 @@ impl fmt::Display for Join {
20382038
}
20392039

20402040
match &self.join_operator {
2041-
JoinOperator::Inner(constraint) => write!(
2041+
JoinOperator::Join(constraint) => write!(
20422042
f,
20432043
" {}JOIN {}{}",
20442044
prefix(constraint),
20452045
self.relation,
20462046
suffix(constraint)
20472047
),
2048+
JoinOperator::Inner(constraint) => write!(
2049+
f,
2050+
" {}INNER JOIN {}{}",
2051+
prefix(constraint),
2052+
self.relation,
2053+
suffix(constraint)
2054+
),
20482055
JoinOperator::LeftOuter(constraint) => write!(
20492056
f,
20502057
" {}LEFT JOIN {}{}",
@@ -2128,6 +2135,7 @@ impl fmt::Display for Join {
21282135
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
21292136
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
21302137
pub enum JoinOperator {
2138+
Join(JoinConstraint),
21312139
Inner(JoinConstraint),
21322140
LeftOuter(JoinConstraint),
21332141
RightOuter(JoinConstraint),

src/ast/spans.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,7 @@ impl Spanned for Join {
20012001
impl Spanned for JoinOperator {
20022002
fn span(&self) -> Span {
20032003
match self {
2004+
JoinOperator::Join(join_constraint) => join_constraint.span(),
20042005
JoinOperator::Inner(join_constraint) => join_constraint.span(),
20052006
JoinOperator::LeftOuter(join_constraint) => join_constraint.span(),
20062007
JoinOperator::RightOuter(join_constraint) => join_constraint.span(),

src/parser/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11000,9 +11000,13 @@ impl<'a> Parser<'a> {
1100011000

1100111001
let join_operator_type = match peek_keyword {
1100211002
Keyword::INNER | Keyword::JOIN => {
11003-
let _ = self.parse_keyword(Keyword::INNER); // [ INNER ]
11003+
let inner = self.parse_keyword(Keyword::INNER); // [ INNER ]
1100411004
self.expect_keyword_is(Keyword::JOIN)?;
11005-
JoinOperator::Inner
11005+
if inner {
11006+
JoinOperator::Inner
11007+
} else {
11008+
JoinOperator::Join
11009+
}
1100611010
}
1100711011
kw @ Keyword::LEFT | kw @ Keyword::RIGHT => {
1100811012
let _ = self.next_token(); // consume LEFT/RIGHT

src/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ pub fn join(relation: TableFactor) -> Join {
403403
Join {
404404
relation,
405405
global: false,
406-
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
406+
join_operator: JoinOperator::Join(JoinConstraint::Natural),
407407
}
408408
}
409409

tests/sqlparser_bigquery.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1602,7 +1602,7 @@ fn parse_join_constraint_unnest_alias() {
16021602
with_ordinality: false,
16031603
},
16041604
global: false,
1605-
join_operator: JoinOperator::Inner(JoinConstraint::On(Expr::BinaryOp {
1605+
join_operator: JoinOperator::Join(JoinConstraint::On(Expr::BinaryOp {
16061606
left: Box::new(Expr::Identifier("c1".into())),
16071607
op: BinaryOperator::Eq,
16081608
right: Box::new(Expr::Identifier("c2".into())),

tests/sqlparser_common.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6457,15 +6457,15 @@ fn parse_implicit_join() {
64576457
joins: vec![Join {
64586458
relation: table_from_name(ObjectName::from(vec!["t1b".into()])),
64596459
global: false,
6460-
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
6460+
join_operator: JoinOperator::Join(JoinConstraint::Natural),
64616461
}],
64626462
},
64636463
TableWithJoins {
64646464
relation: table_from_name(ObjectName::from(vec!["t2a".into()])),
64656465
joins: vec![Join {
64666466
relation: table_from_name(ObjectName::from(vec!["t2b".into()])),
64676467
global: false,
6468-
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
6468+
join_operator: JoinOperator::Join(JoinConstraint::Natural),
64696469
}],
64706470
},
64716471
],
@@ -6523,7 +6523,7 @@ fn parse_joins_on() {
65236523
"t2",
65246524
table_alias("foo"),
65256525
false,
6526-
JoinOperator::Inner,
6526+
JoinOperator::Join,
65276527
)]
65286528
);
65296529
one_statement_parses_to(
@@ -6533,7 +6533,7 @@ fn parse_joins_on() {
65336533
// Test parsing of different join operators
65346534
assert_eq!(
65356535
only(&verified_only_select("SELECT * FROM t1 JOIN t2 ON c1 = c2").from).joins,
6536-
vec![join_with_constraint("t2", None, false, JoinOperator::Inner)]
6536+
vec![join_with_constraint("t2", None, false, JoinOperator::Join)]
65376537
);
65386538
assert_eq!(
65396539
only(&verified_only_select("SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2").from).joins,
@@ -6650,7 +6650,7 @@ fn parse_joins_using() {
66506650
vec![join_with_constraint(
66516651
"t2",
66526652
table_alias("foo"),
6653-
JoinOperator::Inner,
6653+
JoinOperator::Join,
66546654
)]
66556655
);
66566656
one_statement_parses_to(
@@ -6660,6 +6660,10 @@ fn parse_joins_using() {
66606660
// Test parsing of different join operators
66616661
assert_eq!(
66626662
only(&verified_only_select("SELECT * FROM t1 JOIN t2 USING(c1)").from).joins,
6663+
vec![join_with_constraint("t2", None, JoinOperator::Join)]
6664+
);
6665+
assert_eq!(
6666+
only(&verified_only_select("SELECT * FROM t1 INNER JOIN t2 USING(c1)").from).joins,
66636667
vec![join_with_constraint("t2", None, JoinOperator::Inner)]
66646668
);
66656669
assert_eq!(
@@ -6722,9 +6726,14 @@ fn parse_natural_join() {
67226726
}
67236727
}
67246728

6725-
// if not specified, inner join as default
6729+
// unspecified join
67266730
assert_eq!(
67276731
only(&verified_only_select("SELECT * FROM t1 NATURAL JOIN t2").from).joins,
6732+
vec![natural_join(JoinOperator::Join, None)]
6733+
);
6734+
// inner join explicitly
6735+
assert_eq!(
6736+
only(&verified_only_select("SELECT * FROM t1 NATURAL INNER JOIN t2").from).joins,
67286737
vec![natural_join(JoinOperator::Inner, None)]
67296738
);
67306739
// left join explicitly
@@ -6748,7 +6757,7 @@ fn parse_natural_join() {
67486757
// natural join another table with alias
67496758
assert_eq!(
67506759
only(&verified_only_select("SELECT * FROM t1 NATURAL JOIN t2 AS t3").from).joins,
6751-
vec![natural_join(JoinOperator::Inner, table_alias("t3"))]
6760+
vec![natural_join(JoinOperator::Join, table_alias("t3"))]
67526761
);
67536762

67546763
let sql = "SELECT * FROM t1 natural";
@@ -6816,8 +6825,12 @@ fn parse_join_nesting() {
68166825
#[test]
68176826
fn parse_join_syntax_variants() {
68186827
one_statement_parses_to(
6819-
"SELECT c1 FROM t1 INNER JOIN t2 USING(c1)",
68206828
"SELECT c1 FROM t1 JOIN t2 USING(c1)",
6829+
"SELECT c1 FROM t1 JOIN t2 USING(c1)",
6830+
);
6831+
one_statement_parses_to(
6832+
"SELECT c1 FROM t1 INNER JOIN t2 USING(c1)",
6833+
"SELECT c1 FROM t1 INNER JOIN t2 USING(c1)",
68216834
);
68226835
one_statement_parses_to(
68236836
"SELECT c1 FROM t1 LEFT OUTER JOIN t2 USING(c1)",
@@ -6981,7 +6994,7 @@ fn parse_derived_tables() {
69816994
joins: vec![Join {
69826995
relation: table_from_name(ObjectName::from(vec!["t2".into()])),
69836996
global: false,
6984-
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
6997+
join_operator: JoinOperator::Join(JoinConstraint::Natural),
69856998
}],
69866999
}),
69877000
alias: None,

tests/sqlparser_mysql.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2055,7 +2055,7 @@ fn parse_update_with_joins() {
20552055
index_hints: vec![],
20562056
},
20572057
global: false,
2058-
join_operator: JoinOperator::Inner(JoinConstraint::On(Expr::BinaryOp {
2058+
join_operator: JoinOperator::Join(JoinConstraint::On(Expr::BinaryOp {
20592059
left: Box::new(Expr::CompoundIdentifier(vec![
20602060
Ident::new("o"),
20612061
Ident::new("customer_id")

tests/sqlparser_postgres.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4371,7 +4371,7 @@ fn parse_join_constraint_unnest_alias() {
43714371
with_ordinality: false,
43724372
},
43734373
global: false,
4374-
join_operator: JoinOperator::Inner(JoinConstraint::On(Expr::BinaryOp {
4374+
join_operator: JoinOperator::Join(JoinConstraint::On(Expr::BinaryOp {
43754375
left: Box::new(Expr::Identifier("c1".into())),
43764376
op: BinaryOperator::Eq,
43774377
right: Box::new(Expr::Identifier("c2".into())),

0 commit comments

Comments
 (0)