nodedb-query 0.6.2

Shared query evaluation engine for NodeDB — expressions, filters, functions, aggregations, window functions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
// SPDX-License-Identifier: Apache-2.0

//! 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.

use nodedb_types::Value;
use rust_decimal::Decimal;
use rust_decimal::prelude::ToPrimitive;

use crate::expr::EvalError;
use crate::numeric_cmp::{Numeric, parse_numeric_str};

/// Running exact SUM / AVG state. See the module docs for the rule.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ExactSum {
    /// Exact total of the integer inputs.
    int: i128,
    /// Exact total of the fractional `Decimal` inputs.
    frac: Decimal,
    /// At least one fractional `Decimal` input was added.
    has_decimal: bool,
    /// An exact total (`int` or `frac`) left its range.
    exact_overflow: bool,
    /// Running total of the float inputs.
    float: f64,
    /// Neumaier compensation term: the rounding error lost from `float`.
    /// The float total is `float + comp` while `float` is finite.
    comp: f64,
    /// At least one float input was added.
    has_float: bool,
    /// Number of inputs added.
    count: u64,
}

impl ExactSum {
    pub fn new() -> Self {
        Self::default()
    }

    /// Number of inputs added.
    pub fn count(&self) -> u64 {
        self.count
    }

    /// Add one integer input.
    pub fn add_i64(&mut self, v: i64) {
        self.add_int(i128::from(v));
    }

    /// Add one unsigned integer input.
    pub fn add_u64(&mut self, v: u64) {
        self.add_int(i128::from(v));
    }

    /// Add one float input.
    pub fn add_f64(&mut self, v: f64) {
        self.float_add(v);
        self.has_float = true;
        self.count += 1;
    }

    /// Add a numeric `Value`: `Integer`, `Float`, or `Decimal`. Any other
    /// value adds nothing. Returns whether the value was added.
    pub fn add_value(&mut self, v: &Value) -> bool {
        match v {
            Value::Integer(i) => self.add_i64(*i),
            Value::Float(f) => self.add_f64(*f),
            Value::Decimal(d) => self.add_decimal(*d),
            _ => return false,
        }
        true
    }

    /// Add one `Decimal` input: an integral one into the integer total, any
    /// other into the exact decimal total.
    pub fn add_decimal(&mut self, d: Decimal) {
        if d.fract().is_zero()
            && let Some(i) = d.to_i128()
        {
            self.add_int(i);
            return;
        }
        match self.frac.checked_add(d) {
            Some(total) => self.frac = total,
            None => self.exact_overflow = true,
        }
        self.has_decimal = true;
        self.count += 1;
    }

    /// Fold another partial accumulator into this one without loss.
    pub fn merge(&mut self, other: &ExactSum) {
        match self.int.checked_add(other.int) {
            Some(total) => self.int = total,
            None => self.exact_overflow = true,
        }
        match self.frac.checked_add(other.frac) {
            Some(total) => self.frac = total,
            None => self.exact_overflow = true,
        }
        self.exact_overflow |= other.exact_overflow;
        self.has_decimal |= other.has_decimal;
        self.float_add(other.float);
        if self.float.is_finite() {
            self.comp += other.comp;
        }
        self.has_float |= other.has_float;
        self.count += other.count;
    }

    /// SUM of the inputs. NULL for no input.
    pub fn sum(&self) -> Result<Value, EvalError> {
        if self.count == 0 {
            return Ok(Value::Null);
        }
        if self.exact_overflow {
            return Err(EvalError::NumericOverflow { function: "sum" });
        }
        if self.has_float {
            return Ok(Value::Float(self.sum_f64()));
        }
        if self.has_decimal {
            return self
                .exact_decimal_total()
                .map(Value::Decimal)
                .ok_or(EvalError::NumericOverflow { function: "sum" });
        }
        if let Ok(i) = i64::try_from(self.int) {
            return Ok(Value::Integer(i));
        }
        Decimal::try_from_i128_with_scale(self.int, 0)
            .map(Value::Decimal)
            .map_err(|_| EvalError::NumericOverflow { function: "sum" })
    }

