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


##########
datafusion/functions/src/math/nanvl.rs:
##########
@@ -101,7 +104,49 @@ impl ScalarUDFImpl for NanvlFunc {
     }
 
     fn invoke_with_args(&self, args: ScalarFunctionArgs) -> 
Result<ColumnarValue> {
-        make_scalar_function(nanvl, vec![])(&args.args)
+        let [x, y] = take_function_args(self.name(), args.args)?;
+
+        match (&x, &y) {
+            (ColumnarValue::Scalar(x), ColumnarValue::Scalar(y)) => {

Review Comment:
   I think we can do this simpler:
   
   ```rust
   match (x, y) {
       // if x is any scalar nan we return y
       (ColumnarValue::Scalar(ScalarValue::Float16(Some(v))), _) if v.is_nan() 
=> return y,
       (ColumnarValue::Scalar(ScalarValue::Float32(Some(v))), _) if v.is_nan() 
=> return y,
       (ColumnarValue::Scalar(ScalarValue::Float64(Some(v))), _) if v.is_nan() 
=> return y,
       (ColumnarValue::Scalar(_), _) => return x, // x is not scalar nan so we 
can safely return it, ignore y
       // fallback
       (ColumnarValue::Array(x), ColumnarValue::Array(y)) => return nanvl(x, y),
       _ => unreachable_internal_error(),
   }
   ```
   
   - Basically, if the first argument is scalar we never need to expand either 
(we don't need y to be scalar)



##########
datafusion/sqllogictest/test_files/scalar.slt:
##########
@@ -765,11 +765,11 @@ select nanvl(null, 64);
 ----
 NULL
 
-# nanvl scalar nulls #1
+# nanvl scalar nulls #1 - x is not NaN, so return x even if y is NULL
 query R rowsort
 select nanvl(2, null);
 ----
-NULL

Review Comment:
   This is interesting; I think we need to fix the array behaviour as well 
actually



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