etl-unit 0.1.0

Semantic data model for ETL units — qualities and measurements over subjects and time. Built on Polars.
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
//! Per-measurement resampling decision for interval reports.
//!
//! The [`ResamplingPlanner`] is a pure function wrapped in a struct: it
//! takes one measurement's configuration and the request's
//! [`ReportInterval`], and returns a [`ResamplingPlan`] describing what
//! the imperative pipeline should do. No polars work, no side effects —
//! the decision is independently testable.
//!
//! # Decision rule (per measurement, given a report interval)
//!
//! Let `native = unit.sample_rate_ms` and `target = interval.approximate_ms`.
//!
//! - `native == target`       → [`ResamplingPath::Passthrough`]
//! - `native <  target`       → [`ResamplingPath::Aggregate`]  (always — only honest option)
//! - `native >  target`       → depends on [`RateStrategy`]:
//!     * `Auto` → [`ResamplingPath::Upsample`] when the measurement's
//!                schema declared an `upsample_strategy` (the author
//!                opted into upsampling for this measurement); otherwise
//!                [`ResamplingPath::Sparse`]. This is the honest default:
//!                follow schema intent, never fabricate values the
//!                author didn't sanction.
//!     * `AggregateOrSparse` → [`ResamplingPath::Sparse`] always; an
//!                             explicit override of `Auto` for callers
//!                             who want to ignore the schema's upsample
//!                             declaration.
//!     * `Upsample` → [`ResamplingPath::Upsample`] when
//!                    `upsample_strategy` is declared; falls back to
//!                    [`ResamplingPath::Sparse`] otherwise (can't force
//!                    what's not configured).
//!     * `Native`   → [`ResamplingPath::Sparse`] always.
//!
//! The aggregation function used by [`ResamplingPath::Aggregate`] comes
//! from the request override when present (keyed by measurement name),
//! otherwise from the measurement's schema-configured
//! `signal_aggregation`.

use serde::{Deserialize, Serialize};

use super::{RateStrategy, ReportInterval};
use crate::{CanonicalColumnName, aggregation::Aggregate, unit::MeasurementUnit};

// ============================================================================
// Plan output types
// ============================================================================

/// How a single measurement's data maps onto the interval grid. Derived
/// purely from the measurement config and the request.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResamplingPlan {
    pub measurement: CanonicalColumnName,
    pub path: ResamplingPath,
    /// The bucket's approximate length in ms. Purely informative — the
    /// actual boundaries come from [`super::IntervalBucket`]'s
    /// calendar-aware truncation at apply time.
    pub target_rate_ms: i64,
    /// The measurement's native sample rate in ms (copied for
    /// diagnostics — `Option` because it's theoretically possible for a
    /// measurement to arrive without one, even though schema validation
    /// rejects that today).
    pub native_rate_ms: Option<i64>,
    pub aggregation: Aggregate,
    pub aggregation_source: AggregationSource,
    /// One-sentence human-readable explanation, surfaced in diagnostics.
    pub reason: String,
}

/// The per-measurement resampling action.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ResamplingPath {
    /// `native_rate == target`: no resampling. The measurement's signal
    /// policy output lands directly on the report grid.
    Passthrough,
    /// `native_rate < target`: fold many native cells into one report
    /// cell via the chosen aggregation. The only honest option when the
    /// native has more resolution than the report.
    Aggregate,
    /// `native_rate > target`: fill intermediate report cells from the
    /// most recent native cell (forward-fill or interpolate, per the
    /// measurement's `upsample_strategy`). Fabricates cells; use with
    /// care.
    Upsample,
    /// `native_rate > target`: keep the measurement at its native rate.
    /// Cells on the report grid that don't align with a native
    /// observation remain null. Most honest at the cost of visual
    /// density.
    Sparse,
}

/// Where the aggregation function came from.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AggregationSource {
    /// From `MeasurementUnit::signal_aggregation()`.
    Schema,
    /// From `ReportInterval::aggregation_override` for this measurement.
    Override,
}

// ============================================================================
// Planner
// ============================================================================

/// Pure decision machinery for resampling one measurement onto a report
/// interval. All inputs the decision depends on live here; `plan()`
/// returns a [`ResamplingPlan`] without touching a DataFrame.
pub struct ResamplingPlanner<'m, 'i> {
    unit: &'m MeasurementUnit,
    interval: &'i ReportInterval,
}

