You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/08/18 12:34:33 UTC

[GitHub] [arrow] alamb commented on a change in pull request #7984: ARROW-9779: [Rust] [DataFusion] Increase stability of average accumulator

alamb commented on a change in pull request #7984:
URL: https://github.com/apache/arrow/pull/7984#discussion_r472141713



##########
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:
       isn't this calculation still subject to overflow? 




----------------------------------------------------------------
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:
users@infra.apache.org