uqa-operators 0.1.9

Operator trait and primitives: term, vector, filter, score, boolean, hybrid
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Aggregation monoids and posting-list aggregate operators.
//!
//! Aggregation functions
//! form a monoid (Section 5.1, Paper 1) so a parallel executor can
//! split the input, fold each shard with [`AggregationMonoid::accumulate`],
//! merge the partial states with [`AggregationMonoid::combine`], and
//! emit the final value via [`AggregationMonoid::finalize`].
//!
//! # Concrete monoids
//!
//! * [`CountMonoid`] -- `(0, +)` over `u64`.
//! * [`SumMonoid`] -- `(0.0, +)` over `f64`.
//! * [`AvgMonoid`] -- `((0.0, 0), pair-wise +)` over `(sum, count)`.
//! * [`MinMonoid`], [`MaxMonoid`] -- `(infinity, min)` / `(-infinity, max)`
//!   over `f64`.
//! * [`QuantileMonoid`] -- collect-then-finalize quantile estimator.
//!
//! # Operators
//!
//! * [`AggregateOperator`] folds a posting list with the supplied
//!   monoid and emits a single-entry posting list whose payload
//!   carries `_aggregate_field` / `_aggregate` metadata.
//! * [`GroupByOperator`] groups by a field, folds a second field with
//!   the monoid per group, and emits one entry per group.

#![allow(
    clippy::redundant_field_names,
    clippy::similar_names,
    clippy::manual_let_else,
    clippy::explicit_iter_loop
)]

use std::collections::BTreeMap;
use std::sync::Arc;

use uqa_core::{Payload, PostingEntry, PostingList, Value};
use uqa_storage::{StorageBackendError, StorageBackendResult};

use crate::base::{missing_backend, ExecutionContext, Operator, OperatorResult};

/// Pair-wise additive state used by [`AvgMonoid`].
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct AvgState {
    pub sum: f64,
    pub count: u64,
}

/// Generic aggregation state. Concrete monoids consume / emit the
/// variants they care about; a mismatched variant is an execution
/// error rather than a silently accepted partial fold.
#[derive(Debug, Clone, PartialEq)]
pub enum AggState {
    Count(u64),
    Sum(f64),
    Avg(AvgState),
    Min(f64),
    Max(f64),
    Values(Vec<f64>),
}

impl AggState {
    pub fn as_f64(&self) -> Option<f64> {
        match self {
            AggState::Count(n) => Some(*n as f64),
            AggState::Sum(v) | AggState::Min(v) | AggState::Max(v) => Some(*v),
            AggState::Avg(s) => {
                if s.count == 0 {
                    Some(0.0)
                } else {
                    Some(s.sum / s.count as f64)
                }
            }
            AggState::Values(_) => None,
        }
    }
}

/// Aggregation function with monoid structure for parallel
/// decomposition. See Section 5.1, Paper 1.
pub trait AggregationMonoid: Send + Sync {
    fn identity(&self) -> AggState;
    fn accumulate(&self, state: AggState, value: &Value) -> StorageBackendResult<AggState>;
    fn combine(&self, a: AggState, b: AggState) -> StorageBackendResult<AggState>;
    fn finalize(&self, state: AggState) -> StorageBackendResult<Value>;
}

fn invalid_state(operation: &str, expected: &str, actual: &AggState) -> StorageBackendError {
    StorageBackendError::Other(format!(
        "{operation} aggregation expected {expected} state, got {actual:?}"
    ))
}

fn numeric_value(operation: &str, value: &Value) -> StorageBackendResult<Option<f64>> {
    let numeric = match value {
        Value::Null => return Ok(None),
        Value::Int(integer) => *integer as f64,
        Value::Float(float) => *float,
        Value::Bool(true) => 1.0,
        Value::Bool(false) => 0.0,
        _ => {
            return Err(StorageBackendError::Other(format!(
                "{operation} aggregation requires a numeric value, got {value:?}"
            )))
        }
    };
    if !numeric.is_finite() {
        return Err(StorageBackendError::Other(format!(
            "{operation} aggregation requires a finite numeric value, got {numeric}"
        )));
    }
    Ok(Some(numeric))
}

