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


##########
datafusion/sql/src/expr/function.rs:
##########
@@ -270,7 +271,27 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
         // User-defined function (UDF) should have precedence
         if let Some(fm) = self.context_provider.get_function_meta(&name) {
             let args = self.function_args_to_expr(args, schema, 
planner_context)?;
-            return Ok(Expr::ScalarFunction(ScalarFunction::new_udf(fm, args)));
+            let scalar_func_expr = 
Expr::ScalarFunction(ScalarFunction::new_udf(
+                Arc::clone(&fm),
+                args.clone(),
+            ));
+
+            if name.eq_ignore_ascii_case(fm.name()) {
+                return Ok(scalar_func_expr);
+            } else {
+                let arg_names = args
+                    .iter()
+                    .map(|arg| arg.to_string())
+                    .collect::<Vec<_>>()
+                    .join(",");
+                let verbose_alias = format!("{name}({arg_names})");
+
+                return Ok(Expr::Alias(expr::Alias::new(
+                    scalar_func_expr,
+                    None::<datafusion_common::TableReference>,
+                    verbose_alias,
+                )));
+            }

Review Comment:
   ```suggestion
               let inner = ScalarFunction::new_udf(fm, args);
   
               if name.eq_ignore_ascii_case(inner.name()) {
                   return Ok(Expr::ScalarFunction(inner));
               } else {
                   let arg_names = inner
                       .args
                       .iter()
                       .map(|arg| arg.to_string())
                       .collect::<Vec<_>>()
                       .join(",");
                   let verbose_alias = format!("{name}({arg_names})");
   
                   return Ok(Expr::ScalarFunction(inner).alias(verbose_alias));
               }
   ```
   
   Two things here:
   
   - Can remove need to clone `args` by structuring it this way without needing 
as much duplication
   - Can utilize `Expr::alias` for simplification
   
   Also would be a good idea to put a comment here explaining why this logic is 
being done
   
   Same goes for the aggregate function part too



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