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
15 changes: 15 additions & 0 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,16 @@ impl fmt::Display for Join {
JoinOperator::CrossApply => write!(f, " CROSS APPLY {}", self.relation),
JoinOperator::OuterApply => write!(f, " OUTER APPLY {}", self.relation),
JoinOperator::Array => write!(f, " ARRAY JOIN {}", self.relation),
JoinOperator::AsOf {
match_condition,
constraint,
} => write!(
f,
" ASOF JOIN {} MATCH_CONDITION ({}){}",
self.relation,
match_condition,
suffix(constraint)
),
}
}
}
Expand Down Expand Up @@ -1257,6 +1267,11 @@ pub enum JoinOperator {
OuterApply,
/// ARRAY JOIN (ClickHouse)
Array,
/// ASOF JOIN (Snowflake)
AsOf {
match_condition: Expr,
constraint: JoinConstraint,
},
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
Expand Down
5 changes: 5 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ define_keywords!(
AS,
ASC,
ASENSITIVE,
ASOF,
ASSERT,
ASYMMETRIC,
AT,
Expand Down Expand Up @@ -422,6 +423,7 @@ define_keywords!(
MASKING,
MATCH,
MATCHED,
MATCH_CONDITION,
MATCH_RECOGNIZE,
MATERIALIZED,
MAX,
Expand Down Expand Up @@ -829,6 +831,9 @@ pub const RESERVED_FOR_TABLE_ALIAS: &[Keyword] = &[
Keyword::CONNECT,
// Reserved for snowflake MATCH_RECOGNIZE
Keyword::MATCH_RECOGNIZE,
// Reserved for snowflake ASOF JOIN
Keyword::ASOF,
Keyword::MATCH_CONDITION,
];

/// Can't be used as a column alias, so that `SELECT <expr> alias`
Expand Down
18 changes: 18 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7902,6 +7902,24 @@ impl<'a> Parser<'a> {
relation: self.parse_array_join_table_factor()?,
join_operator: JoinOperator::Array,
}
} else if self.parse_keyword(Keyword::ASOF) && dialect_of!(self is SnowflakeDialect) {
self.expect_keyword(Keyword::JOIN)?;
let relation = self.parse_table_factor()?;

self.expect_keyword(Keyword::MATCH_CONDITION)?;
self.expect_token(&Token::LParen)?;
let match_condition = self.parse_expr()?;
self.expect_token(&Token::RParen)?;

let constraint = self.parse_join_constraint(false)?;

Join {
relation,
join_operator: JoinOperator::AsOf {
match_condition,
constraint,
},
}
} else {
let natural = self.parse_keyword(Keyword::NATURAL);
let peek_keyword = if let Token::Word(w) = self.peek_token().token {
Expand Down
5 changes: 5 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1449,3 +1449,8 @@ fn test_table_with_tag() {
fn test_describe_table() {
snowflake().verified_stmt(r#"DESCRIBE TABLE "DW_PROD"."SCH"."TBL""#);
}

#[test]
fn test_asof_join() {
snowflake().verified_stmt("SELECT * FROM table1 ASOF JOIN table2 MATCH_CONDITION (table1.timestamp <= table2.timestamp) ON table1.id = table2.id");
}