pandrs 0.4.1

A high-performance DataFrame library for Rust, providing pandas-like API with advanced features including SIMD optimization, parallel processing, and distributed computing capabilities
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
//! Windowed aggregations for streaming data processing
//!
//! This module provides sophisticated window types and windowed aggregation
//! capabilities for streaming data, including:
//!
//! - Tumbling windows (fixed-size, non-overlapping)
//! - Sliding windows (fixed-size, overlapping)
//! - Session windows (gap-based)
//! - Count-based windows
//! - Custom aggregation functions

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::time::Duration;

use super::StreamRecord;
use crate::error::{Error, Result};

/// Types of windows for stream processing
#[derive(Debug, Clone)]
pub enum WindowType {
    /// Fixed-size non-overlapping windows
    Tumbling {
        /// Window size in duration
        size: Duration,
    },
    /// Fixed-size overlapping windows
    Sliding {
        /// Window size in duration
        size: Duration,
        /// Slide interval
        slide: Duration,
    },
    /// Variable-size windows based on activity gaps
    Session {
        /// Maximum gap between events
        gap: Duration,
        /// Maximum session duration (optional)
        max_duration: Option<Duration>,
    },
    /// Windows based on record count
    Count {
        /// Number of records per window
        size: usize,
        /// Slide by count (for sliding count windows)
        slide: Option<usize>,
    },
    /// Global window (all records in a single window)
    Global,
}

impl Default for WindowType {
    fn default() -> Self {
        WindowType::Tumbling {
            size: Duration::from_secs(60),
        }
    }
}

/// Configuration for windowed processing
#[derive(Debug, Clone)]
pub struct WindowConfig {
    /// Type of window
    pub window_type: WindowType,
    /// Allowed lateness for late-arriving records
    pub allowed_lateness: Duration,
    /// Whether to emit on every record (vs only on window close)
    pub emit_on_every_record: bool,
    /// Whether to include partial windows
    pub include_partial_windows: bool,
    /// Watermark delay for event-time processing
    pub watermark_delay: Duration,
    /// For [`WindowType::Session`], the name of the record field whose
    /// value keys separate concurrent sessions (e.g. `"user_id"`, so each
    /// user accumulates independent sessions instead of all records sharing
    /// one global session). `None` (the default) means every record shares
    /// a single global session key -- documented explicitly here since that
    /// is rarely what a real session-window use case wants, but is the only
    /// sensible default in the absence of a configured key.
    pub session_key_field: Option<String>,
}

impl Default for WindowConfig {
    fn default() -> Self {
        WindowConfig {
            window_type: WindowType::default(),
            allowed_lateness: Duration::from_secs(0),
            emit_on_every_record: false,
            include_partial_windows: false,
            watermark_delay: Duration::from_secs(1),
            session_key_field: None,
        }
    }
}

/// Builder for WindowConfig
pub struct WindowConfigBuilder {
    config: WindowConfig,
}

impl WindowConfigBuilder {
    /// Creates a new builder
    pub fn new() -> Self {
        WindowConfigBuilder {
            config: WindowConfig::default(),
        }
    }

    /// Sets a tumbling window
    pub fn tumbling(mut self, size: Duration) -> Self {
        self.config.window_type = WindowType::Tumbling { size };
        self
    }

    /// Sets a sliding window.
    ///
    /// Note: a `slide` much smaller than `size` makes each record belong to
    /// `size / slide` overlapping windows simultaneously (e.g. a 1-hour
    /// window with a 1ms slide means roughly 3.6 million overlapping
    /// windows are touched per record) -- keep this ratio reasonable for
    /// your workload.
    pub fn sliding(mut self, size: Duration, slide: Duration) -> Self {
        self.config.window_type = WindowType::Sliding { size, slide };
        self
    }

    /// Sets a session window
    pub fn session(mut self, gap: Duration, max_duration: Option<Duration>) -> Self {
        self.config.window_type = WindowType::Session { gap, max_duration };
        self
    }

    /// Sets a count-based window
    pub fn count(mut self, size: usize, slide: Option<usize>) -> Self {
        self.config.window_type = WindowType::Count { size, slide };
        self
    }

    /// Sets a global window
    pub fn global(mut self) -> Self {
        self.config.window_type = WindowType::Global;
        self
    }

    /// Sets allowed lateness
    pub fn allowed_lateness(mut self, lateness: Duration) -> Self {
        self.config.allowed_lateness = lateness;
        self
    }

    /// Sets emit on every record
    pub fn emit_on_every_record(mut self, emit: bool) -> Self {
        self.config.emit_on_every_record = emit;
        self
    }

    /// Sets include partial windows
    pub fn include_partial_windows(mut self, include: bool) -> Self {
        self.config.include_partial_windows = include;
        self
    }

    /// Sets watermark delay
    pub fn watermark_delay(mut self, delay: Duration) -> Self {
        self.config.watermark_delay = delay;
        self
    }

    /// Sets the record field used to key separate sessions for
    /// [`WindowType::Session`]. See [`WindowConfig::session_key_field`].
    pub fn session_key_field(mut self, field: impl Into<String>) -> Self {
        self.config.session_key_field = Some(field.into());
        self
    }

    /// Builds the config.
    ///
    /// Validates window-size invariants that would otherwise cause a panic
    /// deep inside stream processing (`Duration::ZERO` triggering a
    /// divide-by-zero in window alignment) or pathological/unbounded
    /// behavior (a `slide` larger than `size`) rather than being caught at
    /// construction time.
    pub fn build(self) -> Result<WindowConfig> {
        match &self.config.window_type {
            WindowType::Tumbling { size } => {
                if size.is_zero() {
                    return Err(Error::InvalidInput(
                        "tumbling window size must be greater than zero".into(),
                    ));
                }
            }
            WindowType::Sliding { size, slide } => {
                if size.is_zero() {
                    return Err(Error::InvalidInput(
                        "sliding window size must be greater than zero".into(),
                    ));
                }
                if slide.is_zero() {
                    return Err(Error::InvalidInput(
                        "sliding window slide must be greater than zero".into(),
                    ));
                }
                if slide > size {
                    return Err(Error::InvalidInput(
                        "sliding window slide must not exceed size".into(),
                    ));
                }
            }
            WindowType::Count { size, slide } => {
                if *size == 0 {
                    return Err(Error::InvalidInput(
                        "count window size must be greater than zero".into(),
                    ));
                }
                if let Some(slide) = slide {
                    if *slide == 0 {
                        return Err(Error::InvalidInput(
                            "count window slide must be greater than zero".into(),
                        ));
                    }
                    if *slide > *size {
                        return Err(Error::InvalidInput(
                            "count window slide must not exceed size".into(),
                        ));
                    }
                }
            }
            WindowType::Session { .. } | WindowType::Global => {}
        }

        Ok(self.config)
    }
}

