pond-rs 0.1.0

Time series analytics: rolling windows, calendar buckets in any time zone, joins, 109 finance studies and live streams, with Arrow I/O. Same results as pond-ts.
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
//! Buckets over a live source (pond-ts `LiveAggregation`): fixed-length
//! ones, or calendar ones (days, weeks, … in a time zone, so of varying
//! length).
//!
//! Each source event goes to the bucket its start falls in. A bucket closes
//! once the latest start seen (the watermark), less the source's grace
//! window, reaches its end; an event for a bucket that has already closed is
//! skipped. Each event gives, in order: the bucket it joined, as it now
//! stands (`Bucket`), then each bucket it closed, oldest first (`Event`),
//! then `Update`.
//!
//! The bucket states are ports of pond-ts's `bucketState` reducers, step
//! for step: they add values one at a time in arrival order (a plain
//! running sum, not the eight-lane sum batch `aggregate` uses), so the bits
//! are pond-ts's live ones.

use std::cmp::Ordering;
use std::collections::{BTreeMap, HashMap};
use std::sync::Arc;

use crate::column::{Scalar, Value};
use crate::elements::compare_scalars;
use crate::error::{PondError, Result};
use crate::query::EventKey;
use crate::reducer::{Agg, Reducer, clamp_at_zero, percentile_of_sorted};
use crate::sequence::{CALENDAR_RANGE_MS, Sequence};
use crate::series::{ColumnDef, ColumnKind, MAX_TIME_MS, TimeSeries};

use super::buffer::series_of;
use super::event::{LiveEvent, LiveSchema};
use super::states::{ScalarKey, as_key, number, scalar_key, scalars};

const CALL: &str = "LiveSeries::aggregate";

struct Mapped {
    source: usize,
    reducer: Reducer,
}

struct Pending {
    end: i64,
    states: Vec<BucketState>,
}

/// Buckets over a live source: the closed ones, and the ones still open. Built with [`LiveSeries::aggregate`](super::LiveSeries::aggregate);
/// read through the owner.
pub struct LiveAggregation {
    name: String,
    schema: Arc<LiveSchema>,
    columns: Vec<Mapped>,
    grid: Grid,
    grace: i64,
    pending: BTreeMap<i64, Pending>,
    watermark: Option<i64>,
    closed: Vec<LiveEvent>,
    events_observed: u64,
}

impl std::fmt::Debug for LiveAggregation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LiveAggregation")
            .field("name", &self.name)
            .field("closed", &self.closed.len())
            .field("open", &self.pending.len())
            .finish_non_exhaustive()
    }
}

/// Counters for a [`LiveAggregation`] (pond-ts `stats()`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AggregationStats {
    /// Events added to a bucket (late ones skipped are not counted).
    pub events_observed: u64,
    /// Buckets closed, ever.
    pub buckets_closed: u64,
    /// Buckets open now.
    pub open_buckets: usize,
    /// Start of the oldest open bucket.
    pub open_bucket_start: Option<i64>,
}

/// How events map to buckets.
enum Grid {
    Fixed {
        step: i64,
        anchor: i64,
    },
    /// The last bucket looked up is kept: events in the same bucket skip
    /// the time-zone arithmetic.
    Calendar {
        sequence: Sequence,
        last: Option<(i64, i64)>,
    },
}

impl Grid {
    fn bucket(&mut self, ts: i64) -> (i64, i64) {
        match self {
            &mut Grid::Fixed { step, anchor } => {
                bucket_of(ts, step, anchor).expect("edges checked in LiveAggregation::new")
            }
            Grid::Calendar { sequence, last } => {
                if let Some((start, end)) = *last
                    && (start..end).contains(&ts)
                {
                    return (start, end);
                }
                let bucket = sequence
                    .calendar_bucket(ts)
                    .expect("a calendar sequence")
                    .expect("LiveSeries::push keeps times inside CALENDAR_RANGE_MS");
                *last = Some(bucket);
                bucket
            }
        }
    }

