Skip to content

Apache DataFusion SQL Query Engine

display window function's alias name in output (#17788)

Devam Patel1607561aab10
diff --git a/datafusion/core/tests/user_defined/user_defined_window_functions.rs b/datafusion/core/tests/user_defined/user_defined_window_functions.rs
index b3542f4da8..555b57fbe6 100644
--- a/datafusion/core/tests/user_defined/user_defined_window_functions.rs
+++ b/datafusion/core/tests/user_defined/user_defined_window_functions.rs
@@ -145,22 +145,22 @@ async fn test_udwf_with_alias() {
.await
.unwrap();
- insta::assert_snapshot!(batches_to_string(&actual), @r###"
- +---+---+-----+-----------------------------------------------------------------------------------------------------------------------+
- | x | y | val | odd_counter(t.val) PARTITION BY [t.x] ORDER BY [t.y ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
- +---+---+-----+-----------------------------------------------------------------------------------------------------------------------+
- | 1 | a | 0 | 1 |
- | 1 | b | 1 | 1 |
- | 1 | c | 2 | 1 |
- | 2 | d | 3 | 2 |
- | 2 | e | 4 | 2 |
- | 2 | f | 5 | 2 |
- | 2 | g | 6 | 2 |
- | 2 | h | 6 | 2 |
- | 2 | i | 6 | 2 |
- | 2 | j | 6 | 2 |
- +---+---+-----+-----------------------------------------------------------------------------------------------------------------------+
- "###);
+ insta::assert_snapshot!(batches_to_string(&actual), @r"
+ +---+---+-----+--------------------------+
+ | x | y | val | odd_counter_alias(t.val) |
+ +---+---+-----+--------------------------+
+ | 1 | a | 0 | 1 |
+ | 1 | b | 1 | 1 |
+ | 1 | c | 2 | 1 |
+ | 2 | d | 3 | 2 |
+ | 2 | e | 4 | 2 |
+ | 2 | f | 5 | 2 |
+ | 2 | g | 6 | 2 |
+ | 2 | h | 6 | 2 |
+ | 2 | i | 6 | 2 |
+ | 2 | j | 6 | 2 |
+ +---+---+-----+--------------------------+
+ ");
}
/// Basic user defined window function with bounded window
diff --git a/datafusion/sql/src/expr/function.rs b/datafusion/sql/src/expr/function.rs
index 143324e374..a61967ed69 100644
--- a/datafusion/sql/src/expr/function.rs
+++ b/datafusion/sql/src/expr/function.rs
@@ -398,7 +398,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
distinct,
} = window_expr;
- let expr = Expr::from(WindowFunction {
+ let inner = WindowFunction {
fun: func_def,
params: expr::WindowFunctionParams {
args,
@@ -409,9 +409,25 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
null_treatment,
distinct,
},
- });
+ };
- return Ok(expr);
+ if name.eq_ignore_ascii_case(inner.fun.name()) {
+ return Ok(Expr::WindowFunction(Box::new(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::WindowFunction(Box::new(inner)).alias(verbose_alias));
+ }
}
} else {
// User defined aggregate functions (UDAF) have precedence in case it has the same name as a scalar built-in function