Skip to content

Commit 1bd38fd

Browse files
mvzinkayman-sigma
authored andcommitted
Differentiate LEFT JOIN from LEFT OUTER JOIN (apache#1726)
1 parent f5f0747 commit 1bd38fd

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
@@ -2015,7 +2015,9 @@ impl Spanned for JoinOperator {
20152015
match self {
20162016
JoinOperator::Join(join_constraint) => join_constraint.span(),
20172017
JoinOperator::Inner(join_constraint) => join_constraint.span(),
2018+
JoinOperator::Left(join_constraint) => join_constraint.span(),
20182019
JoinOperator::LeftOuter(join_constraint) => join_constraint.span(),
2020+
JoinOperator::Right(join_constraint) => join_constraint.span(),
20192021
JoinOperator::RightOuter(join_constraint) => join_constraint.span(),
20202022
JoinOperator::FullOuter(join_constraint) => join_constraint.span(),
20212023
JoinOperator::CrossJoin => Span::empty(),

src/parser/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5750,7 +5750,7 @@ impl<'a> Parser<'a> {
57505750
drop_behavior,
57515751
})
57525752
}
5753-
/// ```sql
5753+
/// ```sql
57545754
/// DROP CONNECTOR [IF EXISTS] name
57555755
/// ```
57565756
///
@@ -11200,9 +11200,9 @@ impl<'a> Parser<'a> {
1120011200
}
1120111201
Some(Keyword::JOIN) => {
1120211202
if is_left {
11203-
JoinOperator::LeftOuter
11203+
JoinOperator::Left
1120411204
} else {
11205-
JoinOperator::RightOuter
11205+
JoinOperator::Right
1120611206
}
1120711207
}
1120811208
_ => {

tests/sqlparser_common.rs

+42-18
Original file line numberDiff line numberDiff line change
@@ -6662,8 +6662,16 @@ fn parse_joins_on() {
66626662
only(&verified_only_select("SELECT * FROM t1 JOIN t2 ON c1 = c2").from).joins,
66636663
vec![join_with_constraint("t2", None, false, JoinOperator::Join)]
66646664
);
6665+
assert_eq!(
6666+
only(&verified_only_select("SELECT * FROM t1 INNER JOIN t2 ON c1 = c2").from).joins,
6667+
vec![join_with_constraint("t2", None, false, JoinOperator::Inner)]
6668+
);
66656669
assert_eq!(
66666670
only(&verified_only_select("SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2").from).joins,
6671+
vec![join_with_constraint("t2", None, false, JoinOperator::Left)]
6672+
);
6673+
assert_eq!(
6674+
only(&verified_only_select("SELECT * FROM t1 LEFT OUTER JOIN t2 ON c1 = c2").from).joins,
66676675
vec![join_with_constraint(
66686676
"t2",
66696677
None,
@@ -6673,6 +6681,10 @@ fn parse_joins_on() {
66736681
);
66746682
assert_eq!(
66756683
only(&verified_only_select("SELECT * FROM t1 RIGHT JOIN t2 ON c1 = c2").from).joins,
6684+
vec![join_with_constraint("t2", None, false, JoinOperator::Right)]
6685+
);
6686+
assert_eq!(
6687+
only(&verified_only_select("SELECT * FROM t1 RIGHT OUTER JOIN t2 ON c1 = c2").from).joins,
66766688
vec![join_with_constraint(
66776689
"t2",
66786690
None,
@@ -6795,10 +6807,18 @@ fn parse_joins_using() {
67956807
);
67966808
assert_eq!(
67976809
only(&verified_only_select("SELECT * FROM t1 LEFT JOIN t2 USING(c1)").from).joins,
6810+
vec![join_with_constraint("t2", None, JoinOperator::Left)]
6811+
);
6812+
assert_eq!(
6813+
only(&verified_only_select("SELECT * FROM t1 LEFT OUTER JOIN t2 USING(c1)").from).joins,
67986814
vec![join_with_constraint("t2", None, JoinOperator::LeftOuter)]
67996815
);
68006816
assert_eq!(
68016817
only(&verified_only_select("SELECT * FROM t1 RIGHT JOIN t2 USING(c1)").from).joins,
6818+
vec![join_with_constraint("t2", None, JoinOperator::Right)]
6819+
);
6820+
assert_eq!(
6821+
only(&verified_only_select("SELECT * FROM t1 RIGHT OUTER JOIN t2 USING(c1)").from).joins,
68026822
vec![join_with_constraint("t2", None, JoinOperator::RightOuter)]
68036823
);
68046824
assert_eq!(
@@ -6858,20 +6878,34 @@ fn parse_natural_join() {
68586878
only(&verified_only_select("SELECT * FROM t1 NATURAL JOIN t2").from).joins,
68596879
vec![natural_join(JoinOperator::Join, None)]
68606880
);
6881+
68616882
// inner join explicitly
68626883
assert_eq!(
68636884
only(&verified_only_select("SELECT * FROM t1 NATURAL INNER JOIN t2").from).joins,
68646885
vec![natural_join(JoinOperator::Inner, None)]
68656886
);
6887+
68666888
// left join explicitly
68676889
assert_eq!(
68686890
only(&verified_only_select("SELECT * FROM t1 NATURAL LEFT JOIN t2").from).joins,
6891+
vec![natural_join(JoinOperator::Left, None)]
6892+
);
6893+
6894+
// left outer join explicitly
6895+
assert_eq!(
6896+
only(&verified_only_select("SELECT * FROM t1 NATURAL LEFT OUTER JOIN t2").from).joins,
68696897
vec![natural_join(JoinOperator::LeftOuter, None)]
68706898
);
68716899

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

@@ -6951,22 +6985,12 @@ fn parse_join_nesting() {
69516985

69526986
#[test]
69536987
fn parse_join_syntax_variants() {
6954-
one_statement_parses_to(
6955-
"SELECT c1 FROM t1 JOIN t2 USING(c1)",
6956-
"SELECT c1 FROM t1 JOIN t2 USING(c1)",
6957-
);
6958-
one_statement_parses_to(
6959-
"SELECT c1 FROM t1 INNER JOIN t2 USING(c1)",
6960-
"SELECT c1 FROM t1 INNER JOIN t2 USING(c1)",
6961-
);
6962-
one_statement_parses_to(
6963-
"SELECT c1 FROM t1 LEFT OUTER JOIN t2 USING(c1)",
6964-
"SELECT c1 FROM t1 LEFT JOIN t2 USING(c1)",
6965-
);
6966-
one_statement_parses_to(
6967-
"SELECT c1 FROM t1 RIGHT OUTER JOIN t2 USING(c1)",
6968-
"SELECT c1 FROM t1 RIGHT JOIN t2 USING(c1)",
6969-
);
6988+
verified_stmt("SELECT c1 FROM t1 JOIN t2 USING(c1)");
6989+
verified_stmt("SELECT c1 FROM t1 INNER JOIN t2 USING(c1)");
6990+
verified_stmt("SELECT c1 FROM t1 LEFT JOIN t2 USING(c1)");
6991+
verified_stmt("SELECT c1 FROM t1 LEFT OUTER JOIN t2 USING(c1)");
6992+
verified_stmt("SELECT c1 FROM t1 RIGHT JOIN t2 USING(c1)");
6993+
verified_stmt("SELECT c1 FROM t1 RIGHT OUTER JOIN t2 USING(c1)");
69706994
one_statement_parses_to(
69716995
"SELECT c1 FROM t1 FULL OUTER JOIN t2 USING(c1)",
69726996
"SELECT c1 FROM t1 FULL JOIN t2 USING(c1)",
@@ -8028,7 +8052,7 @@ fn lateral_derived() {
80288052
let join = &from.joins[0];
80298053
assert_eq!(
80308054
join.join_operator,
8031-
JoinOperator::LeftOuter(JoinConstraint::On(Expr::Value(test_utils::number("1"))))
8055+
JoinOperator::Left(JoinConstraint::On(Expr::Value(test_utils::number("1"))))
80328056
);
80338057
if let TableFactor::Derived {
80348058
lateral,
@@ -8096,7 +8120,7 @@ fn lateral_function() {
80968120
alias: None,
80978121
},
80988122
global: false,
8099-
join_operator: JoinOperator::LeftOuter(JoinConstraint::None),
8123+
join_operator: JoinOperator::Left(JoinConstraint::None),
81008124
}],
81018125
}],
81028126
lateral_views: vec![],

0 commit comments

Comments
 (0)