-
Notifications
You must be signed in to change notification settings - Fork 2k
Update reverse UDF to emit utf8view when input is utf8view #20604
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
Open
Omega359
wants to merge
4
commits into
apache:main
Choose a base branch
from
Omega359:reverse_utf8view
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.
+58
−19
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
acf6710
Update reverse UDF to emit utf8view when input is utf8view.
Omega359 86c7a9b
Switched from StringArrayBuilderType to StringLikeArrayBuilder
Omega359 a08b07a
Merge branch 'main' into reverse_utf8view
Omega359 a8baa6a
Merge branch 'main' into reverse_utf8view
Omega359 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 hidden or 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 crate::utils::{make_scalar_function, utf8_to_str_type}; | ||
| use DataType::{LargeUtf8, Utf8, Utf8View}; | ||
| use arrow::array::{ | ||
| Array, ArrayRef, AsArray, GenericStringBuilder, OffsetSizeTrait, StringArrayType, | ||
| Array, ArrayRef, AsArray, GenericStringArray, LargeStringBuilder, OffsetSizeTrait, | ||
| StringArrayType, StringBuilder, StringLikeArrayBuilder, StringViewArray, | ||
| StringViewBuilder, | ||
| }; | ||
| use arrow::datatypes::DataType; | ||
| use datafusion_common::{Result, exec_err}; | ||
|
|
@@ -82,7 +84,11 @@ impl ScalarUDFImpl for ReverseFunc { | |
| } | ||
|
|
||
| fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { | ||
| utf8_to_str_type(&arg_types[0], "reverse") | ||
| if arg_types[0] == Utf8View { | ||
| Ok(Utf8View) | ||
| } else { | ||
| utf8_to_str_type(&arg_types[0], "btrim") | ||
| } | ||
| } | ||
|
|
||
| fn invoke_with_args( | ||
|
|
@@ -107,20 +113,38 @@ impl ScalarUDFImpl for ReverseFunc { | |
| /// Reverses the order of the characters in the string `reverse('abcde') = 'edcba'`. | ||
| /// The implementation uses UTF-8 code points as characters | ||
| fn reverse<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { | ||
|
Contributor
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. We probably should take this opportunity to refactor the |
||
| if args[0].data_type() == &Utf8View { | ||
| reverse_impl::<T, _>(&args[0].as_string_view()) | ||
| } else { | ||
| reverse_impl::<T, _>(&args[0].as_string::<T>()) | ||
| let len = args[0].len(); | ||
|
|
||
| match args[0].data_type() { | ||
| Utf8View => reverse_impl::<&StringViewArray, StringViewBuilder>( | ||
| &args[0].as_string_view(), | ||
| StringViewBuilder::with_capacity(len), | ||
| ), | ||
| LargeUtf8 => reverse_impl::<&GenericStringArray<T>, LargeStringBuilder>( | ||
| &args[0].as_string::<T>(), | ||
| LargeStringBuilder::with_capacity(len, 1024), | ||
| ), | ||
| Utf8 => reverse_impl::<&GenericStringArray<T>, StringBuilder>( | ||
| &args[0].as_string::<T>(), | ||
| StringBuilder::with_capacity(len, 1024), | ||
| ), | ||
| _ => unreachable!( | ||
| "Reverse can only be applied to Utf8View, Utf8 and LargeUtf8 types" | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| fn reverse_impl<'a, T: OffsetSizeTrait, V: StringArrayType<'a>>( | ||
| string_array: &V, | ||
| ) -> Result<ArrayRef> { | ||
| let mut builder = GenericStringBuilder::<T>::with_capacity(string_array.len(), 1024); | ||
|
|
||
| fn reverse_impl<'a, StringArrType, StringBuilderType>( | ||
| string_array: &StringArrType, | ||
| mut array_builder: StringBuilderType, | ||
| ) -> Result<ArrayRef> | ||
| where | ||
| StringArrType: StringArrayType<'a>, | ||
| StringBuilderType: StringLikeArrayBuilder, | ||
| { | ||
| let mut string_buf = String::new(); | ||
| let mut byte_buf = Vec::<u8>::new(); | ||
|
|
||
| for string in string_array.iter() { | ||
| if let Some(s) = string { | ||
| if s.is_ascii() { | ||
|
|
@@ -129,25 +153,25 @@ fn reverse_impl<'a, T: OffsetSizeTrait, V: StringArrayType<'a>>( | |
| byte_buf.reverse(); | ||
| // SAFETY: Since the original string was ASCII, reversing the bytes still results in valid UTF-8. | ||
| let reversed = unsafe { std::str::from_utf8_unchecked(&byte_buf) }; | ||
| builder.append_value(reversed); | ||
| array_builder.append_value(reversed); | ||
| byte_buf.clear(); | ||
| } else { | ||
| string_buf.extend(s.chars().rev()); | ||
| builder.append_value(&string_buf); | ||
| array_builder.append_value(&string_buf); | ||
| string_buf.clear(); | ||
| } | ||
| } else { | ||
| builder.append_null(); | ||
| array_builder.append_null(); | ||
| } | ||
| } | ||
|
|
||
| Ok(Arc::new(builder.finish()) as ArrayRef) | ||
| Ok(Arc::new(array_builder.finish()) as ArrayRef) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use arrow::array::{Array, LargeStringArray, StringArray}; | ||
| use arrow::datatypes::DataType::{LargeUtf8, Utf8}; | ||
| use arrow::array::{Array, LargeStringArray, StringArray, StringViewArray}; | ||
| use arrow::datatypes::DataType::{LargeUtf8, Utf8, Utf8View}; | ||
|
|
||
| use datafusion_common::{Result, ScalarValue}; | ||
| use datafusion_expr::{ColumnarValue, ScalarUDFImpl}; | ||
|
|
@@ -180,8 +204,8 @@ mod tests { | |
| vec![ColumnarValue::Scalar(ScalarValue::Utf8View($INPUT))], | ||
| $EXPECTED, | ||
| &str, | ||
| Utf8, | ||
| StringArray | ||
| Utf8View, | ||
| StringViewArray | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
This file contains hidden or 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
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.
Once all the functions that use the
utf8_to_str_typefunction are updated to emit utf8view appropriately a followup pr to update that function to include utf8view -> utf8view would clean this up.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.
Alternatively just