-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implement regexp_matches_utf8
#706
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
Merged
Merged
Changes from all 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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -450,6 +450,136 @@ pub fn nlike_utf8_scalar<OffsetSize: StringOffsetSizeTrait>( | |
| Ok(BooleanArray::from(data)) | ||
| } | ||
|
|
||
| /// Perform SQL `array ~ regex_array` operation on [`StringArray`] / [`LargeStringArray`]. | ||
| /// If `regex_array` element has an empty value, the corresponding result value is always true. | ||
| /// | ||
| /// `flags_array` are optional [`StringArray`] / [`LargeStringArray`] flag, which allow | ||
| /// special search modes, such as case insensitive and multi-line mode. | ||
| /// See the documentation [here](https://docs.rs/regex/1.5.4/regex/#grouping-and-flags) | ||
|
Contributor
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. 👍 |
||
| /// for more information. | ||
| pub fn regexp_is_match_utf8<OffsetSize: StringOffsetSizeTrait>( | ||
| array: &GenericStringArray<OffsetSize>, | ||
| regex_array: &GenericStringArray<OffsetSize>, | ||
| flags_array: Option<&GenericStringArray<OffsetSize>>, | ||
| ) -> Result<BooleanArray> { | ||
| if array.len() != regex_array.len() { | ||
| return Err(ArrowError::ComputeError( | ||
| "Cannot perform comparison operation on arrays of different length" | ||
| .to_string(), | ||
| )); | ||
| } | ||
| let null_bit_buffer = | ||
| combine_option_bitmap(array.data_ref(), regex_array.data_ref(), array.len())?; | ||
|
|
||
| let mut patterns: HashMap<String, Regex> = HashMap::new(); | ||
| let mut result = BooleanBufferBuilder::new(array.len()); | ||
|
|
||
| let complete_pattern = match flags_array { | ||
b41sh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Some(flags) => Box::new(regex_array.iter().zip(flags.iter()).map( | ||
| |(pattern, flags)| { | ||
| pattern.map(|pattern| match flags { | ||
| Some(flag) => format!("(?{}){}", flag, pattern), | ||
| None => pattern.to_string(), | ||
| }) | ||
| }, | ||
| )) as Box<dyn Iterator<Item = Option<String>>>, | ||
| None => Box::new( | ||
| regex_array | ||
| .iter() | ||
| .map(|pattern| pattern.map(|pattern| pattern.to_string())), | ||
| ), | ||
| }; | ||
|
|
||
| array | ||
| .iter() | ||
| .zip(complete_pattern) | ||
| .map(|(value, pattern)| { | ||
| match (value, pattern) { | ||
| // Required for Postgres compatibility: | ||
| // SELECT 'foobarbequebaz' ~ ''); = true | ||
| (Some(_), Some(pattern)) if pattern == *"" => { | ||
| result.append(true); | ||
| } | ||
| (Some(value), Some(pattern)) => { | ||
| let existing_pattern = patterns.get(&pattern); | ||
| let re = match existing_pattern { | ||
| Some(re) => re.clone(), | ||
| None => { | ||
| let re = Regex::new(pattern.as_str()).map_err(|e| { | ||
| ArrowError::ComputeError(format!( | ||
| "Regular expression did not compile: {:?}", | ||
| e | ||
| )) | ||
| })?; | ||
| patterns.insert(pattern, re.clone()); | ||
| re | ||
| } | ||
| }; | ||
| result.append(re.is_match(value)); | ||
| } | ||
| _ => result.append(false), | ||
| } | ||
| Ok(()) | ||
| }) | ||
| .collect::<Result<Vec<()>>>()?; | ||
|
|
||
| let data = ArrayData::new( | ||
| DataType::Boolean, | ||
| array.len(), | ||
| None, | ||
| null_bit_buffer, | ||
| 0, | ||
| vec![result.finish()], | ||
| vec![], | ||
| ); | ||
| Ok(BooleanArray::from(data)) | ||
| } | ||
|
|
||
| /// Perform SQL `array ~ regex_array` operation on [`StringArray`] / | ||
| /// [`LargeStringArray`] and a scalar. | ||
| /// | ||
| /// See the documentation on [`regexp_is_match_utf8`] for more details. | ||
| pub fn regexp_is_match_utf8_scalar<OffsetSize: StringOffsetSizeTrait>( | ||
| array: &GenericStringArray<OffsetSize>, | ||
| regex: &str, | ||
| flag: Option<&str>, | ||
| ) -> Result<BooleanArray> { | ||
| let null_bit_buffer = array.data().null_buffer().cloned(); | ||
| let mut result = BooleanBufferBuilder::new(array.len()); | ||
|
|
||
| let pattern = match flag { | ||
| Some(flag) => format!("(?{}){}", flag, regex), | ||
| None => regex.to_string(), | ||
| }; | ||
| if pattern == *"" { | ||
| for _i in 0..array.len() { | ||
| result.append(true); | ||
| } | ||
| } else { | ||
| let re = Regex::new(pattern.as_str()).map_err(|e| { | ||
| ArrowError::ComputeError(format!( | ||
| "Regular expression did not compile: {:?}", | ||
| e | ||
| )) | ||
| })?; | ||
| for i in 0..array.len() { | ||
| let value = array.value(i); | ||
| result.append(re.is_match(value)); | ||
| } | ||
| } | ||
|
|
||
| let data = ArrayData::new( | ||
| DataType::Boolean, | ||
| array.len(), | ||
| None, | ||
| null_bit_buffer, | ||
| 0, | ||
| vec![result.finish()], | ||
| vec![], | ||
| ); | ||
| Ok(BooleanArray::from(data)) | ||
| } | ||
|
|
||
| /// Perform `left == right` operation on [`StringArray`] / [`LargeStringArray`]. | ||
| pub fn eq_utf8<OffsetSize: StringOffsetSizeTrait>( | ||
| left: &GenericStringArray<OffsetSize>, | ||
|
|
@@ -1438,6 +1568,82 @@ mod tests { | |
| }; | ||
| } | ||
|
|
||
| macro_rules! test_flag_utf8 { | ||
| ($test_name:ident, $left:expr, $right:expr, $op:expr, $expected:expr) => { | ||
| #[test] | ||
| fn $test_name() { | ||
| let left = StringArray::from($left); | ||
| let right = StringArray::from($right); | ||
| let res = $op(&left, &right, None).unwrap(); | ||
| let expected = $expected; | ||
| assert_eq!(expected.len(), res.len()); | ||
| for i in 0..res.len() { | ||
| let v = res.value(i); | ||
| assert_eq!(v, expected[i]); | ||
| } | ||
| } | ||
| }; | ||
| ($test_name:ident, $left:expr, $right:expr, $flag:expr, $op:expr, $expected:expr) => { | ||
| #[test] | ||
| fn $test_name() { | ||
| let left = StringArray::from($left); | ||
| let right = StringArray::from($right); | ||
| let flag = Some(StringArray::from($flag)); | ||
| let res = $op(&left, &right, flag.as_ref()).unwrap(); | ||
| let expected = $expected; | ||
| assert_eq!(expected.len(), res.len()); | ||
| for i in 0..res.len() { | ||
| let v = res.value(i); | ||
| assert_eq!(v, expected[i]); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| macro_rules! test_flag_utf8_scalar { | ||
| ($test_name:ident, $left:expr, $right:expr, $op:expr, $expected:expr) => { | ||
| #[test] | ||
| fn $test_name() { | ||
| let left = StringArray::from($left); | ||
| let res = $op(&left, $right, None).unwrap(); | ||
| let expected = $expected; | ||
| assert_eq!(expected.len(), res.len()); | ||
| for i in 0..res.len() { | ||
| let v = res.value(i); | ||
| assert_eq!( | ||
| v, | ||
| expected[i], | ||
| "unexpected result when comparing {} at position {} to {} ", | ||
| left.value(i), | ||
| i, | ||
| $right | ||
| ); | ||
| } | ||
| } | ||
| }; | ||
| ($test_name:ident, $left:expr, $right:expr, $flag:expr, $op:expr, $expected:expr) => { | ||
b41sh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #[test] | ||
| fn $test_name() { | ||
| let left = StringArray::from($left); | ||
| let flag = Some($flag); | ||
| let res = $op(&left, $right, flag).unwrap(); | ||
| let expected = $expected; | ||
| assert_eq!(expected.len(), res.len()); | ||
| for i in 0..res.len() { | ||
| let v = res.value(i); | ||
| assert_eq!( | ||
| v, | ||
| expected[i], | ||
| "unexpected result when comparing {} at position {} to {} ", | ||
| left.value(i), | ||
| i, | ||
| $right | ||
| ); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| test_utf8!( | ||
| test_utf8_array_like, | ||
| vec!["arrow", "arrow", "arrow", "arrow", "arrow", "arrows", "arrow"], | ||
|
|
@@ -1621,4 +1827,42 @@ mod tests { | |
| gt_eq_utf8_scalar, | ||
| vec![false, false, true, true] | ||
| ); | ||
| test_flag_utf8!( | ||
| test_utf8_array_regexp_is_match, | ||
| vec!["arrow", "arrow", "arrow", "arrow", "arrow", "arrow"], | ||
| vec!["^ar", "^AR", "ow$", "OW$", "foo", ""], | ||
| regexp_is_match_utf8, | ||
| vec![true, false, true, false, false, true] | ||
| ); | ||
| test_flag_utf8!( | ||
| test_utf8_array_regexp_is_match_insensitive, | ||
| vec!["arrow", "arrow", "arrow", "arrow", "arrow", "arrow"], | ||
| vec!["^ar", "^AR", "ow$", "OW$", "foo", ""], | ||
| vec!["i"; 6], | ||
| regexp_is_match_utf8, | ||
| vec![true, true, true, true, false, true] | ||
| ); | ||
|
|
||
| test_flag_utf8_scalar!( | ||
| test_utf8_array_regexp_is_match_scalar, | ||
| vec!["arrow", "ARROW", "parquet", "PARQUET"], | ||
| "^ar", | ||
| regexp_is_match_utf8_scalar, | ||
| vec![true, false, false, false] | ||
| ); | ||
| test_flag_utf8_scalar!( | ||
| test_utf8_array_regexp_is_match_empty_scalar, | ||
| vec!["arrow", "ARROW", "parquet", "PARQUET"], | ||
| "", | ||
| regexp_is_match_utf8_scalar, | ||
| vec![true, true, true, true] | ||
| ); | ||
| test_flag_utf8_scalar!( | ||
| test_utf8_array_regexp_is_match_insensitive_scalar, | ||
| vec!["arrow", "ARROW", "parquet", "PARQUET"], | ||
| "^ar", | ||
| "i", | ||
| regexp_is_match_utf8_scalar, | ||
| vec![true, true, false, false] | ||
| ); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.