impl Default for WindowConfigBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Represents a time window
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeWindow {
    /// Window start time
    pub start: DateTime<Utc>,
    /// Window end time
    pub end: DateTime<Utc>,
    /// Window identifier
    pub id: String,
}

impl TimeWindow {
    /// Creates a new time window.
    ///
    /// `id` is derived at nanosecond precision (falling back, saturating
    /// rather than overflowing/panicking, to a second-precision value for
    /// timestamps outside chrono's nanosecond-representable range, such as
    /// the sentinel `DateTime::MIN_UTC`/`MAX_UTC` used for
    /// [`WindowType::Global`]). A second-precision id, as this used
    /// unconditionally before, can collide for two genuinely distinct
    /// windows that happen to start and end within the same wall-clock
    /// second -- which is routine for count-based windows (see
    /// `WindowedAggregator::process_count`, whose windows' `start`/`end` are
    /// the real observed event times of their records, often only
    /// microseconds apart under any real load) and for a burst of
    /// high-frequency tumbling/sliding windows.
    pub fn new(start: DateTime<Utc>, end: DateTime<Utc>) -> Self {
        let start_id = start
            .timestamp_nanos_opt()
            .unwrap_or_else(|| start.timestamp().saturating_mul(1_000_000_000));
        let end_id = end
            .timestamp_nanos_opt()
            .unwrap_or_else(|| end.timestamp().saturating_mul(1_000_000_000));
        let id = format!("{}-{}", start_id, end_id);
        TimeWindow { start, end, id }
    }

    /// Checks if a timestamp falls within this window
    pub fn contains(&self, timestamp: DateTime<Utc>) -> bool {
        timestamp >= self.start && timestamp < self.end
    }

    /// Gets the duration of the window
    pub fn duration(&self) -> Duration {
        let diff = self.end.signed_duration_since(self.start);
        Duration::from_millis(diff.num_milliseconds().max(0) as u64)
    }
}

/// Aggregation function types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WindowAggregation {
    /// Sum of values
    Sum,
    /// Average (mean) of values
    Avg,
    /// Minimum value
    Min,
    /// Maximum value
    Max,
    /// Count of values
    Count,
    /// First value in window
    First,
    /// Last value in window
    Last,
    /// Standard deviation
    StdDev,
    /// Variance
    Variance,
    /// Median (50th percentile)
    Median,
    /// Percentile (specified, 0-100)
    Percentile(u8),
}

/// Result of a windowed aggregation
#[derive(Debug, Clone)]
pub struct WindowResult {
    /// The window this result belongs to
    pub window: TimeWindow,
    /// Aggregated values by column
    pub values: HashMap<String, f64>,
    /// Number of records in the window
    pub count: usize,
    /// When the result was emitted
    pub emitted_at: DateTime<Utc>,
}

/// A record that was rejected as too late for its target window (see
/// `WindowedAggregator::is_too_late`). Captured with just enough context
/// for diagnostics/side-channel handling rather than a full [`StreamRecord`]
/// clone (which would also retain every field of every dropped record for
/// the lifetime of the aggregator).
#[derive(Debug, Clone)]
pub struct LateRecord {
    /// The id of the window this record would have belonged to.
    pub window_id: String,
    /// The record's event time.
    pub event_time: DateTime<Utc>,
    /// The parsed value that would have been aggregated.
    pub value: f64,
}

/// State for incremental aggregation
#[derive(Debug, Clone)]
struct AggregationState {
    /// Running sum
    sum: f64,
    /// Running count
    count: usize,
    /// Running min
    min: f64,
    /// Running max
    max: f64,
    /// First value seen
    first: Option<f64>,
    /// Last value seen
    last: Option<f64>,
    /// Welford's online mean, used both for `Avg` and as the basis for the
    /// numerically stable variance below.
    mean: f64,
    /// Welford's running sum of squared differences from the mean (`M2`).
    /// Population variance is `m2 / count`; this quantity is mathematically
    /// non-negative by construction (it is a literal sum of squares,
    /// computed incrementally without ever subtracting two similar-sized
    /// large numbers), unlike the previous `E[x^2] - mean^2` formula, which
    /// suffers catastrophic cancellation for data with a large mean and
    /// small spread (e.g. `1e9, 1e9+1, 1e9+2`) and could -- and did --
    /// produce a negative result there. `StdDev` happened to mask that with
    /// a `.max(0.0)` floor before taking the square root; `Variance` did
    /// not, and returned the raw negative value.
    m2: f64,
    /// All raw values seen, for percentile calculations. Populated *only*
    /// when the aggregation actually needs them (`Median`/`Percentile`);
    /// every other aggregation leaves this empty, since retaining every
    /// raw value indefinitely for a `Global`/long-lived `Sum` window would
    /// otherwise grow without bound.
    values: Vec<f64>,
    tracks_raw_values: bool,
}

impl AggregationState {
    fn new(aggregation: WindowAggregation) -> Self {
        AggregationState {
            sum: 0.0,
            count: 0,
            min: f64::INFINITY,
            max: f64::NEG_INFINITY,
            first: None,
            last: None,
            mean: 0.0,
            m2: 0.0,
            values: Vec::new(),
            tracks_raw_values: matches!(
                aggregation,
                WindowAggregation::Median | WindowAggregation::Percentile(_)
            ),
        }
    }

    fn update(&mut self, value: f64) {
        self.sum += value;
        self.count += 1;
        self.min = self.min.min(value);
        self.max = self.max.max(value);

        if self.first.is_none() {
            self.first = Some(value);
        }
        self.last = Some(value);

        // Welford's online algorithm.
        let delta = value - self.mean;
        self.mean += delta / self.count as f64;
        let delta2 = value - self.mean;
        self.m2 += delta * delta2;

        if self.tracks_raw_values {
            self.values.push(value);
        }
    }

