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 12 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
31 changes: 31 additions & 0 deletions datafusion/functions/src/regex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
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 +62,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,6 +119,7 @@ pub fn functions() -> Vec<Arc<datafusion_expr::ScalarUDF>> {
vec![
regexp_count(),
regexp_match(),
regexp_instr(),
regexp_like(),
regexp_replace(),
]
Expand Down
4 changes: 2 additions & 2 deletions datafusion/functions/src/regex/regexpcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ where
}
}

fn compile_and_cache_regex<'strings, 'cache>(
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>,
Expand All @@ -568,7 +568,7 @@ where
Ok(result)
}

fn compile_regex(regex: &str, flags: Option<&str>) -> Result<Regex, ArrowError> {
pub fn compile_regex(regex: &str, flags: Option<&str>) -> Result<Regex, ArrowError> {
let pattern = match flags {
None | Some("") => regex.to_string(),
Some(flags) => {
Expand Down
Loading
Loading