Apache DataFusion SQL Query Engine
fix: keep volatile filters above aggregates (#27076)
Phoenix173f1263431a
diff --git a/datafusion/optimizer/src/push_down_filter.rs b/datafusion/optimizer/src/push_down_filter.rsindex ab3ee7a68b..27744dbfb0 100644--- a/datafusion/optimizer/src/push_down_filter.rs+++ b/datafusion/optimizer/src/push_down_filter.rs@@ -1132,7 +1132,9 @@ impl OptimizerRule for PushDownFilter {let mut push_predicates = vec![];for expr in predicates {let cols = expr.column_refs();- if cols.iter().all(|c| group_expr_columns.contains(c)) {+ if !expr.is_volatile()+ && cols.iter().all(|c| group_expr_columns.contains(c))+ {push_predicates.push(replace_cols_by_column(expr, &replace_map)?);} else {keep_predicates.push(expr);diff --git a/datafusion/sqllogictest/test_files/push_down_filter_regression.slt b/datafusion/sqllogictest/test_files/push_down_filter_regression.sltindex bca5361a6d..dd3ae49373 100644--- a/datafusion/sqllogictest/test_files/push_down_filter_regression.slt+++ b/datafusion/sqllogictest/test_files/push_down_filter_regression.slt@@ -717,14 +717,29 @@ SELECT evaluations();----10+statement ok+set datafusion.explain.logical_plan_only = true;++# Regression for #27074: evaluate random() once per group, while pushing+# the deterministic predicate on the grouping key below the aggregate.+query TT+EXPLAIN SELECT * FROM (+ SELECT column1, COUNT(*) AS c FROM (VALUES (1), (1), (2)) GROUP BY column1+)+WHERE column1 = 1 AND random() < 0.5;+----+logical_plan+01)Projection: column1, count(Int64(1)) AS c+02)--Filter: random() < Float64(0.5)+03)----Aggregate: groupBy=[[column1]], aggr=[[count(Int64(1))]]+04)------Filter: column1 = Int64(1)+05)--------Values: (Int64(1)), (Int64(1)), (Int64(2))+# Window filter pushdown over expression PARTITION BY keys.## A predicate that reads only the partition keys is constant within a partition,# so it is pushed below the window; anything else stays above it.-statement ok-set datafusion.explain.logical_plan_only = true;-statement okcreate table window_expr_key(k varchar, v int) as values('a', 1), ('a', 2), ('a', 3),