You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2025/09/26 21:10:13 UTC

(datafusion) branch main updated: display window function's alias name in output (#17788)

This is an automated email from the ASF dual-hosted git repository.

github-bot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 1607561aab display window function's alias name in output (#17788)
1607561aab is described below

commit 1607561aab10c2dc5a3f809942e9d65b4f206e3c
Author: Devam Patel <11...@users.noreply.github.com>
AuthorDate: Sat Sep 27 02:39:50 2025 +0530

    display window function's alias name in output (#17788)
---
 .../user_defined/user_defined_window_functions.rs  | 32 +++++++++++-----------
 datafusion/sql/src/expr/function.rs                | 22 +++++++++++++--
 2 files changed, 35 insertions(+), 19 deletions(-)

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org