Skip to content

Commit 68c41a9

Browse files
authored
Differentiate LEFT JOIN from LEFT OUTER JOIN (#1726)
1 parent 1c0e5d3 commit 68c41a9

File tree

4 files changed

+65
-23
lines changed

4 files changed

+65
-23
lines changed

src/ast/query.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -2077,20 +2077,34 @@ impl fmt::Display for Join {
20772077
self.relation,
20782078
suffix(constraint)
20792079
),
2080-
JoinOperator::LeftOuter(constraint) => write!(
2080+
JoinOperator::Left(constraint) => write!(
20812081
f,
20822082
" {}LEFT JOIN {}{}",
20832083
prefix(constraint),
20842084
self.relation,
20852085
suffix(constraint)
20862086
),
2087-
JoinOperator::RightOuter(constraint) => write!(
2087+
JoinOperator::LeftOuter(constraint) => write!(
2088+
f,
2089+
" {}LEFT OUTER JOIN {}{}",
2090+
prefix(constraint),
2091+
self.relation,
2092+
suffix(constraint)
2093+
),
2094+
JoinOperator::Right(constraint) => write!(
20882095
f,
20892096
" {}RIGHT JOIN {}{}",
20902097
prefix(constraint),
20912098
self.relation,
20922099
suffix(constraint)
20932100
),
2101+
JoinOperator::RightOuter(constraint) => write!(
2102+
f,
2103+
" {}RIGHT OUTER JOIN {}{}",
2104+
prefix(constraint),
2105+
self.relation,
2106+
suffix(constraint)
2107+
),
20942108
JoinOperator::FullOuter(constraint) => write!(
20952109
f,
20962110
" {}FULL JOIN {}{}",
@@ -2162,7 +2176,9 @@ impl fmt::Display for Join {
21622176
pub enum JoinOperator {
21632177
Join(JoinConstraint),
21642178
Inner(JoinConstraint),
2179+
Left(JoinConstraint),
21652180
LeftOuter(JoinConstraint),
2181+
Right(JoinConstraint),
21662182
RightOuter(JoinConstraint),
21672183
FullOuter(JoinConstraint),
21682184
CrossJoin,

src/ast/spans.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2010,7 +2010,9 @@ impl Spanned for JoinOperator {
20102010
match self {
20112011
JoinOperator::Join(join_constraint) => join_constraint.span(),
20122012
JoinOperator::Inner(join_constraint) => join_constraint.span(),
2013+
JoinOperator::Left(join_constraint) => join_constraint.span(),
20132014
JoinOperator::LeftOuter(join_constraint) => join_constraint.span(),
2015+
JoinOperator::Right(join_constraint) => join_constraint.span(),
20142016
JoinOperator::RightOuter(join_constraint) => join_constraint.span(),
20152017
JoinOperator::FullOuter(join_constraint) => join_constraint.span(),
20162018
JoinOperator::CrossJoin => Span::empty(),

src/parser/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5740,7 +5740,7 @@ impl<'a> Parser<'a> {
57405740
drop_behavior,
57415741
})
57425742
}
5743-
/// ```sql
5743+
/// ```sql
57445744
/// DROP CONNECTOR [IF EXISTS] name
57455745
/// ```
57465746
///
@@ -11190,9 +11190,9 @@ impl<'a> Parser<'a> {
1119011190
}
1119111191
Some(Keyword::JOIN) => {
1119211192
if is_left {
11193-
JoinOperator::LeftOuter
11193+
JoinOperator::Left
1119411194
} else {
11195-
JoinOperator::RightOuter
11195+
JoinOperator::Right
1119611196
}
1119711197
}
1119811198
_ => {

tests/sqlparser_common.rs

+42-18
Original file line numberDiff line numberDiff line change
@@ -6661,8 +6661,16 @@ fn parse_joins_on() {
66616661
only(&verified_only_select("SELECT * FROM t1 JOIN t2 ON c1 = c2").from).joins,
66626662
vec![join_with_constraint("t2", None, false, JoinOperator::Join)]
66636663
);
6664+
assert_eq!(
6665+
only(&verified_only_select("SELECT * FROM t1 INNER JOIN t2 ON c1 = c2").from).joins,
6666+
vec![join_with_constraint("t2", None, false, JoinOperator::Inner)]
6667+
);
66646668
assert_eq!(
66656669
only(&verified_only_select("SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2").from).joins,
6670+
vec![join_with_constraint("t2", None, false, JoinOperator::Left)]
6671+
);
6672+
assert_eq!(
6673+
only(&verified_only_select("SELECT * FROM t1 LEFT OUTER JOIN t2 ON c1 = c2").from).joins,
66666674
vec![join_with_constraint(
66676675
"t2",
66686676
None,
@@ -6672,6 +6680,10 @@ fn parse_joins_on() {
66726680
);
66736681
assert_eq!(
66746682
only(&verified_only_select("SELECT * FROM t1 RIGHT JOIN t2 ON c1 = c2").from).joins,
6683+
vec![join_with_constraint("t2", None, false, JoinOperator::Right)]
6684+
);
6685+
assert_eq!(
6686+
only(&verified_only_select("SELECT * FROM t1 RIGHT OUTER JOIN t2 ON c1 = c2").from).joins,
66756687
vec![join_with_constraint(
66766688
"t2",
66776689
None,
@@ -6794,10 +6806,18 @@ fn parse_joins_using() {
67946806
);
67956807
assert_eq!(
67966808
only(&verified_only_select("SELECT * FROM t1 LEFT JOIN t2 USING(c1)").from).joins,
6809+
vec![join_with_constraint("t2", None, JoinOperator::Left)]
6810+
);
6811+
assert_eq!(
6812+
only(&verified_only_select("SELECT * FROM t1 LEFT OUTER JOIN t2 USING(c1)").from).joins,
67976813
vec![join_with_constraint("t2", None, JoinOperator::LeftOuter)]
67986814
);
67996815
assert_eq!(
68006816
only(&verified_only_select("SELECT * FROM t1 RIGHT JOIN t2 USING(c1)").from).joins,
6817+
vec![join_with_constraint("t2", None, JoinOperator::Right)]
6818+
);
6819+
assert_eq!(
6820+
only(&verified_only_select("SELECT * FROM t1 RIGHT OUTER JOIN t2 USING(c1)").from).joins,
68016821
vec![join_with_constraint("t2", None, JoinOperator::RightOuter)]
68026822
);
68036823
assert_eq!(
@@ -6857,20 +6877,34 @@ fn parse_natural_join() {
68576877
only(&verified_only_select("SELECT * FROM t1 NATURAL JOIN t2").from).joins,
68586878
vec![natural_join(JoinOperator::Join, None)]
68596879
);
6880+
68606881
// inner join explicitly
68616882
assert_eq!(
68626883
only(&verified_only_select("SELECT * FROM t1 NATURAL INNER JOIN t2").from).joins,
68636884
vec![natural_join(JoinOperator::Inner, None)]
68646885
);
6886+
68656887
// left join explicitly
68666888
assert_eq!(
68676889
only(&verified_only_select("SELECT * FROM t1 NATURAL LEFT JOIN t2").from).joins,
6890+
vec![natural_join(JoinOperator::Left, None)]
6891+
);
6892+
6893+
// left outer join explicitly
6894+
assert_eq!(
6895+
only(&verified_only_select("SELECT * FROM t1 NATURAL LEFT OUTER JOIN t2").from).joins,
68686896
vec![natural_join(JoinOperator::LeftOuter, None)]
68696897
);
68706898

68716899
// right join explicitly
68726900
assert_eq!(
68736901
only(&verified_only_select("SELECT * FROM t1 NATURAL RIGHT JOIN t2").from).joins,
6902+
vec![natural_join(JoinOperator::Right, None)]
6903+
);
6904+
6905+
// right outer join explicitly
6906+
assert_eq!(
6907+
only(&verified_only_select("SELECT * FROM t1 NATURAL RIGHT OUTER JOIN t2").from).joins,
68746908
vec![natural_join(JoinOperator::RightOuter, None)]
68756909
);
68766910

@@ -6950,22 +6984,12 @@ fn parse_join_nesting() {
69506984

69516985
#[test]
69526986
fn parse_join_syntax_variants() {
6953-
one_statement_parses_to(
6954-
"SELECT c1 FROM t1 JOIN t2 USING(c1)",
6955-
"SELECT c1 FROM t1 JOIN t2 USING(c1)",
6956-
);
6957-
one_statement_parses_to(
6958-
"SELECT c1 FROM t1 INNER JOIN t2 USING(c1)",
6959-
"SELECT c1 FROM t1 INNER JOIN t2 USING(c1)",
6960-
);
6961-
one_statement_parses_to(
6962-
"SELECT c1 FROM t1 LEFT OUTER JOIN t2 USING(c1)",
6963-
"SELECT c1 FROM t1 LEFT JOIN t2 USING(c1)",
6964-
);
6965-
one_statement_parses_to(
6966-
"SELECT c1 FROM t1 RIGHT OUTER JOIN t2 USING(c1)",
6967-
"SELECT c1 FROM t1 RIGHT JOIN t2 USING(c1)",
6968-
);
6987+
verified_stmt("SELECT c1 FROM t1 JOIN t2 USING(c1)");
6988+
verified_stmt("SELECT c1 FROM t1 INNER JOIN t2 USING(c1)");
6989+
verified_stmt("SELECT c1 FROM t1 LEFT JOIN t2 USING(c1)");
6990+
verified_stmt("SELECT c1 FROM t1 LEFT OUTER JOIN t2 USING(c1)");
6991+
verified_stmt("SELECT c1 FROM t1 RIGHT JOIN t2 USING(c1)");
6992+
verified_stmt("SELECT c1 FROM t1 RIGHT OUTER JOIN t2 USING(c1)");
69696993
one_statement_parses_to(
69706994
"SELECT c1 FROM t1 FULL OUTER JOIN t2 USING(c1)",
69716995
"SELECT c1 FROM t1 FULL JOIN t2 USING(c1)",
@@ -8027,7 +8051,7 @@ fn lateral_derived() {
80278051
let join = &from.joins[0];
80288052
assert_eq!(
80298053
join.join_operator,
8030-
JoinOperator::LeftOuter(JoinConstraint::On(Expr::Value(test_utils::number("1"))))
8054+
JoinOperator::Left(JoinConstraint::On(Expr::Value(test_utils::number("1"))))
80318055
);
80328056
if let TableFactor::Derived {
80338057
lateral,
@@ -8095,7 +8119,7 @@ fn lateral_function() {
80958119
alias: None,
80968120
},
80978121
global: false,
8098-
join_operator: JoinOperator::LeftOuter(JoinConstraint::None),
8122+
join_operator: JoinOperator::Left(JoinConstraint::None),
80998123
}],
81008124
}],
81018125
lateral_views: vec![],

0 commit comments

Comments
 (0)