    fn is_calendar(&self) -> bool {
        matches!(self, Grid::Calendar { .. })
    }
}

/// What one source event did: the bucket it joined (when wanted) and the
/// buckets it closed.
pub(crate) struct Ingested {
    pub(crate) bucket: Option<LiveEvent>,
    pub(crate) closed: Vec<LiveEvent>,
}

impl LiveAggregation {
    /// Check the mapping against the source schema and replay `history`.
    pub(crate) fn new(
        name: String,
        source: &[ColumnDef],
        sequence: &Sequence,
        grace: i64,
        mapping: &[Agg],
        history: impl Iterator<Item = LiveEvent>,
    ) -> Result<Self> {
        let (out_defs, plan) = plan_mapping(
            CALL,
            ColumnDef::required("time", ColumnKind::Interval),
            source,
            mapping,
        )?;
        let columns = plan
            .into_iter()
            .map(|(source, reducer)| Mapped { source, reducer })
            .collect();
        let grid = match sequence.fixed_step() {
            Some((step, anchor)) => {
                // Every bucket a supported time can fall in must have i64
                // edges; checked here at the extremes so `ingest` cannot
                // overflow.
                for t in [-MAX_TIME_MS, MAX_TIME_MS] {
                    if bucket_of(t, step, anchor).is_none() {
                        return Err(PondError::InvalidData {
                            call: CALL,
                            message: format!(
                                "step {step} ms with anchor {anchor} ms puts bucket edges outside the i64 range"
                            ),
                        });
                    }
                }
                Grid::Fixed { step, anchor }
            }
            None => Grid::Calendar {
                sequence: sequence.clone(),
                last: None,
            },
        };
        let mut agg = Self {
            name,
            schema: LiveSchema::new(out_defs),
            columns,
            grid,
            grace,
            pending: BTreeMap::new(),
            watermark: None,
            closed: Vec::new(),
            events_observed: 0,
        };
        let (lo, hi) = CALENDAR_RANGE_MS;
        for event in history {
            if agg.grid.is_calendar() && !(lo..hi).contains(&event.begin()) {
                return Err(PondError::InvalidData {
                    call: CALL,
                    message: format!(
                        "an event at {} ms is outside years -9997 to 9997 (UTC), the range a calendar aggregation takes",
                        event.begin()
                    ),
                });
            }
            agg.ingest(&event, false);
        }
        Ok(agg)
    }

    /// Add one source event. `want_bucket`: build the joined bucket's
    /// snapshot (only when something listens for it).
    pub(crate) fn ingest(&mut self, event: &LiveEvent, want_bucket: bool) -> Ingested {
        let ts = event.begin();
        let (start, end) = self.grid.bucket(ts);
        let watermark = self.watermark.map_or(ts, |w| w.max(ts));
        self.watermark = Some(watermark);
        let cutoff = watermark.saturating_sub(self.grace);
        let mut done = Ingested {
            bucket: None,
            closed: Vec::new(),
        };
        if end <= cutoff {
            return done;
        }
        self.events_observed += 1;
        let columns = &self.columns;
        let pending = self.pending.entry(start).or_insert_with(|| Pending {
            end,
            states: columns
                .iter()
                .map(|c| BucketState::new(c.reducer))
                .collect(),
        });
        for (state, c) in pending.states.iter_mut().zip(columns) {
            if let Some(v) = &event.values()[c.source] {
                state.add(v);
            }
        }
        if want_bucket {
            done.bucket = Some(self.bucket_event(start, end, &self.pending[&start].states));
        }
        // Close every bucket the cutoff has passed, oldest first.
        while let Some((&start, b)) = self.pending.first_key_value() {
            if b.end > cutoff {
                break;
            }
            let b = self.pending.remove(&start).expect("present");
            let closed = self.bucket_event(start, b.end, &b.states);
            self.closed.push(closed.clone());
            done.closed.push(closed);
        }
        done
    }

