-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add regexp_extract func #14282
Open
SKY-ALIN
wants to merge
2
commits into
apache:main
Choose a base branch
from
SKY-ALIN:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+364
−0
Open
Add regexp_extract func #14282
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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,289 @@ | ||
// 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. | ||
|
||
use std::sync::Arc; | ||
|
||
use arrow::array::builder::GenericStringBuilder; | ||
use arrow::array::{Array, ArrayRef, AsArray, OffsetSizeTrait}; | ||
use arrow::datatypes::{DataType, Int64Type}; | ||
use datafusion_common::{ | ||
cast::as_generic_string_array, exec_err, internal_err, plan_err, DataFusionError, | ||
Result, | ||
}; | ||
use datafusion_expr::{ | ||
ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature, | ||
TypeSignature, Volatility, | ||
}; | ||
use datafusion_macros::user_doc; | ||
use regex::Regex; | ||
|
||
#[user_doc( | ||
doc_section(label = "Regular Expression Functions"), | ||
description = "Extract a specific group matched by [regular expression](https://docs.rs/regex/latest/regex/#syntax). If the regex did not match, or the specified group did not match, an empty string is returned..", | ||
syntax_example = "regexp_extract(str, regexp, idx)", | ||
sql_example = r#"```sql | ||
> select regexp_extract('100-200', '(\d+)-(\d+)', 1); | ||
+---------------------------------------------------------------+ | ||
| regexp_extract(Utf8("100-200"),Utf8("(\d+)-(\d+)"), Int64(1)) | | ||
+---------------------------------------------------------------+ | ||
| [100] | | ||
+---------------------------------------------------------------+ | ||
``` | ||
"#, | ||
standard_argument(name = "str", prefix = "String"), | ||
standard_argument(name = "regexp", prefix = "Regular"), | ||
standard_argument(name = "idx", prefix = "Integer") | ||
)] | ||
#[derive(Debug)] | ||
pub struct RegexpExtractFunc { | ||
signature: Signature, | ||
} | ||
|
||
impl Default for RegexpExtractFunc { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl RegexpExtractFunc { | ||
pub fn new() -> Self { | ||
Self { | ||
signature: Signature::one_of( | ||
vec![ | ||
TypeSignature::Exact(vec![ | ||
DataType::Utf8, | ||
DataType::Utf8, | ||
DataType::Int64, | ||
]), | ||
TypeSignature::Exact(vec![ | ||
DataType::LargeUtf8, | ||
DataType::LargeUtf8, | ||
DataType::Int64, | ||
]), | ||
TypeSignature::Exact(vec![ | ||
DataType::Utf8View, | ||
DataType::Utf8View, | ||
DataType::Int64, | ||
]), | ||
], | ||
Volatility::Immutable, | ||
), | ||
} | ||
} | ||
} | ||
|
||
impl ScalarUDFImpl for RegexpExtractFunc { | ||
fn as_any(&self) -> &dyn std::any::Any { | ||
self | ||
} | ||
|
||
fn name(&self) -> &str { | ||
"regexp_extract" | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
&self.signature | ||
} | ||
|
||
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { | ||
use DataType::*; | ||
Ok(match &arg_types[0] { | ||
LargeUtf8 => LargeUtf8, | ||
Utf8 => Utf8, | ||
Utf8View => Utf8View, | ||
Null => Null, | ||
other => { | ||
return plan_err!( | ||
"The regexp_extract function can only accept strings. Got {other}" | ||
); | ||
} | ||
}) | ||
} | ||
|
||
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
let args_len = args.args.len(); | ||
if args_len != 3 { | ||
return exec_err!("regexp_extract was called with {args_len} arguments, but it can accept only 3."); | ||
} | ||
|
||
let target = args.args[0].to_array(args.number_rows)?; | ||
let pattern = args.args[1].to_array(args.number_rows)?; | ||
let idx = args.args[2].to_array(args.number_rows)?; | ||
|
||
let res = regexp_extract_func(&[target, pattern, idx])?; | ||
Ok(ColumnarValue::Array(res)) | ||
} | ||
|
||
fn documentation(&self) -> Option<&Documentation> { | ||
self.doc() | ||
} | ||
} | ||
|
||
fn regexp_extract_func(args: &[ArrayRef; 3]) -> Result<ArrayRef> { | ||
match args[0].data_type() { | ||
DataType::Utf8 | DataType::Utf8View => regexp_extract::<i32>(args), | ||
DataType::LargeUtf8 => regexp_extract::<i64>(args), | ||
other => { | ||
internal_err!("Unsupported data type {other:?} for function regexp_extract") | ||
} | ||
} | ||
} | ||
|
||
fn regexp_extract<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { | ||
let target = as_generic_string_array::<T>(&args[0])?; | ||
let pattern = as_generic_string_array::<T>(&args[1])?; | ||
let idx = args[2].as_primitive::<Int64Type>(); | ||
|
||
let mut builder = GenericStringBuilder::<T>::new(); | ||
|
||
for ((t_opt, p_opt), i_opt) in target.iter().zip(pattern).zip(idx) { | ||
match (t_opt, p_opt, i_opt) { | ||
(None, _, _) | (_, None, _) | (_, _, None) => { | ||
// If any of arguments is null, the result will be null too | ||
builder.append_null(); | ||
} | ||
(Some(target_str), Some(pattern_str), Some(idx_val)) => { | ||
if idx_val < 0 { | ||
return exec_err!("idx in regexp_extract can't be negative"); | ||
} | ||
|
||
let re = Regex::new(pattern_str).map_err(|e| { | ||
DataFusionError::Execution(format!("Can't compile regexp: {e}")) | ||
})?; | ||
let caps_opt = re.captures(target_str); | ||
|
||
match caps_opt { | ||
Some(caps) => { | ||
// If regexp matches string | ||
let group_idx = idx_val as usize; | ||
if group_idx < caps.len() { | ||
// If specified group index really exists | ||
if let Some(m) = caps.get(group_idx) { | ||
// If the specified group has a match | ||
builder.append_value(m.as_str()); | ||
} else { | ||
builder.append_value(""); | ||
} | ||
} else { | ||
builder.append_value(""); | ||
} | ||
} | ||
None => { | ||
builder.append_value(""); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
Ok(Arc::new(builder.finish())) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use arrow::array::{Int64Array, StringArray}; | ||
use datafusion_common::cast::as_generic_string_array; | ||
use std::sync::Arc; | ||
|
||
#[test] | ||
fn test_regexp_extract_basic_cases() { | ||
let target_arr = StringArray::from(vec![ | ||
Some("100-200"), // 0 | ||
Some("foo"), // 1 | ||
Some("aaaac"), // 2 | ||
Some("abc"), // 3 | ||
Some("xyz"), // 4 | ||
None, // 5 | ||
Some("some text"), // 6 | ||
]); | ||
|
||
let pattern_arr = StringArray::from(vec![ | ||
Some(r"(\d+)-(\d+)"), // 0 | ||
Some(r"(\d+)"), // 1 | ||
Some("(a+)(b)?(c)"), // 2 | ||
Some(r"(a)(b)(c)"), // 3 | ||
Some("abc"), // 4 | ||
Some(r"(\d+)"), // 5 | ||
None, // 6 | ||
]); | ||
|
||
let idx_arr = Int64Array::from(vec![ | ||
Some(1), // 0 | ||
Some(1), // 1 | ||
Some(2), // 2 | ||
Some(3), // 3 | ||
Some(0), // 4 | ||
Some(1), // 5 | ||
Some(1), // 6 | ||
]); | ||
|
||
let expected = StringArray::from(vec![ | ||
Some("100"), | ||
Some(""), | ||
Some(""), | ||
Some("c"), // "abc", (a)(b)(c), idx=4 => (group(0) = abc, group(1)=a, group(2)=b, group(3)=c) => "c" | ||
Some(""), // "xyz", "abc" => "" | ||
None, // target=NULL => NULL | ||
None, // pattern=NULL => NULL | ||
]); | ||
|
||
let args = ScalarFunctionArgs { | ||
args: vec![ | ||
ColumnarValue::Array(Arc::new(target_arr)), | ||
ColumnarValue::Array(Arc::new(pattern_arr)), | ||
ColumnarValue::Array(Arc::new(idx_arr)), | ||
], | ||
number_rows: 7, | ||
return_type: &DataType::Utf8, | ||
}; | ||
|
||
let udf = RegexpExtractFunc::new(); | ||
|
||
let res = udf.invoke_with_args(args).unwrap(); | ||
let result_array = match res { | ||
ColumnarValue::Array(arr) => arr, | ||
_ => { | ||
panic!("Expected an Array result"); | ||
} | ||
}; | ||
|
||
let result = as_generic_string_array::<i32>(&result_array).unwrap(); | ||
|
||
assert_eq!(result.len(), expected.len()); | ||
assert_eq!(result.null_count(), expected.null_count()); | ||
|
||
for i in 0..result.len() { | ||
let res_is_null = result.is_null(i); | ||
let exp_is_null = expected.is_null(i); | ||
assert_eq!( | ||
res_is_null, | ||
exp_is_null, | ||
"Mismatch in nullity at row {i}. result={res_is_null}, expected={exp_is_null}" | ||
); | ||
|
||
if !res_is_null { | ||
let res_val = result.value(i); | ||
let exp_val = expected.value(i); | ||
assert_eq!( | ||
res_val, exp_val, | ||
"Mismatch at row {i}. result={res_val}, expected={exp_val}" | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd have to check but I don't think this would work with DataType::Utf8View. Could some sql tests be added that cover all the possible input types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added one more sql test and according to its result it works