-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Update REVERSE scalar function to support Utf8View #11973
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,12 +18,14 @@ | |
use std::any::Any; | ||
use std::sync::Arc; | ||
|
||
use arrow::array::{ArrayRef, GenericStringArray, OffsetSizeTrait}; | ||
use arrow::array::{ | ||
Array, ArrayAccessor, ArrayIter, ArrayRef, AsArray, GenericStringArray, | ||
OffsetSizeTrait, | ||
}; | ||
use arrow::datatypes::DataType; | ||
|
||
use datafusion_common::cast::as_generic_string_array; | ||
use datafusion_common::{exec_err, Result}; | ||
use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; | ||
use DataType::{LargeUtf8, Utf8, Utf8View}; | ||
|
||
use crate::utils::{make_scalar_function, utf8_to_str_type}; | ||
|
||
|
@@ -44,7 +46,7 @@ impl ReverseFunc { | |
Self { | ||
signature: Signature::uniform( | ||
1, | ||
vec![Utf8, LargeUtf8], | ||
vec![Utf8View, Utf8, LargeUtf8], | ||
Volatility::Immutable, | ||
), | ||
} | ||
|
@@ -70,8 +72,8 @@ impl ScalarUDFImpl for ReverseFunc { | |
|
||
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
match args[0].data_type() { | ||
DataType::Utf8 => make_scalar_function(reverse::<i32>, vec![])(args), | ||
DataType::LargeUtf8 => make_scalar_function(reverse::<i64>, vec![])(args), | ||
Utf8 | Utf8View => make_scalar_function(reverse::<i32>, vec![])(args), | ||
LargeUtf8 => make_scalar_function(reverse::<i64>, vec![])(args), | ||
other => { | ||
exec_err!("Unsupported data type {other:?} for function reverse") | ||
} | ||
|
@@ -83,10 +85,17 @@ impl ScalarUDFImpl for ReverseFunc { | |
/// reverse('abcde') = 'edcba' | ||
/// The implementation uses UTF-8 code points as characters | ||
pub fn reverse<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { | ||
let string_array = as_generic_string_array::<T>(&args[0])?; | ||
if args[0].data_type() == &Utf8View { | ||
reverse_impl::<T, _>(args[0].as_string_view()) | ||
} else { | ||
reverse_impl::<T, _>(args[0].as_string::<T>()) | ||
} | ||
} | ||
|
||
let result = string_array | ||
.iter() | ||
fn reverse_impl<'a, T: OffsetSizeTrait, V: ArrayAccessor<Item = &'a str>>( | ||
string_array: V, | ||
) -> Result<ArrayRef> { | ||
let result = ArrayIter::new(string_array) | ||
.map(|string| string.map(|string: &str| string.chars().rev().collect::<String>())) | ||
.collect::<GenericStringArray<T>>(); | ||
|
||
|
@@ -95,59 +104,58 @@ pub fn reverse<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { | |
|
||
#[cfg(test)] | ||
mod tests { | ||
use arrow::array::{Array, StringArray}; | ||
use arrow::datatypes::DataType::Utf8; | ||
use arrow::array::{Array, LargeStringArray, StringArray}; | ||
use arrow::datatypes::DataType::{LargeUtf8, Utf8}; | ||
|
||
use datafusion_common::{Result, ScalarValue}; | ||
use datafusion_expr::{ColumnarValue, ScalarUDFImpl}; | ||
|
||
use crate::unicode::reverse::ReverseFunc; | ||
use crate::utils::test::test_function; | ||
|
||
macro_rules! test_reverse { | ||
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. is it possible to have a function rather than macro? 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. Could, sure. I'm curious as to why though. This is just the same approach as I used for lpad. 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. yeah this testing pattern is somewhat common in the string functions I happen to like .slt in general, but I know that is not for everyone 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. I agree, we trying to use macros if other options didn't work. It takes more time when people debug tests to expand the macros and go through them. if its already used in string tests its okay, perhaps we want to revisit macros and replace them with funcs whenever possible 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. Great explanation ,thanks. |
||
($INPUT:expr, $EXPECTED:expr) => { | ||
test_function!( | ||
ReverseFunc::new(), | ||
&[ColumnarValue::Scalar(ScalarValue::Utf8($INPUT))], | ||
$EXPECTED, | ||
&str, | ||
Utf8, | ||
StringArray | ||
); | ||
|
||
test_function!( | ||
ReverseFunc::new(), | ||
&[ColumnarValue::Scalar(ScalarValue::LargeUtf8($INPUT))], | ||
$EXPECTED, | ||
&str, | ||
LargeUtf8, | ||
LargeStringArray | ||
); | ||
|
||
test_function!( | ||
ReverseFunc::new(), | ||
&[ColumnarValue::Scalar(ScalarValue::Utf8View($INPUT))], | ||
$EXPECTED, | ||
&str, | ||
Utf8, | ||
StringArray | ||
); | ||
}; | ||
} | ||
|
||
#[test] | ||
fn test_functions() -> Result<()> { | ||
test_function!( | ||
ReverseFunc::new(), | ||
&[ColumnarValue::Scalar(ScalarValue::from("abcde"))], | ||
Ok(Some("edcba")), | ||
&str, | ||
Utf8, | ||
StringArray | ||
); | ||
test_function!( | ||
ReverseFunc::new(), | ||
&[ColumnarValue::Scalar(ScalarValue::from("loẅks"))], | ||
Ok(Some("sk̈wol")), | ||
&str, | ||
Utf8, | ||
StringArray | ||
); | ||
test_function!( | ||
ReverseFunc::new(), | ||
&[ColumnarValue::Scalar(ScalarValue::from("loẅks"))], | ||
Ok(Some("sk̈wol")), | ||
&str, | ||
Utf8, | ||
StringArray | ||
); | ||
test_function!( | ||
ReverseFunc::new(), | ||
&[ColumnarValue::Scalar(ScalarValue::Utf8(None))], | ||
Ok(None), | ||
&str, | ||
Utf8, | ||
StringArray | ||
); | ||
test_reverse!(Some("abcde".into()), Ok(Some("edcba"))); | ||
test_reverse!(Some("loẅks".into()), Ok(Some("sk̈wol"))); | ||
test_reverse!(Some("loẅks".into()), Ok(Some("sk̈wol"))); | ||
test_reverse!(None, Ok(None)); | ||
#[cfg(not(feature = "unicode_expressions"))] | ||
test_function!( | ||
ReverseFunc::new(), | ||
&[ColumnarValue::Scalar(ScalarValue::from("abcde"))], | ||
test_reverse!( | ||
Some("abcde".into()), | ||
internal_err!( | ||
"function reverse requires compilation with feature flag: unicode_expressions." | ||
), | ||
&str, | ||
Utf8, | ||
StringArray | ||
); | ||
|
||
Ok(()) | ||
|
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.
probably we can keep same pattern like in
invoke
? it would be more readableThere 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.
as in using a match? Sure. It won't be the same match arms though.