    fn bucket_event(&self, start: i64, end: i64, states: &[BucketState]) -> LiveEvent {
        let key = EventKey::interval(start, start, end).expect("start < end, finite label");
        LiveEvent::new(
            Arc::clone(&self.schema),
            key,
            states.iter().map(BucketState::snapshot).collect(),
        )
    }

    pub(crate) fn is_calendar(&self) -> bool {
        self.grid.is_calendar()
    }

    pub(crate) fn schema_arc(&self) -> Arc<LiveSchema> {
        Arc::clone(&self.schema)
    }

    /// The source's name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The output schema: `time` (interval keys labelled by their start),
    /// then one column per mapping entry, each allowing gaps.
    pub fn schema(&self) -> &[ColumnDef] {
        &self.schema.columns
    }

    /// Number of closed buckets.
    pub fn len(&self) -> usize {
        self.closed.len()
    }

    /// `true` when there is nothing in it.
    pub fn is_empty(&self) -> bool {
        self.closed.is_empty()
    }

    /// The closed bucket at `index`; negative positions count from the end.
    pub fn at(&self, index: i64) -> Option<&LiveEvent> {
        let i = if index < 0 {
            self.closed.len() as i64 + index
        } else {
            index
        };
        usize::try_from(i).ok().and_then(|i| self.closed.get(i))
    }

    /// The closed buckets, oldest first.
    pub fn events(&self) -> impl ExactSizeIterator<Item = &LiveEvent> {
        self.closed.iter()
    }

    /// Whether a bucket is still open.
    pub fn has_open_bucket(&self) -> bool {
        !self.pending.is_empty()
    }

    /// The closed buckets as a batch series.
    pub fn closed(&self) -> Result<TimeSeries> {
        series_of(
            "LiveAggregation::closed",
            &self.name,
            &self.schema,
            self.closed.iter(),
        )
    }

    /// The closed buckets, then the open ones as they stand.
    pub fn snapshot(&self) -> Result<TimeSeries> {
        let open: Vec<LiveEvent> = self
            .pending
            .iter()
            .map(|(&start, b)| self.bucket_event(start, b.end, &b.states))
            .collect();
        series_of(
            "LiveAggregation::snapshot",
            &self.name,
            &self.schema,
            self.closed.iter().chain(&open),
        )
    }

    /// Its counters (pond-ts `stats()`).
    pub fn stats(&self) -> AggregationStats {
        AggregationStats {
            events_observed: self.events_observed,
            buckets_closed: self.closed.len() as u64,
            open_buckets: self.pending.len(),
            open_bucket_start: self.pending.keys().next().copied(),
        }
    }
}

/// What [`plan_mapping`] gives: the output schema, and per output column
/// its source value column and reducer.
pub(super) type MappingPlan = (Vec<ColumnDef>, Vec<(usize, Reducer)>);

/// Check a mapping against a source schema (key column first): the
/// output schema (`key`, then one optional column per entry) and, per
/// entry, its source value column and reducer. Shared by `aggregate`,
/// `rolling` and `reduce`.
pub(super) fn plan_mapping(
    call: &'static str,
    key: ColumnDef,
    source: &[ColumnDef],
    mapping: &[Agg],
) -> Result<MappingPlan> {
    let values = &source[1..];
    let mut out_defs = vec![key];
    let mut columns = Vec::with_capacity(mapping.len());
    for agg in mapping {
        let reducer = agg.reducer();
        reducer.check(call)?;
        let i = values
            .iter()
            .position(|c| c.name == agg.source())
            .ok_or_else(|| PondError::UnknownColumn {
                call,
                column: agg.source().to_string(),
                available: values.iter().map(|c| c.name.clone()).collect(),
            })?;
        let kind = values[i].kind;
        if out_defs.iter().any(|d| d.name == agg.output()) {
            return Err(PondError::DuplicateOutput {
                call,
                column: agg.output().to_string(),
            });
        }
        let numeric = !matches!(
            reducer,
            Reducer::Count
                | Reducer::First
                | Reducer::Last
                | Reducer::Keep
                | Reducer::Unique
                | Reducer::Top(_)
                | Reducer::Samples
        );
        if numeric && kind != ColumnKind::Number {
            return Err(PondError::WrongKind {
                call,
                column: agg.source().to_string(),
                kind: kind.as_str(),
                reason: "this reducer needs a number column; string, boolean and array columns take count, first, last, keep, unique, top<N> or samples".into(),
            });
        }
        let out_kind = match reducer {
            Reducer::First | Reducer::Last | Reducer::Keep => kind,
            r if r.gives_list() => ColumnKind::Array,
            _ => ColumnKind::Number,
        };
        out_defs.push(ColumnDef::optional(agg.output(), out_kind));
        columns.push((i, reducer));
    }
    Ok((out_defs, columns))
}

