Skip to content

Commit 29ea210

Browse files
committed
Fix for Postgres regex and like binary operators
1 parent b0bcc46 commit 29ea210

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

src/parser/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3478,10 +3478,18 @@ impl<'a> Parser<'a> {
34783478
| BinaryOperator::LtEq
34793479
| BinaryOperator::Eq
34803480
| BinaryOperator::NotEq
3481+
| BinaryOperator::PGRegexMatch
3482+
| BinaryOperator::PGRegexIMatch
3483+
| BinaryOperator::PGRegexNotMatch
3484+
| BinaryOperator::PGRegexNotIMatch
3485+
| BinaryOperator::PGLikeMatch
3486+
| BinaryOperator::PGILikeMatch
3487+
| BinaryOperator::PGNotLikeMatch
3488+
| BinaryOperator::PGNotILikeMatch
34813489
) {
34823490
return parser_err!(
34833491
format!(
3484-
"Expected one of [=, >, <, =>, =<, !=] as comparison operator, found: {op}"
3492+
"Expected one of [=, >, <, =>, =<, !=, ~, ~*, !~, !~*, ~~, ~~*, !~~, !~~*] as comparison operator, found: {op}"
34853493
),
34863494
span.start
34873495
);

tests/sqlparser_postgres.rs

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,19 +2181,35 @@ fn parse_pg_regex_match_ops() {
21812181
];
21822182

21832183
for (str_op, op) in pg_regex_match_ops {
2184-
let select = pg().verified_only_select(&format!("SELECT 'abc' {} '^a'", &str_op));
2184+
// Basic binary operation usage
2185+
let select = pg().verified_only_select(&format!("SELECT 'abc' {str_op} '^a'"));
21852186
assert_eq!(
21862187
SelectItem::UnnamedExpr(Expr::BinaryOp {
2187-
left: Box::new(Expr::Value(
2188-
(Value::SingleQuotedString("abc".into())).with_empty_span()
2189-
)),
2188+
left: Box::new(Expr::Value(single_quoted_string("abc").with_empty_span(),)),
21902189
op: op.clone(),
2191-
right: Box::new(Expr::Value(
2192-
(Value::SingleQuotedString("^a".into())).with_empty_span()
2193-
)),
2190+
right: Box::new(Expr::Value(single_quoted_string("^a").with_empty_span(),)),
21942191
}),
2195-
select.projection[0]
2192+
select.projection[0],
21962193
);
2194+
2195+
// Binary operator with ANY operator
2196+
let select =
2197+
pg().verified_only_select(&format!("SELECT 'abc' {str_op} ANY(ARRAY['^a', 'x'])"));
2198+
assert_eq!(
2199+
SelectItem::UnnamedExpr(Expr::AnyOp {
2200+
left: Box::new(Expr::Value(single_quoted_string("abc").with_empty_span(),)),
2201+
compare_op: op.clone(),
2202+
right: Box::new(Expr::Array(Array {
2203+
elem: vec![
2204+
Expr::Value(single_quoted_string("^a").with_empty_span()),
2205+
Expr::Value(single_quoted_string("x").with_empty_span()),
2206+
],
2207+
named: true,
2208+
})),
2209+
is_some: false,
2210+
}),
2211+
select.projection[0],
2212+
)
21972213
}
21982214
}
21992215

@@ -2207,19 +2223,31 @@ fn parse_pg_like_match_ops() {
22072223
];
22082224

22092225
for (str_op, op) in pg_like_match_ops {
2210-
let select = pg().verified_only_select(&format!("SELECT 'abc' {} 'a_c%'", &str_op));
2226+
// Basic binary operation usage
2227+
let select = pg().verified_only_select(&format!("SELECT 'abc' {str_op} 'a_c%'"));
22112228
assert_eq!(
22122229
SelectItem::UnnamedExpr(Expr::BinaryOp {
2213-
left: Box::new(Expr::Value(
2214-
(Value::SingleQuotedString("abc".into())).with_empty_span()
2215-
)),
2230+
left: Box::new(Expr::Value(single_quoted_string("abc").with_empty_span(),)),
22162231
op: op.clone(),
2217-
right: Box::new(Expr::Value(
2218-
(Value::SingleQuotedString("a_c%".into())).with_empty_span()
2219-
)),
2232+
right: Box::new(Expr::Value(single_quoted_string("a_c%").with_empty_span(),)),
22202233
}),
2221-
select.projection[0]
2234+
select.projection[0],
22222235
);
2236+
2237+
// Binary operator with ALL operator
2238+
let select =
2239+
pg().verified_only_select(&format!("SELECT 'abc' {str_op} ALL(ARRAY['a_c%'])"));
2240+
assert_eq!(
2241+
SelectItem::UnnamedExpr(Expr::AllOp {
2242+
left: Box::new(Expr::Value(single_quoted_string("abc").with_empty_span(),)),
2243+
compare_op: op.clone(),
2244+
right: Box::new(Expr::Array(Array {
2245+
elem: vec![Expr::Value(single_quoted_string("a_c%").with_empty_span())],
2246+
named: true,
2247+
})),
2248+
}),
2249+
select.projection[0],
2250+
)
22232251
}
22242252
}
22252253

0 commit comments

Comments
 (0)