-
Notifications
You must be signed in to change notification settings - Fork 612
Support for SIMILAR TO
syntax, change Like
and ILike
to Expr
variants, allow escape char for like/ilike
#569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
3f21435
0e28dd1
ab12adf
de06632
dc8615e
70cc517
1babc7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// SPDX-FileCopyrightText: Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
@@ -268,6 +270,27 @@ pub enum Expr { | |
op: BinaryOperator, | ||
right: Box<Expr>, | ||
}, | ||
/// LIKE | ||
Like { | ||
negated: bool, | ||
expr: Box<Expr>, | ||
pattern: Box<Value>, | ||
escape_char: Option<char>, | ||
}, | ||
/// ILIKE (case-insensitive LIKE) | ||
ILike { | ||
negated: bool, | ||
expr: Box<Expr>, | ||
pattern: Box<Value>, | ||
escape_char: Option<char>, | ||
}, | ||
/// SIMILAR TO regex | ||
SimilarTo { | ||
negated: bool, | ||
expr: Box<Expr>, | ||
pattern: Box<Value>, | ||
escape_char: Option<char>, | ||
}, | ||
/// Any operation e.g. `1 ANY (1)` or `foo > ANY(bar)`, It will be wrapped in the right side of BinaryExpr | ||
AnyOp(Box<Expr>), | ||
/// ALL operation e.g. `1 ALL (1)` or `foo > ALL(bar)`, It will be wrapped in the right side of BinaryExpr | ||
|
@@ -438,6 +461,72 @@ impl fmt::Display for Expr { | |
high | ||
), | ||
Expr::BinaryOp { left, op, right } => write!(f, "{} {} {}", left, op, right), | ||
Expr::Like { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems to match https://www.postgresql.org/docs/current/functions-matching.html so 👍 |
||
negated, | ||
expr, | ||
pattern, | ||
escape_char, | ||
} => match escape_char { | ||
Some(ch) => write!( | ||
f, | ||
"{} {}LIKE {} ESCAPE '{}'", | ||
expr, | ||
if *negated { "NOT " } else { "" }, | ||
pattern, | ||
ch | ||
), | ||
_ => write!( | ||
f, | ||
"{} {}LIKE {}", | ||
expr, | ||
if *negated { "NOT " } else { "" }, | ||
pattern | ||
), | ||
}, | ||
Expr::ILike { | ||
negated, | ||
expr, | ||
pattern, | ||
escape_char, | ||
} => match escape_char { | ||
Some(ch) => write!( | ||
f, | ||
"{} {}ILIKE {} ESCAPE '{}'", | ||
expr, | ||
if *negated { "NOT " } else { "" }, | ||
pattern, | ||
ch | ||
), | ||
_ => write!( | ||
f, | ||
"{} {}ILIKE {}", | ||
expr, | ||
if *negated { "NOT " } else { "" }, | ||
pattern | ||
), | ||
}, | ||
Expr::SimilarTo { | ||
negated, | ||
expr, | ||
pattern, | ||
escape_char, | ||
} => match escape_char { | ||
Some(ch) => write!( | ||
f, | ||
"{} {}SIMILAR TO {} ESCAPE '{}'", | ||
expr, | ||
if *negated { "NOT " } else { "" }, | ||
pattern, | ||
ch | ||
), | ||
_ => write!( | ||
f, | ||
"{} {}SIMILAR TO {}", | ||
expr, | ||
if *negated { "NOT " } else { "" }, | ||
pattern | ||
), | ||
}, | ||
Expr::AnyOp(expr) => write!(f, "ANY({})", expr), | ||
Expr::AllOp(expr) => write!(f, "ALL({})", expr), | ||
Expr::UnaryOp { op, expr } => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// SPDX-FileCopyrightText: Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
ayushdg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
@@ -76,10 +78,6 @@ pub enum BinaryOperator { | |
And, | ||
Or, | ||
Xor, | ||
Like, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
NotLike, | ||
ILike, | ||
NotILike, | ||
BitwiseOr, | ||
BitwiseAnd, | ||
BitwiseXor, | ||
|
@@ -116,10 +114,6 @@ impl fmt::Display for BinaryOperator { | |
BinaryOperator::And => f.write_str("AND"), | ||
BinaryOperator::Or => f.write_str("OR"), | ||
BinaryOperator::Xor => f.write_str("XOR"), | ||
BinaryOperator::Like => f.write_str("LIKE"), | ||
BinaryOperator::NotLike => f.write_str("NOT LIKE"), | ||
BinaryOperator::ILike => f.write_str("ILIKE"), | ||
BinaryOperator::NotILike => f.write_str("NOT ILIKE"), | ||
BinaryOperator::BitwiseOr => f.write_str("|"), | ||
BinaryOperator::BitwiseAnd => f.write_str("&"), | ||
BinaryOperator::BitwiseXor => f.write_str("^"), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// SPDX-FileCopyrightText: Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
@@ -857,10 +859,11 @@ fn parse_not_precedence() { | |
verified_expr(sql), | ||
Expr::UnaryOp { | ||
op: UnaryOperator::Not, | ||
expr: Box::new(Expr::BinaryOp { | ||
left: Box::new(Expr::Value(Value::SingleQuotedString("a".into()))), | ||
op: BinaryOperator::NotLike, | ||
right: Box::new(Expr::Value(Value::SingleQuotedString("b".into()))), | ||
expr: Box::new(Expr::Like { | ||
expr: Box::new(Expr::Value(Value::SingleQuotedString("a".into()))), | ||
negated: true, | ||
pattern: Box::new(Value::SingleQuotedString("b".into())), | ||
escape_char: None | ||
}), | ||
}, | ||
); | ||
|
@@ -889,14 +892,11 @@ fn parse_like() { | |
); | ||
let select = verified_only_select(sql); | ||
assert_eq!( | ||
Expr::BinaryOp { | ||
left: Box::new(Expr::Identifier(Ident::new("name"))), | ||
op: if negated { | ||
BinaryOperator::NotLike | ||
} else { | ||
BinaryOperator::Like | ||
}, | ||
right: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))), | ||
Expr::Like { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please also extend the |
||
expr: Box::new(Expr::Identifier(Ident::new("name"))), | ||
negated, | ||
pattern: Box::new(Value::SingleQuotedString("%a".to_string())), | ||
escape_char: None | ||
}, | ||
select.selection.unwrap() | ||
); | ||
|
@@ -909,14 +909,11 @@ fn parse_like() { | |
); | ||
let select = verified_only_select(sql); | ||
assert_eq!( | ||
Expr::IsNull(Box::new(Expr::BinaryOp { | ||
left: Box::new(Expr::Identifier(Ident::new("name"))), | ||
op: if negated { | ||
BinaryOperator::NotLike | ||
} else { | ||
BinaryOperator::Like | ||
}, | ||
right: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))), | ||
Expr::IsNull(Box::new(Expr::Like { | ||
expr: Box::new(Expr::Identifier(Ident::new("name"))), | ||
negated, | ||
pattern: Box::new(Value::SingleQuotedString("%a".to_string())), | ||
escape_char: None | ||
})), | ||
select.selection.unwrap() | ||
); | ||
|
@@ -934,14 +931,11 @@ fn parse_ilike() { | |
); | ||
let select = verified_only_select(sql); | ||
assert_eq!( | ||
Expr::BinaryOp { | ||
left: Box::new(Expr::Identifier(Ident::new("name"))), | ||
op: if negated { | ||
BinaryOperator::NotILike | ||
} else { | ||
BinaryOperator::ILike | ||
}, | ||
right: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))), | ||
Expr::ILike { | ||
expr: Box::new(Expr::Identifier(Ident::new("name"))), | ||
negated, | ||
pattern: Box::new(Value::SingleQuotedString("%a".to_string())), | ||
escape_char: None | ||
}, | ||
select.selection.unwrap() | ||
); | ||
|
@@ -954,14 +948,50 @@ fn parse_ilike() { | |
); | ||
let select = verified_only_select(sql); | ||
assert_eq!( | ||
Expr::IsNull(Box::new(Expr::BinaryOp { | ||
left: Box::new(Expr::Identifier(Ident::new("name"))), | ||
op: if negated { | ||
BinaryOperator::NotILike | ||
} else { | ||
BinaryOperator::ILike | ||
}, | ||
right: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))), | ||
Expr::IsNull(Box::new(Expr::ILike { | ||
expr: Box::new(Expr::Identifier(Ident::new("name"))), | ||
negated, | ||
pattern: Box::new(Value::SingleQuotedString("%a".to_string())), | ||
escape_char: None | ||
})), | ||
select.selection.unwrap() | ||
); | ||
} | ||
chk(false); | ||
chk(true); | ||
} | ||
|
||
#[test] | ||
fn parse_similar_to() { | ||
fn chk(negated: bool) { | ||
let sql = &format!( | ||
"SELECT * FROM customers WHERE name {}SIMILAR TO '%a' ESCAPE '\\'", | ||
if negated { "NOT " } else { "" } | ||
); | ||
let select = verified_only_select(sql); | ||
assert_eq!( | ||
Expr::SimilarTo { | ||
expr: Box::new(Expr::Identifier(Ident::new("name"))), | ||
negated, | ||
pattern: Box::new(Value::SingleQuotedString("%a".to_string())), | ||
escape_char: Some('\\') | ||
}, | ||
select.selection.unwrap() | ||
); | ||
|
||
// This statement tests that LIKE and NOT LIKE have the same precedence. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment appears to be out of date |
||
// This was previously mishandled (#81). | ||
let sql = &format!( | ||
"SELECT * FROM customers WHERE name {}SIMILAR TO '%a' ESCAPE '\\' IS NULL", | ||
if negated { "NOT " } else { "" } | ||
); | ||
let select = verified_only_select(sql); | ||
alamb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_eq!( | ||
Expr::IsNull(Box::new(Expr::SimilarTo { | ||
expr: Box::new(Expr::Identifier(Ident::new("name"))), | ||
negated, | ||
pattern: Box::new(Value::SingleQuotedString("%a".to_string())), | ||
escape_char: Some('\\') | ||
})), | ||
select.selection.unwrap() | ||
); | ||
|
Uh oh!
There was an error while loading. Please reload this page.