/// The bucket `[start, end)` holding `ts`, worked out in i128 (as batch
/// `Sequence` does); `None` when an edge does not fit in an i64.
fn bucket_of(ts: i64, step: i64, anchor: i64) -> Option<(i64, i64)> {
    let (step, anchor) = (i128::from(step), i128::from(anchor));
    let start = (i128::from(ts) - anchor).div_euclid(step) * step + anchor;
    Some((
        i64::try_from(start).ok()?,
        i64::try_from(start + step).ok()?,
    ))
}

/// pond-ts `===` on two cells; array cells by their elements bit for bit
/// (pond-ts compares the array objects, so two equal arrays differ there).
fn same(a: &Value, b: &Value) -> bool {
    match (a, b) {
        (Value::Number(x), Value::Number(y)) => x == y,
        (Value::Array(x), Value::Array(y)) => {
            x.len() == y.len()
                && x.iter().zip(y).all(|(p, q)| match (p, q) {
                    (Scalar::Number(p), Scalar::Number(q)) => p.to_bits() == q.to_bits(),
                    _ => p == q,
                })
        }
        _ => a == b,
    }
}

/// One output column's state for one bucket (pond-ts `bucketState`).
enum BucketState {
    Count(u64),
    Sum(f64),
    Avg {
        s: f64,
        n: u64,
    },
    Min(Option<f64>),
    Max(Option<f64>),
    First(Option<Value>),
    Last(Option<Value>),
    /// `median` and percentiles: every number, sorted at snapshot.
    Sorted {
        q: f64,
        values: Vec<f64>,
    },
    Stdev {
        n: f64,
        mean: f64,
        m2: f64,
    },
    Difference {
        lo: Option<f64>,
        hi: Option<f64>,
    },
    Keep {
        first: Option<Value>,
        same: bool,
    },
    Unique(HashMap<ScalarKey, Scalar>),
    Top {
        n: usize,
        counts: HashMap<ScalarKey, (Scalar, u64)>,
    },
    Samples(Vec<Scalar>),
}

impl BucketState {
    fn new(reducer: Reducer) -> Self {
        match reducer {
            Reducer::Count => Self::Count(0),
            Reducer::Sum => Self::Sum(0.0),
            Reducer::Avg => Self::Avg { s: 0.0, n: 0 },
            Reducer::Min => Self::Min(None),
            Reducer::Max => Self::Max(None),
            Reducer::First => Self::First(None),
            Reducer::Last => Self::Last(None),
            Reducer::Median => Self::Sorted {
                q: 50.0,
                values: Vec::new(),
            },
            Reducer::Percentile(q) => Self::Sorted {
                q,
                values: Vec::new(),
            },
            Reducer::Stdev => Self::Stdev {
                n: 0.0,
                mean: 0.0,
                m2: 0.0,
            },
            Reducer::Difference => Self::Difference { lo: None, hi: None },
            Reducer::Keep => Self::Keep {
                first: None,
                same: true,
            },
            Reducer::Unique => Self::Unique(HashMap::new()),
            Reducer::Top(n) => Self::Top {
                n,
                counts: HashMap::new(),
            },
            Reducer::Samples => Self::Samples(Vec::new()),
        }
    }

