-
Notifications
You must be signed in to change notification settings - Fork 2.2k
perf: optimize regexp_match for literal pattern usage (20% faster)
#23547
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
andygrove
merged 5 commits into
apache:main
from
andygrove:auto-opt/regexp_match-datafusion-20260713-235232
Jul 17, 2026
+273
−1
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e9ae884
perf: optimize regexp_match in datafusion-functions
andygrove 988f085
fix: satisfy clippy needless_pass_by_value in regexp_match bench
andygrove 4b156d4
docs: describe the regexp_match literal-pattern path without referrin…
andygrove c120b71
Update datafusion/functions/src/regex/regexpmatch.rs
andygrove 103191b
style: restore explanatory comments and fix indentation in regexp_mat…
andygrove 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 |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! Benchmarks `regexp_match` through `invoke_with_args`, which is how a query | ||
| //! plan calls it. The pattern (and flags) are literals, as in | ||
| //! `regexp_match(col, '[a-z]+')`. | ||
|
|
||
| use std::hint::black_box; | ||
| use std::sync::Arc; | ||
|
|
||
| use arrow::array::{ArrayRef, StringArray}; | ||
| use arrow::compute::cast; | ||
| use arrow::datatypes::{DataType, Field}; | ||
| use criterion::{Criterion, criterion_group, criterion_main}; | ||
| use datafusion_common::ScalarValue; | ||
| use datafusion_common::config::ConfigOptions; | ||
| use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl}; | ||
| use datafusion_functions::regex::regexpmatch::RegexpMatchFunc; | ||
| use rand::Rng; | ||
| use rand::distr::Alphanumeric; | ||
| use rand::rngs::ThreadRng; | ||
|
|
||
| const SIZE: usize = 1000; | ||
| const PATTERN: &str = ".*([A-Z]{1}).*"; | ||
|
|
||
| fn data(rng: &mut ThreadRng) -> StringArray { | ||
| (0..SIZE) | ||
| .map(|_| { | ||
| rng.sample_iter(&Alphanumeric) | ||
| .take(7) | ||
| .map(char::from) | ||
| .collect::<String>() | ||
| }) | ||
| .collect::<Vec<_>>() | ||
| .into() | ||
| } | ||
|
|
||
| fn run(c: &mut Criterion, name: &str, values: &ArrayRef, args: &[ColumnarValue]) { | ||
| let func = RegexpMatchFunc::new(); | ||
| let arg_fields: Vec<_> = args | ||
| .iter() | ||
| .enumerate() | ||
| .map(|(idx, arg)| Field::new(format!("arg_{idx}"), arg.data_type(), true).into()) | ||
| .collect(); | ||
| let return_field = Arc::new(Field::new_list( | ||
| "f", | ||
| Field::new_list_field(values.data_type().clone(), true), | ||
| true, | ||
| )); | ||
| let config_options = Arc::new(ConfigOptions::default()); | ||
|
|
||
| c.bench_function(name, |b| { | ||
| b.iter(|| { | ||
| black_box( | ||
| func.invoke_with_args(ScalarFunctionArgs { | ||
| args: args.to_vec(), | ||
| arg_fields: arg_fields.clone(), | ||
| number_rows: SIZE, | ||
| return_field: Arc::clone(&return_field), | ||
| config_options: Arc::clone(&config_options), | ||
| }) | ||
| .expect("regexp_match should work on valid values"), | ||
| ) | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| fn criterion_benchmark(c: &mut Criterion) { | ||
| let mut rng = rand::rng(); | ||
| let utf8 = Arc::new(data(&mut rng)) as ArrayRef; | ||
| let utf8view = cast(&utf8, &DataType::Utf8View).unwrap(); | ||
|
|
||
| run( | ||
| c, | ||
| "regexp_match_1000 literal pattern", | ||
| &utf8, | ||
| &[ | ||
| ColumnarValue::Array(Arc::clone(&utf8)), | ||
| ColumnarValue::Scalar(ScalarValue::Utf8(Some(PATTERN.to_string()))), | ||
| ], | ||
| ); | ||
|
|
||
| run( | ||
| c, | ||
| "regexp_match_1000 literal pattern and flags", | ||
| &utf8, | ||
| &[ | ||
| ColumnarValue::Array(Arc::clone(&utf8)), | ||
| ColumnarValue::Scalar(ScalarValue::Utf8(Some(PATTERN.to_string()))), | ||
| ColumnarValue::Scalar(ScalarValue::Utf8(Some("i".to_string()))), | ||
| ], | ||
| ); | ||
|
|
||
| run( | ||
| c, | ||
| "regexp_match_1000 literal pattern utf8view", | ||
| &utf8view, | ||
| &[ | ||
| ColumnarValue::Array(Arc::clone(&utf8view)), | ||
| ColumnarValue::Scalar(ScalarValue::Utf8View(Some(PATTERN.to_string()))), | ||
| ], | ||
| ); | ||
|
|
||
| // Covers the path where the pattern varies per row and so cannot be | ||
| // compiled once for the whole array. | ||
| let patterns = Arc::new(StringArray::from( | ||
| (0..SIZE) | ||
| .map(|i| if i % 2 == 0 { PATTERN } else { "^(A).*" }) | ||
| .collect::<Vec<_>>(), | ||
| )) as ArrayRef; | ||
| run( | ||
| c, | ||
| "regexp_match_1000 pattern array", | ||
| &utf8, | ||
| &[ | ||
| ColumnarValue::Array(Arc::clone(&utf8)), | ||
| ColumnarValue::Array(patterns), | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
| criterion_group!(benches, criterion_benchmark); | ||
| criterion_main!(benches); |
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.