jayzhan211 commented on code in PR #27117:
URL: https://github.com/apache/datafusion/pull/27117#discussion_r4550879334
##########
datafusion/optimizer/src/push_down_filter.rs:
##########
@@ -1132,7 +1134,9 @@ impl OptimizerRule for PushDownFilter {
let mut push_predicates = vec![];
for expr in predicates {
let cols = expr.column_refs();
+ // A false predicate below a grand total still leaves a
result row.
if !expr.is_volatile()
+ && (!cols.is_empty() || !emits_on_empty)
Review Comment:
Optional cleanup: when the aggregate emits on empty input,
`group_expr_columns` never contains an output column (it is empty, or the
single `ROLLUP (...)`-style name), so `!cols.is_empty()` never lets a predicate
through. Saying "push nothing below a grand total" is simpler and matches the
physical side (#22926). Tests and probes give the same results.
```suggestion
// A grand total emits a row even when the predicate
empties its input.
if !expr.is_volatile()
&& !emits_on_empty
```
##########
datafusion/sqllogictest/test_files/push_down_filter_regression.slt:
##########
@@ -4,6 +4,22 @@
# Test push down filter
+# Regression test for https://github.com/apache/datafusion/issues/26545
+# Pushing the filter below COUNT(*) would return one row containing zero.
+query I
+SELECT c FROM (
+ SELECT 'all' AS k, COUNT(*) AS c FROM (VALUES (1)) AS t(a)
+) AS s WHERE k = 'none';
+----
+
+# The empty grouping set in ROLLUP also produces a grand total on empty input.
+query II
+SELECT a, c FROM (
+ SELECT a, 'all' AS k, COUNT(*) AS c FROM (VALUES (1)) AS t(a)
+ GROUP BY ROLLUP(a)
+) AS s WHERE k = 'none';
Review Comment:
Optional: two more shapes from the same bug fail on `main` and pass here, a
HAVING with no column references and the issue's UNION ALL query. Could you add
them?
```suggestion
----
# A HAVING predicate without column references must not filter the
# aggregate's input either.
query I
SELECT COUNT(*) FROM (VALUES (1)) AS t(a)
HAVING (SELECT max(b) FROM (VALUES (5)) AS u(b)) > 100;
----
# The filter reaches each UNION ALL branch; the 'one' branch must not
# leave a row with a count of zero.
query I
SELECT c FROM (
SELECT 'one' AS k, COUNT(*) AS c FROM (VALUES (1), (2)) AS t(a) WHERE a = 1
UNION ALL
SELECT 'all' AS k, COUNT(*) AS c FROM (VALUES (1), (2)) AS t(a)
) AS s WHERE k = 'all';
----
2
```
--
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]