Skip to content

Add support for NULL escape char in pattern match searches #1913

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ pub enum Expr {
any: bool,
expr: Box<Expr>,
pattern: Box<Expr>,
escape_char: Option<String>,
escape_char: Option<Value>,
},
/// `ILIKE` (case-insensitive `LIKE`)
ILike {
Expand All @@ -819,14 +819,14 @@ pub enum Expr {
any: bool,
expr: Box<Expr>,
pattern: Box<Expr>,
escape_char: Option<String>,
escape_char: Option<Value>,
},
/// SIMILAR TO regex
SimilarTo {
negated: bool,
expr: Box<Expr>,
pattern: Box<Expr>,
escape_char: Option<String>,
escape_char: Option<Value>,
},
/// MySQL: RLIKE regex or REGEXP regex
RLike {
Expand Down Expand Up @@ -1486,7 +1486,7 @@ impl fmt::Display for Expr {
} => match escape_char {
Some(ch) => write!(
f,
"{} {}LIKE {}{} ESCAPE '{}'",
"{} {}LIKE {}{} ESCAPE {}",
expr,
if *negated { "NOT " } else { "" },
if *any { "ANY " } else { "" },
Expand All @@ -1511,7 +1511,7 @@ impl fmt::Display for Expr {
} => match escape_char {
Some(ch) => write!(
f,
"{} {}ILIKE {}{} ESCAPE '{}'",
"{} {}ILIKE {}{} ESCAPE {}",
expr,
if *negated { "NOT " } else { "" },
if *any { "ANY" } else { "" },
Expand Down Expand Up @@ -1566,7 +1566,7 @@ impl fmt::Display for Expr {
} => match escape_char {
Some(ch) => write!(
f,
"{} {}SIMILAR TO {} ESCAPE '{}'",
"{} {}SIMILAR TO {} ESCAPE {}",
expr,
if *negated { "NOT " } else { "" },
pattern,
Expand Down
4 changes: 2 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3642,9 +3642,9 @@ impl<'a> Parser<'a> {
}

/// Parse the `ESCAPE CHAR` portion of `LIKE`, `ILIKE`, and `SIMILAR TO`
pub fn parse_escape_char(&mut self) -> Result<Option<String>, ParserError> {
pub fn parse_escape_char(&mut self) -> Result<Option<Value>, ParserError> {
if self.parse_keyword(Keyword::ESCAPE) {
Ok(Some(self.parse_literal_string()?))
Ok(Some(self.parse_value()?.into()))
} else {
Ok(None)
}
Expand Down
25 changes: 21 additions & 4 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2040,7 +2040,7 @@ fn parse_ilike() {
pattern: Box::new(Expr::Value(
(Value::SingleQuotedString("%a".to_string())).with_empty_span()
)),
escape_char: Some('^'.to_string()),
escape_char: Some(Value::SingleQuotedString('^'.to_string())),
any: false,
},
select.selection.unwrap()
Expand Down Expand Up @@ -2104,7 +2104,7 @@ fn parse_like() {
pattern: Box::new(Expr::Value(
(Value::SingleQuotedString("%a".to_string())).with_empty_span()
)),
escape_char: Some('^'.to_string()),
escape_char: Some(Value::SingleQuotedString('^'.to_string())),
any: false,
},
select.selection.unwrap()
Expand Down Expand Up @@ -2167,7 +2167,24 @@ fn parse_similar_to() {
pattern: Box::new(Expr::Value(
(Value::SingleQuotedString("%a".to_string())).with_empty_span()
)),
escape_char: Some('^'.to_string()),
escape_char: Some(Value::SingleQuotedString('^'.to_string())),
},
select.selection.unwrap()
);

let sql = &format!(
"SELECT * FROM customers WHERE name {}SIMILAR TO '%a' ESCAPE NULL",
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(Expr::Value(
(Value::SingleQuotedString("%a".to_string())).with_empty_span()
)),
escape_char: Some(Value::Null),
},
select.selection.unwrap()
);
Expand All @@ -2185,7 +2202,7 @@ fn parse_similar_to() {
pattern: Box::new(Expr::Value(
(Value::SingleQuotedString("%a".to_string())).with_empty_span()
)),
escape_char: Some('^'.to_string()),
escape_char: Some(Value::SingleQuotedString('^'.to_string())),
})),
select.selection.unwrap()
);
Expand Down
Loading