Skip to content

Implementation for regex_instr #15928

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
41 changes: 41 additions & 0 deletions datafusion/functions/benches/regx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use arrow::compute::cast;
use arrow::datatypes::DataType;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use datafusion_functions::regex::regexpcount::regexp_count_func;
use datafusion_functions::regex::regexpinstr::regexp_instr_func;
use datafusion_functions::regex::regexplike::regexp_like;
use datafusion_functions::regex::regexpmatch::regexp_match;
use datafusion_functions::regex::regexpreplace::regexp_replace;
Expand Down Expand Up @@ -127,6 +128,46 @@ fn criterion_benchmark(c: &mut Criterion) {
})
});

c.bench_function("regexp_instr_1000 string", |b| {
let mut rng = rand::thread_rng();
let data = Arc::new(data(&mut rng)) as ArrayRef;
let regex = Arc::new(regex(&mut rng)) as ArrayRef;
let start = Arc::new(start(&mut rng)) as ArrayRef;
let flags = Arc::new(flags(&mut rng)) as ArrayRef;

b.iter(|| {
black_box(
regexp_instr_func(&[
Arc::clone(&data),
Arc::clone(&regex),
Arc::clone(&start),
Copy link
Preview

Copilot AI Jun 17, 2025

Choose a reason for hiding this comment

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

The benchmark passes flags as the fourth argument, but according to the UDF signature it should be the fifth. Add the missing n (occurrence) argument or adjust the argument order.

Suggested change
Arc::clone(&start),
Arc::clone(&start),
Arc::new(n(&mut rng)) as ArrayRef,

Copilot uses AI. Check for mistakes.

Arc::clone(&flags),
])
.expect("regexp_instr should work on utf8"),
)
})
});

c.bench_function("regexp_instr_1000 utf8view", |b| {
let mut rng = rand::thread_rng();
let data = cast(&data(&mut rng), &DataType::Utf8View).unwrap();
let regex = cast(&regex(&mut rng), &DataType::Utf8View).unwrap();
let start = Arc::new(start(&mut rng)) as ArrayRef;
let flags = cast(&flags(&mut rng), &DataType::Utf8View).unwrap();

b.iter(|| {
black_box(
regexp_instr_func(&[
Arc::clone(&data),
Arc::clone(&regex),
Arc::clone(&start),
Arc::clone(&flags),
])
.expect("regexp_instr should work on utf8view"),
)
})
});