fn finite_result(operation: &str, value: f64) -> StorageBackendResult<f64> {
    if value.is_finite() {
        Ok(value)
    } else {
        Err(StorageBackendError::Other(format!(
            "{operation} aggregation overflowed the finite numeric range"
        )))
    }
}

#[derive(Debug, Clone, Copy, Default)]
pub struct CountMonoid;

impl AggregationMonoid for CountMonoid {
    fn identity(&self) -> AggState {
        AggState::Count(0)
    }
    fn accumulate(&self, state: AggState, _value: &Value) -> StorageBackendResult<AggState> {
        let AggState::Count(count) = state else {
            return Err(invalid_state("count", "Count", &state));
        };
        Ok(AggState::Count(count.checked_add(1).ok_or_else(|| {
            StorageBackendError::Other("count aggregation overflowed u64".to_string())
        })?))
    }
    fn combine(&self, a: AggState, b: AggState) -> StorageBackendResult<AggState> {
        let (AggState::Count(left), AggState::Count(right)) = (&a, &b) else {
            return Err(StorageBackendError::Other(format!(
                "count aggregation expected Count states, got {a:?} and {b:?}"
            )));
        };
        Ok(AggState::Count(left.checked_add(*right).ok_or_else(
            || StorageBackendError::Other("count aggregation overflowed u64".to_string()),
        )?))
    }
    fn finalize(&self, state: AggState) -> StorageBackendResult<Value> {
        let AggState::Count(count) = state else {
            return Err(invalid_state("count", "Count", &state));
        };
        let count = i64::try_from(count).map_err(|_| {
            StorageBackendError::Other(format!(
                "count aggregation result {count} exceeds the Value::Int range"
            ))
        })?;
        Ok(Value::Int(count))
    }
}

#[derive(Debug, Clone, Copy, Default)]
pub struct SumMonoid;

impl AggregationMonoid for SumMonoid {
    fn identity(&self) -> AggState {
        AggState::Sum(0.0)
    }
    fn accumulate(&self, state: AggState, value: &Value) -> StorageBackendResult<AggState> {
        let Some(delta) = numeric_value("sum", value)? else {
            return Ok(state);
        };
        let AggState::Sum(sum) = state else {
            return Err(invalid_state("sum", "Sum", &state));
        };
        Ok(AggState::Sum(finite_result("sum", sum + delta)?))
    }
    fn combine(&self, a: AggState, b: AggState) -> StorageBackendResult<AggState> {
        let (AggState::Sum(left), AggState::Sum(right)) = (&a, &b) else {
            return Err(StorageBackendError::Other(format!(
                "sum aggregation expected Sum states, got {a:?} and {b:?}"
            )));
        };
        Ok(AggState::Sum(finite_result("sum", left + right)?))
    }
    fn finalize(&self, state: AggState) -> StorageBackendResult<Value> {
        let AggState::Sum(sum) = state else {
            return Err(invalid_state("sum", "Sum", &state));
        };
        Ok(Value::Float(finite_result("sum", sum)?))
    }
}

#[derive(Debug, Clone, Copy, Default)]
pub struct AvgMonoid;

impl AggregationMonoid for AvgMonoid {
    fn identity(&self) -> AggState {
        AggState::Avg(AvgState::default())
    }
    fn accumulate(&self, state: AggState, value: &Value) -> StorageBackendResult<AggState> {
        let Some(delta) = numeric_value("avg", value)? else {
            return Ok(state);
        };
        let AggState::Avg(average) = state else {
            return Err(invalid_state("avg", "Avg", &state));
        };
        Ok(AggState::Avg(AvgState {
            sum: finite_result("avg", average.sum + delta)?,
            count: average.count.checked_add(1).ok_or_else(|| {
                StorageBackendError::Other("avg aggregation count overflowed u64".to_string())
            })?,
        }))
    }
    fn combine(&self, a: AggState, b: AggState) -> StorageBackendResult<AggState> {
        let (AggState::Avg(left), AggState::Avg(right)) = (&a, &b) else {
            return Err(StorageBackendError::Other(format!(
                "avg aggregation expected Avg states, got {a:?} and {b:?}"
            )));
        };
        Ok(AggState::Avg(AvgState {
            sum: finite_result("avg", left.sum + right.sum)?,
            count: left.count.checked_add(right.count).ok_or_else(|| {
                StorageBackendError::Other("avg aggregation count overflowed u64".to_string())
            })?,
        }))
    }
    fn finalize(&self, state: AggState) -> StorageBackendResult<Value> {
        let AggState::Avg(average) = state else {
            return Err(invalid_state("avg", "Avg", &state));
        };
        if average.count == 0 {
            Ok(Value::Float(0.0))
        } else {
            Ok(Value::Float(finite_result(
                "avg",
                average.sum / average.count as f64,
            )?))
        }
    }
}

