Jefffrey commented on code in PR #20069:
URL: https://github.com/apache/datafusion/pull/20069#discussion_r2750438870


##########
datafusion/functions/src/unicode/right.rs:
##########
@@ -119,58 +119,140 @@ impl ScalarUDFImpl for RightFunc {
     }
 }
 
-/// Returns last n characters in the string, or when n is negative, returns 
all but first |n| characters.
+/// Returns right n characters in the string, or when n is negative, returns 
all but first |n| characters.
 /// right('abcde', 2) = 'de'
+/// right('abcde', -2) = 'cde'
 /// The implementation uses UTF-8 code points as characters
-fn right<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
+fn right(args: &[ArrayRef]) -> Result<ArrayRef> {
     let n_array = as_int64_array(&args[1])?;
-    if args[0].data_type() == &DataType::Utf8View {
-        // string_view_right(args)
-        let string_array = as_string_view_array(&args[0])?;
-        right_impl::<T, _>(&mut string_array.iter(), n_array)
-    } else {
-        // string_right::<T>(args)
-        let string_array = &as_generic_string_array::<T>(&args[0])?;
-        right_impl::<T, _>(&mut string_array.iter(), n_array)
+
+    match args[0].data_type() {
+        DataType::Utf8 => {
+            let string_array = as_generic_string_array::<i32>(&args[0])?;
+            right_impl::<i32, _>(string_array, n_array)
+        }
+        DataType::LargeUtf8 => {
+            let string_array = as_generic_string_array::<i64>(&args[0])?;
+            right_impl::<i64, _>(string_array, n_array)
+        }
+        DataType::Utf8View => {
+            let string_view_array = as_string_view_array(&args[0])?;
+            right_impl_view(string_view_array, n_array)
+        }
+        _ => exec_err!("Not supported"),
     }
 }
 
-// Currently the return type can only be Utf8 or LargeUtf8, to reach fully 
support, we need
-// to edit the `get_optimal_return_type` in utils.rs to make the udfs be able 
to return Utf8View
-// See 
https://github.com/apache/datafusion/issues/11790#issuecomment-2283777166
+/// `right` implementation for strings
 fn right_impl<'a, T: OffsetSizeTrait, V: ArrayAccessor<Item = &'a str>>(
-    string_array_iter: &mut ArrayIter<V>,
+    string_array: V,
     n_array: &Int64Array,
 ) -> Result<ArrayRef> {
-    let result = string_array_iter
+    let iter = ArrayIter::new(string_array);
+    let result = iter
         .zip(n_array.iter())
         .map(|(string, n)| match (string, n) {
-            (Some(string), Some(n)) => match n.cmp(&0) {
-                Ordering::Less => Some(
-                    string
-                        .chars()
-                        .skip(n.unsigned_abs() as usize)
-                        .collect::<String>(),
-                ),
-                Ordering::Equal => Some("".to_string()),
-                Ordering::Greater => Some(
-                    string
-                        .chars()
-                        .skip(max(string.chars().count() as i64 - n, 0) as 
usize)
-                        .collect::<String>(),
-                ),
-            },
+            (Some(string), Some(n)) => {
+                let byte_length = right_byte_length(string, n);
+                // println!(
+                //     "Input string: {}, n: {} -> byte_length: {} -> {}",
+                //     &string,
+                //     n,
+                //     byte_length,
+                //     &string[byte_length..]
+                // );
+                // Extract starting from `byte_length` bytes from a 
byte-indexed slice
+                Some(&string[byte_length..])
+            }
             _ => None,
         })
         .collect::<GenericStringArray<T>>();
 
     Ok(Arc::new(result) as ArrayRef)
 }
 
+/// `right` implementation for StringViewArray
+fn right_impl_view(
+    string_view_array: &StringViewArray,
+    n_array: &Int64Array,
+) -> Result<ArrayRef> {
+    let len = n_array.len();
+
+    let views = string_view_array.views();
+    // Every string in StringViewArray has one corresponding view in `views`
+    debug_assert!(views.len() == string_view_array.len());
+
+    // Compose null buffer at once
+    let string_nulls = string_view_array.nulls();
+    let n_nulls = n_array.nulls();
+    let new_nulls = NullBuffer::union(string_nulls, n_nulls);
+
+    let new_views = (0..len)
+        .map(|idx| {
+            let view = views[idx];
+
+            let is_valid = match &new_nulls {
+                Some(nulls_buf) => nulls_buf.is_valid(idx),
+                None => true,
+            };
+
+            if is_valid {
+                let string: &str = string_view_array.value(idx);
+                let n = n_array.value(idx);
+
+                let new_offset = right_byte_length(string, n);
+                let result_bytes = &string.as_bytes()[new_offset..];
+
+                if result_bytes.len() > 12 {
+                    let byte_view = ByteView::from(view);
+                    // Reuse buffer, but adjust offset and length
+                    make_view(
+                        result_bytes,
+                        byte_view.buffer_index,
+                        byte_view.offset + new_offset as u32,
+                    )
+                } else {
+                    // inline value does not need block id or offset
+                    make_view(result_bytes, 0, 0)
+                }
+            } else {
+                // For nulls, keep the original view
+                view
+            }
+        })
+        .collect::<Vec<u128>>();
+
+    // Buffers are unchanged
+    let result = StringViewArray::try_new(
+        ScalarBuffer::from(new_views),
+        Vec::from(string_view_array.data_buffers()),
+        new_nulls,
+    )?;
+    Ok(Arc::new(result) as ArrayRef)
+}
+
+/// Calculate the byte length of the substring of last `n` chars from string 
`string`
+/// (or all but first `|n|` chars if n is negative)
+fn right_byte_length(string: &str, n: i64) -> usize {

Review Comment:
   I do think we could deduplicate somewhat, and perhaps having this function 
return a `Range` instead of just a `usize` might make it more feasible; but can 
explore this in a followup 👍 



##########
datafusion/functions/src/unicode/right.rs:
##########
@@ -119,58 +119,133 @@ impl ScalarUDFImpl for RightFunc {
     }
 }
 
-/// Returns last n characters in the string, or when n is negative, returns 
all but first |n| characters.
+/// Returns right n characters in the string, or when n is negative, returns 
all but first |n| characters.
 /// right('abcde', 2) = 'de'
+/// right('abcde', -2) = 'cde'
 /// The implementation uses UTF-8 code points as characters
-fn right<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
+fn right(args: &[ArrayRef]) -> Result<ArrayRef> {
     let n_array = as_int64_array(&args[1])?;
-    if args[0].data_type() == &DataType::Utf8View {
-        // string_view_right(args)
-        let string_array = as_string_view_array(&args[0])?;
-        right_impl::<T, _>(&mut string_array.iter(), n_array)
-    } else {
-        // string_right::<T>(args)
-        let string_array = &as_generic_string_array::<T>(&args[0])?;
-        right_impl::<T, _>(&mut string_array.iter(), n_array)
+
+    match args[0].data_type() {
+        DataType::Utf8 => {
+            let string_array = as_generic_string_array::<i32>(&args[0])?;
+            right_impl::<i32, _>(string_array, n_array)
+        }
+        DataType::LargeUtf8 => {
+            let string_array = as_generic_string_array::<i64>(&args[0])?;
+            right_impl::<i64, _>(string_array, n_array)
+        }
+        DataType::Utf8View => {
+            let string_view_array = as_string_view_array(&args[0])?;
+            right_impl_view(string_view_array, n_array)
+        }
+        _ => exec_err!("Not supported"),
     }
 }
 
-// Currently the return type can only be Utf8 or LargeUtf8, to reach fully 
support, we need
-// to edit the `get_optimal_return_type` in utils.rs to make the udfs be able 
to return Utf8View
-// See 
https://github.com/apache/datafusion/issues/11790#issuecomment-2283777166
+/// `right` implementation for strings
 fn right_impl<'a, T: OffsetSizeTrait, V: ArrayAccessor<Item = &'a str>>(
-    string_array_iter: &mut ArrayIter<V>,
+    string_array: V,
     n_array: &Int64Array,
 ) -> Result<ArrayRef> {
-    let result = string_array_iter
+    let iter = ArrayIter::new(string_array);
+    let result = iter
         .zip(n_array.iter())
         .map(|(string, n)| match (string, n) {
-            (Some(string), Some(n)) => match n.cmp(&0) {
-                Ordering::Less => Some(
-                    string
-                        .chars()
-                        .skip(n.unsigned_abs() as usize)
-                        .collect::<String>(),
-                ),
-                Ordering::Equal => Some("".to_string()),
-                Ordering::Greater => Some(
-                    string
-                        .chars()
-                        .skip(max(string.chars().count() as i64 - n, 0) as 
usize)
-                        .collect::<String>(),
-                ),
-            },
+            (Some(string), Some(n)) => {
+                let byte_length = right_byte_length(string, n);
+                // Extract starting from `byte_length` bytes from a 
byte-indexed slice
+                Some(&string[byte_length..])
+            }
             _ => None,
         })
         .collect::<GenericStringArray<T>>();
 
     Ok(Arc::new(result) as ArrayRef)
 }
 
+/// `right` implementation for StringViewArray
+fn right_impl_view(
+    string_view_array: &StringViewArray,
+    n_array: &Int64Array,
+) -> Result<ArrayRef> {
+    let len = n_array.len();
+
+    let views = string_view_array.views();
+    // Every string in StringViewArray has one corresponding view in `views`
+    debug_assert!(views.len() == string_view_array.len());
+
+    // Compose null buffer at once
+    let string_nulls = string_view_array.nulls();
+    let n_nulls = n_array.nulls();
+    let new_nulls = NullBuffer::union(string_nulls, n_nulls);
+
+    let new_views = (0..len)
+        .map(|idx| {
+            let view = views[idx];
+
+            let is_valid = match &new_nulls {
+                Some(nulls_buf) => nulls_buf.is_valid(idx),
+                None => true,
+            };
+
+            if is_valid {
+                let string: &str = string_view_array.value(idx);
+                let n = n_array.value(idx);
+
+                let new_offset = right_byte_length(string, n);
+                let result_bytes = &string.as_bytes()[new_offset..];
+
+                if result_bytes.len() > 12 {
+                    let byte_view = ByteView::from(view);
+                    // Reuse buffer, but adjust offset and length
+                    make_view(
+                        result_bytes,
+                        byte_view.buffer_index,
+                        byte_view.offset + new_offset as u32,
+                    )
+                } else {
+                    // inline value does not need block id or offset
+                    make_view(result_bytes, 0, 0)
+                }

Review Comment:
   ```suggestion
                   let byte_view = ByteView::from(view);
                   make_view(
                       result_bytes,
                       byte_view.buffer_index,
                       byte_view.offset + new_offset as u32,
                   )
   ```
   
   We could probably avoid this outside if check since `make_view` already 
checks this for us



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to