impl<'m, 'i> ResamplingPlanner<'m, 'i> {
    pub fn new(unit: &'m MeasurementUnit, interval: &'i ReportInterval) -> Self {
        Self { unit, interval }
    }

    pub fn plan(&self) -> ResamplingPlan {
        let native_rate_ms = self.unit.sample_rate_ms;
        let target_rate_ms = self.interval.bucket.approximate_ms();

        let (aggregation, aggregation_source) = self.choose_aggregation();
        let (path, reason) = self.choose_path(native_rate_ms, target_rate_ms);

        ResamplingPlan {
            measurement: self.unit.name.clone(),
            path,
            target_rate_ms,
            native_rate_ms,
            aggregation,
            aggregation_source,
            reason,
        }
    }

    fn choose_aggregation(&self) -> (Aggregate, AggregationSource) {
        if let Some(ref overrides) = self.interval.aggregation_override
            && let Some(agg) = overrides.get(&self.unit.name)
        {
            return (*agg, AggregationSource::Override);
        }
        (self.unit.signal_aggregation(), AggregationSource::Schema)
    }

    fn choose_path(
        &self,
        native_rate_ms: Option<i64>,
        target_rate_ms: i64,
    ) -> (ResamplingPath, String) {
        // WholeWindow is a special bucket: the whole request window is
        // one bucket, so every measurement's native cells fold in.
        // Always Aggregate regardless of native rate or strategy.
        if matches!(self.interval.bucket, super::IntervalBucket::WholeWindow,) {
            return (
                ResamplingPath::Aggregate,
                "whole-window bucket folds every observation per subject".into(),
            );
        }

        let Some(native) = native_rate_ms else {
            return (
                ResamplingPath::Passthrough,
                "no native sample rate configured — passthrough".into(),
            );
        };

        if native == target_rate_ms {
            return (
                ResamplingPath::Passthrough,
                format!("native rate {native}ms matches interval"),
            );
        }

        if native < target_rate_ms {
            return (
                ResamplingPath::Aggregate,
                format!(
                    "native {native}ms finer than interval {target_rate_ms}ms — aggregate \
					 (no upsample/downsample choice needed)"
                ),
            );
        }

        // native > target: the middle case where strategy and schema
        // config combine.
        let has_upsample_strategy = self.unit.upsample_strategy.is_some();
        match self.interval.strategy {
            RateStrategy::Auto => {
                if has_upsample_strategy {
                    (
                        ResamplingPath::Upsample,
                        format!(
                            "native {native}ms coarser than interval {target_rate_ms}ms; \
							 schema declares upsample_strategy — honoring author intent"
                        ),
                    )
                } else {
                    (
                        ResamplingPath::Sparse,
                        format!(
                            "native {native}ms coarser than interval {target_rate_ms}ms; \
							 no upsample_strategy declared on measurement — sparse"
                        ),
                    )
                }
            }
            RateStrategy::Upsample => {
                if has_upsample_strategy {
                    (
                        ResamplingPath::Upsample,
                        format!(
                            "native {native}ms coarser than interval {target_rate_ms}ms, \
							 strategy=Upsample with declared upsample_strategy — forward-fill"
                        ),
                    )
                } else {
                    (
                        ResamplingPath::Sparse,
                        format!(
                            "native {native}ms coarser than interval {target_rate_ms}ms, \
							 strategy=Upsample but measurement has no upsample_strategy — \
							 cannot force what isn't configured; sparse"
                        ),
                    )
                }
            }
            RateStrategy::AggregateOrSparse => (
                ResamplingPath::Sparse,
                format!(
                    "native {native}ms coarser than interval {target_rate_ms}ms, \
					 strategy=AggregateOrSparse — sparse (ignores schema's upsample_strategy)"
                ),
            ),
            RateStrategy::Native => (
                ResamplingPath::Sparse,
                format!(
                    "native {native}ms coarser than interval {target_rate_ms}ms, \
					 strategy=Native — sparse on report grid"
                ),
            ),
        }
    }
}

