You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2023/04/12 19:12:28 UTC

[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #5973: Row `AVG` accumulator support Decimal type

alamb commented on code in PR #5973:
URL: https://github.com/apache/arrow-datafusion/pull/5973#discussion_r1164540227


##########
datafusion/physical-expr/src/aggregate/average.rs:
##########
@@ -298,16 +305,40 @@ impl RowAccumulator for AvgRowAccumulator {
     }
 
     fn evaluate(&self, accessor: &RowAccessor) -> Result<ScalarValue> {
-        assert_eq!(self.sum_datatype, DataType::Float64);
-        Ok(match accessor.get_u64_opt(self.state_index()) {
-            None => ScalarValue::Float64(None),
-            Some(0) => ScalarValue::Float64(None),
-            Some(n) => ScalarValue::Float64(
-                accessor
-                    .get_f64_opt(self.state_index() + 1)
-                    .map(|f| f / n as f64),
-            ),
-        })
+        match self.sum_datatype {
+            DataType::Decimal128(p, s) => {
+                match accessor.get_u64_opt(self.state_index()) {
+                    None => Ok(ScalarValue::Decimal128(None, p, s)),
+                    Some(0) => Ok(ScalarValue::Decimal128(None, p, s)),

Review Comment:
   Why do we translate `0` --> `null` here? (it is also done for Float64 below)? 
   
   I see you are just following the existing pattern, but it seems like this could be incorrect?
   
   Maybe we could add a test that calls `AVG` on (`-1` and `1`) to see if we get `0` or `NULL`



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org