#[derive(Debug, Clone, Copy, Default)]
pub struct MinMonoid;

impl AggregationMonoid for MinMonoid {
    fn identity(&self) -> AggState {
        AggState::Min(f64::INFINITY)
    }
    fn accumulate(&self, state: AggState, value: &Value) -> StorageBackendResult<AggState> {
        let Some(value) = numeric_value("min", value)? else {
            return Ok(state);
        };
        let AggState::Min(minimum) = state else {
            return Err(invalid_state("min", "Min", &state));
        };
        Ok(AggState::Min(minimum.min(value)))
    }
    fn combine(&self, a: AggState, b: AggState) -> StorageBackendResult<AggState> {
        let (AggState::Min(left), AggState::Min(right)) = (&a, &b) else {
            return Err(StorageBackendError::Other(format!(
                "min aggregation expected Min states, got {a:?} and {b:?}"
            )));
        };
        Ok(AggState::Min(left.min(*right)))
    }
    fn finalize(&self, state: AggState) -> StorageBackendResult<Value> {
        let AggState::Min(minimum) = state else {
            return Err(invalid_state("min", "Min", &state));
        };
        if minimum == f64::INFINITY {
            Ok(Value::Null)
        } else {
            Ok(Value::Float(finite_result("min", minimum)?))
        }
    }
}

#[derive(Debug, Clone, Copy, Default)]
pub struct MaxMonoid;

impl AggregationMonoid for MaxMonoid {
    fn identity(&self) -> AggState {
        AggState::Max(f64::NEG_INFINITY)
    }
    fn accumulate(&self, state: AggState, value: &Value) -> StorageBackendResult<AggState> {
        let Some(value) = numeric_value("max", value)? else {
            return Ok(state);
        };
        let AggState::Max(maximum) = state else {
            return Err(invalid_state("max", "Max", &state));
        };
        Ok(AggState::Max(maximum.max(value)))
    }
    fn combine(&self, a: AggState, b: AggState) -> StorageBackendResult<AggState> {
        let (AggState::Max(left), AggState::Max(right)) = (&a, &b) else {
            return Err(StorageBackendError::Other(format!(
                "max aggregation expected Max states, got {a:?} and {b:?}"
            )));
        };
        Ok(AggState::Max(left.max(*right)))
    }
    fn finalize(&self, state: AggState) -> StorageBackendResult<Value> {
        let AggState::Max(maximum) = state else {
            return Err(invalid_state("max", "Max", &state));
        };
        if maximum == f64::NEG_INFINITY {
            Ok(Value::Null)
        } else {
            Ok(Value::Float(finite_result("max", maximum)?))
        }
    }
}

/// Quantile aggregation: collects observed values and computes the
/// requested quantile at finalize. `quantile = 0.5` is the median.
#[derive(Debug, Clone, Copy)]
pub struct QuantileMonoid {
    quantile: f64,
}

impl QuantileMonoid {
    pub fn new(quantile: f64) -> StorageBackendResult<Self> {
        if !quantile.is_finite() || !(0.0..=1.0).contains(&quantile) {
            return Err(StorageBackendError::Other(format!(
                "quantile must be finite and in [0, 1], got {quantile}"
            )));
        }
        Ok(Self { quantile })
    }
}