    fn compute(&self, agg: WindowAggregation) -> f64 {
        match agg {
            WindowAggregation::Sum => self.sum,
            WindowAggregation::Avg => {
                if self.count > 0 {
                    self.mean
                } else {
                    0.0
                }
            }
            WindowAggregation::Min => {
                if self.count > 0 {
                    self.min
                } else {
                    0.0
                }
            }
            WindowAggregation::Max => {
                if self.count > 0 {
                    self.max
                } else {
                    0.0
                }
            }
            WindowAggregation::Count => self.count as f64,
            WindowAggregation::First => self.first.unwrap_or(0.0),
            WindowAggregation::Last => self.last.unwrap_or(0.0),
            WindowAggregation::StdDev => {
                if self.count > 1 {
                    (self.m2 / self.count as f64).sqrt()
                } else {
                    0.0
                }
            }
            WindowAggregation::Variance => {
                if self.count > 1 {
                    self.m2 / self.count as f64
                } else {
                    0.0
                }
            }
            WindowAggregation::Median => self.compute_percentile(50),
            WindowAggregation::Percentile(p) => self.compute_percentile(p),
        }
    }

    fn compute_percentile(&self, percentile: u8) -> f64 {
        if self.values.is_empty() {
            return 0.0;
        }

        let mut sorted = self.values.clone();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let p = (percentile as f64 / 100.0).clamp(0.0, 1.0);
        let idx = (p * (sorted.len() - 1) as f64).round() as usize;
        sorted[idx]
    }
}

/// What kind of window a [`WindowEntry`] represents, so
/// [`WindowedAggregator::close_expired_windows`] can tell time-based windows
/// (which it evicts on watermark passage) apart from count/session/global
/// windows (which complete via their own logic and must never be evicted by
/// the watermark -- previously, count windows were given a nonsensical
/// fabricated time range derived from interpreting the record *count* as
/// *seconds*, specifically so they could participate in this same
/// watermark-based eviction, which then either never fired at all or fired
/// for the wrong reason).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum WindowKind {
    /// Tumbling or sliding: closed by watermark passage.
    Time,
    /// Count-based: closed (and removed) as soon as it reaches its target
    /// count, in `process_count` itself.
    Count,
    /// Session: closed by a gap timeout or `max_duration`, in
    /// `process_session` itself.
    Session,
    /// Global: closed only by `flush()` at end-of-stream.
    Global,
}

/// One active window's window metadata, kind, and aggregation state.
#[derive(Debug, Clone)]
struct WindowEntry {
    window: TimeWindow,
    kind: WindowKind,
    state: AggregationState,
}

/// Manages windows and aggregations for streaming data
#[derive(Debug)]
pub struct WindowedAggregator {
    /// Configuration
    config: WindowConfig,
    /// Column to aggregate
    column: String,
    /// Aggregation function
    aggregation: WindowAggregation,
    /// Active windows and their states
    windows: HashMap<String, WindowEntry>,
    /// Closed window results
    results: Vec<WindowResult>,
    /// Current watermark
    watermark: DateTime<Utc>,
    /// Record count for count-based windows
    record_count: usize,
    /// Session start times by key
    session_starts: HashMap<String, DateTime<Utc>>,
    /// Last record time by key
    last_record_times: HashMap<String, DateTime<Utc>>,
    /// Ids of windows that have already closed, so a late-arriving record
    /// can never "resurrect" one under the same id as a lookalike -- but
    /// distinct -- window with an incomplete aggregate.
    closed_window_ids: HashSet<String>,
    /// Records rejected as too late for their target (time-based) window.
    late_records: Vec<LateRecord>,
}

impl WindowedAggregator {
    /// Creates a new windowed aggregator
    pub fn new(config: WindowConfig, column: &str, aggregation: WindowAggregation) -> Self {
        WindowedAggregator {
            config,
            column: column.to_string(),
            aggregation,
            windows: HashMap::new(),
            results: Vec::new(),
            watermark: Utc::now() - chrono::Duration::days(1),
            record_count: 0,
            session_starts: HashMap::new(),
            last_record_times: HashMap::new(),
            closed_window_ids: HashSet::new(),
            late_records: Vec::new(),
        }
    }

    /// Processes a record and returns any completed window results
    pub fn process(&mut self, record: &StreamRecord) -> Result<Vec<WindowResult>> {
        let mut completed_results = Vec::new();

        // Get the event time (or use current time if not available)
        let event_time = self.get_event_time(record);

        // Update watermark
        self.update_watermark(event_time);

        // Get the value to aggregate
        let value = record
            .fields
            .get(&self.column)
            .and_then(|v| v.parse::<f64>().ok())
            .unwrap_or(0.0);

        self.record_count += 1;

        // Handle based on window type
        match &self.config.window_type {
            WindowType::Tumbling { size } => {
                completed_results.extend(self.process_tumbling(event_time, value, *size)?);
            }
            WindowType::Sliding { size, slide } => {
                completed_results.extend(self.process_sliding(event_time, value, *size, *slide)?);
            }
            WindowType::Session { gap, max_duration } => {
                let key = self.session_key(record);
                completed_results.extend(self.process_session(
                    event_time,
                    value,
                    *gap,
                    *max_duration,
                    key,
                )?);
            }
            WindowType::Count { size, slide } => {
                completed_results.extend(self.process_count(event_time, value, *size, *slide)?);
            }
            WindowType::Global => {
                self.process_global(value);
            }
        }

        // Close time-based windows that are past the watermark. Count,
        // session, and global windows are intentionally excluded (see
        // `WindowKind`): they close via their own logic above, not via the
        // watermark.
        completed_results.extend(self.close_expired_windows()?);

        Ok(completed_results)
    }

    /// Gets the event time from a record
    fn get_event_time(&self, record: &StreamRecord) -> DateTime<Utc> {
        // Try to get event_time field from record
        if let Some(ts_str) = record.fields.get("event_time") {
            if let Ok(ts) = ts_str.parse::<i64>() {
                return DateTime::from_timestamp(ts, 0).unwrap_or_else(Utc::now);
            }
        }

        // Fall back to processing time
        Utc::now()
    }

