viirya commented on code in PR #513:
URL: https://github.com/apache/datafusion-comet/pull/513#discussion_r1632159142


##########
core/src/execution/datafusion/expressions/scalar_funcs/chr.rs:
##########
@@ -94,15 +94,43 @@ impl ScalarUDFImpl for ChrFunc {
     }
 
     fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
-        let array = args[0].clone();
-        match array {
-            ColumnarValue::Array(array) => {
-                let array = chr(&[array])?;
-                Ok(ColumnarValue::Array(array))
-            }
-            _ => {
-                exec_err!("The first argument must be an array, but got: 
{:?}", array)
-            }
+        make_scalar_function(chr)(args)
+    }
+}
+
+/// The make_scalar_function function is a higher-order function that:
+/// - Takes a function inner designed to operate on arrays.
+/// - Wraps this function in a closure that can accept a mix of scalar and 
array inputs.
+/// - Converts scalar inputs to arrays, calls the inner function, and then 
converts the result back to a scalar if the original inputs were all scalars.
+/// 
+/// taken from datafusion utils
+
+fn make_scalar_function<F>(inner: F) -> impl Fn(&[ColumnarValue]) -> 
Result<ColumnarValue>

Review Comment:
   Hmm, as Spark `Chr` expression has only one argument. It is either a 
`ColumnarValue::Array` or a `ColumnarValue::Scalar`. We don't need to make it 
complicated like this.
   
   We can do simply:
   
   ```rust
   let array = args[0].clone();
   match array {
       ColumnarValue::Array(array) => {
           let array = chr(&[array])?;
           Ok(ColumnarValue::Array(array))
       }
       ColumnarValue::Scalar(Utf8(Some(_))) => {
           let values = array.into_array(1);
           let array = chr(&[array])?;
           Ok(ColumnarValue::Scalar(Utf8(Some(array.value(0).to_string()))))
       }
       ColumnarValue::Scalar(Utf8(None)) => {
           Ok(ColumnarValue::Scalar(Utf8(None)))
       }
       _ => {
           ...
       }
   ```



-- 
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: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to