impl AggregationMonoid for QuantileMonoid {
    fn identity(&self) -> AggState {
        AggState::Values(Vec::new())
    }
    fn accumulate(&self, state: AggState, value: &Value) -> StorageBackendResult<AggState> {
        let Some(value) = numeric_value("quantile", value)? else {
            return Ok(state);
        };
        let AggState::Values(mut values) = state else {
            return Err(invalid_state("quantile", "Values", &state));
        };
        values.push(value);
        Ok(AggState::Values(values))
    }
    fn combine(&self, a: AggState, b: AggState) -> StorageBackendResult<AggState> {
        let (AggState::Values(mut left), AggState::Values(right)) = (a, b) else {
            return Err(StorageBackendError::Other(
                "quantile aggregation expected Values states".to_string(),
            ));
        };
        left.extend(right);
        Ok(AggState::Values(left))
    }
    fn finalize(&self, state: AggState) -> StorageBackendResult<Value> {
        let mut buf = match state {
            AggState::Values(v) => v,
            other => return Err(invalid_state("quantile", "Values", &other)),
        };
        if buf.is_empty() {
            return Ok(Value::Null);
        }
        if buf.iter().any(|value| !value.is_finite()) {
            return Err(StorageBackendError::Other(
                "quantile aggregation state contains a non-finite value".to_string(),
            ));
        }
        buf.sort_by(f64::total_cmp);
        let n = buf.len();
        let idx = self.quantile * (n - 1) as f64;
        let lower = idx.floor() as usize;
        let upper = (lower + 1).min(n - 1);
        let frac = idx - lower as f64;
        Ok(Value::Float(buf[lower] * (1.0 - frac) + buf[upper] * frac))
    }
}

// -------------------------------------------------------------------------
// Operators
// -------------------------------------------------------------------------

/// Apply a monoid over a single field across the rows the source
/// operator produces. Emits a one-entry posting list whose payload
/// carries `_aggregate_field` / `_aggregate` so downstream callers
/// can pick the result by name.
pub struct AggregateOperator {
    pub source: Option<Arc<dyn Operator>>,
    pub field: String,
    pub monoid: Arc<dyn AggregationMonoid>,
}

impl AggregateOperator {
    pub fn new(
        source: Option<Arc<dyn Operator>>,
        field: impl Into<String>,
        monoid: Arc<dyn AggregationMonoid>,
    ) -> Self {
        Self {
            source,
            field: field.into(),
            monoid,
        }
    }
}

impl Operator for AggregateOperator {
    fn execute(&self, ctx: &ExecutionContext) -> OperatorResult {
        let doc_ids: Vec<u64> = if let Some(op) = &self.source {
            op.execute(ctx)?.iter().map(|e| e.doc_id).collect()
        } else {
            let store = ctx
                .document_store
                .as_ref()
                .ok_or_else(|| missing_backend("document-store", "field aggregation"))?;
            let mut ids = store.doc_ids()?;
            ids.sort_unstable();
            ids
        };

        let mut state = self.monoid.identity();
        let store = ctx
            .document_store
            .as_ref()
            .ok_or_else(|| missing_backend("document-store", "field aggregation"))?;
        for doc_id in doc_ids {
            if store.get(doc_id)?.is_none() {
                return Err(StorageBackendError::Other(format!(
                    "field aggregate candidate {doc_id} is missing from the document store"
                )));
            }
            if let Some(value) = store.get_field(doc_id, &self.field)? {
                state = self.monoid.accumulate(state, &value)?;
            }
        }

        let result = self.monoid.finalize(state)?;
        let score = match &result {
            Value::Int(i) => *i as f64,
            Value::Float(f) => *f,
            _ => 0.0,
        };
        let mut fields: BTreeMap<String, Value> = BTreeMap::new();
        fields.insert("_aggregate_field".into(), Value::Str(self.field.clone()));
        fields.insert("_aggregate".into(), result);
        Ok(PostingList::from_sorted_unchecked(vec![PostingEntry::new(
            0,
            Payload {
                score,
                fields,
                ..Default::default()
            },
        )]))
    }
}

