jorgecarleitao commented on a change in pull request #7984:
URL: https://github.com/apache/arrow/pull/7984#discussion_r472386086
##########
File path: rust/datafusion/src/execution/physical_plan/expressions.rs
##########
@@ -337,40 +336,86 @@ impl AggregateExpr for Avg {
}
}
-macro_rules! avg_accumulate {
+macro_rules! avg_accumulate_scalar {
($SELF:ident, $VALUE:expr, $ARRAY_TYPE:ident) => {{
- match ($SELF.sum, $SELF.count) {
- (Some(sum), Some(count)) => {
- $SELF.sum = Some(sum + $VALUE as f64);
+ // details here: https://stackoverflow.com/a/23493727/931303
+ match ($SELF.avg, $SELF.count) {
+ (Some(avg), Some(count)) => {
$SELF.count = Some(count + 1);
+ let online = (avg * (count as f64) + $VALUE as f64) / (count +
1) as f64;
+ $SELF.avg = Some(online);
}
_ => {
- $SELF.sum = Some($VALUE as f64);
$SELF.count = Some(1);
+ $SELF.avg = Some($VALUE as f64);
}
};
}};
}
+
+macro_rules! avg_accumulate_array {
+ ($SELF:ident, $ARRAY:expr, $ARRAY_TYPE:ident) => {{
+ // details here: https://stackoverflow.com/a/23493727/931303
+ let m = $ARRAY.len();
+ let sum =
compute::sum($ARRAY.as_any().downcast_ref::<$ARRAY_TYPE>().unwrap());
Review comment:
It is less likely to overflow over a single record batch than over a the
set of all record batches in a single partition.
I am taking a text book authoritative argument here, as I do not have
sufficient knowledge to take this debate to its details. I can see that spark
[uses
sum,count](https://github.com/apache/spark/blob/5264164a67df498b73facae207eda12ee133be7d/examples/src/main/java/org/apache/spark/examples/sql/JavaUserDefinedTypedAggregation.java#L66),
so that is another argument (for what is worth).
No specific preference here. I implemented it as an example of an aggregate
UDF in one of my other PRs, and thought that it could be useful here.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]