Skip to content

Commit 91a77de

Browse files
mvzinkayman-sigma
authored andcommitted
Allow plain JOIN without turning it into INNER (apache#1692)
1 parent f270212 commit 91a77de

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
@@ -2006,6 +2006,7 @@ impl Spanned for Join {
20062006
impl Spanned for JoinOperator {
20072007
fn span(&self) -> Span {
20082008
match self {
2009+
JoinOperator::Join(join_constraint) => join_constraint.span(),
20092010
JoinOperator::Inner(join_constraint) => join_constraint.span(),
20102011
JoinOperator::LeftOuter(join_constraint) => join_constraint.span(),
20112012
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
@@ -11010,9 +11010,13 @@ impl<'a> Parser<'a> {
1101011010

1101111011
let join_operator_type = match peek_keyword {
1101211012
Keyword::INNER | Keyword::JOIN => {
11013-
let _ = self.parse_keyword(Keyword::INNER); // [ INNER ]
11013+
let inner = self.parse_keyword(Keyword::INNER); // [ INNER ]
1101411014
self.expect_keyword_is(Keyword::JOIN)?;
11015-
JoinOperator::Inner
11015+
if inner {
11016+
JoinOperator::Inner
11017+
} else {
11018+
JoinOperator::Join
11019+
}
1101611020
}
1101711021
kw @ Keyword::LEFT | kw @ Keyword::RIGHT => {
1101811022
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
@@ -6458,15 +6458,15 @@ fn parse_implicit_join() {
64586458
joins: vec![Join {
64596459
relation: table_from_name(ObjectName::from(vec!["t1b".into()])),
64606460
global: false,
6461-
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
6461+
join_operator: JoinOperator::Join(JoinConstraint::Natural),
64626462
}],
64636463
},
64646464
TableWithJoins {
64656465
relation: table_from_name(ObjectName::from(vec!["t2a".into()])),
64666466
joins: vec![Join {
64676467
relation: table_from_name(ObjectName::from(vec!["t2b".into()])),
64686468
global: false,
6469-
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
6469+
join_operator: JoinOperator::Join(JoinConstraint::Natural),
64706470
}],
64716471
},
64726472
],
@@ -6524,7 +6524,7 @@ fn parse_joins_on() {
65246524
"t2",
65256525
table_alias("foo"),
65266526
false,
6527-
JoinOperator::Inner,
6527+
JoinOperator::Join,
65286528
)]
65296529
);
65306530
one_statement_parses_to(
@@ -6534,7 +6534,7 @@ fn parse_joins_on() {
65346534
// Test parsing of different join operators
65356535
assert_eq!(
65366536
only(&verified_only_select("SELECT * FROM t1 JOIN t2 ON c1 = c2").from).joins,
6537-
vec![join_with_constraint("t2", None, false, JoinOperator::Inner)]
6537+
vec![join_with_constraint("t2", None, false, JoinOperator::Join)]
65386538
);
65396539
assert_eq!(
65406540
only(&verified_only_select("SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2").from).joins,
@@ -6651,7 +6651,7 @@ fn parse_joins_using() {
66516651
vec![join_with_constraint(
66526652
"t2",
66536653
table_alias("foo"),
6654-
JoinOperator::Inner,
6654+
JoinOperator::Join,
66556655
)]
66566656
);
66576657
one_statement_parses_to(
@@ -6661,6 +6661,10 @@ fn parse_joins_using() {
66616661
// Test parsing of different join operators
66626662
assert_eq!(
66636663
only(&verified_only_select("SELECT * FROM t1 JOIN t2 USING(c1)").from).joins,
6664+
vec![join_with_constraint("t2", None, JoinOperator::Join)]
6665+
);
6666+
assert_eq!(
6667+
only(&verified_only_select("SELECT * FROM t1 INNER JOIN t2 USING(c1)").from).joins,
66646668
vec![join_with_constraint("t2", None, JoinOperator::Inner)]
66656669
);
66666670
assert_eq!(
@@ -6723,9 +6727,14 @@ fn parse_natural_join() {
67236727
}
67246728
}
67256729

6726-
// if not specified, inner join as default
6730+
// unspecified join
67276731
assert_eq!(
67286732
only(&verified_only_select("SELECT * FROM t1 NATURAL JOIN t2").from).joins,
6733+
vec![natural_join(JoinOperator::Join, None)]
6734+
);
6735+
// inner join explicitly
6736+
assert_eq!(
6737+
only(&verified_only_select("SELECT * FROM t1 NATURAL INNER JOIN t2").from).joins,
67296738
vec![natural_join(JoinOperator::Inner, None)]
67306739
);
67316740
// left join explicitly
@@ -6749,7 +6758,7 @@ fn parse_natural_join() {
67496758
// natural join another table with alias
67506759
assert_eq!(
67516760
only(&verified_only_select("SELECT * FROM t1 NATURAL JOIN t2 AS t3").from).joins,
6752-
vec![natural_join(JoinOperator::Inner, table_alias("t3"))]
6761+
vec![natural_join(JoinOperator::Join, table_alias("t3"))]
67536762
);
67546763

67556764
let sql = "SELECT * FROM t1 natural";
@@ -6817,8 +6826,12 @@ fn parse_join_nesting() {
68176826
#[test]
68186827
fn parse_join_syntax_variants() {
68196828
one_statement_parses_to(
6820-
"SELECT c1 FROM t1 INNER JOIN t2 USING(c1)",
68216829
"SELECT c1 FROM t1 JOIN t2 USING(c1)",
6830+
"SELECT c1 FROM t1 JOIN t2 USING(c1)",
6831+
);
6832+
one_statement_parses_to(
6833+
"SELECT c1 FROM t1 INNER JOIN t2 USING(c1)",
6834+
"SELECT c1 FROM t1 INNER JOIN t2 USING(c1)",
68226835
);
68236836
one_statement_parses_to(
68246837
"SELECT c1 FROM t1 LEFT OUTER JOIN t2 USING(c1)",
@@ -6982,7 +6995,7 @@ fn parse_derived_tables() {
69826995
joins: vec![Join {
69836996
relation: table_from_name(ObjectName::from(vec!["t2".into()])),
69846997
global: false,
6985-
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
6998+
join_operator: JoinOperator::Join(JoinConstraint::Natural),
69866999
}],
69877000
}),
69887001
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)