AdamGS commented on code in PR #22714:
URL: https://github.com/apache/datafusion/pull/22714#discussion_r3560629731
##########
datafusion/functions-aggregate/src/average.rs:
##########
@@ -125,6 +128,85 @@ impl Default for Avg {
}
}
+/// Digits reserved above the input precision for `avg`'s intermediate sum: 4
for
+/// the scale-up [`DecimalAverager`] applies before dividing
(`Avg::return_type`
+/// adds 4 to the scale), 9 for the row count.
+///
+/// The 9 is a row budget. A sum of `n` rows of `Decimal(p, _)` is bounded by
+/// `n * 10^p`, so a sum type with `p + 4 + 9` digits holds `10^9` rows. The
sum
+/// wraps on overflow, like the `sum` aggregate; the budget is what puts that
out
+/// of reach. `Decimal256` input near max precision is the exception: no wider
+/// type exists, so its sum keeps only whatever headroom `Decimal256(76, _)`
has
+/// left, as before this budget was introduced.
+const AVG_SUM_HEADROOM_DIGITS: u8 = 4 + 9;
Review Comment:
This seems like a reasonable heuristic to me, while keeping reasonable
performance in most cases. The alternative here would be to always use i256 for
the intermediate sum which is much slower.
--
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]