-
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
Update SPLIT_PART scalar function to support Utf8View #11975
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,9 @@ use std::sync::Arc; | |
use arrow::array::{ArrayRef, GenericStringArray, OffsetSizeTrait}; | ||
use arrow::datatypes::DataType; | ||
|
||
use datafusion_common::cast::{as_generic_string_array, as_int64_array}; | ||
use datafusion_common::cast::{ | ||
as_generic_string_array, as_int64_array, as_string_view_array, | ||
}; | ||
use datafusion_common::{exec_err, Result}; | ||
use datafusion_expr::TypeSignature::*; | ||
use datafusion_expr::{ColumnarValue, Volatility}; | ||
|
@@ -46,7 +48,12 @@ impl SplitPartFunc { | |
Self { | ||
signature: Signature::one_of( | ||
vec![ | ||
Exact(vec![Utf8View, Utf8View, Int64]), | ||
Exact(vec![Utf8View, Utf8, Int64]), | ||
Exact(vec![Utf8View, LargeUtf8, Int64]), | ||
Exact(vec![Utf8, Utf8View, Int64]), | ||
Exact(vec![Utf8, Utf8, Int64]), | ||
Exact(vec![LargeUtf8, Utf8View, Int64]), | ||
Exact(vec![LargeUtf8, Utf8, Int64]), | ||
Exact(vec![Utf8, LargeUtf8, Int64]), | ||
Exact(vec![LargeUtf8, LargeUtf8, Int64]), | ||
|
@@ -75,50 +82,101 @@ impl ScalarUDFImpl for SplitPartFunc { | |
} | ||
|
||
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
match args[0].data_type() { | ||
DataType::Utf8 => make_scalar_function(split_part::<i32>, vec![])(args), | ||
DataType::LargeUtf8 => make_scalar_function(split_part::<i64>, vec![])(args), | ||
other => { | ||
exec_err!("Unsupported data type {other:?} for function split_part") | ||
match (args[0].data_type(), args[1].data_type()) { | ||
( | ||
DataType::Utf8 | DataType::Utf8View, | ||
DataType::Utf8 | DataType::Utf8View, | ||
) => make_scalar_function(split_part::<i32, i32>, vec![])(args), | ||
(DataType::LargeUtf8, DataType::LargeUtf8) => { | ||
make_scalar_function(split_part::<i64, i64>, vec![])(args) | ||
} | ||
(_, DataType::LargeUtf8) => { | ||
make_scalar_function(split_part::<i32, i64>, vec![])(args) | ||
} | ||
(DataType::LargeUtf8, _) => { | ||
make_scalar_function(split_part::<i64, i32>, vec![])(args) | ||
} | ||
(first_type, second_type) => exec_err!( | ||
"unsupported first type {} and second type {} for split_part function", | ||
first_type, | ||
second_type | ||
), | ||
} | ||
} | ||
} | ||
|
||
macro_rules! process_split_part { | ||
($string_array: expr, $delimiter_array: expr, $n_array: expr) => {{ | ||
let result = $string_array | ||
.iter() | ||
.zip($delimiter_array.iter()) | ||
.zip($n_array.iter()) | ||
.map(|((string, delimiter), n)| match (string, delimiter, n) { | ||
(Some(string), Some(delimiter), Some(n)) => { | ||
let split_string: Vec<&str> = string.split(delimiter).collect(); | ||
let len = split_string.len(); | ||
|
||
let index = match n.cmp(&0) { | ||
std::cmp::Ordering::Less => len as i64 + n, | ||
std::cmp::Ordering::Equal => { | ||
return exec_err!("field position must not be zero"); | ||
} | ||
std::cmp::Ordering::Greater => n - 1, | ||
} as usize; | ||
|
||
if index < len { | ||
Ok(Some(split_string[index])) | ||
} else { | ||
Ok(Some("")) | ||
} | ||
} | ||
_ => Ok(None), | ||
}) | ||
.collect::<Result<GenericStringArray<StringLen>>>()?; | ||
Ok(Arc::new(result) as ArrayRef) | ||
}}; | ||
} | ||
|
||
/// Splits string at occurrences of delimiter and returns the n'th field (counting from one). | ||
/// split_part('abc~@~def~@~ghi', '~@~', 2) = 'def' | ||
fn split_part<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { | ||
let string_array = as_generic_string_array::<T>(&args[0])?; | ||
let delimiter_array = as_generic_string_array::<T>(&args[1])?; | ||
fn split_part<StringLen: OffsetSizeTrait, DelimiterLen: OffsetSizeTrait>( | ||
args: &[ArrayRef], | ||
) -> Result<ArrayRef> { | ||
let n_array = as_int64_array(&args[2])?; | ||
let result = string_array | ||
.iter() | ||
.zip(delimiter_array.iter()) | ||
.zip(n_array.iter()) | ||
.map(|((string, delimiter), n)| match (string, delimiter, n) { | ||
(Some(string), Some(delimiter), Some(n)) => { | ||
let split_string: Vec<&str> = string.split(delimiter).collect(); | ||
let len = split_string.len(); | ||
|
||
let index = match n.cmp(&0) { | ||
std::cmp::Ordering::Less => len as i64 + n, | ||
std::cmp::Ordering::Equal => { | ||
return exec_err!("field position must not be zero"); | ||
} | ||
std::cmp::Ordering::Greater => n - 1, | ||
} as usize; | ||
|
||
if index < len { | ||
Ok(Some(split_string[index])) | ||
} else { | ||
Ok(Some("")) | ||
match (args[0].data_type(), args[1].data_type()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. split_part is one of the functions where we could actually use StringViewArray to speed up its implementation (by avoiding a string copy). This function as written will copy the parts along It might be a fun optimization project to try as a follow on |
||
(DataType::Utf8View, _) => { | ||
let string_array = as_string_view_array(&args[0])?; | ||
match args[1].data_type() { | ||
DataType::Utf8View => { | ||
let delimiter_array = as_string_view_array(&args[1])?; | ||
process_split_part!(string_array, delimiter_array, n_array) | ||
} | ||
_ => { | ||
let delimiter_array = | ||
as_generic_string_array::<DelimiterLen>(&args[1])?; | ||
process_split_part!(string_array, delimiter_array, n_array) | ||
} | ||
} | ||
_ => Ok(None), | ||
}) | ||
.collect::<Result<GenericStringArray<T>>>()?; | ||
|
||
Ok(Arc::new(result) as ArrayRef) | ||
} | ||
(_, DataType::Utf8View) => { | ||
let delimiter_array = as_string_view_array(&args[1])?; | ||
match args[0].data_type() { | ||
DataType::Utf8View => { | ||
let string_array = as_string_view_array(&args[0])?; | ||
process_split_part!(string_array, delimiter_array, n_array) | ||
} | ||
_ => { | ||
let string_array = as_generic_string_array::<StringLen>(&args[0])?; | ||
process_split_part!(string_array, delimiter_array, n_array) | ||
} | ||
} | ||
} | ||
(_, _) => { | ||
let string_array = as_generic_string_array::<StringLen>(&args[0])?; | ||
let delimiter_array = as_generic_string_array::<DelimiterLen>(&args[1])?; | ||
process_split_part!(string_array, delimiter_array, n_array) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
|
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
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.
FWIW I think we could make this macro a generic function with
ArrayAccessor
like in this pR from @PsiACE #11974