adriangb commented on code in PR #25964:
URL: https://github.com/apache/datafusion/pull/25964#discussion_r4299219932


##########
datafusion/functions/src/core/nvl.rs:
##########
@@ -140,6 +159,23 @@ impl ScalarUDFImpl for NVLFunc {
         self.coalesce.short_circuits()
     }
 
+    fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+        if arg_types.len() != 2 {
+            return exec_err!(
+                "nvl expects exactly two arguments, but received {}",
+                arg_types.len()
+            );
+        }
+        let listed = arg_types
+            .iter()
+            .all(|t| t.is_null() || SUPPORTED_NVL_TYPES.contains(t));
+        if !listed && let Ok(coerced) = self.coalesce.coerce_types(arg_types) {

Review Comment:
   The rule now depends on *which* types are in the call, so the result is not 
always the same as `coalesce`:
   
   - `nvl(Int8, UInt64)` → `Float32` (old path), but `nvl(Int8, 
Decimal128(5,2))` → decimal (coalesce path).
   - `nvl(1, false)` → `Utf8View`, but `coalesce(1, false)` is an error.
   
   This keeps every query that plans today, which is good. But it adds a second 
rule to maintain. An alternative is to use only the coalesce coercion and 
accept the small breaking change (update the `IFNULL(column1, false)` case in 
`expr.slt`). Maybe a maintainer can say which they prefer? If we keep this 
approach, a short comment here with the two examples above helps future readers.



##########
datafusion/functions/src/core/nvl.rs:
##########
@@ -140,6 +159,23 @@ impl ScalarUDFImpl for NVLFunc {
         self.coalesce.short_circuits()
     }
 
+    fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+        if arg_types.len() != 2 {
+            return exec_err!(
+                "nvl expects exactly two arguments, but received {}",
+                arg_types.len()
+            );
+        }

Review Comment:
   `exec_err!` is for execution errors, but `coerce_types` runs at planning 
time. `take_function_args` is the usual helper here (`nvl2` uses it). It gives 
a plan error with the standard message.
   
   ```suggestion
           take_function_args(self.name(), arg_types)?;
   ```
   
   (Import `datafusion_common::utils::take_function_args` and remove 
`exec_err`.)



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