/// Group documents by `group_field` and fold `agg_field` per group.
pub struct GroupByOperator {
    pub source: Arc<dyn Operator>,
    pub group_field: String,
    pub agg_field: String,
    pub monoid: Arc<dyn AggregationMonoid>,
}

impl GroupByOperator {
    pub fn new(
        source: Arc<dyn Operator>,
        group_field: impl Into<String>,
        agg_field: impl Into<String>,
        monoid: Arc<dyn AggregationMonoid>,
    ) -> Self {
        Self {
            source,
            group_field: group_field.into(),
            agg_field: agg_field.into(),
            monoid,
        }
    }
}

impl Operator for GroupByOperator {
    fn execute(&self, ctx: &ExecutionContext) -> OperatorResult {
        let source_pl = self.source.execute(ctx)?;
        let store = ctx
            .document_store
            .as_ref()
            .ok_or_else(|| missing_backend("document-store", "group-by aggregation"))?;

        let mut groups: BTreeMap<String, AggState> = BTreeMap::new();
        for entry in source_pl.iter() {
            if store.get(entry.doc_id)?.is_none() {
                return Err(StorageBackendError::Other(format!(
                    "group-by candidate {} is missing from the document store",
                    entry.doc_id
                )));
            }
            let Some(group_val) = store.get_field(entry.doc_id, &self.group_field)? else {
                continue;
            };
            let key = value_to_key(&group_val);
            let state = groups.entry(key).or_insert_with(|| self.monoid.identity());
            if let Some(agg_val) = store.get_field(entry.doc_id, &self.agg_field)? {
                let new_state = self
                    .monoid
                    .accumulate(std::mem::replace(state, self.monoid.identity()), &agg_val)?;
                *state = new_state;
            }
        }

        let mut entries: Vec<PostingEntry> = Vec::with_capacity(groups.len());
        for (i, (group_key, state)) in groups.into_iter().enumerate() {
            let result = self.monoid.finalize(state)?;
            let score = match &result {
                Value::Int(i) => *i as f64,
                Value::Float(f) => *f,
                _ => 0.0,
            };
            let mut fields: BTreeMap<String, Value> = BTreeMap::new();
            fields.insert("_group_key".into(), Value::Str(group_key));
            fields.insert("_group_field".into(), Value::Str(self.group_field.clone()));
            fields.insert("_aggregate_result".into(), result);
            entries.push(PostingEntry::new(
                u64::try_from(i).map_err(|_| {
                    StorageBackendError::Other(format!(
                        "group-by bucket index {i} exceeds the document-id range"
                    ))
                })?,
                Payload {
                    score,
                    fields,
                    ..Default::default()
                },
            ));
        }
        Ok(PostingList::from_sorted_unchecked(entries))
    }
}