    /// Determines the session key for `record`, per
    /// `WindowConfig::session_key_field`.
    fn session_key(&self, record: &StreamRecord) -> String {
        match &self.config.session_key_field {
            Some(field) => record
                .fields
                .get(field)
                .cloned()
                .unwrap_or_else(|| format!("__missing_{field}__")),
            None => "__global__".to_string(),
        }
    }

    /// Updates the watermark
    fn update_watermark(&mut self, event_time: DateTime<Utc>) {
        let watermark_candidate = event_time
            - chrono::Duration::from_std(self.config.watermark_delay)
                .unwrap_or(chrono::Duration::zero());

        if watermark_candidate > self.watermark {
            self.watermark = watermark_candidate;
        }
    }

    /// Returns true if `window_end` is too late to admit a record into
    /// (whether the window in question currently exists, has already
    /// closed and been removed, or was never created at all): its window
    /// would already be eligible for eviction even accounting for
    /// `allowed_lateness`. Because the watermark only ever moves forward,
    /// this check is at least as strict for any *previously* closed window
    /// as the check that closed it was, so it also prevents resurrecting a
    /// closed window id (belt-and-suspenders alongside the explicit
    /// `closed_window_ids` check in the time-based window processors).
    fn is_too_late(&self, window_end: DateTime<Utc>) -> bool {
        let watermark_with_lateness = self.watermark
            - chrono::Duration::from_std(self.config.allowed_lateness)
                .unwrap_or(chrono::Duration::zero());
        window_end <= watermark_with_lateness
    }

    /// Processes a tumbling window
    fn process_tumbling(
        &mut self,
        event_time: DateTime<Utc>,
        value: f64,
        size: Duration,
    ) -> Result<Vec<WindowResult>> {
        let window_start = self.align_to_window(event_time, size);
        let window_end =
            window_start + chrono::Duration::from_std(size).unwrap_or(chrono::Duration::zero());

        let window = TimeWindow::new(window_start, window_end);
        let window_id = window.id.clone();

        if self.closed_window_ids.contains(&window_id) || self.is_too_late(window_end) {
            self.late_records.push(LateRecord {
                window_id,
                event_time,
                value,
            });
            return Ok(vec![]);
        }

        let entry = self
            .windows
            .entry(window_id)
            .or_insert_with(|| WindowEntry {
                window: window.clone(),
                kind: WindowKind::Time,
                state: AggregationState::new(self.aggregation),
            });
        entry.state.update(value);

        // Emit if configured to emit on every record
        if self.config.emit_on_every_record {
            let result = WindowResult {
                window: entry.window.clone(),
                values: [(self.column.clone(), entry.state.compute(self.aggregation))]
                    .into_iter()
                    .collect(),
                count: entry.state.count,
                emitted_at: Utc::now(),
            };
            return Ok(vec![result]);
        }

        Ok(vec![])
    }

    /// Processes a sliding window
    fn process_sliding(
        &mut self,
        event_time: DateTime<Utc>,
        value: f64,
        size: Duration,
        slide: Duration,
    ) -> Result<Vec<WindowResult>> {
        let mut results = Vec::new();

        // Calculate all windows this record belongs to
        let slide_duration = chrono::Duration::from_std(slide).unwrap_or(chrono::Duration::zero());
        let size_duration = chrono::Duration::from_std(size).unwrap_or(chrono::Duration::zero());

        // Align to slide boundary
        let base_start = self.align_to_window(event_time, slide);

        // Go back to find all windows containing this event
        let mut window_start = base_start;
        while window_start + size_duration > event_time {
            let window_end = window_start + size_duration;
            let window = TimeWindow::new(window_start, window_end);
            let window_id = window.id.clone();

            // Skip (rather than resurrect or wastefully re-create-then-evict)
            // any of this record's overlapping sub-windows that are
            // individually too late; the record can still be admitted into
            // whichever of its other overlapping sub-windows are not.
            if !self.closed_window_ids.contains(&window_id) && !self.is_too_late(window_end) {
                let entry = self
                    .windows
                    .entry(window_id)
                    .or_insert_with(|| WindowEntry {
                        window: window.clone(),
                        kind: WindowKind::Time,
                        state: AggregationState::new(self.aggregation),
                    });
                entry.state.update(value);

                if self.config.emit_on_every_record {
                    let result = WindowResult {
                        window: entry.window.clone(),
                        values: [(self.column.clone(), entry.state.compute(self.aggregation))]
                            .into_iter()
                            .collect(),
                        count: entry.state.count,
                        emitted_at: Utc::now(),
                    };
                    results.push(result);
                }
            }

            window_start = window_start - slide_duration;
        }

        Ok(results)
    }

    /// Processes a session window
    fn process_session(
        &mut self,
        event_time: DateTime<Utc>,
        value: f64,
        gap: Duration,
        max_duration: Option<Duration>,
        key: String,
    ) -> Result<Vec<WindowResult>> {
        let mut results = Vec::new();

        let gap_duration = chrono::Duration::from_std(gap).unwrap_or(chrono::Duration::zero());

        // Check if we need to start a new session
        let last_time = self.last_record_times.get(&key).cloned();
        let session_start = self.session_starts.get(&key).cloned();

        let (should_start_new, close_old) = if let Some(last) = last_time {
            let time_since_last = event_time.signed_duration_since(last);
            if time_since_last > gap_duration {
                (true, session_start.is_some())
            } else if let (Some(start), Some(max)) = (session_start, max_duration) {
                let max_dur = chrono::Duration::from_std(max).unwrap_or(chrono::Duration::zero());
                if event_time.signed_duration_since(start) > max_dur {
                    (true, true)
                } else {
                    (false, false)
                }
            } else {
                (false, false)
            }
        } else {
            (true, false)
        };

        // Close old session if needed
        if close_old {
            if let Some(start) = session_start {
                let window_id = Self::session_window_id(&key, start);
                if let Some(entry) = self.windows.remove(&window_id) {
                    self.closed_window_ids.insert(window_id);
                    let result = WindowResult {
                        window: entry.window,
                        values: [(self.column.clone(), entry.state.compute(self.aggregation))]
                            .into_iter()
                            .collect(),
                        count: entry.state.count,
                        emitted_at: Utc::now(),
                    };
                    self.results.push(result.clone());
                    results.push(result);
                }
            }
        }

        // Start new session if needed
        if should_start_new {
            self.session_starts.insert(key.clone(), event_time);
        }

        // Update current session
        let session_start = self.session_starts.get(&key).cloned().unwrap_or(event_time);
        let window_id = Self::session_window_id(&key, session_start);

        let entry = self
            .windows
            .entry(window_id)
            .or_insert_with(|| WindowEntry {
                window: TimeWindow::new(session_start, event_time),
                kind: WindowKind::Session,
                state: AggregationState::new(self.aggregation),
            });
        // Extend the session's end to the latest record seen so far, rather
        // than leaving it fixed at whatever it was when the entry was first
        // created (which previously left `end == start` of the *first*
        // record forever, since `or_insert_with` only runs once per key).
        // Guarded with `max` rather than an unconditional assignment: a
        // record that arrives out of event-time order but still within gap
        // tolerance (so it does not start a new session) must not be able
        // to pull the window's `end` backward -- e.g. before its own
        // `start` -- just because its timestamp happens to be earlier than
        // one already folded into this session.
        entry.window.end = entry.window.end.max(event_time);
        entry.state.update(value);

        self.last_record_times.insert(key, event_time);

        Ok(results)
    }

