Skip to content
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

fix(compute): LIKE/ILIKE/NLIKE escape parenthesis #1042

Merged
merged 1 commit into from
Dec 20, 2021
Merged
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
39 changes: 36 additions & 3 deletions arrow/src/compute/kernels/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,11 @@ pub fn like_utf8_scalar<OffsetSize: StringOffsetSizeTrait>(
}
}
} else {
let re_pattern = right.replace("%", ".*").replace("_", ".");
let re_pattern = right
.replace("%", ".*")
.replace("_", ".")
.replace("(", r#"\("#)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about his more, I wonder if other special regexp characters need to be replaced too (e.g. . and +)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the list of special characters is here: https://docs.rs/regex/1.5.4/regex/#syntax

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call. maybe we should pass right through this, then add the wildcards
https://docs.rs/regex/0.2.2/regex/fn.escape.html

(I'm happy to help test this @ovr )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is a great call @jwdeitch -- so something like

let re_pattern = escape(right)
  .replace('%', '.*')
  .replace('_', '.')?

Perhaps

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the rust regex crate syntax difficult at the time so I would suggest adding test cases for all their escaping. Regex is so powerful it would be easy to miss something.

.replace(")", r#"\)"#);
let re = Regex::new(&format!("^{}$", re_pattern)).map_err(|e| {
ArrowError::ComputeError(format!(
"Unable to build regex from LIKE pattern: {}",
Expand Down Expand Up @@ -436,7 +440,11 @@ pub fn nlike_utf8_scalar<OffsetSize: StringOffsetSizeTrait>(
result.append(!left.value(i).ends_with(&right[1..]));
}
} else {
let re_pattern = right.replace("%", ".*").replace("_", ".");
let re_pattern = right
.replace("%", ".*")
.replace("_", ".")
.replace("(", r#"\("#)
.replace(")", r#"\)"#);
let re = Regex::new(&format!("^{}$", re_pattern)).map_err(|e| {
ArrowError::ComputeError(format!(
"Unable to build regex from LIKE pattern: {}",
Expand Down Expand Up @@ -517,7 +525,11 @@ pub fn ilike_utf8_scalar<OffsetSize: StringOffsetSizeTrait>(
);
}
} else {
let re_pattern = right.replace("%", ".*").replace("_", ".");
let re_pattern = right
.replace("%", ".*")
.replace("_", ".")
.replace("(", r#"\("#)
.replace(")", r#"\)"#);
let re = Regex::new(&format!("(?i)^{}$", re_pattern)).map_err(|e| {
ArrowError::ComputeError(format!(
"Unable to build regex from ILIKE pattern: {}",
Expand Down Expand Up @@ -2228,6 +2240,13 @@ mod tests {
vec![true, true, true, false, false, true, false]
);

test_utf8_scalar!(
test_utf8_array_like_scalar_escape_testing,
vec!["varchar(255)", "int(255)", "varchar", "int"],
"%(%)%",
like_utf8_scalar,
vec![true, true, false, false]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

);
test_utf8_scalar!(
test_utf8_array_like_scalar,
vec!["arrow", "parquet", "datafusion", "flight"],
Expand Down Expand Up @@ -2289,6 +2308,13 @@ mod tests {
nlike_utf8,
vec![false, false, false, true, true, false, true]
);
test_utf8_scalar!(
test_utf8_array_nlike_escape_testing,
vec!["varchar(255)", "int(255)", "varchar", "int"],
"%(%)%",
nlike_utf8_scalar,
vec![false, false, true, true]
);
test_utf8_scalar!(
test_utf8_array_nlike_scalar,
vec!["arrow", "parquet", "datafusion", "flight"],
Expand Down Expand Up @@ -2336,6 +2362,13 @@ mod tests {
ilike_utf8,
vec![true, true, true, false, false, true, false]
);
test_utf8_scalar!(
ilike_utf8_scalar_escape_testing,
vec!["varchar(255)", "int(255)", "varchar", "int"],
"%(%)%",
ilike_utf8_scalar,
vec![true, true, false, false]
);
test_utf8_scalar!(
test_utf8_array_ilike_scalar,
vec!["arrow", "parquet", "datafusion", "flight"],
Expand Down