This is an automated email from the ASF dual-hosted git repository.

github-bot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 9de1253f2a Update repeat UDF to emit utf8view when input is utf8view 
(#20645)
9de1253f2a is described below

commit 9de1253f2a8435fe486a93686db77531b54e7a3e
Author: Bruce Ritchie <[email protected]>
AuthorDate: Tue Mar 31 14:33:11 2026 -0400

    Update repeat UDF to emit utf8view when input is utf8view (#20645)
    
    ## Which issue does this PR close?
    
    <!--
    We generally require a GitHub issue to be filed for all bug fixes and
    enhancements and this helps us generate change logs for our releases.
    You can link an issue to this PR using the GitHub syntax. For example
    `Closes #123` indicates that this PR will close issue #123.
    -->
    
    Part of https://github.com/apache/datafusion/issues/20585
    
    ## Rationale for this change
    
    <!--
    Why are you proposing this change? If this is already explained clearly
    in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand
    your changes and offer better suggestions for fixes.
    -->
    
    Functions ideally should emit strings in the same format as the input
    and previously the repeat function was emitting using utf8 for input
    that was in utf8view.
    
    ## What changes are included in this PR?
    
    <!--
    There is no need to duplicate the description in the issue here but it
    is sometimes worth providing a summary of the individual changes in this
    PR.
    -->
    Code, tests
    
    ## Are these changes tested?
    
    Yes
    
    <!--
    We typically require tests for all PRs in order to:
    1. Prevent the code from being accidentally broken by subsequent changes
    2. Serve as another way to document the expected behavior of the code
    
    If tests are not included in your PR, please explain why (for example,
    are they covered by existing tests)?
    -->
    
    ## Are there any user-facing changes?
    
    <!--
    If there are user-facing changes then we may require documentation to be
    updated before approving the PR.
    -->
    
    <!--
    If there are any breaking changes to public APIs, please add the `api
    change` label.
    -->
---
 datafusion/functions/src/string/repeat.rs          | 98 ++++++++++++++++------
 .../test_files/string/string_literal.slt           | 24 ++++++
 2 files changed, 95 insertions(+), 27 deletions(-)

diff --git a/datafusion/functions/src/string/repeat.rs 
b/datafusion/functions/src/string/repeat.rs
index 4e38ec9af3..f100a29e30 100644
--- a/datafusion/functions/src/string/repeat.rs
+++ b/datafusion/functions/src/string/repeat.rs
@@ -20,7 +20,7 @@ use std::sync::Arc;
 use crate::utils::utf8_to_str_type;
 use arrow::array::{
     Array, ArrayRef, AsArray, GenericStringArray, GenericStringBuilder, 
Int64Array,
-    OffsetSizeTrait, StringArrayType, StringViewArray,
+    StringArrayType, StringLikeArrayBuilder, StringViewArray, 
StringViewBuilder,
 };
 use arrow::datatypes::DataType;
 use arrow::datatypes::DataType::{LargeUtf8, Utf8, Utf8View};
@@ -91,6 +91,9 @@ impl ScalarUDFImpl for RepeatFunc {
     }
 
     fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+        if arg_types[0] == Utf8View {
+            return Ok(Utf8View);
+        }
         utf8_to_str_type(&arg_types[0], "repeat")
     }
 
@@ -126,13 +129,12 @@ impl ScalarUDFImpl for RepeatFunc {
                 };
 
                 let result = match string_scalar {
-                    ScalarValue::Utf8(Some(s)) | 
ScalarValue::Utf8View(Some(s)) => {
-                        ScalarValue::Utf8(Some(compute_repeat(
-                            s,
-                            count,
-                            i32::MAX as usize,
-                        )?))
-                    }
+                    ScalarValue::Utf8View(Some(s)) => 
ScalarValue::Utf8View(Some(
+                        compute_repeat(s, count, i32::MAX as usize)?,
+                    )),
+                    ScalarValue::Utf8(Some(s)) => ScalarValue::Utf8(Some(
+                        compute_repeat(s, count, i32::MAX as usize)?,
+                    )),
                     ScalarValue::LargeUtf8(Some(s)) => 
ScalarValue::LargeUtf8(Some(
                         compute_repeat(s, count, i64::MAX as usize)?,
                     )),
@@ -183,26 +185,47 @@ fn repeat(string_array: &ArrayRef, count_array: 
&ArrayRef) -> Result<ArrayRef> {
     match string_array.data_type() {
         Utf8View => {
             let string_view_array = string_array.as_string_view();
-            repeat_impl::<i32, &StringViewArray>(
+            let (_, max_item_capacity) = calculate_capacities(
                 &string_view_array,
                 number_array,
                 i32::MAX as usize,
+            )?;
+            let builder = StringViewBuilder::with_capacity(string_array.len());
+            repeat_impl::<&StringViewArray, StringViewBuilder>(
+                &string_view_array,
+                number_array,
+                max_item_capacity,
+                builder,
             )
         }
         Utf8 => {
             let string_arr = string_array.as_string::<i32>();
-            repeat_impl::<i32, &GenericStringArray<i32>>(
+            let (total_capacity, max_item_capacity) =
+                calculate_capacities(&string_arr, number_array, i32::MAX as 
usize)?;
+            let builder = GenericStringBuilder::<i32>::with_capacity(
+                string_array.len(),
+                total_capacity,
+            );
+            repeat_impl::<&GenericStringArray<i32>, GenericStringBuilder<i32>>(
                 &string_arr,
                 number_array,
-                i32::MAX as usize,
+                max_item_capacity,
+                builder,
             )
         }
         LargeUtf8 => {
             let string_arr = string_array.as_string::<i64>();
-            repeat_impl::<i64, &GenericStringArray<i64>>(
+            let (total_capacity, max_item_capacity) =
+                calculate_capacities(&string_arr, number_array, i64::MAX as 
usize)?;
+            let builder = GenericStringBuilder::<i64>::with_capacity(
+                string_array.len(),
+                total_capacity,
+            );
+            repeat_impl::<&GenericStringArray<i64>, GenericStringBuilder<i64>>(
                 &string_arr,
                 number_array,
-                i64::MAX as usize,
+                max_item_capacity,
+                builder,
             )
         }
         other => exec_err!(
@@ -212,17 +235,17 @@ fn repeat(string_array: &ArrayRef, count_array: 
&ArrayRef) -> Result<ArrayRef> {
     }
 }
 
-fn repeat_impl<'a, T, S>(
+fn calculate_capacities<'a, S>(
     string_array: &S,
     number_array: &Int64Array,
     max_str_len: usize,
-) -> Result<ArrayRef>
+) -> Result<(usize, usize)>
 where
-    T: OffsetSizeTrait,
-    S: StringArrayType<'a> + 'a,
+    S: StringArrayType<'a>,
 {
     let mut total_capacity = 0;
     let mut max_item_capacity = 0;
+
     string_array.iter().zip(number_array.iter()).try_for_each(
         |(string, number)| -> Result<(), DataFusionError> {
             match (string, number) {
@@ -244,9 +267,19 @@ where
         },
     )?;
 
-    let mut builder =
-        GenericStringBuilder::<T>::with_capacity(string_array.len(), 
total_capacity);
+    Ok((total_capacity, max_item_capacity))
+}
 
+fn repeat_impl<'a, S, B>(
+    string_array: &S,
+    number_array: &Int64Array,
+    max_item_capacity: usize,
+    mut builder: B,
+) -> Result<ArrayRef>
+where
+    S: StringArrayType<'a> + 'a,
+    B: StringLikeArrayBuilder,
+{
     // Reusable buffer to avoid allocations in string.repeat()
     let mut buffer = Vec::<u8>::with_capacity(max_item_capacity);
 
@@ -303,8 +336,8 @@ where
 
 #[cfg(test)]
 mod tests {
-    use arrow::array::{Array, StringArray};
-    use arrow::datatypes::DataType::Utf8;
+    use arrow::array::{Array, LargeStringArray, StringArray, StringViewArray};
+    use arrow::datatypes::DataType::{LargeUtf8, Utf8, Utf8View};
 
     use datafusion_common::ScalarValue;
     use datafusion_common::{Result, exec_err};
@@ -357,8 +390,8 @@ mod tests {
             ],
             Ok(Some("PgPgPgPg")),
             &str,
-            Utf8,
-            StringArray
+            Utf8View,
+            StringViewArray
         );
         test_function!(
             RepeatFunc::new(),
@@ -368,8 +401,19 @@ mod tests {
             ],
             Ok(None),
             &str,
-            Utf8,
-            StringArray
+            Utf8View,
+            StringViewArray
+        );
+        test_function!(
+            RepeatFunc::new(),
+            vec![
+                
ColumnarValue::Scalar(ScalarValue::LargeUtf8(Some(String::from("Pg")))),
+                ColumnarValue::Scalar(ScalarValue::Int64(None)),
+            ],
+            Ok(None),
+            &str,
+            LargeUtf8,
+            LargeStringArray
         );
         test_function!(
             RepeatFunc::new(),
@@ -379,8 +423,8 @@ mod tests {
             ],
             Ok(None),
             &str,
-            Utf8,
-            StringArray
+            Utf8View,
+            StringViewArray
         );
         test_function!(
             RepeatFunc::new(),
diff --git a/datafusion/sqllogictest/test_files/string/string_literal.slt 
b/datafusion/sqllogictest/test_files/string/string_literal.slt
index 569dfe0336..d4fe8ee178 100644
--- a/datafusion/sqllogictest/test_files/string/string_literal.slt
+++ b/datafusion/sqllogictest/test_files/string/string_literal.slt
@@ -347,11 +347,35 @@ SELECT repeat('foo', 3)
 ----
 foofoofoo
 
+query T
+SELECT repeat(arrow_cast('foo', 'LargeUtf8'), 3)
+----
+foofoofoo
+
+query T
+SELECT repeat(arrow_cast('foo', 'Utf8View'), 3)
+----
+foofoofoo
+
 query T
 SELECT repeat(arrow_cast('foo', 'Dictionary(Int32, Utf8)'), 3)
 ----
 foofoofoo
 
+query T
+SELECT arrow_typeof(repeat('foo', 3))
+----
+Utf8
+
+query T
+SELECT arrow_typeof(repeat(arrow_cast('foo', 'LargeUtf8'), 3))
+----
+LargeUtf8
+
+query T
+SELECT arrow_typeof(repeat(arrow_cast('foo', 'Utf8View'), 3))
+----
+Utf8View
 
 query T
 SELECT replace('foobar', 'bar', 'hello')


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

Reply via email to