-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
implement utf8_view for replace #12004
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
60ff4f6
draft implement utf8_view for replace
thinh2 7a3569b
add function signature
thinh2 b08f96e
Add sql test
alamb 14a124b
Merge remote-tracking branch 'apache/main' into utf8view_for_replace
alamb ab85d92
move macro util to replace function
thinh2 8ce4028
remove unused import
thinh2 b948b0f
rust format
thinh2 d26f21a
change return type from utf8view to utf8
thinh2 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 |
---|---|---|
|
@@ -18,16 +18,16 @@ | |
use std::any::Any; | ||
use std::sync::Arc; | ||
|
||
use arrow::array::{ArrayRef, GenericStringArray, OffsetSizeTrait}; | ||
use arrow::array::{ArrayRef, GenericStringArray, OffsetSizeTrait, StringViewArray}; | ||
use arrow::datatypes::DataType; | ||
|
||
use datafusion_common::cast::as_generic_string_array; | ||
use datafusion_common::cast::{as_generic_string_array, as_string_view_array}; | ||
use datafusion_common::{exec_err, Result}; | ||
use datafusion_expr::TypeSignature::*; | ||
use datafusion_expr::{ColumnarValue, Volatility}; | ||
use datafusion_expr::{ScalarUDFImpl, Signature}; | ||
|
||
use crate::utils::{make_scalar_function, utf8_to_str_type}; | ||
use crate::utils::make_scalar_function; | ||
|
||
#[derive(Debug)] | ||
pub struct ReplaceFunc { | ||
|
@@ -45,7 +45,11 @@ impl ReplaceFunc { | |
use DataType::*; | ||
Self { | ||
signature: Signature::one_of( | ||
vec![Exact(vec![Utf8, Utf8, Utf8])], | ||
vec![ | ||
Exact(vec![Utf8View, Utf8View, Utf8View]), | ||
Exact(vec![Utf8, Utf8, Utf8]), | ||
Exact(vec![LargeUtf8, LargeUtf8, LargeUtf8]), | ||
], | ||
Volatility::Immutable, | ||
), | ||
} | ||
|
@@ -66,20 +70,62 @@ impl ScalarUDFImpl for ReplaceFunc { | |
} | ||
|
||
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { | ||
utf8_to_str_type(&arg_types[0], "replace") | ||
match arg_types[0].clone() { | ||
DataType::Utf8 => return Ok(DataType::Utf8), | ||
DataType::LargeUtf8 => return Ok(DataType::LargeUtf8), | ||
DataType::Utf8View => return Ok(DataType::Utf8View), | ||
DataType::Dictionary(_, value_type) => match *value_type { | ||
DataType::LargeUtf8 | DataType::LargeBinary => { | ||
return Ok(DataType::LargeUtf8) | ||
} | ||
DataType::Utf8 | DataType::Binary => return Ok(DataType::Utf8), | ||
DataType::Utf8View | DataType::BinaryView => { | ||
return Ok(DataType::Utf8View) | ||
} | ||
DataType::Null => return Ok(DataType::Null), | ||
_ => { | ||
return exec_err!( | ||
"The replace function can only accept strings, but got {:?}.", | ||
*value_type | ||
); | ||
} | ||
}, | ||
DataType::Null => return Ok(DataType::Null), | ||
other => { | ||
exec_err!("Unsupported data type {other:?} for function replace") | ||
} | ||
} | ||
} | ||
|
||
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
match args[0].data_type() { | ||
DataType::Utf8 => make_scalar_function(replace::<i32>, vec![])(args), | ||
DataType::LargeUtf8 => make_scalar_function(replace::<i64>, vec![])(args), | ||
DataType::Utf8View => make_scalar_function(replace_view, vec![])(args), | ||
other => { | ||
exec_err!("Unsupported data type {other:?} for function replace") | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn replace_view(args: &[ArrayRef]) -> Result<ArrayRef> { | ||
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. ❤️ |
||
let string_array = as_string_view_array(&args[0])?; | ||
let from_array = as_string_view_array(&args[1])?; | ||
let to_array = as_string_view_array(&args[2])?; | ||
|
||
let result = string_array | ||
.iter() | ||
.zip(from_array.iter()) | ||
.zip(to_array.iter()) | ||
.map(|((string, from), to)| match (string, from, to) { | ||
(Some(string), Some(from), Some(to)) => Some(string.replace(from, to)), | ||
_ => None, | ||
}) | ||
.collect::<StringViewArray>(); | ||
|
||
Ok(Arc::new(result) as ArrayRef) | ||
} | ||
/// Replaces all occurrences in string of substring from with substring to. | ||
/// replace('abcdefabcdef', 'cd', 'XX') = 'abXXefabXXef' | ||
fn replace<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { | ||
|
@@ -100,4 +146,61 @@ fn replace<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { | |
Ok(Arc::new(result) as ArrayRef) | ||
} | ||
|
||
mod test {} | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::utils::test::test_function; | ||
use arrow::array::Array; | ||
use arrow::array::LargeStringArray; | ||
use arrow::array::StringArray; | ||
use arrow::array::StringViewArray; | ||
use arrow::datatypes::DataType::{LargeUtf8, Utf8, Utf8View}; | ||
use datafusion_common::ScalarValue; | ||
#[test] | ||
fn test_functions() -> Result<()> { | ||
test_function!( | ||
ReplaceFunc::new(), | ||
&[ | ||
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("aabbdqcbb")))), | ||
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("bb")))), | ||
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("ccc")))), | ||
], | ||
Ok(Some("aacccdqcccc")), | ||
&str, | ||
Utf8, | ||
StringArray | ||
); | ||
|
||
test_function!( | ||
ReplaceFunc::new(), | ||
&[ | ||
ColumnarValue::Scalar(ScalarValue::LargeUtf8(Some(String::from( | ||
"aabbb" | ||
)))), | ||
ColumnarValue::Scalar(ScalarValue::LargeUtf8(Some(String::from("bbb")))), | ||
ColumnarValue::Scalar(ScalarValue::LargeUtf8(Some(String::from("cc")))), | ||
], | ||
Ok(Some("aacc")), | ||
&str, | ||
LargeUtf8, | ||
LargeStringArray | ||
); | ||
|
||
test_function!( | ||
ReplaceFunc::new(), | ||
&[ | ||
ColumnarValue::Scalar(ScalarValue::Utf8View(Some(String::from( | ||
"aabbbcw" | ||
)))), | ||
ColumnarValue::Scalar(ScalarValue::Utf8View(Some(String::from("bb")))), | ||
ColumnarValue::Scalar(ScalarValue::Utf8View(Some(String::from("cc")))), | ||
], | ||
Ok(Some("aaccbcw")), | ||
&str, | ||
Utf8View, | ||
StringViewArray | ||
); | ||
|
||
Ok(()) | ||
} | ||
} |
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.
Thank you for also implementing support for LargeUTF8