fn value_to_key(v: &Value) -> String {
    match v {
        Value::Null => "\x00".into(),
        Value::Int(i) => i.to_string(),
        Value::Float(f) => format!("{f:.17}"),
        Value::Str(s) => s.clone(),
        Value::Bool(b) => b.to_string(),
        other => format!("{other:?}"),
    }
}

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

    #[test]
    fn count_monoid_combines_partial_folds() {
        let m = CountMonoid;
        let mut a = m.identity();
        let mut b = m.identity();
        a = m.accumulate(a, &Value::Null).unwrap();
        a = m.accumulate(a, &Value::Null).unwrap();
        b = m.accumulate(b, &Value::Null).unwrap();
        let merged = m.combine(a, b).unwrap();
        assert_eq!(m.finalize(merged).unwrap(), Value::Int(3));
    }

    #[test]
    fn sum_monoid_rejects_non_numeric() {
        let m = SumMonoid;
        let mut s = m.identity();
        s = m.accumulate(s, &Value::Float(1.5)).unwrap();
        s = m.accumulate(s, &Value::Int(2)).unwrap();
        assert_eq!(m.finalize(s).unwrap(), Value::Float(3.5));

        let error = m
            .accumulate(m.identity(), &Value::Str("nope".into()))
            .unwrap_err();
        assert!(error.to_string().contains("requires a numeric value"));
    }

    #[test]
    fn avg_monoid_divides_by_count() {
        let m = AvgMonoid;
        let mut s = m.identity();
        for v in [Value::Int(2), Value::Int(4), Value::Int(6)] {
            s = m.accumulate(s, &v).unwrap();
        }
        assert_eq!(m.finalize(s).unwrap(), Value::Float(4.0));
    }

    #[test]
    fn min_max_track_extremes() {
        let mn = MinMonoid;
        let mx = MaxMonoid;
        let mut a = mn.identity();
        let mut b = mx.identity();
        for v in [Value::Float(3.0), Value::Float(1.0), Value::Float(2.0)] {
            a = mn.accumulate(a, &v).unwrap();
            b = mx.accumulate(b, &v).unwrap();
        }
        assert_eq!(mn.finalize(a).unwrap(), Value::Float(1.0));
        assert_eq!(mx.finalize(b).unwrap(), Value::Float(3.0));
    }

    #[test]
    fn quantile_median_interpolates() {
        let q = QuantileMonoid::new(0.5).unwrap();
        let mut s = q.identity();
        for v in [
            Value::Float(1.0),
            Value::Float(2.0),
            Value::Float(3.0),
            Value::Float(4.0),
        ] {
            s = q.accumulate(s, &v).unwrap();
        }
        // median of [1,2,3,4] interpolates between idx 1 and 2 = (2+3)/2 = 2.5
        assert_eq!(q.finalize(s).unwrap(), Value::Float(2.5));
    }

    #[test]
    fn quantile_constructor_rejects_invalid_values() {
        for quantile in [f64::NAN, -0.1, 1.1] {
            let error = QuantileMonoid::new(quantile).unwrap_err();
            assert!(error.to_string().contains("finite and in [0, 1]"));
        }
    }

    #[test]
    fn aggregation_state_mismatches_are_errors() {
        let count = CountMonoid;
        assert!(count
            .accumulate(AggState::Sum(0.0), &Value::Int(1))
            .unwrap_err()
            .to_string()
            .contains("expected Count state"));
        assert!(count
            .combine(AggState::Count(1), AggState::Sum(2.0))
            .unwrap_err()
            .to_string()
            .contains("expected Count states"));
        assert!(count
            .finalize(AggState::Values(Vec::new()))
            .unwrap_err()
            .to_string()
            .contains("expected Count state"));
    }

    #[test]
    fn aggregation_counters_and_value_widths_are_checked() {
        let count = CountMonoid;
        assert!(count
            .accumulate(AggState::Count(u64::MAX), &Value::Null)
            .unwrap_err()
            .to_string()
            .contains("overflowed u64"));
        assert!(count
            .combine(AggState::Count(u64::MAX), AggState::Count(1))
            .unwrap_err()
            .to_string()
            .contains("overflowed u64"));
        assert!(count
            .finalize(AggState::Count(i64::MAX as u64 + 1))
            .unwrap_err()
            .to_string()
            .contains("Value::Int range"));

        let avg = AvgMonoid;
        assert!(avg
            .accumulate(
                AggState::Avg(AvgState {
                    sum: 1.0,
                    count: u64::MAX,
                }),
                &Value::Int(1),
            )
            .unwrap_err()
            .to_string()
            .contains("count overflowed"));
    }

    #[test]
    fn numeric_aggregations_reject_invalid_types_and_non_finite_values() {
        for result in [
            AvgMonoid.accumulate(AvgMonoid.identity(), &Value::Str("bad".into())),
            MinMonoid.accumulate(MinMonoid.identity(), &Value::Float(f64::NAN)),
            MaxMonoid.accumulate(MaxMonoid.identity(), &Value::Float(f64::INFINITY)),
        ] {
            assert!(result.is_err());
        }
        assert_eq!(
            MinMonoid.finalize(MinMonoid.identity()).unwrap(),
            Value::Null
        );
        assert_eq!(
            MaxMonoid.finalize(MaxMonoid.identity()).unwrap(),
            Value::Null
        );
        let quantile = QuantileMonoid::new(0.5).unwrap();
        assert_eq!(quantile.finalize(quantile.identity()).unwrap(), Value::Null);
    }
}