Add support for the SQL OVERLAPS predicate (#1638)

This commit is contained in:
Yoav Cohen 2025-01-06 16:35:24 +01:00 committed by GitHub
parent e23877cb2d
commit 17e22f0a60
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 13 additions and 0 deletions

View file

@ -248,6 +248,11 @@ pub enum BinaryOperator {
/// See [CREATE OPERATOR](https://www.postgresql.org/docs/current/sql-createoperator.html)
/// for more information.
PGCustomBinaryOperator(Vec<String>),
/// The `OVERLAPS` operator
///
/// Specifies a test for an overlap between two datetime periods:
/// <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#overlaps-predicate>
Overlaps,
}
impl fmt::Display for BinaryOperator {
@ -304,6 +309,7 @@ impl fmt::Display for BinaryOperator {
BinaryOperator::PGCustomBinaryOperator(idents) => {
write!(f, "OPERATOR({})", display_separated(idents, "."))
}
BinaryOperator::Overlaps => f.write_str("OVERLAPS"),
}
}
}

View file

@ -512,6 +512,7 @@ pub trait Dialect: Debug + Any {
Token::Word(w) if w.keyword == Keyword::IS => Ok(p!(Is)),
Token::Word(w) if w.keyword == Keyword::IN => Ok(p!(Between)),
Token::Word(w) if w.keyword == Keyword::BETWEEN => Ok(p!(Between)),
Token::Word(w) if w.keyword == Keyword::OVERLAPS => Ok(p!(Between)),
Token::Word(w) if w.keyword == Keyword::LIKE => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::RLIKE => Ok(p!(Like)),

View file

@ -3060,6 +3060,7 @@ impl<'a> Parser<'a> {
Keyword::AND => Some(BinaryOperator::And),
Keyword::OR => Some(BinaryOperator::Or),
Keyword::XOR => Some(BinaryOperator::Xor),
Keyword::OVERLAPS => Some(BinaryOperator::Overlaps),
Keyword::OPERATOR if dialect_is!(dialect is PostgreSqlDialect | GenericDialect) => {
self.expect_token(&Token::LParen)?;
// there are special rules for operator names in

View file

@ -12805,3 +12805,8 @@ fn parse_update_from_before_select() {
parse_sql_statements(query).unwrap_err()
);
}
#[test]
fn parse_overlaps() {
verified_stmt("SELECT (DATE '2016-01-10', DATE '2016-02-01') OVERLAPS (DATE '2016-01-20', DATE '2016-02-10')");
}