Skip to main content

Module numeric_sum

Module numeric_sum 

Source
Expand description

Exact SUM / AVG accumulation, shared by every aggregate path.

The rule, applied the same way everywhere:

  • An integer input adds exactly into an i128. A u64 above i64::MAX and an integral Decimal are integer inputs.
  • A fractional Decimal adds exactly into a Decimal.
  • A float input adds into a Neumaier-compensated f64. Once the float total is not finite, compensation stops and the total stays as IEEE arithmetic gives it: an overflow is Infinity, Infinity plus -Infinity is NaN, as in PostgreSQL.
  • A numeric string is the number it spells: an integer, or a Decimal when it has a fraction. A DECIMAL cell is stored as its text, so this keeps a DECIMAL column exact.
  • SUM with only integer inputs is an Integer when the total fits i64. A larger total is a Decimal.
  • SUM with a fractional Decimal input and no float input is a Decimal: the exact integer part plus the exact decimal part.
  • An exact total past the Decimal range is EvalError::NumericOverflow.
  • SUM with at least one float input is a Float: the exact parts plus the float part.
  • AVG is a Float. With no float input it divides the exact total by the count, with no rounding of the total first.
  • SUM and AVG over no input are NULL.
  • Two partial accumulators merge without loss: the integer parts add exactly, so a shard or spill merge gives the single-pass result.

STDDEV and VARIANCE stay in f64 (Welford); they are not exact sums.

Structs§

ExactSum
Running exact SUM / AVG state. See the module docs for the rule.

Functions§

numeric_text
The number a numeric string spells, exactly where it can be: an integer as [numeric_to_value] gives it, a fraction in decimal notation as a Decimal, and any other numeric text (an exponent, a fraction past the Decimal precision) as a Float. None for non-numeric text.
sum_input
A SUM / AVG argument value as the number it contributes: a number as itself, a numeric string as the number it spells (see numeric_text). None for any other value, which contributes nothing.