Skip to content
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

feat: Add regexp_substr function #14323

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
feat: Add regexp_substr function
osipovartem committed Jan 27, 2025
commit 050d9dd50f299e9211019bb5da7586c35be53d5c
30 changes: 29 additions & 1 deletion datafusion/functions/src/regex/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
@@ -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];
@@ -91,5 +118,6 @@ pub fn functions() -> Vec<Arc<datafusion_expr::ScalarUDF>> {
regexp_match(),
regexp_like(),
regexp_replace(),
regexp_substr(),
]
}
}
Loading