Skip to content

Commit b3004e5

Browse files
committed
Clarify that CSE treats filtered window arguments conservatively
1 parent 4e24df6 commit b3004e5

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

‎datafusion/optimizer/src/common_subexpr_eliminate.rs‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -722,9 +722,14 @@ impl CSEController for ExprCSEController<'_> {
722722
.collect(),
723723
)),
724724

725-
// The same applies to the arguments of a window function with a `FILTER`
726-
// clause. The `PARTITION BY` and `ORDER BY` expressions of the window are
727-
// evaluated for every input row.
725+
// The arguments of a window function with a `FILTER` clause are
726+
// conservatively handled like the arguments of a filtered aggregate.
727+
// The window executor does not guarantee the same today:
728+
// `AggregateWindowExpr::get_result_column` evaluates the arguments
729+
// before it applies the filter. This rule must not rely on that order,
730+
// so it does not extract a subexpression that occurs only in such
731+
// arguments. The `PARTITION BY` and `ORDER BY` expressions of the
732+
// window are evaluated for every input row.
728733
Expr::WindowFunction(window_function) => {
729734
let WindowFunction {
730735
params:
@@ -2136,8 +2141,10 @@ mod test {
21362141
fn test_window_filter_arguments() -> Result<()> {
21372142
let table_scan = test_table_scan()?;
21382143

2139-
// The arguments of a window function with a `FILTER` are conditionally
2140-
// evaluated. The `PARTITION BY` expressions and the filter are not.
2144+
// CSE conservatively treats the arguments of a window function with a
2145+
// `FILTER` as conditionally evaluated, although the window executor
2146+
// evaluates them before it applies the filter today. The `PARTITION BY`
2147+
// expressions and the filter are evaluated for every input row.
21412148
let window_sum = |arg: Expr| {
21422149
Expr::from(WindowFunction::new(
21432150
WindowFunctionDefinition::AggregateUDF(sum_udaf()),

0 commit comments

Comments
 (0)