feat: support PGOverlap operator (#912)

This commit is contained in:
Igor Izvekov 2023-07-06 16:27:18 +03:00 committed by GitHub
parent 20ac38b4da
commit a50671d95d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 2 deletions

View file

@ -95,6 +95,7 @@ pub enum BinaryOperator {
PGBitwiseShiftLeft,
PGBitwiseShiftRight,
PGExp,
PGOverlap,
PGRegexMatch,
PGRegexIMatch,
PGRegexNotMatch,
@ -135,6 +136,7 @@ impl fmt::Display for BinaryOperator {
BinaryOperator::PGBitwiseShiftLeft => f.write_str("<<"),
BinaryOperator::PGBitwiseShiftRight => f.write_str(">>"),
BinaryOperator::PGExp => f.write_str("^"),
BinaryOperator::PGOverlap => f.write_str("&&"),
BinaryOperator::PGRegexMatch => f.write_str("~"),
BinaryOperator::PGRegexIMatch => f.write_str("~*"),
BinaryOperator::PGRegexNotMatch => f.write_str("!~"),

View file

@ -1662,6 +1662,9 @@ impl<'a> Parser<'a> {
Token::Sharp if dialect_of!(self is PostgreSqlDialect) => {
Some(BinaryOperator::PGBitwiseXor)
}
Token::Overlap if dialect_of!(self is PostgreSqlDialect | GenericDialect) => {
Some(BinaryOperator::PGOverlap)
}
Token::Tilde => Some(BinaryOperator::PGRegexMatch),
Token::TildeAsterisk => Some(BinaryOperator::PGRegexIMatch),
Token::ExclamationMarkTilde => Some(BinaryOperator::PGRegexNotMatch),
@ -2050,6 +2053,7 @@ impl<'a> Parser<'a> {
Token::LBracket
| Token::LongArrow
| Token::Arrow
| Token::Overlap
| Token::HashArrow
| Token::HashLongArrow
| Token::AtArrow

View file

@ -150,6 +150,8 @@ pub enum Token {
ShiftLeft,
/// `>>`, a bitwise shift right operator in PostgreSQL
ShiftRight,
/// '&&', an overlap operator in PostgreSQL
Overlap,
/// Exclamation Mark `!` used for PostgreSQL factorial operator
ExclamationMark,
/// Double Exclamation Mark `!!` used for PostgreSQL prefix factorial operator
@ -158,7 +160,7 @@ pub enum Token {
AtSign,
/// `|/`, a square root math operator in PostgreSQL
PGSquareRoot,
/// `||/` , a cube root math operator in PostgreSQL
/// `||/`, a cube root math operator in PostgreSQL
PGCubeRoot,
/// `?` or `$` , a prepared statement arg placeholder
Placeholder(String),
@ -245,6 +247,7 @@ impl fmt::Display for Token {
Token::AtSign => f.write_str("@"),
Token::ShiftLeft => f.write_str("<<"),
Token::ShiftRight => f.write_str(">>"),
Token::Overlap => f.write_str("&&"),
Token::PGSquareRoot => f.write_str("|/"),
Token::PGCubeRoot => f.write_str("||/"),
Token::Placeholder(ref s) => write!(f, "{s}"),
@ -858,7 +861,14 @@ impl<'a> Tokenizer<'a> {
'\\' => self.consume_and_return(chars, Token::Backslash),
'[' => self.consume_and_return(chars, Token::LBracket),
']' => self.consume_and_return(chars, Token::RBracket),
'&' => self.consume_and_return(chars, Token::Ampersand),
'&' => {
chars.next(); // consume the '&'
match chars.peek() {
Some('&') => self.consume_and_return(chars, Token::Overlap),
// Bitshift '&' operator
_ => Ok(Some(Token::Ampersand)),
}
}
'^' => self.consume_and_return(chars, Token::Caret),
'{' => self.consume_and_return(chars, Token::LBrace),
'}' => self.consume_and_return(chars, Token::RBrace),

View file

@ -1613,6 +1613,7 @@ fn parse_pg_binary_ops() {
("^", BinaryOperator::PGExp, pg()),
(">>", BinaryOperator::PGBitwiseShiftRight, pg_and_generic()),
("<<", BinaryOperator::PGBitwiseShiftLeft, pg_and_generic()),
("&&", BinaryOperator::PGOverlap, pg()),
];
for (str_op, op, dialects) in binary_ops {