c.bench_function("regexp_like_1000", |b| {
let mut rng = rand::thread_rng();
let data = Arc::new(data(&mut rng)) as ArrayRef;
Expand Down
72 changes: 71 additions & 1 deletion datafusion/functions/src/regex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@

//! "regex" DataFusion functions

use arrow::error::ArrowError;
use regex::Regex;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::sync::Arc;

pub mod regexpcount;
pub mod regexpinstr;
pub mod regexplike;
pub mod regexpmatch;
pub mod regexpreplace;

// create UDFs
make_udf_function!(regexpcount::RegexpCountFunc, regexp_count);
make_udf_function!(regexpinstr::RegexpInstrFunc, regexp_instr);
make_udf_function!(regexpmatch::RegexpMatchFunc, regexp_match);
make_udf_function!(regexplike::RegexpLikeFunc, regexp_like);
make_udf_function!(regexpreplace::RegexpReplaceFunc, regexp_replace);
Expand Down Expand Up @@ -60,6 +65,34 @@ pub mod expr_fn {
super::regexp_match().call(args)
}

/// Returns index of regular expression matches in a string.
pub fn regexp_instr(
values: Expr,
regex: Expr,
start: Option<Expr>,
n: Option<Expr>,
endoption: Option<Expr>,
flags: Option<Expr>,
subexpr: Option<Expr>,
) -> Expr {
let mut args = vec![values, regex];
if let Some(start) = start {
args.push(start);
};
if let Some(n) = n {
args.push(n);
};
if let Some(endoption) = endoption {
args.push(endoption);
};
if let Some(flags) = flags {
args.push(flags);
};
if let Some(subexpr) = subexpr {
args.push(subexpr);
};
super::regexp_instr().call(args)
}
/// Returns true if a has at least one match in a string, false otherwise.
pub fn regexp_like(values: Expr, regex: Expr, flags: Option<Expr>) -> Expr {
let mut args = vec![values, regex];
Expand Down Expand Up @@ -89,7 +122,44 @@ pub fn functions() -> Vec<Arc<datafusion_expr::ScalarUDF>> {
vec![
regexp_count(),
regexp_match(),
regexp_instr(),
regexp_like(),
regexp_replace(),
]
}

pub fn compile_and_cache_regex<'strings, 'cache>(
regex: &'strings str,
flags: Option<&'strings str>,
regex_cache: &'cache mut HashMap<(&'strings str, Option<&'strings str>), Regex>,
) -> Result<&'cache Regex, ArrowError>
where
'strings: 'cache,
{
let result = match regex_cache.entry((regex, flags)) {
Entry::Occupied(occupied_entry) => occupied_entry.into_mut(),
Entry::Vacant(vacant_entry) => {
let compiled = compile_regex(regex, flags)?;
vacant_entry.insert(compiled)
}
};
Ok(result)
}

pub fn compile_regex(regex: &str, flags: Option<&str>) -> Result<Regex, ArrowError> {
let pattern = match flags {
None | Some("") => regex.to_string(),
Some(flags) => {
if flags.contains("g") {
return Err(ArrowError::ComputeError(
"regexp_count() does not support global flag".to_string(),
));
}
format!("(?{flags}){regex}")
}
};

Regex::new(&pattern).map_err(|_| {
ArrowError::ComputeError(format!("Regular expression did not compile: {pattern}"))
})
}
39 changes: 2 additions & 37 deletions datafusion/functions/src/regex/regexpcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use crate::regex::{compile_and_cache_regex, compile_regex};
use arrow::array::{Array, ArrayRef, AsArray, Datum, Int64Array, StringArrayType};
use arrow::datatypes::{DataType, Int64Type};
use arrow::datatypes::{
Expand All @@ -29,10 +30,10 @@ use datafusion_expr::{
use datafusion_macros::user_doc;
use itertools::izip;
use regex::Regex;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::sync::Arc;

// Ensure the `compile_and_cache_regex` function is defined in the `regex` module or imported correctly.
#[user_doc(
doc_section(label = "Regular Expression Functions"),
description = "Returns the number of matches that a [regular expression](https://docs.rs/regex/latest/regex/#syntax) has in a string.",
Expand Down Expand Up @@ -550,42 +551,6 @@ where
}
}

fn compile_and_cache_regex<'strings, 'cache>(
regex: &'strings str,
flags: Option<&'strings str>,
regex_cache: &'cache mut HashMap<(&'strings str, Option<&'strings str>), Regex>,
) -> Result<&'cache Regex, ArrowError>
where
'strings: 'cache,
{
let result = match regex_cache.entry((regex, flags)) {
Entry::Occupied(occupied_entry) => occupied_entry.into_mut(),
Entry::Vacant(vacant_entry) => {
let compiled = compile_regex(regex, flags)?;
vacant_entry.insert(compiled)
}
};
Ok(result)
}

fn compile_regex(regex: &str, flags: Option<&str>) -> Result<Regex, ArrowError> {
let pattern = match flags {
None | Some("") => regex.to_string(),
Some(flags) => {
if flags.contains("g") {
return Err(ArrowError::ComputeError(
"regexp_count() does not support global flag".to_string(),
));
}
format!("(?{flags}){regex}")
}
};

Regex::new(&pattern).map_err(|_| {
ArrowError::ComputeError(format!("Regular expression did not compile: {pattern}"))
})
}

fn count_matches(
value: Option<&str>,
pattern: &Regex,
Expand Down
Loading
Loading