Skip to content

Commit 52a96bb

Browse files
committed
fix(optimizer): emit bare CSE columns inside window expressions (apache#26620)
1 parent 8ce9f60 commit 52a96bb

2 files changed

Lines changed: 104 additions & 35 deletions

File tree

‎datafusion/optimizer/src/common_subexpr_eliminate.rs‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -816,12 +816,14 @@ impl CSEController for ExprCSEController<'_> {
816816
}
817817

818818
fn rewrite_f_down(&mut self, node: &Expr) {
819-
if matches!(node, Expr::Alias(_)) {
819+
// Window children do not need name-preserving aliases. Such aliases can
820+
// make otherwise identical window sort keys compare unequal.
821+
if matches!(node, Expr::Alias(_) | Expr::WindowFunction(_)) {
820822
self.alias_counter += 1;
821823
}
822824
}
823825
fn rewrite_f_up(&mut self, node: &Expr) {
824-
if matches!(node, Expr::Alias(_)) {
826+
if matches!(node, Expr::Alias(_) | Expr::WindowFunction(_)) {
825827
self.alias_counter -= 1
826828
}
827829
}
@@ -2163,10 +2165,39 @@ mod test {
21632165
plan,
21642166
@ "
21652167
Projection: test.a, test.b, test.c, sum(test.a + test.b) FILTER (WHERE test.c > Int32(0)) PARTITION BY [test.a * test.b] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, sum(test.a + test.b * Int32(2)) FILTER (WHERE test.c > Int32(0)) PARTITION BY [test.a * test.b] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
2166-
WindowAggr: windowExpr=[[sum(test.a + test.b) FILTER (WHERE __common_expr_2 AS test.c > Int32(0)) PARTITION BY [__common_expr_1 AS test.a * test.b] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING AS sum(test.a + test.b) FILTER (WHERE test.c > Int32(0)) PARTITION BY [test.a * test.b] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, sum((test.a + test.b) * Int32(2)) FILTER (WHERE __common_expr_2 AS test.c > Int32(0)) PARTITION BY [__common_expr_1 AS test.a * test.b] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING AS sum(test.a + test.b * Int32(2)) FILTER (WHERE test.c > Int32(0)) PARTITION BY [test.a * test.b] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING]]
2168+
WindowAggr: windowExpr=[[sum(test.a + test.b) FILTER (WHERE __common_expr_2) PARTITION BY [__common_expr_1] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING AS sum(test.a + test.b) FILTER (WHERE test.c > Int32(0)) PARTITION BY [test.a * test.b] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, sum((test.a + test.b) * Int32(2)) FILTER (WHERE __common_expr_2) PARTITION BY [__common_expr_1] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING AS sum(test.a + test.b * Int32(2)) FILTER (WHERE test.c > Int32(0)) PARTITION BY [test.a * test.b] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING]]
21672169
Projection: test.a * test.b AS __common_expr_1, test.c > Int32(0) AS __common_expr_2, test.a, test.b, test.c
21682170
TableScan: test
21692171
"
21702172
)
21712173
}
2174+
2175+
#[test]
2176+
fn test_window_order_by_rewrites_without_alias() -> Result<()> {
2177+
// https://github.com/apache/datafusion/issues/26620
2178+
// Both windows must use the same bare column for their common sort key.
2179+
let table_scan = test_table_scan()?;
2180+
2181+
let window_sum = |arg: Expr| {
2182+
Expr::from(WindowFunction::new(
2183+
WindowFunctionDefinition::AggregateUDF(sum_udaf()),
2184+
vec![arg],
2185+
))
2186+
.order_by(vec![(col("a") + lit(1)).sort(true, false)])
2187+
.build()
2188+
};
2189+
let plan = LogicalPlanBuilder::from(table_scan)
2190+
.window(vec![window_sum(col("b"))?, window_sum(col("c"))?])?
2191+
.build()?;
2192+
2193+
assert_optimized_plan_equal!(
2194+
plan,
2195+
@ "
2196+
Projection: test.a, test.b, test.c, sum(test.b) ORDER BY [test.a + Int32(1) ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, sum(test.c) ORDER BY [test.a + Int32(1) ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
2197+
WindowAggr: windowExpr=[[sum(test.b) ORDER BY [__common_expr_1 ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS sum(test.b) ORDER BY [test.a + Int32(1) ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, sum(test.c) ORDER BY [__common_expr_1 ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS sum(test.c) ORDER BY [test.a + Int32(1) ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
2198+
Projection: test.a + Int32(1) AS __common_expr_1, test.a, test.b, test.c
2199+
TableScan: test
2200+
"
2201+
)
2202+
}
21722203
}

0 commit comments

Comments
 (0)