    /// The total read as an `f64`, for a float-typed result. Rounds an
    /// exact total above 2^53; [`Self::sum`] keeps it exact. `0.0` for no
    /// input. A float total that is not finite (`Infinity`, `-Infinity`,
    /// NaN) is the result alone.
    pub fn sum_f64(&self) -> f64 {
        if !self.float.is_finite() {
            return self.float;
        }
        self.int as f64 + self.frac.as_f64() + (self.float + self.comp)
    }

    /// The integer and decimal totals as one `Decimal`. `None` past the
    /// `Decimal` range.
    fn exact_decimal_total(&self) -> Option<Decimal> {
        Decimal::try_from_i128_with_scale(self.int, 0)
            .ok()?
            .checked_add(self.frac)
    }

    /// AVG of the inputs as a `Float`. NULL for no input.
    pub fn avg(&self) -> Result<Value, EvalError> {
        match self.avg_f64()? {
            Some(avg) => Ok(Value::Float(avg)),
            None => Ok(Value::Null),
        }
    }

    /// AVG of the inputs as an `f64`. `None` for no input.
    pub fn avg_f64(&self) -> Result<Option<f64>, EvalError> {
        if self.count == 0 {
            return Ok(None);
        }
        if self.exact_overflow {
            return Err(EvalError::NumericOverflow { function: "avg" });
        }
        let n = i128::from(self.count);
        let avg = if self.has_float {
            self.sum_f64() / self.count as f64
        } else if self.has_decimal {
            // The exact total divides as a `Decimal`, so only the quotient
            // rounds.
            let total = self
                .exact_decimal_total()
                .ok_or(EvalError::NumericOverflow { function: "avg" })?;
            total
                .checked_div(Decimal::from(self.count))
                .and_then(|avg| avg.to_f64())
                .ok_or(EvalError::NumericOverflow { function: "avg" })?
        } else {
            // Quotient and remainder keep the exact total: no rounding of a
            // total above 2^53 before the division.
            (self.int / n) as f64 + (self.int % n) as f64 / self.count as f64
        };
        Ok(Some(avg))
    }

    fn add_int(&mut self, v: i128) {
        match self.int.checked_add(v) {
            Some(total) => self.int = total,
            None => self.exact_overflow = true,
        }
        self.count += 1;
    }

    /// Neumaier summation step. A total that is not finite takes no
    /// compensation: `Infinity` and NaN propagate as IEEE arithmetic gives
    /// them.
    fn float_add(&mut self, v: f64) {
        let t = self.float + v;
        if t.is_finite() {
            if self.float.abs() >= v.abs() {
                self.comp += (self.float - t) + v;
            } else {
                self.comp += (v - t) + self.float;
            }
        }
        self.float = t;
    }
}

/// 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.
pub fn sum_input(v: &Value) -> Option<Value> {
    match v {
        Value::Integer(_) | Value::Float(_) | Value::Decimal(_) => Some(v.clone()),
        Value::String(s) => numeric_text(s),
        _ => None,
    }
}

/// 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.
pub fn numeric_text(s: &str) -> Option<Value> {
    parse_numeric_str(s).map(numeric_to_value)
}

/// A numeric reading as a `Value`. An integer past `i64` is a `Decimal`.
/// An integer past the `Decimal` range is the nearest `Float`, the same
/// reading `f64` parsing gives it.
pub(crate) fn numeric_to_value(n: Numeric) -> Value {
    match n {
        Numeric::Int(i) => match i64::try_from(i) {
            Ok(small) => Value::Integer(small),
            Err(_) => Decimal::try_from_i128_with_scale(i, 0)
                .map(Value::Decimal)
                .unwrap_or(Value::Float(i as f64)),
        },
        Numeric::Decimal(d) => Value::Decimal(d),
        Numeric::Float(f) => Value::Float(f),
    }
}

