Skip to content

Commit 020464b

Browse files
izveigorserprex
authored andcommitted
feat: support PGOverlap operator (apache#912)
1 parent 7a0f5f0 commit 020464b

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

src/ast/operator.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pub enum BinaryOperator {
9595
PGBitwiseShiftLeft,
9696
PGBitwiseShiftRight,
9797
PGExp,
98+
PGOverlap,
9899
PGRegexMatch,
99100
PGRegexIMatch,
100101
PGRegexNotMatch,
@@ -135,6 +136,7 @@ impl fmt::Display for BinaryOperator {
135136
BinaryOperator::PGBitwiseShiftLeft => f.write_str("<<"),
136137
BinaryOperator::PGBitwiseShiftRight => f.write_str(">>"),
137138
BinaryOperator::PGExp => f.write_str("^"),
139+
BinaryOperator::PGOverlap => f.write_str("&&"),
138140
BinaryOperator::PGRegexMatch => f.write_str("~"),
139141
BinaryOperator::PGRegexIMatch => f.write_str("~*"),
140142
BinaryOperator::PGRegexNotMatch => f.write_str("!~"),

src/parser.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,9 @@ impl<'a> Parser<'a> {
16661666
Token::Sharp if dialect_of!(self is PostgreSqlDialect) => {
16671667
Some(BinaryOperator::PGBitwiseXor)
16681668
}
1669+
Token::Overlap if dialect_of!(self is PostgreSqlDialect | GenericDialect) => {
1670+
Some(BinaryOperator::PGOverlap)
1671+
}
16691672
Token::Tilde => Some(BinaryOperator::PGRegexMatch),
16701673
Token::TildeAsterisk => Some(BinaryOperator::PGRegexIMatch),
16711674
Token::ExclamationMarkTilde => Some(BinaryOperator::PGRegexNotMatch),
@@ -2054,6 +2057,7 @@ impl<'a> Parser<'a> {
20542057
Token::LBracket
20552058
| Token::LongArrow
20562059
| Token::Arrow
2060+
| Token::Overlap
20572061
| Token::HashArrow
20582062
| Token::HashLongArrow
20592063
| Token::AtArrow

src/tokenizer.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ pub enum Token {
150150
ShiftLeft,
151151
/// `>>`, a bitwise shift right operator in PostgreSQL
152152
ShiftRight,
153+
/// '&&', an overlap operator in PostgreSQL
154+
Overlap,
153155
/// Exclamation Mark `!` used for PostgreSQL factorial operator
154156
ExclamationMark,
155157
/// Double Exclamation Mark `!!` used for PostgreSQL prefix factorial operator
@@ -158,7 +160,7 @@ pub enum Token {
158160
AtSign,
159161
/// `|/`, a square root math operator in PostgreSQL
160162
PGSquareRoot,
161-
/// `||/` , a cube root math operator in PostgreSQL
163+
/// `||/`, a cube root math operator in PostgreSQL
162164
PGCubeRoot,
163165
/// `?` or `$` , a prepared statement arg placeholder
164166
Placeholder(String),
@@ -245,6 +247,7 @@ impl fmt::Display for Token {
245247
Token::AtSign => f.write_str("@"),
246248
Token::ShiftLeft => f.write_str("<<"),
247249
Token::ShiftRight => f.write_str(">>"),
250+
Token::Overlap => f.write_str("&&"),
248251
Token::PGSquareRoot => f.write_str("|/"),
249252
Token::PGCubeRoot => f.write_str("||/"),
250253
Token::Placeholder(ref s) => write!(f, "{s}"),
@@ -858,7 +861,14 @@ impl<'a> Tokenizer<'a> {
858861
'\\' => self.consume_and_return(chars, Token::Backslash),
859862
'[' => self.consume_and_return(chars, Token::LBracket),
860863
']' => self.consume_and_return(chars, Token::RBracket),
861-
'&' => self.consume_and_return(chars, Token::Ampersand),
864+
'&' => {
865+
chars.next(); // consume the '&'
866+
match chars.peek() {
867+
Some('&') => self.consume_and_return(chars, Token::Overlap),
868+
// Bitshift '&' operator
869+
_ => Ok(Some(Token::Ampersand)),
870+
}
871+
}
862872
'^' => self.consume_and_return(chars, Token::Caret),
863873
'{' => self.consume_and_return(chars, Token::LBrace),
864874
'}' => self.consume_and_return(chars, Token::RBrace),

tests/sqlparser_postgres.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,7 @@ fn parse_pg_binary_ops() {
16141614
("^", BinaryOperator::PGExp, pg()),
16151615
(">>", BinaryOperator::PGBitwiseShiftRight, pg_and_generic()),
16161616
("<<", BinaryOperator::PGBitwiseShiftLeft, pg_and_generic()),
1617+
("&&", BinaryOperator::PGOverlap, pg()),
16171618
];
16181619

16191620
for (str_op, op, dialects) in binary_ops {

0 commit comments

Comments
 (0)