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 like regex escaping #1085

Merged
merged 4 commits into from
Dec 22, 2021
Merged
Changes from 3 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
69 changes: 45 additions & 24 deletions arrow/src/compute/kernels/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::datatypes::{
};
use crate::error::{ArrowError, Result};
use crate::util::bit_util;
use regex::Regex;
use regex::{escape, Regex};
use std::any::type_name;
use std::collections::HashMap;

Expand Down Expand Up @@ -259,14 +259,14 @@ where
let mut result = BooleanBufferBuilder::new(left.len());
for i in 0..left.len() {
let haystack = left.value(i);
let pat = right.value(i);
let re = if let Some(ref regex) = map.get(pat) {
let pat = escape(right.value(i));
let re = if let Some(ref regex) = map.get(&pat) {
regex
} else {
let re_pattern = pat.replace("%", ".*").replace("_", ".");
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need this pat.replace call?

Copy link
Contributor Author

@Dandandan Dandandan Dec 22, 2021

Choose a reason for hiding this comment

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

Yeah, to make a proper regex out of it (instead of literally matching on the percentage or underscore character).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But we could minimize some borrow / cloning by moving the escape... So now it looks more similar to the other places.

let re = op(&re_pattern)?;
map.insert(pat, re);
map.get(pat).unwrap()
map.insert(pat.clone(), re);
map.get(&pat).unwrap()
};

result.append(if negate_regex {
Expand Down Expand Up @@ -303,7 +303,7 @@ where
/// use arrow::compute::like_utf8;
///
/// let strings = StringArray::from(vec!["Arrow", "Arrow", "Arrow", "Ar"]);
/// let patterns = StringArray::from(vec!["A%", "B%", "A.", "A."]);
/// let patterns = StringArray::from(vec!["A%", "B%", "A.", "A_"]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lol - even the doctest here was relying on a non-escaped .

///
/// let result = like_utf8(&strings, &patterns).unwrap();
/// assert_eq!(result, BooleanArray::from(vec![true, false, false, true]));
Expand Down Expand Up @@ -360,11 +360,7 @@ pub fn like_utf8_scalar<OffsetSize: StringOffsetSizeTrait>(
}
}
} else {
let re_pattern = right
.replace("%", ".*")
.replace("_", ".")
.replace("(", r#"\("#)
.replace(")", r#"\)"#);
let re_pattern = escape(right).replace("%", ".*").replace("_", ".");
let re = Regex::new(&format!("^{}$", re_pattern)).map_err(|e| {
ArrowError::ComputeError(format!(
"Unable to build regex from LIKE pattern: {}",
Expand Down Expand Up @@ -440,11 +436,7 @@ pub fn nlike_utf8_scalar<OffsetSize: StringOffsetSizeTrait>(
result.append(!left.value(i).ends_with(&right[1..]));
}
} else {
let re_pattern = right
.replace("%", ".*")
.replace("_", ".")
.replace("(", r#"\("#)
.replace(")", r#"\)"#);
let re_pattern = escape(right).replace("%", ".*").replace("_", ".");
let re = Regex::new(&format!("^{}$", re_pattern)).map_err(|e| {
ArrowError::ComputeError(format!(
"Unable to build regex from LIKE pattern: {}",
Expand Down Expand Up @@ -525,11 +517,7 @@ pub fn ilike_utf8_scalar<OffsetSize: StringOffsetSizeTrait>(
);
}
} else {
let re_pattern = right
.replace("%", ".*")
.replace("_", ".")
.replace("(", r#"\("#)
.replace(")", r#"\)"#);
let re_pattern = escape(right).replace("%", ".*").replace("_", ".");
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 @@ -2235,10 +2223,10 @@ mod tests {

test_utf8!(
test_utf8_array_like,
vec!["arrow", "arrow", "arrow", "arrow", "arrow", "arrows", "arrow"],
vec!["arrow", "ar%", "%ro%", "foo", "arr", "arrow_", "arrow_"],
vec!["arrow", "arrow", "arrow", "arrow", "arrow", "arrows", "arrow", "arrow"],
vec!["arrow", "ar%", "%ro%", "foo", "arr", "arrow_", "arrow_", ".*"],
like_utf8,
vec![true, true, true, false, false, true, false]
vec![true, true, true, false, false, true, false, false]
);

test_utf8_scalar!(
Expand All @@ -2248,6 +2236,23 @@ mod tests {
like_utf8_scalar,
vec![true, true, false, false]
);

test_utf8_scalar!(
test_utf8_array_like_scalar_escape_regex,
vec![".*", "a", "*"],
".*",
like_utf8_scalar,
vec![true, false, false]
);

test_utf8_scalar!(
test_utf8_array_like_scalar_escape_regex_dot,
vec![".", "a", "*"],
".",
like_utf8_scalar,
vec![true, false, false]
);

test_utf8_scalar!(
test_utf8_array_like_scalar,
vec!["arrow", "parquet", "datafusion", "flight"],
Expand Down Expand Up @@ -2316,6 +2321,22 @@ mod tests {
nlike_utf8_scalar,
vec![false, false, true, true]
);

test_utf8_scalar!(
test_utf8_array_nlike_scalar_escape_regex,
vec![".*", "a", "*"],
".*",
nlike_utf8_scalar,
vec![false, true, true]
);

test_utf8_scalar!(
test_utf8_array_nlike_scalar_escape_regex_dot,
vec![".", "a", "*"],
".",
nlike_utf8_scalar,
vec![false, true, true]
);
test_utf8_scalar!(
test_utf8_array_nlike_scalar,
vec!["arrow", "parquet", "datafusion", "flight"],
Expand Down