Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2655,6 +2655,8 @@ pub struct Join {
/// ClickHouse supports the optional `GLOBAL` keyword before the join operator.
/// See [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/select/join)
pub global: bool,
/// Snowflake `DIRECTED` join-order directive.
pub directed: bool,
/// The join operator and its constraint (INNER/LEFT/RIGHT/CROSS/ASOF/etc.).
pub join_operator: JoinOperator,
}
Expand Down Expand Up @@ -2685,52 +2687,53 @@ impl fmt::Display for Join {
if self.global {
write!(f, "GLOBAL ")?;
}
let directed = if self.directed { "DIRECTED " } else { "" };

match &self.join_operator {
JoinOperator::Join(constraint) => f.write_fmt(format_args!(
"{}JOIN {}{}",
"{}{directed}JOIN {}{}",
prefix(constraint),
self.relation,
suffix(constraint)
)),
JoinOperator::Inner(constraint) => f.write_fmt(format_args!(
"{}INNER JOIN {}{}",
"{}INNER {directed}JOIN {}{}",
prefix(constraint),
self.relation,
suffix(constraint)
)),
JoinOperator::Left(constraint) => f.write_fmt(format_args!(
"{}LEFT JOIN {}{}",
"{}LEFT {directed}JOIN {}{}",
prefix(constraint),
self.relation,
suffix(constraint)
)),
JoinOperator::LeftOuter(constraint) => f.write_fmt(format_args!(
"{}LEFT OUTER JOIN {}{}",
"{}LEFT OUTER {directed}JOIN {}{}",
prefix(constraint),
self.relation,
suffix(constraint)
)),
JoinOperator::Right(constraint) => f.write_fmt(format_args!(
"{}RIGHT JOIN {}{}",
"{}RIGHT {directed}JOIN {}{}",
prefix(constraint),
self.relation,
suffix(constraint)
)),
JoinOperator::RightOuter(constraint) => f.write_fmt(format_args!(
"{}RIGHT OUTER JOIN {}{}",
"{}RIGHT OUTER {directed}JOIN {}{}",
prefix(constraint),
self.relation,
suffix(constraint)
)),
JoinOperator::FullOuter(constraint) => f.write_fmt(format_args!(
"{}FULL JOIN {}{}",
"{}FULL {directed}JOIN {}{}",
prefix(constraint),
self.relation,
suffix(constraint)
)),
JoinOperator::CrossJoin(constraint) => f.write_fmt(format_args!(
"CROSS JOIN {}{}",
"CROSS {directed}JOIN {}{}",
self.relation,
suffix(constraint)
)),
Expand Down
3 changes: 2 additions & 1 deletion src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2220,7 +2220,8 @@ impl Spanned for Join {
fn span(&self) -> Span {
let Join {
relation,
global: _, // bool
global: _, // bool
directed: _, // bool
join_operator,
} = self;

Expand Down
5 changes: 5 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,11 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports Snowflake-style `DIRECTED` joins.
fn supports_directed_join(&self) -> bool {
false
}

/// Returns true if the dialect supports CONNECT BY.
fn supports_connect_by(&self) -> bool {
false
Expand Down
6 changes: 6 additions & 0 deletions src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,10 @@ impl Dialect for SnowflakeDialect {
false
}

fn supports_directed_join(&self) -> bool {
true
}

fn is_reserved_for_identifier(&self, kw: Keyword) -> bool {
// Unreserve some keywords that Snowflake accepts as identifiers
// See: https://docs.snowflake.com/en/sql-reference/reserved-keywords
Expand Down Expand Up @@ -589,6 +593,8 @@ impl Dialect for SnowflakeDialect {

Keyword::GLOBAL if parser.peek_keyword(Keyword::FULL) => false,

Keyword::DIRECTED if parser.peek_keyword(Keyword::JOIN) => false,

// Reserved keywords by the Snowflake dialect, which seem to be less strictive
// than what is listed in `keywords::RESERVED_FOR_TABLE_ALIAS`. The following
// keywords were tested with the this statement: `SELECT <KW>.* FROM tbl <KW>`.
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ define_keywords!(
DETAIL,
DETERMINISTIC,
DIMENSIONS,
DIRECTED,
DIRECTORY,
DISABLE,
DISCARD,
Expand Down
32 changes: 30 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15784,9 +15784,11 @@ impl<'a> Parser<'a> {
loop {
let global = self.parse_keyword(Keyword::GLOBAL);
let join = if self.parse_keyword(Keyword::CROSS) {
let directed =
self.dialect.supports_directed_join() && self.parse_keyword(Keyword::DIRECTED);
let join_operator = if self.parse_keyword(Keyword::JOIN) {
JoinOperator::CrossJoin(JoinConstraint::None)
} else if self.parse_keyword(Keyword::APPLY) {
} else if !directed && self.parse_keyword(Keyword::APPLY) {
// MSSQL extension, similar to CROSS JOIN LATERAL
JoinOperator::CrossApply
} else {
Expand All @@ -15804,6 +15806,7 @@ impl<'a> Parser<'a> {
Join {
relation,
global,
directed,
join_operator,
}
} else if self.parse_keyword(Keyword::OUTER) {
Expand All @@ -15812,6 +15815,7 @@ impl<'a> Parser<'a> {
Join {
relation: self.parse_table_factor()?,
global,
directed: false,
join_operator: JoinOperator::OuterApply,
}
} else if self.parse_keyword(Keyword::ASOF) {
Expand All @@ -15822,6 +15826,7 @@ impl<'a> Parser<'a> {
Join {
relation,
global,
directed: false,
join_operator: JoinOperator::AsOf {
match_condition,
constraint: self.parse_join_constraint(false)?,
Expand All @@ -15834,6 +15839,7 @@ impl<'a> Parser<'a> {
Join {
relation: self.parse_table_factor()?,
global,
directed: false,
join_operator: JoinOperator::InnerArrayJoin,
}
} else if self.dialect.supports_array_join_syntax()
Expand All @@ -15843,6 +15849,7 @@ impl<'a> Parser<'a> {
Join {
relation: self.parse_table_factor()?,
global,
directed: false,
join_operator: JoinOperator::LeftArrayJoin,
}
} else if self.dialect.supports_array_join_syntax()
Expand All @@ -15852,10 +15859,12 @@ impl<'a> Parser<'a> {
Join {
relation: self.parse_table_factor()?,
global,
directed: false,
join_operator: JoinOperator::ArrayJoin,
}
} else {
let natural = self.parse_keyword(Keyword::NATURAL);
let mut directed = false;
let peek_keyword = if let Token::Word(w) = &self.peek_token_ref().token {
w.keyword
} else {
Expand All @@ -15865,6 +15874,10 @@ impl<'a> Parser<'a> {
let join_operator_type = match peek_keyword {
Keyword::INNER | Keyword::JOIN => {
let inner = self.parse_keyword(Keyword::INNER); // [ INNER ]
if inner {
directed = self.dialect.supports_directed_join()
&& self.parse_keyword(Keyword::DIRECTED);
}
self.expect_keyword_is(Keyword::JOIN)?;
if inner {
JoinOperator::Inner
Expand All @@ -15879,10 +15892,13 @@ impl<'a> Parser<'a> {
Keyword::OUTER,
Keyword::SEMI,
Keyword::ANTI,
Keyword::DIRECTED,
Keyword::JOIN,
]);
match join_type {
Some(Keyword::OUTER) => {
directed = self.dialect.supports_directed_join()
&& self.parse_keyword(Keyword::DIRECTED);
self.expect_keyword_is(Keyword::JOIN)?;
if is_left {
JoinOperator::LeftOuter
Expand All @@ -15906,6 +15922,15 @@ impl<'a> Parser<'a> {
JoinOperator::RightAnti
}
}
Some(Keyword::DIRECTED) if self.dialect.supports_directed_join() => {
directed = true;
self.expect_keyword_is(Keyword::JOIN)?;
if is_left {
JoinOperator::Left
} else {
JoinOperator::Right
}
}
Some(Keyword::JOIN) => {
if is_left {
JoinOperator::Left
Expand All @@ -15915,7 +15940,7 @@ impl<'a> Parser<'a> {
}
_ => {
return Err(ParserError::ParserError(format!(
"expected OUTER, SEMI, ANTI or JOIN after {kw:?}"
"expected OUTER, SEMI, ANTI, DIRECTED or JOIN after {kw:?}"
)))
}
}
Expand All @@ -15933,6 +15958,8 @@ impl<'a> Parser<'a> {
Keyword::FULL => {
let _ = self.next_token(); // consume FULL
let _ = self.parse_keyword(Keyword::OUTER); // [ OUTER ]
directed = self.dialect.supports_directed_join()
&& self.parse_keyword(Keyword::DIRECTED);
self.expect_keyword_is(Keyword::JOIN)?;
JoinOperator::FullOuter
}
Expand Down Expand Up @@ -15967,6 +15994,7 @@ impl<'a> Parser<'a> {
Join {
relation,
global,
directed,
join_operator: join_operator_type(join_constraint),
}
};
Expand Down
1 change: 1 addition & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ pub fn join(relation: TableFactor) -> Join {
Join {
relation,
global: false,
directed: false,
join_operator: JoinOperator::Join(JoinConstraint::Natural),
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,7 @@ fn parse_join_constraint_unnest_alias() {
with_ordinality: false,
},
global: false,
directed: false,
join_operator: JoinOperator::Join(JoinConstraint::On(Expr::BinaryOp {
left: Box::new(Expr::Identifier("c1".into())),
op: BinaryOperator::Eq,
Expand Down
9 changes: 9 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7326,6 +7326,7 @@ fn parse_implicit_join() {
joins: vec![Join {
relation: table_from_name(ObjectName::from(vec!["t1b".into()])),
global: false,
directed: false,
join_operator: JoinOperator::Join(JoinConstraint::Natural),
}],
},
Expand All @@ -7334,6 +7335,7 @@ fn parse_implicit_join() {
joins: vec![Join {
relation: table_from_name(ObjectName::from(vec!["t2b".into()])),
global: false,
directed: false,
join_operator: JoinOperator::Join(JoinConstraint::Natural),
}],
},
Expand All @@ -7350,6 +7352,7 @@ fn parse_cross_join() {
Join {
relation: table_from_name(ObjectName::from(vec![Ident::new("t2")])),
global: false,
directed: false,
join_operator: JoinOperator::CrossJoin(JoinConstraint::None),
},
only(only(select.from).joins),
Expand All @@ -7362,6 +7365,7 @@ fn parse_cross_join_constraint() {
Join {
relation: table_from_name(ObjectName::from(vec![Ident::new("t2")])),
global: false,
directed: false,
join_operator: JoinOperator::CrossJoin(constraint),
}
}
Expand Down Expand Up @@ -7411,6 +7415,7 @@ fn parse_joins_on() {
index_hints: vec![],
},
global,
directed: false,
join_operator: f(JoinConstraint::On(Expr::BinaryOp {
left: Box::new(Expr::Identifier("c1".into())),
op: BinaryOperator::Eq,
Expand Down Expand Up @@ -7553,6 +7558,7 @@ fn parse_joins_using() {
index_hints: vec![],
},
global: false,
directed: false,
join_operator: f(JoinConstraint::Using(vec![ObjectName::from(vec![
"c1".into()
])])),
Expand Down Expand Up @@ -7648,6 +7654,7 @@ fn parse_natural_join() {
index_hints: vec![],
},
global: false,
directed: false,
join_operator: f(JoinConstraint::Natural),
}
}
Expand Down Expand Up @@ -7953,6 +7960,7 @@ fn parse_derived_tables() {
joins: vec![Join {
relation: table_from_name(ObjectName::from(vec!["t2".into()])),
global: false,
directed: false,
join_operator: JoinOperator::Join(JoinConstraint::Natural),
}],
}),
Expand Down Expand Up @@ -9055,6 +9063,7 @@ fn lateral_function() {
alias: None,
},
global: false,
directed: false,
join_operator: JoinOperator::Left(JoinConstraint::None),
}],
}],
Expand Down
5 changes: 5 additions & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ fn parse_mssql_openjson() {
alias: table_alias(true, "B")
},
global: false,
directed: false,
join_operator: JoinOperator::CrossApply
}]
}],
Expand Down Expand Up @@ -576,6 +577,7 @@ fn parse_mssql_openjson() {
alias: table_alias(true, "B")
},
global: false,
directed: false,
join_operator: JoinOperator::CrossApply
}]
}],
Expand Down Expand Up @@ -630,6 +632,7 @@ fn parse_mssql_openjson() {
alias: table_alias(true, "B")
},
global: false,
directed: false,
join_operator: JoinOperator::CrossApply
}]
}],
Expand Down Expand Up @@ -664,6 +667,7 @@ fn parse_mssql_openjson() {
alias: table_alias(true, "B")
},
global: false,
directed: false,
join_operator: JoinOperator::CrossApply
}]
}],
Expand Down Expand Up @@ -698,6 +702,7 @@ fn parse_mssql_openjson() {
alias: table_alias(true, "B")
},
global: false,
directed: false,
join_operator: JoinOperator::CrossApply
}]
}],
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2756,6 +2756,7 @@ fn parse_update_with_joins() {
index_hints: vec![],
},
global: false,
directed: false,
join_operator: JoinOperator::Join(JoinConstraint::On(Expr::BinaryOp {
left: Box::new(Expr::CompoundIdentifier(vec![
Ident::new("o"),
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5712,6 +5712,7 @@ fn parse_join_constraint_unnest_alias() {
with_ordinality: false,
},
global: false,
directed: false,
join_operator: JoinOperator::Join(JoinConstraint::On(Expr::BinaryOp {
left: Box::new(Expr::Identifier("c1".into())),
op: BinaryOperator::Eq,
Expand Down
Loading
Loading