    /// Builds a session window's id from its key and start time, at
    /// nanosecond precision. The previous second-granularity id
    /// (`session_{start.timestamp()}`) meant two *different* sessions --
    /// whether for different keys (there was only ever one key, `"default"`,
    /// before this configurable keying existed) or, now, two independent
    /// keys' sessions that happen to start within the same second --  could
    /// collide onto the same map entry and silently merge their states.
    fn session_window_id(key: &str, start: DateTime<Utc>) -> String {
        format!(
            "session_{}_{}",
            key,
            start.timestamp_nanos_opt().unwrap_or(0)
        )
    }

    /// Processes a count-based window
    fn process_count(
        &mut self,
        event_time: DateTime<Utc>,
        value: f64,
        size: usize,
        slide: Option<usize>,
    ) -> Result<Vec<WindowResult>> {
        let mut results = Vec::new();
        let slide = slide.unwrap_or(size).max(1);
        let size = size.max(1);

        // Calculate window indices this record belongs to
        let record_idx = self.record_count - 1;
        let first_window_idx = record_idx / slide;

        // For tumbling windows (slide == size), only one window
        // For sliding windows, multiple windows
        let windows_per_size = (size + slide - 1) / slide;

        for i in 0..windows_per_size {
            if first_window_idx < i {
                continue;
            }

            let window_idx = first_window_idx - i;
            let window_start_count = window_idx * slide;
            let window_end_count = window_start_count + size;

            // Check if this record belongs to this window
            if record_idx >= window_start_count && record_idx < window_end_count {
                let window_id = format!("count_{}", window_idx);

                // Real, observed event-time extent of the records actually
                // admitted into this window (extended below), rather than
                // the previous fabricated range that interpreted the record
                // *count* `size` as a number of *seconds*.
                let entry = self
                    .windows
                    .entry(window_id.clone())
                    .or_insert_with(|| WindowEntry {
                        window: TimeWindow::new(event_time, event_time),
                        kind: WindowKind::Count,
                        state: AggregationState::new(self.aggregation),
                    });
                entry.window.end = event_time;
                entry.state.update(value);

                // Check if window is complete
                if entry.state.count >= size {
                    let result = WindowResult {
                        window: entry.window.clone(),
                        values: [(self.column.clone(), entry.state.compute(self.aggregation))]
                            .into_iter()
                            .collect(),
                        count: entry.state.count,
                        emitted_at: Utc::now(),
                    };
                    results.push(result);

                    // Emit-once: remove immediately on completion regardless
                    // of tumbling (slide == size) vs sliding (slide < size)
                    // -- a completed window must never be able to
                    // accumulate further updates and be re-emitted as a
                    // duplicate, monotonically-growing result. This also
                    // means count windows never need (and, per `WindowKind`,
                    // never receive) watermark-based eviction: they are
                    // always removed here, exactly once, right when they
                    // complete.
                    self.windows.remove(&window_id);
                    self.closed_window_ids.insert(window_id);
                }
            }
        }

        Ok(results)
    }

    /// Processes a global window
    fn process_global(&mut self, value: f64) {
        let window_id = "global".to_string();

        let entry = self
            .windows
            .entry(window_id)
            .or_insert_with(|| WindowEntry {
                // A sentinel spanning the full representable range, since a
                // global window has no natural start/end. The previous
                // implementation used `DateTime::from_timestamp(i64::MAX /
                // 2, 0)`, which is outside chrono's representable range and
                // therefore always returned `None` -- unconditionally
                // panicking (via `.expect(...)`) the first time any record
                // was ever processed with `WindowType::Global`.
                window: TimeWindow::new(DateTime::<Utc>::MIN_UTC, DateTime::<Utc>::MAX_UTC),
                kind: WindowKind::Global,
                state: AggregationState::new(self.aggregation),
            });

        entry.state.update(value);
    }

    /// Closes time-based windows that are past the watermark. Count,
    /// session, and global windows are excluded (see [`WindowKind`]): they
    /// close via their own dedicated logic, never via the watermark.
    fn close_expired_windows(&mut self) -> Result<Vec<WindowResult>> {
        let mut results = Vec::new();

        let watermark_with_lateness = self.watermark
            - chrono::Duration::from_std(self.config.allowed_lateness)
                .unwrap_or(chrono::Duration::zero());

        // Find and close expired windows
        let expired_ids: Vec<String> = self
            .windows
            .iter()
            .filter(|(_, entry)| {
                entry.kind == WindowKind::Time && entry.window.end <= watermark_with_lateness
            })
            .map(|(id, _)| id.clone())
            .collect();

        for window_id in expired_ids {
            if let Some(entry) = self.windows.remove(&window_id) {
                self.closed_window_ids.insert(window_id);
                let result = WindowResult {
                    window: entry.window,
                    values: [(self.column.clone(), entry.state.compute(self.aggregation))]
                        .into_iter()
                        .collect(),
                    count: entry.state.count,
                    emitted_at: Utc::now(),
                };
                self.results.push(result.clone());
                results.push(result);
            }
        }

        Ok(results)
    }