// ============================================================================
// Unit tests
// ============================================================================

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use super::*;
    use crate::{
        MeasurementKind, ResampleStrategy, interval::IntervalBucket, signal_policy::SignalPolicy,
        unit::MeasurementUnit,
    };

    // ------------------------------------------------------------------------
    // Fixture helpers
    // ------------------------------------------------------------------------

    fn unit_named(name: &str, kind: MeasurementKind, native_rate_ms: i64) -> MeasurementUnit {
        MeasurementUnit::new("subject", "time", name, kind)
            .with_signal_policy(SignalPolicy::instant())
            .with_sample_rate_ms(native_rate_ms)
    }

    fn sump_unit() -> MeasurementUnit {
        unit_named("sump", MeasurementKind::Measure, 60_000) // 60s
    }

    fn precip_unit() -> MeasurementUnit {
        unit_named("historical_precip", MeasurementKind::Measure, 3_600_000) // 1h
    }

    fn monthly_auto() -> ReportInterval {
        ReportInterval {
            bucket: IntervalBucket::Months(1),
            strategy: RateStrategy::Auto,
            aggregation_override: None,
            empty_bucket: super::super::EmptyBucketPolicy::Null,
        }
    }

    fn five_min_auto() -> ReportInterval {
        ReportInterval {
            bucket: IntervalBucket::Fixed {
                duration_ms: 5 * 60_000,
            },
            strategy: RateStrategy::Auto,
            aggregation_override: None,
            empty_bucket: super::super::EmptyBucketPolicy::Null,
        }
    }

    fn one_min_auto() -> ReportInterval {
        ReportInterval {
            bucket: IntervalBucket::Fixed {
                duration_ms: 60_000,
            },
            strategy: RateStrategy::Auto,
            aggregation_override: None,
            empty_bucket: super::super::EmptyBucketPolicy::Null,
        }
    }

    // ------------------------------------------------------------------------
    // Path: Aggregate (native finer than interval)
    // ------------------------------------------------------------------------

    #[test]
    fn aggregate_when_native_finer_than_interval() {
        // sump is 60s; monthly bucket is much coarser.
        let plan = ResamplingPlanner::new(&sump_unit(), &monthly_auto()).plan();
        assert_eq!(plan.path, ResamplingPath::Aggregate);
        assert_eq!(plan.aggregation, Aggregate::Mean);
        assert_eq!(plan.aggregation_source, AggregationSource::Schema);
    }

    #[test]
    fn aggregate_path_independent_of_strategy_when_native_finer() {
        // Every strategy picks Aggregate when native < interval — there's
        // no other honest option.
        for strategy in [
            RateStrategy::Auto,
            RateStrategy::Upsample,
            RateStrategy::Native,
            RateStrategy::AggregateOrSparse,
        ] {
            let interval = ReportInterval {
                bucket: IntervalBucket::Months(1),
                strategy,
                aggregation_override: None,
                empty_bucket: super::super::EmptyBucketPolicy::Null,
            };
            let plan = ResamplingPlanner::new(&sump_unit(), &interval).plan();
            assert_eq!(
                plan.path,
                ResamplingPath::Aggregate,
                "strategy {strategy:?} must choose Aggregate when native < interval",
            );
        }
    }

    // ------------------------------------------------------------------------
    // Path: Passthrough (native == interval)
    // ------------------------------------------------------------------------

    #[test]
    fn passthrough_when_native_matches_interval() {
        // sump is 60s; request interval is also 60s.
        let plan = ResamplingPlanner::new(&sump_unit(), &one_min_auto()).plan();
        assert_eq!(plan.path, ResamplingPath::Passthrough);
    }

    // ------------------------------------------------------------------------
    // Path: Sparse (native coarser than interval, Auto default)
    // ------------------------------------------------------------------------

    #[test]
    fn auto_sparse_when_native_coarser_and_no_upsample_declared() {
        // precip is 1h with NO upsample_strategy declared. 5-minute
        // interval. Auto honors schema intent: no upsample_strategy →
        // Sparse (honest; doesn't fabricate).
        let plan = ResamplingPlanner::new(&precip_unit(), &five_min_auto()).plan();
        assert_eq!(plan.path, ResamplingPath::Sparse);
        assert!(plan.reason.contains("no upsample_strategy declared"));
    }

    #[test]
    fn auto_upsamples_when_schema_declares_upsample_strategy() {
        // Same precip (1h) but WITH upsample_strategy = ForwardFill
        // declared in the schema. The author has opted in to
        // upsampling this measurement. Auto honors that — no need for
        // the caller to pass strategy=Upsample explicitly.
        let precip = precip_unit().with_upsample(ResampleStrategy::ForwardFill);
        let plan = ResamplingPlanner::new(&precip, &five_min_auto()).plan();
        assert_eq!(
            plan.path,
            ResamplingPath::Upsample,
            "Auto + schema upsample_strategy → Upsample (honor author intent)",
        );
        assert!(plan.reason.contains("honoring author intent"));
    }

    #[test]
    fn aggregate_or_sparse_ignores_schema_upsample_declaration() {
        // Even with upsample_strategy declared, AggregateOrSparse forces
        // Sparse. It's the explicit "never upsample" override for callers
        // who want to ignore the schema's opt-in.
        let precip = precip_unit().with_upsample(ResampleStrategy::ForwardFill);
        let interval = ReportInterval {
            bucket: IntervalBucket::Fixed {
                duration_ms: 5 * 60_000,
            },
            strategy: RateStrategy::AggregateOrSparse,
            aggregation_override: None,
            empty_bucket: super::super::EmptyBucketPolicy::Null,
        };
        let plan = ResamplingPlanner::new(&precip, &interval).plan();
        assert_eq!(plan.path, ResamplingPath::Sparse);
        assert!(plan.reason.contains("ignores schema's upsample_strategy"));
    }

    #[test]
    fn sparse_when_native_strategy_and_native_coarser() {
        let interval = ReportInterval {
            bucket: IntervalBucket::Fixed {
                duration_ms: 5 * 60_000,
            },
            strategy: RateStrategy::Native,
            aggregation_override: None,
            empty_bucket: super::super::EmptyBucketPolicy::Null,
        };
        let plan = ResamplingPlanner::new(&precip_unit(), &interval).plan();
        assert_eq!(plan.path, ResamplingPath::Sparse);
    }

    // ------------------------------------------------------------------------
    // Path: Upsample (native coarser, strategy requests it, strategy available)
    // ------------------------------------------------------------------------

    #[test]
    fn upsample_when_strategy_requests_and_measurement_declares_upsample() {
        let precip = precip_unit().with_upsample(ResampleStrategy::ForwardFill);
        let interval = ReportInterval {
            bucket: IntervalBucket::Fixed {
                duration_ms: 5 * 60_000,
            },
            strategy: RateStrategy::Upsample,
            aggregation_override: None,
            empty_bucket: super::super::EmptyBucketPolicy::Null,
        };
        let plan = ResamplingPlanner::new(&precip, &interval).plan();
        assert_eq!(plan.path, ResamplingPath::Upsample);
    }

    #[test]
    fn falls_back_to_sparse_when_upsample_requested_but_not_declared() {
        // precip has no upsample_strategy. Request Upsample — we can't.
        // The planner falls back to Sparse rather than silently doing
        // nothing surprising.
        let interval = ReportInterval {
            bucket: IntervalBucket::Fixed {
                duration_ms: 5 * 60_000,
            },
            strategy: RateStrategy::Upsample,
            aggregation_override: None,
            empty_bucket: super::super::EmptyBucketPolicy::Null,
        };
        let plan = ResamplingPlanner::new(&precip_unit(), &interval).plan();
        assert_eq!(plan.path, ResamplingPath::Sparse);
        assert!(plan.reason.contains("no upsample_strategy"));
    }

    // ------------------------------------------------------------------------
    // Aggregation source: schema vs override
    // ------------------------------------------------------------------------

    #[test]
    fn aggregation_uses_schema_default_when_no_override() {
        let plan = ResamplingPlanner::new(&sump_unit(), &monthly_auto()).plan();
        assert_eq!(plan.aggregation, Aggregate::Mean);
        assert_eq!(plan.aggregation_source, AggregationSource::Schema);
    }

    #[test]
    fn aggregation_override_wins_over_schema_default() {
        let mut overrides = HashMap::new();
        overrides.insert(CanonicalColumnName::new("sump"), Aggregate::Max);

        let interval = ReportInterval {
            bucket: IntervalBucket::Months(1),
            strategy: RateStrategy::Auto,
            aggregation_override: Some(overrides),
            empty_bucket: super::super::EmptyBucketPolicy::Null,
        };
        let plan = ResamplingPlanner::new(&sump_unit(), &interval).plan();
        assert_eq!(plan.aggregation, Aggregate::Max);
        assert_eq!(plan.aggregation_source, AggregationSource::Override);
    }

    #[test]
    fn aggregation_override_for_different_measurement_is_ignored() {
        // Override keyed on "precip" shouldn't affect sump's plan.
        let mut overrides = HashMap::new();
        overrides.insert(
            CanonicalColumnName::new("historical_precip"),
            Aggregate::Sum,
        );

        let interval = ReportInterval {
            bucket: IntervalBucket::Months(1),
            strategy: RateStrategy::Auto,
            aggregation_override: Some(overrides),
            empty_bucket: super::super::EmptyBucketPolicy::Null,
        };
        let plan = ResamplingPlanner::new(&sump_unit(), &interval).plan();
        assert_eq!(plan.aggregation, Aggregate::Mean);
        assert_eq!(plan.aggregation_source, AggregationSource::Schema);
    }

    // ------------------------------------------------------------------------
    // Plan metadata
    // ------------------------------------------------------------------------

    #[test]
    fn plan_records_measurement_name_and_rates() {
        let plan = ResamplingPlanner::new(&sump_unit(), &monthly_auto()).plan();
        assert_eq!(plan.measurement, CanonicalColumnName::new("sump"));
        assert_eq!(plan.native_rate_ms, Some(60_000));
        assert_eq!(
            plan.target_rate_ms,
            IntervalBucket::Months(1).approximate_ms()
        );
        assert!(
            !plan.reason.is_empty(),
            "reason must be populated for diagnostics"
        );
    }

    // ------------------------------------------------------------------------
    // IntervalBucket approximate_ms sanity
    // ------------------------------------------------------------------------

    #[test]
    fn interval_bucket_approximate_ms_orders_correctly() {
        // The planner only needs ordering correctness, not exact values.
        let minute = IntervalBucket::Fixed {
            duration_ms: 60_000,
        }
        .approximate_ms();
        let hour = IntervalBucket::Hours(1).approximate_ms();
        let day = IntervalBucket::Days(1).approximate_ms();
        let week = IntervalBucket::Weeks(1).approximate_ms();
        let month = IntervalBucket::Months(1).approximate_ms();
        let whole = IntervalBucket::WholeWindow.approximate_ms();

        assert!(minute < hour);
        assert!(hour < day);
        assert!(day < week);
        assert!(week < month);
        assert!(month < whole, "WholeWindow must sort coarsest");
    }

    // ------------------------------------------------------------------------
    // WholeWindow always picks Aggregate
    // ------------------------------------------------------------------------

    fn whole_window(strategy: RateStrategy) -> ReportInterval {
        ReportInterval {
            bucket: IntervalBucket::WholeWindow,
            strategy,
            aggregation_override: None,
            empty_bucket: super::super::EmptyBucketPolicy::Null,
        }
    }

    #[test]
    fn whole_window_always_picks_aggregate_regardless_of_strategy() {
        // Native rates varying, strategies varying — WholeWindow always
        // picks Aggregate because the bucket subsumes everything.
        for strategy in [
            RateStrategy::Auto,
            RateStrategy::Upsample,
            RateStrategy::Native,
            RateStrategy::AggregateOrSparse,
        ] {
            let plan = ResamplingPlanner::new(&sump_unit(), &whole_window(strategy)).plan();
            assert_eq!(
                plan.path,
                ResamplingPath::Aggregate,
                "WholeWindow must pick Aggregate for strategy={strategy:?}",
            );
            assert!(
                plan.reason.contains("whole-window"),
                "reason must name the bucket: got {:?}",
                plan.reason,
            );
        }
    }

    #[test]
    fn whole_window_aggregation_honors_override() {
        let mut overrides = HashMap::new();
        overrides.insert(CanonicalColumnName::new("sump"), Aggregate::Max);
        let interval = ReportInterval {
            bucket: IntervalBucket::WholeWindow,
            strategy: RateStrategy::Auto,
            aggregation_override: Some(overrides),
            empty_bucket: super::super::EmptyBucketPolicy::Null,
        };
        let plan = ResamplingPlanner::new(&sump_unit(), &interval).plan();
        assert_eq!(plan.path, ResamplingPath::Aggregate);
        assert_eq!(plan.aggregation, Aggregate::Max);
        assert_eq!(plan.aggregation_source, AggregationSource::Override);
    }
}