LiaCastaneda commented on code in PR #27099:
URL: https://github.com/apache/datafusion/pull/27099#discussion_r4616073130


##########
datafusion/substrait/src/logical_plan/consumer/expr/scalar_function.rs:
##########
@@ -76,26 +79,75 @@ pub async fn from_scalar_function(
             Ok(expr.resolve_lambda_variables(input_schema)?.data)
         }
     } else if let Ok(func) = udf_func {
-        Ok(Expr::ScalarFunction(expr::ScalarFunction::new_udf(
-            func.to_owned(),
-            args,
-        )))
+        build_naming_struct_literals_if_needed(args, input_schema, |args| {
+            Ok(Expr::ScalarFunction(expr::ScalarFunction::new_udf(
+                func.to_owned(),
+                args,
+            )))
+        })
     } else if let Some(op) = name_to_op(fn_name) {
         if args.len() < 2 {
             return not_impl_err!(
                 "Expect at least two arguments for binary operator {op:?}, the 
provided number of operators is {:?}",
                 f.arguments.len()
             );
         }
+        // an operator always combines its operands, so they need the same 
field names
+        name_struct_literals_like_siblings(
+            &mut args.iter_mut().collect::<Vec<_>>(),
+            input_schema,
+        );
         // In those cases we build a balanced tree of BinaryExprs
         arg_list_to_binary_op_tree(op, args)
     } else if let Some(builder) = BuiltinExprBuilder::try_from_name(fn_name) {
+        // these combine their arguments too, as in `s BETWEEN low AND high`
+        name_struct_literals_like_siblings(
+            &mut args.iter_mut().collect::<Vec<_>>(),
+            input_schema,
+        );
         builder.build(consumer, f, args)
     } else {
         not_impl_err!("Unsupported function name: {fn_name:?}")
     }
 }
 
+/// Builds a function call with `build`, renaming its struct literals after a 
same-shaped
+/// argument only if the call doesn't type-check without it.
+///
+/// `coalesce(s, [])` needs one type for `s: List<Struct{a, b}>` and the 
literal's
+/// `List<Struct{c0, c1}>`, so the literal takes `s`'s names. 
`named_struct('left', s,
+/// 'right', {c0: 42})` type-checks as is, and renaming would break a later
+/// `get_field(..., 'c0')`, so the literal is left alone. Function signatures 
can't tell
+/// these apart, hence the check; operators always combine their operands and 
always
+/// rename.
+fn build_naming_struct_literals_if_needed(

Review Comment:
   I don't think there's a cleaner way to handle this while still covering 
every case. I was thinking maybe we could do some logic to rename only if the 
function's output type matches the literal's type. This covers functions that 
output structs like coalesce, but it misses cases like contains functions (that 
return a different type) etc. The current approach covers every case.



##########
datafusion/substrait/src/logical_plan/consumer/expr/scalar_function.rs:
##########
@@ -76,26 +79,75 @@ pub async fn from_scalar_function(
             Ok(expr.resolve_lambda_variables(input_schema)?.data)
         }
     } else if let Ok(func) = udf_func {
-        Ok(Expr::ScalarFunction(expr::ScalarFunction::new_udf(
-            func.to_owned(),
-            args,
-        )))
+        build_naming_struct_literals_if_needed(args, input_schema, |args| {
+            Ok(Expr::ScalarFunction(expr::ScalarFunction::new_udf(
+                func.to_owned(),
+                args,
+            )))
+        })
     } else if let Some(op) = name_to_op(fn_name) {
         if args.len() < 2 {
             return not_impl_err!(
                 "Expect at least two arguments for binary operator {op:?}, the 
provided number of operators is {:?}",
                 f.arguments.len()
             );
         }
+        // an operator always combines its operands, so they need the same 
field names
+        name_struct_literals_like_siblings(
+            &mut args.iter_mut().collect::<Vec<_>>(),
+            input_schema,
+        );
         // In those cases we build a balanced tree of BinaryExprs
         arg_list_to_binary_op_tree(op, args)
     } else if let Some(builder) = BuiltinExprBuilder::try_from_name(fn_name) {
+        // these combine their arguments too, as in `s BETWEEN low AND high`
+        name_struct_literals_like_siblings(
+            &mut args.iter_mut().collect::<Vec<_>>(),
+            input_schema,
+        );
         builder.build(consumer, f, args)
     } else {
         not_impl_err!("Unsupported function name: {fn_name:?}")
     }
 }
 
+/// Builds a function call with `build`, renaming its struct literals after a 
same-shaped
+/// argument only if the call doesn't type-check without it.
+///
+/// `coalesce(s, [])` needs one type for `s: List<Struct{a, b}>` and the 
literal's
+/// `List<Struct{c0, c1}>`, so the literal takes `s`'s names. 
`named_struct('left', s,
+/// 'right', {c0: 42})` type-checks as is, and renaming would break a later
+/// `get_field(..., 'c0')`, so the literal is left alone. Function signatures 
can't tell
+/// these apart, hence the check; operators always combine their operands and 
always
+/// rename.
+fn build_naming_struct_literals_if_needed(

Review Comment:
   I don't think there's a cleaner way to handle this while still covering 
every case. I was thinking maybe we could do some logic to rename only if the 
function's output type matches the literal's type. This covers functions that 
output structs like coalesce, but it misses cases like contains functions (that 
return a different type like bool) etc. The current approach covers every case.



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