timsaucer commented on code in PR #24887:
URL: https://github.com/apache/datafusion/pull/24887#discussion_r3918762773


##########
datafusion/physical-expr/src/aggregate.rs:
##########
@@ -933,24 +933,44 @@ impl AggregateFunctionExpr {
     /// For aggregates that do not support calculation in reverse,
     /// returns None (which is the default value).
     pub fn reverse_expr(&self) -> Option<AggregateFunctionExpr> {
+        self.reverse_expr_inner(false)
+    }
+
+    /// Same as [`Self::reverse_expr`], but the output `name` is always carried
+    /// over unchanged.
+    ///
+    /// Window execs derive their output schema from `WindowExpr::field()`, 
which
+    /// for aggregate-backed window expressions is this expression's `name`. If
+    /// reversal renamed it, the rebuilt window exec would expose a differently
+    /// named column while parent plan nodes still reference the old one. This
+    /// mirrors `WindowUDFExpr::reverse_expr`, which preserves its name for the
+    /// same reason. `AggregateExec`, by contrast, pins its schema at
+    /// construction, so it can use [`Self::reverse_expr`] and let the name
+    /// reflect the function actually being evaluated.
+    pub fn reverse_expr_preserving_name(&self) -> 
Option<AggregateFunctionExpr> {
+        self.reverse_expr_inner(true)
+    }
+
+    fn reverse_expr_inner(&self, preserve_name: bool) -> 
Option<AggregateFunctionExpr> {
         match self.fun.reverse_udf() {
             ReversedUDAF::NotSupported => None,
             ReversedUDAF::Identical => Some(self.clone()),
             ReversedUDAF::Reversed(reverse_udf) => {
-                let was_aliased = self.human_display_alias().is_some();
+                let keep_name = preserve_name || 
self.human_display_alias().is_some();

Review Comment:
   This is really the crux of this PR. There is a bit of refactoring to take 
common code from `datafusion/physical-expr/src/window/sliding_aggregate.rs` and 
also `datafusion/physical-expr/src/window/aggregate.rs` so that they treat it 
identically. Then we just need to pass around that boolean that says whether or 
not to preserver the name after rewriting. 



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