zhuqi-lucas commented on code in PR #27450:
URL: https://github.com/apache/datafusion/pull/27450#discussion_r4779539180
##########
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();
Review Comment:
nit: the same name check (and the HACK for #18643) is in `AggregateExec`'s
dynamic filter setup; a shared helper would keep the two in sync.
##########
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 every row group
is decoded and buffered 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?
--
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]