/// Element count of the MessagePack form: the array `[int_hi, int_lo, frac,
/// has_decimal, exact_overflow, float, comp, has_float, count]`. The `i128`
/// total is split into its high `i64` and low `u64` halves, and `frac` is
/// the 16-byte `Decimal` serialization, so both round-trip exactly.
const MSGPACK_FIELDS: usize = 9;

impl zerompk::ToMessagePack for ExactSum {
    fn write<W: zerompk::Write>(&self, writer: &mut W) -> zerompk::Result<()> {
        writer.write_array_len(MSGPACK_FIELDS)?;
        writer.write_i64((self.int >> 64) as i64)?;
        writer.write_u64(self.int as u64)?;
        writer.write_binary(&self.frac.serialize())?;
        writer.write_boolean(self.has_decimal)?;
        writer.write_boolean(self.exact_overflow)?;
        writer.write_f64(self.float)?;
        writer.write_f64(self.comp)?;
        writer.write_boolean(self.has_float)?;
        writer.write_u64(self.count)
    }
}

impl<'a> zerompk::FromMessagePack<'a> for ExactSum {
    fn read<R: zerompk::Read<'a>>(reader: &mut R) -> zerompk::Result<Self> {
        reader.check_array_len(MSGPACK_FIELDS)?;
        let hi = reader.read_i64()?;
        let lo = reader.read_u64()?;
        let frac_bytes = reader.read_binary()?;
        let frac_bytes: [u8; 16] = frac_bytes
            .as_ref()
            .try_into()
            .map_err(|_| zerompk::Error::BufferTooSmall)?;
        Ok(Self {
            int: (i128::from(hi) << 64) | i128::from(lo),
            frac: Decimal::deserialize(frac_bytes),
            has_decimal: reader.read_boolean()?,
            exact_overflow: reader.read_boolean()?,
            float: reader.read_f64()?,
            comp: reader.read_f64()?,
            has_float: reader.read_boolean()?,
            count: reader.read_u64()?,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const ABOVE: i64 = 9_007_199_254_740_993;
    const AT: i64 = 9_007_199_254_740_992;

    fn sum_of(values: &[Value]) -> Result<Value, EvalError> {
        let mut acc = ExactSum::new();
        for v in values {
            acc.add_value(v);
        }
        acc.sum()
    }

    #[test]
    fn integers_past_two_pow_53_sum_exactly() {
        let total = sum_of(&[Value::Integer(ABOVE), Value::Integer(AT)]).unwrap();
        assert_eq!(total, Value::Integer(18_014_398_509_481_985));
    }

    #[test]
    fn nanosecond_timestamps_sum_exactly() {
        let t = [
            Value::Integer(1_700_000_000_000_000_001),
            Value::Integer(1_700_000_000_000_000_002),
        ];
        assert_eq!(
            sum_of(&t).unwrap(),
            Value::Integer(3_400_000_000_000_000_003)
        );
    }

    #[test]
    fn total_past_i64_is_decimal() {
        let total = sum_of(&[Value::Integer(i64::MAX), Value::Integer(i64::MAX)]).unwrap();
        assert_eq!(
            total,
            Value::Decimal(Decimal::from_i128_with_scale(2 * i128::from(i64::MAX), 0))
        );
    }

    #[test]
    fn u64_above_i64_max_sums_exactly() {
        let mut acc = ExactSum::new();
        acc.add_u64(u64::MAX);
        acc.add_value(&Value::from_u64(u64::MAX));
        assert_eq!(
            acc.sum().unwrap(),
            Value::Decimal(Decimal::from_i128_with_scale(2 * i128::from(u64::MAX), 0))
        );
        let mut small = ExactSum::new();
        small.add_u64(u64::MAX);
        small.add_i64(-1);
        small.add_i64(i64::MIN);
        assert_eq!(small.sum().unwrap(), Value::Integer(i64::MAX - 1));
    }

    #[test]
    fn total_past_decimal_range_is_overflow_error() {
        let mut acc = ExactSum::new();
        // Four times 2^94 is 2^96, past the 96-bit `Decimal` mantissa.
        for _ in 0..4 {
            acc.add_int(1i128 << 94);
        }
        assert_eq!(
            acc.sum(),
            Err(EvalError::NumericOverflow { function: "sum" })
        );
    }

    #[test]
    fn i128_overflow_is_overflow_error() {
        let mut acc = ExactSum::new();
        acc.add_int(i128::MAX);
        acc.add_int(1);
        assert_eq!(
            acc.sum(),
            Err(EvalError::NumericOverflow { function: "sum" })
        );
        assert_eq!(
            acc.avg(),
            Err(EvalError::NumericOverflow { function: "avg" })
        );
    }

    #[test]
    fn mixed_int_float_is_float() {
        let total = sum_of(&[Value::Integer(2), Value::Float(0.5)]).unwrap();
        assert_eq!(total, Value::Float(2.5));
        let total = sum_of(&[Value::Integer(ABOVE), Value::Float(1.0)]).unwrap();
        assert_eq!(total, Value::Float(ABOVE as f64 + 1.0));
    }

    #[test]
    fn nan_input_makes_float_nan() {
        let total = sum_of(&[Value::Integer(1), Value::Float(f64::NAN)]).unwrap();
        assert!(matches!(total, Value::Float(f) if f.is_nan()));
    }

    #[test]
    fn empty_is_null() {
        let acc = ExactSum::new();
        assert_eq!(acc.sum().unwrap(), Value::Null);
        assert_eq!(acc.avg().unwrap(), Value::Null);
        assert_eq!(sum_of(&[Value::String("x".into())]).unwrap(), Value::Null);
    }

    #[test]
    fn avg_uses_the_exact_total() {
        let mut acc = ExactSum::new();
        acc.add_i64(ABOVE);
        acc.add_i64(AT);
        // Exact mean is 2^53 + 0.5; the f64 nearest is 2^53.
        assert_eq!(acc.avg().unwrap(), Value::Float(AT as f64));
        let mut big = ExactSum::new();
        big.add_i64(i64::MAX);
        big.add_i64(i64::MAX);
        assert_eq!(big.avg().unwrap(), Value::Float(i64::MAX as f64));
        let mut ints = ExactSum::new();
        ints.add_i64(10);
        ints.add_i64(20);
        ints.add_i64(25);
        assert_eq!(ints.avg_f64().unwrap(), Some(55.0 / 3.0));
    }

    #[test]
    fn partial_merge_keeps_exactness() {
        let mut a = ExactSum::new();
        a.add_i64(ABOVE);
        let mut b = ExactSum::new();
        b.add_i64(AT);
        b.add_i64(i64::MAX);
        a.merge(&b);
        let mut single = ExactSum::new();
        for v in [ABOVE, AT, i64::MAX] {
            single.add_i64(v);
        }
        assert_eq!(a.sum().unwrap(), single.sum().unwrap());
        assert_eq!(a.count(), 3);
    }

    fn float_sum(values: &[f64]) -> f64 {
        let mut acc = ExactSum::new();
        for v in values {
            acc.add_f64(*v);
        }
        match acc.sum().unwrap() {
            Value::Float(f) => f,
            other => panic!("expected a float SUM, got {other:?}"),
        }
    }

    /// Float overflow is `Infinity` and `Infinity` plus `-Infinity` is NaN,
    /// as in PostgreSQL.
    #[test]
    fn non_finite_float_totals_follow_ieee() {
        assert_eq!(float_sum(&[1e308, 1e308]), f64::INFINITY);
        assert_eq!(float_sum(&[-1e308, -1e308]), f64::NEG_INFINITY);
        assert_eq!(float_sum(&[f64::INFINITY, 1.0]), f64::INFINITY);
        assert_eq!(float_sum(&[1.0, f64::NEG_INFINITY]), f64::NEG_INFINITY);
        assert!(float_sum(&[f64::INFINITY, f64::NEG_INFINITY]).is_nan());
        assert!(float_sum(&[f64::NAN, f64::INFINITY]).is_nan());
        let mut avg = ExactSum::new();
        avg.add_f64(1e308);
        avg.add_f64(1e308);
        assert_eq!(avg.avg().unwrap(), Value::Float(f64::INFINITY));
        let mut mixed = ExactSum::new();
        mixed.add_i64(5);
        mixed.add_f64(f64::INFINITY);
        assert_eq!(mixed.sum().unwrap(), Value::Float(f64::INFINITY));
    }

    /// Neumaier compensation keeps a small term that a large pair cancels.
    #[test]
    fn compensation_survives_large_cancelling_terms() {
        assert_eq!(float_sum(&[1.0, 1e100, 1.0, -1e100]), 2.0);
        assert_eq!(float_sum(&[0.1; 10]), 1.0);
    }

    #[test]
    fn merge_keeps_non_finite_totals() {
        let mut a = ExactSum::new();
        a.add_f64(1e308);
        let mut b = ExactSum::new();
        b.add_f64(1e308);
        a.merge(&b);
        assert_eq!(a.sum().unwrap(), Value::Float(f64::INFINITY));
        let mut pos = ExactSum::new();
        pos.add_f64(f64::INFINITY);
        let mut neg = ExactSum::new();
        neg.add_f64(f64::NEG_INFINITY);
        pos.merge(&neg);
        assert!(matches!(pos.sum().unwrap(), Value::Float(f) if f.is_nan()));
        let mut small = ExactSum::new();
        small.add_f64(1.0);
        let mut big = ExactSum::new();
        big.add_f64(1e100);
        big.add_f64(1.0);
        big.add_f64(-1e100);
        small.merge(&big);
        assert_eq!(small.sum().unwrap(), Value::Float(2.0));
    }

    #[test]
    fn msgpack_round_trip_keeps_non_finite_floats() {
        for v in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
            let mut acc = ExactSum::new();
            acc.add_f64(v);
            let bytes = zerompk::to_msgpack_vec(&acc).unwrap();
            let back: ExactSum = zerompk::from_msgpack(&bytes).unwrap();
            assert_eq!(back.count(), 1);
            match (back.sum().unwrap(), acc.sum().unwrap()) {
                (Value::Float(x), Value::Float(y)) => {
                    assert!(x == y || (x.is_nan() && y.is_nan()));
                }
                other => panic!("expected float sums, got {other:?}"),
            }
        }
    }

    #[test]
    fn msgpack_round_trip_keeps_every_part() {
        let mut big = ExactSum::new();
        big.add_u64(u64::MAX);
        big.add_u64(u64::MAX);
        let mut negative = ExactSum::new();
        negative.add_i64(i64::MIN);
        negative.add_i64(i64::MIN);
        negative.add_f64(0.1);
        let mut overflow = ExactSum::new();
        overflow.add_int(i128::MAX);
        overflow.add_int(1);
        for acc in [ExactSum::new(), big, negative, overflow] {
            let bytes = zerompk::to_msgpack_vec(&acc).unwrap();
            let back: ExactSum = zerompk::from_msgpack(&bytes).unwrap();
            assert_eq!(back, acc);
            assert_eq!(back.sum(), acc.sum());
        }
    }

    #[test]
    fn coerced_strings_stay_exact() {
        let mut acc = ExactSum::new();
        let text = sum_input(&Value::String("9007199254740993".into())).unwrap();
        assert_eq!(text, Value::Integer(ABOVE));
        assert!(acc.add_value(&text));
        assert!(acc.add_value(&Value::Integer(AT)));
        assert!(!acc.add_value(&Value::String("1".into())));
        assert_eq!(acc.sum().unwrap(), Value::Integer(18_014_398_509_481_985));
        assert_eq!(
            sum_input(&Value::String("18446744073709551615".into())),
            Some(Value::Decimal(Decimal::from(u64::MAX)))
        );
        assert_eq!(sum_input(&Value::String("x".into())), None);
        assert_eq!(sum_input(&Value::Bool(true)), None);
    }

    fn dec(text: &str) -> Decimal {
        Decimal::from_str_exact(text).unwrap()
    }

    #[test]
    fn fractional_decimals_sum_exactly() {
        let tenths = vec![Value::Decimal(dec("0.1")); 3];
        assert_eq!(sum_of(&tenths).unwrap(), Value::Decimal(dec("0.3")));
        let mixed = [Value::Decimal(dec("0.25")), Value::Integer(i64::MAX)];
        assert_eq!(
            sum_of(&mixed).unwrap(),
            Value::Decimal(Decimal::from(i64::MAX) + dec("0.25"))
        );
        let with_float = [Value::Decimal(dec("0.5")), Value::Float(0.25)];
        assert_eq!(sum_of(&with_float).unwrap(), Value::Float(0.75));
    }

    #[test]
    fn decimal_avg_divides_the_exact_total() {
        let mut acc = ExactSum::new();
        acc.add_value(&Value::Decimal(dec("0.1")));
        acc.add_value(&Value::Decimal(dec("0.2")));
        assert_eq!(acc.avg().unwrap(), Value::Float(0.15));
    }

    #[test]
    fn a_decimal_total_past_the_range_is_overflow_error() {
        let mut acc = ExactSum::new();
        acc.add_value(&Value::Decimal(Decimal::MAX - dec("0.5")));
        acc.add_value(&Value::Decimal(Decimal::MAX - dec("0.5")));
        assert_eq!(
            acc.sum(),
            Err(EvalError::NumericOverflow { function: "sum" })
        );
        let mut joined = ExactSum::new();
        joined.add_value(&Value::Decimal(Decimal::MAX));
        joined.add_value(&Value::Decimal(dec("0.5")));
        assert_eq!(
            joined.sum(),
            Err(EvalError::NumericOverflow { function: "sum" })
        );
    }

    #[test]
    fn numeric_text_is_exact_where_it_can_be() {
        assert_eq!(numeric_text("12"), Some(Value::Integer(12)));
        assert_eq!(numeric_text("0.1"), Some(Value::Decimal(dec("0.1"))));
        assert_eq!(numeric_text("1e3"), Some(Value::Float(1000.0)));
        assert_eq!(numeric_text("x"), None);
        let mut acc = ExactSum::new();
        for text in ["0.1", "0.2"] {
            assert!(acc.add_value(&sum_input(&Value::String(text.into())).unwrap()));
        }
        assert_eq!(acc.sum().unwrap(), Value::Decimal(dec("0.3")));
    }

    #[test]
    fn decimal_parts_merge_and_round_trip() {
        let mut a = ExactSum::new();
        a.add_value(&Value::Decimal(dec("0.1")));
        let mut b = ExactSum::new();
        b.add_value(&Value::Decimal(dec("0.2")));
        b.add_i64(1);
        a.merge(&b);
        assert_eq!(a.sum().unwrap(), Value::Decimal(dec("1.3")));
        let bytes = zerompk::to_msgpack_vec(&a).unwrap();
        let back: ExactSum = zerompk::from_msgpack(&bytes).unwrap();
        assert_eq!(back, a);
    }

    #[test]
    fn sum_inputs_sum_exactly() {
        let mut acc = ExactSum::new();
        for v in [
            Value::Integer(ABOVE),
            Value::from_u64(u64::MAX),
            Value::String("2".into()),
        ] {
            assert!(acc.add_value(&sum_input(&v).unwrap()));
        }
        assert_eq!(sum_input(&Value::Null), None);
        assert_eq!(
            acc.sum().unwrap(),
            Value::Decimal(Decimal::from_i128_with_scale(
                i128::from(ABOVE) + i128::from(u64::MAX) + 2,
                0
            ))
        );
    }
}