    /// Aligns a timestamp to a window boundary
    fn align_to_window(&self, timestamp: DateTime<Utc>, window_size: Duration) -> DateTime<Utc> {
        let millis = timestamp.timestamp_millis();
        // `WindowConfigBuilder::build` rejects `Duration::ZERO` sizes, but
        // `WindowConfig` is constructible directly (all fields are `pub`),
        // bypassing that validation -- guard here too rather than dividing
        // by zero. Also convert via `try_from` rather than `as i64`: the
        // latter silently truncates for values that don't fit (irrelevant
        // for realistic window sizes, but a real footgun for a
        // user-supplied `Duration` in principle).
        let window_millis: i64 = i64::try_from(window_size.as_millis())
            .unwrap_or(i64::MAX)
            .max(1);
        let aligned_millis = millis.div_euclid(window_millis) * window_millis;

        DateTime::from_timestamp_millis(aligned_millis).unwrap_or(timestamp)
    }

    /// Flushes all active windows and returns results
    pub fn flush(&mut self) -> Vec<WindowResult> {
        let mut results = Vec::new();

        for (_, entry) in self.windows.drain() {
            if entry.state.count > 0 || self.config.include_partial_windows {
                let result = WindowResult {
                    window: entry.window,
                    values: [(self.column.clone(), entry.state.compute(self.aggregation))]
                        .into_iter()
                        .collect(),
                    count: entry.state.count,
                    emitted_at: Utc::now(),
                };
                results.push(result);
            }
        }

        results
    }

    /// Gets all completed window results accumulated so far. Unbounded: for
    /// a long-running aggregator, use [`WindowedAggregator::take_results`]
    /// periodically instead if you don't want this history to grow for the
    /// aggregator's whole lifetime.
    pub fn results(&self) -> &[WindowResult] {
        &self.results
    }

    /// Drains and returns all completed window results accumulated so far,
    /// leaving `results()` empty afterward.
    pub fn take_results(&mut self) -> Vec<WindowResult> {
        std::mem::take(&mut self.results)
    }

    /// Drains and returns all records rejected as too late for their target
    /// window (see `WindowedAggregator::is_too_late`).
    pub fn take_late_records(&mut self) -> Vec<LateRecord> {
        std::mem::take(&mut self.late_records)
    }

    /// Gets the current watermark
    pub fn watermark(&self) -> DateTime<Utc> {
        self.watermark
    }
}

/// Multi-column windowed aggregator
#[derive(Debug)]
pub struct MultiColumnAggregator {
    /// Configuration
    config: WindowConfig,
    /// Aggregations by column
    aggregations: HashMap<String, WindowAggregation>,
    /// Active windows and their states
    windows: HashMap<String, (TimeWindow, HashMap<String, AggregationState>)>,
    /// Completed results
    results: Vec<WindowResult>,
    /// Current watermark
    watermark: DateTime<Utc>,
}

impl MultiColumnAggregator {
    /// Creates a new multi-column aggregator
    pub fn new(config: WindowConfig) -> Self {
        MultiColumnAggregator {
            config,
            aggregations: HashMap::new(),
            windows: HashMap::new(),
            results: Vec::new(),
            watermark: Utc::now() - chrono::Duration::days(1),
        }
    }

    /// Adds an aggregation for a column
    pub fn add_aggregation(&mut self, column: &str, agg: WindowAggregation) -> &mut Self {
        self.aggregations.insert(column.to_string(), agg);
        self
    }

    /// Processes a record
    pub fn process(&mut self, record: &StreamRecord) -> Result<Vec<WindowResult>> {
        let event_time = Utc::now(); // Simplified - could extract from record

        // Update watermark
        let watermark_candidate = event_time
            - chrono::Duration::from_std(self.config.watermark_delay)
                .unwrap_or(chrono::Duration::zero());
        if watermark_candidate > self.watermark {
            self.watermark = watermark_candidate;
        }

        // Handle tumbling windows (simplified)
        if let WindowType::Tumbling { size } = &self.config.window_type {
            let millis = event_time.timestamp_millis();
            let window_millis = i64::try_from(size.as_millis()).unwrap_or(i64::MAX).max(1);
            let window_start_millis = millis.div_euclid(window_millis) * window_millis;
            let window_start =
                DateTime::from_timestamp_millis(window_start_millis).unwrap_or(event_time);
            let window_end = window_start
                + chrono::Duration::from_std(*size).unwrap_or(chrono::Duration::zero());

            let window = TimeWindow::new(window_start, window_end);
            let window_id = window.id.clone();

            // Get or create window state
            let (_, states) = self
                .windows
                .entry(window_id)
                .or_insert_with(|| (window.clone(), HashMap::new()));

            // Update states for each aggregated column
            for (column, agg_type) in &self.aggregations {
                let state = states
                    .entry(column.clone())
                    .or_insert_with(|| AggregationState::new(*agg_type));

                if let Some(value) = record.fields.get(column) {
                    if let Ok(v) = value.parse::<f64>() {
                        state.update(v);
                    }
                }
            }
        }

        // Close expired windows
        self.close_expired_windows()
    }

    /// Closes windows past the watermark
    fn close_expired_windows(&mut self) -> Result<Vec<WindowResult>> {
        let mut results = Vec::new();

        let watermark_with_lateness = self.watermark
            - chrono::Duration::from_std(self.config.allowed_lateness)
                .unwrap_or(chrono::Duration::zero());

        let expired_ids: Vec<String> = self
            .windows
            .iter()
            .filter(|(_, (window, _))| window.end <= watermark_with_lateness)
            .map(|(id, _)| id.clone())
            .collect();

        for window_id in expired_ids {
            if let Some((window, states)) = self.windows.remove(&window_id) {
                let mut values = HashMap::new();
                let mut total_count = 0;

                for (column, agg) in &self.aggregations {
                    if let Some(state) = states.get(column) {
                        values.insert(column.clone(), state.compute(*agg));
                        total_count = total_count.max(state.count);
                    }
                }

                if !values.is_empty() {
                    let result = WindowResult {
                        window,
                        values,
                        count: total_count,
                        emitted_at: Utc::now(),
                    };
                    results.push(result.clone());
                    self.results.push(result);
                }
            }
        }

        Ok(results)
    }

