jayzhan211 commented on code in PR #27827:
URL: https://github.com/apache/datafusion/pull/27827#discussion_r4754418783
##########
datafusion/physical-plan/src/aggregates/mod.rs:
##########
@@ -3081,6 +3087,54 @@ fn determine_finer(
}
}
+/// Reverses the expressions in `aggr_expr` that the producer of the state in
+/// `input` has reversed (#27458).
+///
+/// Only the direction is copied: the producer's expressions can carry
+/// settings valid only for its own input, such as `with_beneficial_ordering`.
+fn match_state_direction(
+ aggr_expr: &[Arc<AggregateFunctionExpr>],
+ input: &Arc<dyn ExecutionPlan>,
+) -> Vec<Arc<AggregateFunctionExpr>> {
+ let Some(producer) = find_state_producer(input) else {
+ return aggr_expr.to_vec();
+ };
+ let producer = producer.aggr_expr();
+ if producer.len() != aggr_expr.len() {
+ return aggr_expr.to_vec();
+ }
+ aggr_expr
+ .iter()
+ .zip(producer)
+ .map(|(expr, produced)| {
+ // `first_value` and `last_value` reverse into each other and merge
+ // by comparing orderings, so they need no sync.
Review Comment:
Optional cleanup: no test covers the function-name check. Without it the
Final turns into `last_value(r.x) ORDER BY [r.ts DESC NULLS FIRST]`, while its
output field and the projection above still read `first_value(...)`, and every
test still passes. Could you say that in the comment and add the EXPLAIN below
after Query 2 in `aggregate.slt`? It passes on this head and fails with the
name check removed.
```suggestion
// `first_value` and `last_value` reverse into each other and
merge
// by comparing orderings, so they need no sync. Reversing them
would
// also rename the expression away from its output field.
```
<details>
<summary>sqllogictest: the Final keeps first_value/last_value names after
the Partial swaps them</summary>
```
# first_value and last_value merge by comparing orderings, so the Final keeps
# its names when the Partial reverses them into each other.
query TT
EXPLAIN SELECT first_value(r.x ORDER BY r.ts), last_value(r.x ORDER BY r.ts)
FROM l JOIN r ON l.k = r.ts;
----
logical_plan
01)Aggregate: groupBy=[[]], aggr=[[first_value(r.x) ORDER BY [r.ts ASC NULLS
LAST], last_value(r.x) ORDER BY [r.ts ASC NULLS LAST]]]
02)--Projection: r.ts, r.x
03)----Inner Join: l.k = r.ts
04)------TableScan: l projection=[k]
05)------TableScan: r projection=[ts, x]
physical_plan
01)AggregateExec: mode=Final, gby=[], aggr=[first_value(r.x) ORDER BY [r.ts
ASC NULLS LAST], last_value(r.x) ORDER BY [r.ts ASC NULLS LAST]]
02)--CoalescePartitionsExec
03)----AggregateExec: mode=Partial, gby=[], aggr=[last_value(r.x) ORDER BY
[r.ts DESC NULLS FIRST], first_value(r.x) ORDER BY [r.ts DESC NULLS FIRST]]
04)------RepartitionExec: partitioning=RoundRobinBatch(4),
input_partitions=1, maintains_sort_order=true
05)--------HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(ts@0,
k@0)], projection=[ts@0, x@1]
06)----------DataSourceExec: file_groups={1 group:
[[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/aggregate/nth_r.parquet]]},
projection=[ts, x], output_ordering=[ts@0 ASC NULLS LAST], file_type=parquet
07)----------DataSourceExec: file_groups={1 group:
[[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/aggregate/nth_l.parquet]]},
projection=[k], output_ordering=[k@0 DESC], file_type=parquet,
predicate=DynamicFilter [ empty ], dynamic_rg_pruning=eligible
```
</details>
--
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]