zhuqi-lucas commented on code in PR #27450:
URL: https://github.com/apache/datafusion/pull/27450#discussion_r4779539175


##########
datafusion/physical-optimizer/src/pushdown_sort.rs:
##########
@@ -177,7 +182,53 @@ fn pushdown_sorts(
             }
         }
 
-        // Pattern 2: Standalone SortExec (no SPM parent)
+        // Pattern 2: Ungrouped single-column MIN/MAX AggregateExec.
+        // Push down sort order so the scan reads files and row groups with
+        // the largest (or smallest) values first for early dynamic filter 
pruning.
+        if config.optimizer.enable_dynamic_filter_pushdown
+            && config.optimizer.enable_aggregate_dynamic_filter_pushdown
+            && let Some(agg) = plan.downcast_ref::<AggregateExec>()
+            && !agg.dynamic_expressions_produced().is_empty()
+            && agg.group_expr().is_true_no_grouping()
+            // Multiple aggregates (e.g. MIN(a), MAX(b)) may need different 
sort orders
+            && let [aggr_expr] = agg.aggr_expr()
+            && let [arg] = aggr_expr.expressions().as_slice()
+            // Expressions like MAX(-ts) or MAX(a + b) may not match the 
column sort order
+            && arg.is::<Column>()
+            && !input_has_fetch(agg.input())?
+        {
+            let fun_name = aggr_expr.fun().name();
+            let descending = if fun_name.eq_ignore_ascii_case("max") {
+                Some(true)
+            } else if fun_name.eq_ignore_ascii_case("min") {
+                Some(false)
+            } else {
+                None
+            };
+            if let Some(descending) = descending {
+                let sort_expr = PhysicalSortExpr::new(
+                    Arc::clone(arg),
+                    SortOptions {
+                        descending,
+                        // MIN/MAX ignore NULLs, so always push NULLs last
+                        nulls_first: false,
+                    },
+                );
+                match agg.input().try_pushdown_sort(&[sort_expr])? {

Review Comment:
   With `enable_exact_reverse_scan` on and a non-null column stored ASC, the 
parquet source answers `Exact` here and sets `reverse_rows`, so each row group 
it reads is buffered whole to reverse its rows. MAX only needs the file and row 
group visiting order.
   
   I checked it locally: `SELECT MAX(ts) FROM t WHERE val > 0` gets 
`reverse_rows=true`, and only `reverse_row_groups=true` with the option off. 
Could this path avoid the exact row reversal, so it keeps the row group reorder 
without buffering?



##########
datafusion/physical-optimizer/src/pushdown_sort.rs:
##########
@@ -177,7 +182,53 @@ fn pushdown_sorts(
             }
         }
 
-        // Pattern 2: Standalone SortExec (no SPM parent)
+        // Pattern 2: Ungrouped single-column MIN/MAX AggregateExec.

Review Comment:
   A further idea, here or as a follow-up: with a `WHERE`, a row group whose 
statistics show every row passes the filter (fully matched, which the pruner 
already detects) has its `MAX(ts)` equal to its `ts` max statistic when that 
statistic is exact, so it wouldn't need decoding at all. `aggregate_statistics` 
can't help once there is a filter because the file-level stats are no longer 
exact, but per row group the fully matched ones could answer from statistics, 
and only partially matched ones would be decoded.



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