Skip to content

Commit

Permalink
feat: Add regexp_substr function
Browse files Browse the repository at this point in the history
  • Loading branch information
osipovartem committed Jan 27, 2025
1 parent 334d6ec commit 050d9dd
Show file tree
Hide file tree
Showing 3 changed files with 628 additions and 1 deletion.
30 changes: 29 additions & 1 deletion datafusion/functions/src/regex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ pub mod regexpcount;
pub mod regexplike;
pub mod regexpmatch;
pub mod regexpreplace;
pub mod regexpsubstr;

// create UDFs
make_udf_function!(regexpcount::RegexpCountFunc, regexp_count);
make_udf_function!(regexpmatch::RegexpMatchFunc, regexp_match);
make_udf_function!(regexplike::RegexpLikeFunc, regexp_like);
make_udf_function!(regexpreplace::RegexpReplaceFunc, regexp_replace);
make_udf_function!(regexpsubstr::RegexpSubstrFunc, regexp_substr);

pub mod expr_fn {
use datafusion_expr::Expr;
Expand Down Expand Up @@ -60,6 +62,31 @@ pub mod expr_fn {
super::regexp_match().call(args)
}

/// Returns the substring that matches a regular expression within a string.
pub fn regexp_substr(
values: Expr,
regex: Expr,
start: Option<Expr>,
occurrence: Option<Expr>,
flags: Option<Expr>,
group_num: Option<Expr>,
) -> Expr {
let mut args = vec![values, regex];
if let Some(start) = start {
args.push(start);
};
if let Some(occurrence) = occurrence {
args.push(occurrence);
};
if let Some(flags) = flags {
args.push(flags);
};
if let Some(group_num) = group_num {
args.push(group_num);
};
super::regexp_substr().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 @@ -91,5 +118,6 @@ pub fn functions() -> Vec<Arc<datafusion_expr::ScalarUDF>> {
regexp_match(),
regexp_like(),
regexp_replace(),
regexp_substr(),
]
}
}
Loading

0 comments on commit 050d9dd

Please sign in to comment.