kosiew commented on code in PR #26184:
URL: https://github.com/apache/datafusion/pull/26184#discussion_r4248111738


##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -695,6 +698,57 @@ impl CSEController for ExprCSEController<'_> {
                     .chain(else_expr.iter().map(|e| e.as_ref()))
                     .collect(),
             )),
+
+            // An aggregate function with a `FILTER` clause evaluates the 
filter for
+            // every input row, but it evaluates its arguments only for the 
rows that
+            // pass the filter. Extracting an argument subexpression into a 
projection
+            // below the aggregate would evaluate it for the rejected rows 
too, which
+            // can raise an error (for example a failed cast or a division by 
zero)
+            // that the filter must prevent. The `ORDER BY` expressions of the
+            // aggregate are conservatively handled like the arguments.
+            Expr::AggregateFunction(AggregateFunction {
+                params:
+                    AggregateFunctionParams {
+                        args,
+                        filter: Some(filter),
+                        order_by,
+                        ..
+                    },
+                ..
+            }) => Some((
+                vec![filter.as_ref()],
+                args.iter()
+                    .chain(order_by.iter().map(|sort| &sort.expr))
+                    .collect(),
+            )),
+
+            // The same applies to the arguments of a window function with a 
`FILTER`

Review Comment:
   Could we clarify that filtered window arguments are treated conservatively 
by CSE, rather than actually being evaluated only for rows passing the filter? 
`AggregateWindowExpr::get_result_column` currently evaluates arguments before 
applying the filter. Please update this comment and the test comment around 
line 2139 to reflect that distinction.



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