-
Notifications
You must be signed in to change notification settings - Fork 2k
Optimize regexp_replace by stripping trailing .* from anchored patterns. 2.4x improvement (ClickBench Q28)
#21379
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
+121
−13
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f16a427
Optimize regexp_replace by stripping trailing .* from anchored patterns
Dandandan b8b5d0e
fix clippy
alamb 114eec6
Add test
alamb abd37d1
Update datafusion/functions/src/regex/regexpreplace.rs
Dandandan 2b0cedd
Merge branch 'main' into optimize-regexp-replace-v2
Dandandan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,8 @@ | |
| // under the License. | ||
|
|
||
| //! Regex expressions | ||
| use memchr::memchr; | ||
|
|
||
| use arrow::array::ArrayDataBuilder; | ||
| use arrow::array::BufferBuilder; | ||
| use arrow::array::GenericStringArray; | ||
|
|
@@ -199,6 +201,24 @@ fn regex_replace_posix_groups(replacement: &str) -> String { | |
| .into_owned() | ||
| } | ||
|
|
||
| /// For anchored patterns like `^...(capture)....*$` where the replacement | ||
| /// is `\1`, build a shorter regex (stripping trailing `.*$`) and use | ||
| /// `captures_read` with `CaptureLocations` for direct extraction — no | ||
| /// `expand()`, no `String` allocation. | ||
| /// This pattern appears in ClickBench Q28: which uses a regexp like | ||
| /// `^https?://(?:www\.)?([^/]+)/.*$` | ||
| fn try_build_short_extract_regex(pattern: &str, replacement: &str) -> Option<Regex> { | ||
| if replacement != "${1}" || !pattern.starts_with('^') || !pattern.ends_with(".*$") { | ||
| return None; | ||
| } | ||
| let short = &pattern[..pattern.len() - 3]; | ||
| let re = Regex::new(short).ok()?; | ||
| if re.captures_len() != 2 { | ||
| return None; | ||
| } | ||
| Some(re) | ||
| } | ||
|
|
||
| /// Replaces substring(s) matching a PCRE-like regular expression. | ||
| /// | ||
| /// The full list of supported features and syntax can be found at | ||
|
|
@@ -457,6 +477,14 @@ fn _regexp_replace_static_pattern_replace<T: OffsetSizeTrait>( | |
| // with rust ones. | ||
| let replacement = regex_replace_posix_groups(replacement); | ||
|
|
||
| // For anchored patterns like ^...(capture)....*$, build a shorter | ||
| // regex and use captures_read for direct extraction. | ||
| let short_re = if limit == 1 { | ||
| try_build_short_extract_regex(&pattern, &replacement) | ||
| } else { | ||
| None | ||
| }; | ||
|
|
||
| let string_array_type = args[0].data_type(); | ||
| match string_array_type { | ||
| DataType::Utf8 | DataType::LargeUtf8 => { | ||
|
|
@@ -473,13 +501,37 @@ fn _regexp_replace_static_pattern_replace<T: OffsetSizeTrait>( | |
| let mut new_offsets = BufferBuilder::<T>::new(string_array.len() + 1); | ||
| new_offsets.append(T::zero()); | ||
|
|
||
| string_array.iter().for_each(|val| { | ||
| if let Some(val) = val { | ||
| let result = re.replacen(val, limit, replacement.as_str()); | ||
| vals.append_slice(result.as_bytes()); | ||
| } | ||
| new_offsets.append(T::from_usize(vals.len()).unwrap()); | ||
| }); | ||
| if let Some(ref short_re) = short_re { | ||
|
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. |
||
| let mut locs = short_re.capture_locations(); | ||
| string_array.iter().for_each(|val| { | ||
| if let Some(val) = val { | ||
| if short_re.captures_read(&mut locs, val).is_some() { | ||
| let match_end = locs.get(0).unwrap().1; | ||
| if memchr(b'\n', &val.as_bytes()[match_end..]).is_none() { | ||
| if let Some((start, end)) = locs.get(1) { | ||
| vals.append_slice(&val.as_bytes()[start..end]); | ||
| } | ||
| } else { | ||
| // Newline in remainder: .*$ wouldn't match without 's' flag | ||
| let result = | ||
| re.replacen(val, limit, replacement.as_str()); | ||
| vals.append_slice(result.as_bytes()); | ||
| } | ||
| } else { | ||
| vals.append_slice(val.as_bytes()); | ||
| } | ||
| } | ||
| new_offsets.append(T::from_usize(vals.len()).unwrap()); | ||
| }); | ||
| } else { | ||
| string_array.iter().for_each(|val| { | ||
| if let Some(val) = val { | ||
| let result = re.replacen(val, limit, replacement.as_str()); | ||
| vals.append_slice(result.as_bytes()); | ||
| } | ||
| new_offsets.append(T::from_usize(vals.len()).unwrap()); | ||
| }); | ||
| } | ||
|
|
||
| let data = ArrayDataBuilder::new(GenericStringArray::<T>::DATA_TYPE) | ||
| .len(string_array.len()) | ||
|
|
@@ -494,12 +546,39 @@ fn _regexp_replace_static_pattern_replace<T: OffsetSizeTrait>( | |
|
|
||
| let mut builder = StringViewBuilder::with_capacity(string_view_array.len()); | ||
|
|
||
| for val in string_view_array.iter() { | ||
| if let Some(val) = val { | ||
| let result = re.replacen(val, limit, replacement.as_str()); | ||
| builder.append_value(result); | ||
| } else { | ||
| builder.append_null(); | ||
| if let Some(ref short_re) = short_re { | ||
|
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. |
||
| let mut locs = short_re.capture_locations(); | ||
| for val in string_view_array.iter() { | ||
| if let Some(val) = val { | ||
| if short_re.captures_read(&mut locs, val).is_some() { | ||
| let match_end = locs.get(0).unwrap().1; | ||
| if memchr(b'\n', &val.as_bytes()[match_end..]).is_none() { | ||
| if let Some((start, end)) = locs.get(1) { | ||
| builder.append_value(&val[start..end]); | ||
| } else { | ||
| builder.append_value(""); | ||
| } | ||
| } else { | ||
| // Newline in remainder: .*$ wouldn't match without 's' flag | ||
| let result = | ||
| re.replacen(val, limit, replacement.as_str()); | ||
| builder.append_value(result); | ||
| } | ||
| } else { | ||
| builder.append_value(val); | ||
| } | ||
| } else { | ||
| builder.append_null(); | ||
| } | ||
| } | ||
| } else { | ||
| for val in string_view_array.iter() { | ||
| if let Some(val) = val { | ||
| let result = re.replacen(val, limit, replacement.as_str()); | ||
| builder.append_value(result); | ||
| } else { | ||
| builder.append_null(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
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
Oops, something went wrong.
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.