    /// Add one present cell.
    fn add(&mut self, v: &Value) {
        let x = v.as_f64();
        match self {
            Self::Count(n) => *n += 1,
            Self::Sum(s) => *s += x.expect("number column"),
            Self::Avg { s, n } => {
                *s += x.expect("number column");
                *n += 1;
            }
            Self::Min(lo) => {
                let x = x.expect("number column");
                if lo.is_none_or(|lo| x < lo) {
                    *lo = Some(x);
                }
            }
            Self::Max(hi) => {
                let x = x.expect("number column");
                if hi.is_none_or(|hi| x > hi) {
                    *hi = Some(x);
                }
            }
            Self::First(first) => {
                if first.is_none() {
                    *first = Some(v.clone());
                }
            }
            Self::Last(last) => *last = Some(v.clone()),
            Self::Sorted { values, .. } => values.push(x.expect("number column")),
            Self::Stdev { n, mean, m2 } => {
                let x = x.expect("number column");
                *n += 1.0;
                let delta = x - *mean;
                *mean += delta / *n;
                *m2 += delta * (x - *mean);
            }
            Self::Difference { lo, hi } => {
                let x = x.expect("number column");
                if lo.is_none_or(|lo| x < lo) {
                    *lo = Some(x);
                }
                if hi.is_none_or(|hi| x > hi) {
                    *hi = Some(x);
                }
            }
            Self::Keep { first, same: all } => match first {
                None => *first = Some(v.clone()),
                Some(f) => {
                    if !same(f, v) {
                        *all = false;
                    }
                }
            },
            Self::Unique(set) => scalars(v, |s| {
                set.entry(scalar_key(s)).or_insert_with(|| as_key(s));
            }),
            Self::Top { counts, .. } => scalars(v, |s| {
                counts
                    .entry(scalar_key(s))
                    .or_insert_with(|| (as_key(s), 0))
                    .1 += 1;
            }),
            Self::Samples(items) => scalars(v, |s| items.push(s.clone())),
        }
    }

    fn snapshot(&self) -> Option<Value> {
        match self {
            Self::Count(n) => number(*n as f64),
            Self::Sum(s) => number(*s),
            Self::Avg { s, n } => (*n != 0).then(|| s / *n as f64).and_then(number),
            Self::Min(v) | Self::Max(v) => v.and_then(number),
            Self::First(v) | Self::Last(v) => v.clone(),
            Self::Sorted { q, values } => {
                if values.is_empty() {
                    return None;
                }
                // JavaScript's `sort((a, b) => a - b)`: stable, `0` and `-0`
                // equal.
                let mut sorted = values.clone();
                sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));
                number(percentile_of_sorted(&sorted, *q))
            }
            Self::Stdev { n, m2, .. } => (*n != 0.0)
                .then(|| clamp_at_zero(m2 / n).sqrt())
                .and_then(number),
            Self::Difference { lo, hi } => match (lo, hi) {
                (Some(lo), Some(hi)) => number(hi - lo),
                _ => None,
            },
            Self::Keep { first, same } => first.clone().filter(|_| *same),
            Self::Unique(set) => {
                let mut out: Vec<Scalar> = set.values().cloned().collect();
                out.sort_by(compare_scalars);
                Some(Value::Array(out))
            }
            Self::Top { n, counts } => {
                let mut entries: Vec<&(Scalar, u64)> = counts.values().collect();
                entries.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| compare_scalars(&a.0, &b.0)));
                Some(Value::Array(
                    entries.iter().take(*n).map(|e| e.0.clone()).collect(),
                ))
            }
            Self::Samples(items) => Some(Value::Array(items.clone())),
        }
    }
}