Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions datafusion-examples/examples/date_time_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,14 @@ async fn query_to_char() -> Result<()> {

assert_batches_eq!(
&[
"+------------------------------+",
"| to_char(t.values,t.patterns) |",
"+------------------------------+",
"| 2020-09-01 |",
"| 2020:09:02 |",
"| 20200903 |",
"| 04-09-2020 |",
"+------------------------------+",
"+----------------------------------+",
"| date_format(t.values,t.patterns) |",
"+----------------------------------+",
"| 2020-09-01 |",
"| 2020:09:02 |",
"| 20200903 |",
"| 04-09-2020 |",
"+----------------------------------+",
],
&result
);
Expand Down
14 changes: 7 additions & 7 deletions datafusion/core/tests/user_defined/user_defined_aggregates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,13 @@ async fn test_user_defined_functions_with_alias() -> Result<()> {

let alias_result = plan_and_collect(&ctx, "SELECT dummy_alias(i) FROM t").await?;

insta::assert_snapshot!(batches_to_string(&alias_result), @r###"
+------------+
| dummy(t.i) |
+------------+
| 1.0 |
+------------+
"###);
insta::assert_snapshot!(batches_to_string(&alias_result), @r"
+------------------+
| dummy_alias(t.i) |
+------------------+
| 1.0 |
+------------------+
");

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,13 @@ async fn test_user_defined_functions_with_alias() -> Result<()> {
"###);

let alias_result = plan_and_collect(&ctx, "SELECT dummy_alias(i) FROM t").await?;
insta::assert_snapshot!(batches_to_string(&alias_result), @r###"
+------------+
| dummy(t.i) |
+------------+
| 1 |
+------------+
"###);
insta::assert_snapshot!(batches_to_string(&alias_result), @r"
+------------------+
| dummy_alias(t.i) |
+------------------+
| 1 |
+------------------+
");

Ok(())
}
Expand Down
41 changes: 38 additions & 3 deletions datafusion/sql/src/expr/function.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we do the same for WindowFunction?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think so -- I added a note to the ticket

Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,24 @@ 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 inner = ScalarFunction::new_udf(fm, args);

if name.eq_ignore_ascii_case(inner.name()) {
return Ok(Expr::ScalarFunction(inner));
} else {
// If the function is called by an alias, a verbose string representation is created
// (e.g., "my_alias(arg1, arg2)") and the expression is wrapped in an `Alias`
// to ensure the output column name matches the user's query.
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));
}
}

// Build Unnest expression
Expand Down Expand Up @@ -472,14 +489,32 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
null_treatment,
} = aggregate_expr;

return Ok(Expr::AggregateFunction(expr::AggregateFunction::new_udf(
let inner = expr::AggregateFunction::new_udf(
func,
args,
distinct,
filter,
order_by,
null_treatment,
)));
);

if name.eq_ignore_ascii_case(inner.func.name()) {
return Ok(Expr::AggregateFunction(inner));
} else {
// If the function is called by an alias, a verbose string representation is created
// (e.g., "my_alias(arg1, arg2)") and the expression is wrapped in an `Alias`
// to ensure the output column name matches the user's query.
let arg_names = inner
.params
.args
.iter()
.map(|arg| arg.to_string())
.collect::<Vec<_>>()
.join(",");
let verbose_alias = format!("{name}({arg_names})");

return Ok(Expr::AggregateFunction(inner).alias(verbose_alias));
}
}
}

Expand Down