alamb commented on code in PR #12015:
URL: https://github.com/apache/datafusion/pull/12015#discussion_r1719655976


##########
datafusion/functions/src/string/repeat.rs:
##########
@@ -79,51 +81,53 @@ impl ScalarUDFImpl for RepeatFunc {
     }
 
     fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
-        match args[0].data_type() {
-            DataType::Utf8View => make_scalar_function(repeat_utf8view, 
vec![])(args),
-            DataType::Utf8 => make_scalar_function(repeat::<i32>, 
vec![])(args),
-            DataType::LargeUtf8 => make_scalar_function(repeat::<i64>, 
vec![])(args),
-            other => exec_err!("Unsupported data type {other:?} for function 
repeat. Expected Utf8, Utf8View or LargeUtf8"),
-        }
+        make_scalar_function(repeat, vec![])(args)
     }
 }
 
 /// Repeats string the specified number of times.
 /// repeat('Pg', 4) = 'PgPgPgPg'
-fn repeat<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
-    let string_array = as_generic_string_array::<T>(&args[0])?;
+fn repeat(args: &[ArrayRef]) -> Result<ArrayRef> {
     let number_array = as_int64_array(&args[1])?;
-
-    let result = string_array
-        .iter()
-        .zip(number_array.iter())
-        .map(|(string, number)| repeat_common(string, number))
-        .collect::<GenericStringArray<T>>();
-
-    Ok(Arc::new(result) as ArrayRef)
+    match args[0].data_type() {
+        Utf8View => {
+            let string_view_array = args[0].as_string_view();
+            repeat_impl::<i32, &StringViewArray>(string_view_array, 
number_array)
+        }
+        Utf8 => {
+            let string_array = args[0].as_string::<i32>();
+            repeat_impl::<i32, &GenericStringArray<i32>>(string_array, 
number_array)
+        }
+        LargeUtf8 => {
+            let string_array = args[0].as_string::<i64>();
+            repeat_impl::<i64, &GenericStringArray<i64>>(string_array, 
number_array)
+        }
+        other => exec_err!(
+            "Unsupported data type {other:?} for function repeat. \
+        Expected Utf8, Utf8View or LargeUtf8."
+        ),
+    }
 }
 
-fn repeat_utf8view(args: &[ArrayRef]) -> Result<ArrayRef> {
-    let string_view_array = as_string_view_array(&args[0])?;
-    let number_array = as_int64_array(&args[1])?;
-
-    let result = string_view_array
+fn repeat_impl<'a, T, S>(string_array: S, number_array: &Int64Array) -> 
Result<ArrayRef>
+where
+    T: OffsetSizeTrait,
+    S: StringArrayType<'a>,
+{
+    let mut builder: GenericStringBuilder<T> = GenericStringBuilder::new();
+    string_array
         .iter()
         .zip(number_array.iter())
-        .map(|(string, number)| repeat_common(string, number))
-        .collect::<StringArray>();
-
-    Ok(Arc::new(result) as ArrayRef)
-}
-
-fn repeat_common(string: Option<&str>, number: Option<i64>) -> Option<String> {
-    match (string, number) {
-        (Some(string), Some(number)) if number >= 0 => {
-            Some(string.repeat(number as usize))
-        }
-        (Some(_), Some(_)) => Some("".to_string()),
-        _ => None,
-    }
+        .for_each(|(string, number)| match (string, number) {

Review Comment:
   👏  -- very nice



-- 
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