    /// Flushes all active windows
    pub fn flush(&mut self) -> Vec<WindowResult> {
        let mut results = Vec::new();

        for (_, (window, states)) in self.windows.drain() {
            let mut values = HashMap::new();
            let mut total_count = 0;

            for (column, agg) in &self.aggregations {
                if let Some(state) = states.get(column) {
                    values.insert(column.clone(), state.compute(*agg));
                    total_count = total_count.max(state.count);
                }
            }

            if !values.is_empty() || self.config.include_partial_windows {
                let result = WindowResult {
                    window,
                    values,
                    count: total_count,
                    emitted_at: Utc::now(),
                };
                results.push(result);
            }
        }

        results
    }

    /// Gets all completed results
    pub fn results(&self) -> &[WindowResult] {
        &self.results
    }

    /// Drains and returns all completed results accumulated so far, leaving
    /// `results()` empty afterward.
    pub fn take_results(&mut self) -> Vec<WindowResult> {
        std::mem::take(&mut self.results)
    }
}

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

    fn create_record(value: f64) -> StreamRecord {
        let mut fields = HashMap::new();
        fields.insert("value".to_string(), value.to_string());
        StreamRecord::new(fields)
    }

    fn create_record_with_field(value: f64, field: &str, field_value: &str) -> StreamRecord {
        let mut fields = HashMap::new();
        fields.insert("value".to_string(), value.to_string());
        fields.insert(field.to_string(), field_value.to_string());
        StreamRecord::new(fields)
    }

    fn create_record_with_time(value: f64, event_time: i64) -> StreamRecord {
        let mut fields = HashMap::new();
        fields.insert("value".to_string(), value.to_string());
        fields.insert("event_time".to_string(), event_time.to_string());
        StreamRecord::new(fields)
    }

    #[test]
    fn test_tumbling_window() {
        let config = WindowConfigBuilder::new()
            .tumbling(Duration::from_secs(10))
            .include_partial_windows(true)
            .build()
            .expect("valid config");

        let mut agg = WindowedAggregator::new(config, "value", WindowAggregation::Sum);

        // Process some records
        for i in 0..5 {
            agg.process(&create_record(i as f64))
                .expect("operation should succeed");
        }

        // Flush to get results
        let results = agg.flush();
        assert!(!results.is_empty());

        let total: f64 = results.iter().map(|r| r.values["value"]).sum();
        assert_eq!(total, 10.0); // 0 + 1 + 2 + 3 + 4
    }

    #[test]
    fn test_count_window() {
        let config = WindowConfigBuilder::new()
            .count(3, None)
            .build()
            .expect("valid config");

        let mut agg = WindowedAggregator::new(config, "value", WindowAggregation::Avg);

        // Process exactly 3 records
        for i in 1..=3 {
            let results = agg
                .process(&create_record(i as f64))
                .expect("operation should succeed");
            if i == 3 {
                // Window should complete on 3rd record
                assert!(!results.is_empty());
                assert!((results[0].values["value"] - 2.0).abs() < 0.001); // avg(1, 2, 3) = 2
            } else {
                assert!(results.is_empty());
            }
        }
    }

    #[test]
    fn test_count_window_does_not_duplicate_after_completion() {
        // A sliding count window (slide < size): once a window completes it
        // must never be emitted again as later records that overlap other,
        // still-growing windows are processed. Uses explicit, widely spaced
        // `event_time` values (rather than relying on `Utc::now()`'s real
        // wall-clock resolution) so the test is deterministic regardless of
        // how fast it happens to run.
        let config = WindowConfigBuilder::new()
            .count(4, Some(2))
            .build()
            .expect("valid config");
        let mut agg = WindowedAggregator::new(config, "value", WindowAggregation::Sum);

        let mut emitted_window_ids = Vec::new();
        for i in 1..=10 {
            let results = agg
                .process(&create_record_with_time(i as f64, 1_700_000_000 + i * 1000))
                .expect("operation should succeed");
            for r in results {
                emitted_window_ids.push(r.window.id.clone());
            }
        }

        assert_eq!(
            emitted_window_ids.len(),
            4,
            "expected exactly 4 completed count windows (size=4, slide=2, 10 records), got {:?}",
            emitted_window_ids
        );
        let unique: std::collections::HashSet<_> = emitted_window_ids.iter().cloned().collect();
        assert_eq!(
            emitted_window_ids.len(),
            unique.len(),
            "each completed count window must be emitted exactly once, got {:?}",
            emitted_window_ids
        );
    }

    #[test]
    fn test_aggregation_types() {
        let config = WindowConfigBuilder::new()
            .tumbling(Duration::from_secs(60))
            .include_partial_windows(true)
            .build()
            .expect("valid config");

        // Test sum
        let mut agg = WindowedAggregator::new(config.clone(), "value", WindowAggregation::Sum);
        for i in 1..=5 {
            agg.process(&create_record(i as f64))
                .expect("operation should succeed");
        }
        let results = agg.flush();
        assert_eq!(results[0].values["value"], 15.0);

        // Test avg
        let mut agg = WindowedAggregator::new(config.clone(), "value", WindowAggregation::Avg);
        for i in 1..=5 {
            agg.process(&create_record(i as f64))
                .expect("operation should succeed");
        }
        let results = agg.flush();
        assert_eq!(results[0].values["value"], 3.0);

        // Test min
        let mut agg = WindowedAggregator::new(config.clone(), "value", WindowAggregation::Min);
        for i in 1..=5 {
            agg.process(&create_record(i as f64))
                .expect("operation should succeed");
        }
        let results = agg.flush();
        assert_eq!(results[0].values["value"], 1.0);

        // Test max
        let mut agg = WindowedAggregator::new(config.clone(), "value", WindowAggregation::Max);
        for i in 1..=5 {
            agg.process(&create_record(i as f64))
                .expect("operation should succeed");
        }
        let results = agg.flush();
        assert_eq!(results[0].values["value"], 5.0);
    }

    #[test]
    fn test_variance_is_never_negative_for_large_mean_small_spread() {
        // Values clustered tightly around a large mean are exactly the case
        // that made the old `E[x^2] - mean^2` formula cancel catastrophically
        // and go negative.
        let config = WindowConfigBuilder::new()
            .global()
            .include_partial_windows(true)
            .build()
            .expect("valid config");
        let mut agg = WindowedAggregator::new(config, "value", WindowAggregation::Variance);

        for v in [1.0e9, 1.0e9 + 1.0, 1.0e9 + 2.0] {
            agg.process(&create_record(v))
                .expect("operation should succeed");
        }

        let results = agg.flush();
        let variance = results[0].values["value"];
        assert!(
            variance >= 0.0,
            "variance must never be negative, got {variance}"
        );
        // True population variance of {0, 1, 2} shifted by a constant is
        // exactly 2/3.
        assert!(
            (variance - (2.0 / 3.0)).abs() < 1e-6,
            "expected variance close to 2/3, got {variance}"
        );
    }

    #[test]
    fn test_multi_column_aggregator() {
        let config = WindowConfigBuilder::new()
            .tumbling(Duration::from_secs(60))
            .include_partial_windows(true)
            .build()
            .expect("valid config");

        let mut agg = MultiColumnAggregator::new(config);
        agg.add_aggregation("price", WindowAggregation::Sum)
            .add_aggregation("quantity", WindowAggregation::Avg);

        for i in 1..=5 {
            let mut fields = HashMap::new();
            fields.insert("price".to_string(), (i * 10).to_string());
            fields.insert("quantity".to_string(), i.to_string());
            let record = StreamRecord::new(fields);
            agg.process(&record).expect("operation should succeed");
        }

        let results = agg.flush();
        assert!(!results.is_empty());
        assert_eq!(results[0].values["price"], 150.0); // 10 + 20 + 30 + 40 + 50
        assert_eq!(results[0].values["quantity"], 3.0); // avg(1, 2, 3, 4, 5)
    }

    #[test]
    fn test_window_config_builder() {
        let config = WindowConfigBuilder::new()
            .sliding(Duration::from_secs(30), Duration::from_secs(10))
            .allowed_lateness(Duration::from_secs(5))
            .emit_on_every_record(true)
            .build()
            .expect("valid config");

        assert!(matches!(config.window_type, WindowType::Sliding { .. }));
        assert_eq!(config.allowed_lateness, Duration::from_secs(5));
        assert!(config.emit_on_every_record);
    }

    #[test]
    fn test_window_config_builder_rejects_zero_size() {
        let result = WindowConfigBuilder::new().tumbling(Duration::ZERO).build();
        assert!(result.is_err());
    }

    #[test]
    fn test_window_config_builder_rejects_slide_larger_than_size() {
        let result = WindowConfigBuilder::new()
            .sliding(Duration::from_secs(5), Duration::from_secs(10))
            .build();
        assert!(result.is_err());
    }

    #[test]
    fn test_time_window() {
        let start = Utc::now();
        let end = start + chrono::Duration::seconds(60);
        let window = TimeWindow::new(start, end);

        assert!(window.contains(start));
        assert!(window.contains(start + chrono::Duration::seconds(30)));
        assert!(!window.contains(end));
        assert!(!window.contains(start - chrono::Duration::seconds(1)));
    }

    #[test]
    fn test_session_window() {
        let config = WindowConfigBuilder::new()
            .session(Duration::from_millis(100), None)
            .build()
            .expect("valid config");

        let mut agg = WindowedAggregator::new(config, "value", WindowAggregation::Sum);

        // Process records - they should be in same session
        for i in 1..=3 {
            agg.process(&create_record(i as f64))
                .expect("operation should succeed");
        }

        // Simulate gap - flush should close session
        let results = agg.flush();
        assert!(!results.is_empty());
    }

    #[test]
    fn test_session_window_keyed_by_field_isolates_sessions() {
        let config = WindowConfigBuilder::new()
            .session(Duration::from_secs(60), None)
            .session_key_field("user")
            .include_partial_windows(true)
            .build()
            .expect("valid config");

        let mut agg = WindowedAggregator::new(config, "value", WindowAggregation::Sum);

        agg.process(&create_record_with_field(1.0, "user", "alice"))
            .expect("operation should succeed");
        agg.process(&create_record_with_field(10.0, "user", "bob"))
            .expect("operation should succeed");
        agg.process(&create_record_with_field(2.0, "user", "alice"))
            .expect("operation should succeed");
        agg.process(&create_record_with_field(20.0, "user", "bob"))
            .expect("operation should succeed");

        let results = agg.flush();
        // Two independent sessions (one per user), not one merged session.
        assert_eq!(results.len(), 2);
        let sums: Vec<f64> = results.iter().map(|r| r.values["value"]).collect();
        assert!(
            sums.contains(&3.0),
            "alice's session should sum to 3.0, got {sums:?}"
        );
        assert!(
            sums.contains(&30.0),
            "bob's session should sum to 30.0, got {sums:?}"
        );
    }

    #[test]
    fn test_session_window_end_never_regresses_for_out_of_order_record() {
        // gap tolerance wide enough that an out-of-order record (arriving
        // with an *earlier* event_time than one already folded into the
        // session) still belongs to the same session rather than starting a
        // new one.
        let config = WindowConfigBuilder::new()
            .session(Duration::from_secs(120), None)
            .include_partial_windows(true)
            .build()
            .expect("valid config");
        let mut agg = WindowedAggregator::new(config, "value", WindowAggregation::Sum);

        let base = 1_700_000_000i64;
        agg.process(&create_record_with_time(1.0, base))
            .expect("operation should succeed");
        agg.process(&create_record_with_time(2.0, base + 30))
            .expect("operation should succeed");
        // Out of order: earlier event_time than the previous record, but
        // still within the 120s gap tolerance of it, so it must extend the
        // *same* session rather than start a new one or shrink `end`.
        agg.process(&create_record_with_time(5.0, base + 10))
            .expect("operation should succeed");

        let results = agg.flush();
        assert_eq!(
            results.len(),
            1,
            "the out-of-order record must join the single existing session, not start a new one"
        );
        assert_eq!(results[0].values["value"], 1.0 + 2.0 + 5.0);
        assert_eq!(
            results[0].window.start.timestamp(),
            base,
            "session start must remain the first record's time"
        );
        assert_eq!(
            results[0].window.end.timestamp(),
            base + 30,
            "session end must stay at the latest-seen record's time, not regress to an \
             out-of-order record's earlier timestamp"
        );
    }
}