-
Notifications
You must be signed in to change notification settings - Fork 867
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
Fix like regex escaping #1085
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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("_", "."); | ||
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 { | ||
|
@@ -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_"]); | ||
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. 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])); | ||
|
@@ -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: {}", | ||
|
@@ -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: {}", | ||
|
@@ -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: {}", | ||
|
@@ -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!( | ||
|
@@ -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"], | ||
|
@@ -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"], | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.