reifydb-sdk 0.8.1

SDK for building ReifyDB operators, procedures, transforms and more
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 ReifyDB

use std::{
	collections::{BTreeMap, BTreeSet, HashMap},
	ffi::c_void,
	ptr::null,
};

use reifydb_abi::context::context::ContextFFI;
use reifydb_core::{
	common::CommitVersion,
	interface::{
		catalog::flow::FlowNodeId,
		change::{Change, Diff},
	},
	row::Row as CoreRow,
	value::column::columns::Columns,
	window::{
		accumulator::WindowAccumulator,
		span::{Slot, WindowSpan},
	},
};
use reifydb_value::value::{datetime::DateTime, row_number::RowNumber};

use super::{
	context::ChaosContext,
	event::{ChaosBatch, ChaosEvent},
	materialize::materialize_history,
	oracle::MaterializedTable,
};
use crate::{
	operator::{
		column::{row::Row, sink::native::NativeRowSink},
		context::ffi::FFIOperatorContext,
		view::{ColumnsView, native::NativeColumnsView},
		windowed::{
			multi_rolling::MultiRollingOperator, rolling::RollingOperator,
			rolling_incremental::RollingIncrementalOperator, tumbling::TumblingOperator,
			tumbling_carry::TumblingCarryOperator,
		},
	},
	testing::{callbacks::create_test_callbacks, context::TestContext},
};

fn with_oracle_ctx<R>(f: impl FnOnce(&mut FFIOperatorContext) -> R) -> R {
	let test_ctx = TestContext::new(CommitVersion(1));
	let mut ffi_context = ContextFFI {
		txn_ptr: &test_ctx as *const TestContext as *mut c_void,
		executor_ptr: null(),
		operator_id: 1,
		clock_now_nanos: 0,
		callbacks: create_test_callbacks(),
	};
	let mut op_ctx = FFIOperatorContext::new(&mut ffi_context as *mut ContextFFI);
	f(&mut op_ctx)
}

type Coord<A> = <A as TumblingOperator>::WindowCoord;
type Group<A> = <A as TumblingOperator>::GroupKey;
type WindowKey<A> = (Group<A>, Coord<A>);

pub fn tumbling_accumulator_oracle<A>(
	aggregate: &A,
	ctx: &ChaosContext,
	batches: &[ChaosBatch],
	output_key_columns: &[String],
) -> MaterializedTable
where
	A: TumblingOperator,
	A::Output: Row,
{
	let mut accumulators: HashMap<WindowKey<A>, A::Accumulator> = HashMap::new();
	let mut spans: HashMap<WindowKey<A>, WindowSpan<Coord<A>>> = HashMap::new();
	let mut high_water: HashMap<Group<A>, Coord<A>> = HashMap::new();
	let mut last_visible: HashMap<WindowKey<A>, A::Output> = HashMap::new();

	for batch in batches {
		let snapshot = HashMap::new();
		let mut touched: BTreeSet<WindowKey<A>> = BTreeSet::new();

		for event in &batch.events {
			match event {
				ChaosEvent::Insert {
					row,
					..
				} => apply_leg(
					aggregate,
					row,
					true,
					&snapshot,
					&mut accumulators,
					&mut spans,
					&mut touched,
				),
				ChaosEvent::Update {
					pre,
					post,
					..
				} => {
					apply_leg(
						aggregate,
						pre,
						false,
						&snapshot,
						&mut accumulators,
						&mut spans,
						&mut touched,
					);
					apply_leg(
						aggregate,
						post,
						true,
						&snapshot,
						&mut accumulators,
						&mut spans,
						&mut touched,
					);
				}
				ChaosEvent::Remove {
					row,
					..
				} => apply_leg(
					aggregate,
					row,
					false,
					&snapshot,
					&mut accumulators,
					&mut spans,
					&mut touched,
				),
			}
		}

		for key in touched {
			let hw = high_water.entry(key.0.clone()).or_insert(key.1);
			if key.1 > *hw {
				*hw = key.1;
			}
			let finalized = accumulators.get(&key).and_then(|a| a.finalize());
			if let Some(value) = finalized
				&& let Some(span) = spans.get(&key).copied()
				&& let Some(out) = aggregate.build_output(&key.0, span, value)
			{
				last_visible.insert(key.clone(), out);
			} else {
				last_visible.remove(&key);
			}
		}
	}

	materialize_outputs(last_visible.into_values(), ctx.now_nanos(), output_key_columns)
}

#[allow(clippy::type_complexity)]
fn apply_leg<A>(
	aggregate: &A,
	row: &CoreRow,
	is_add: bool,
	snapshot: &HashMap<Group<A>, Coord<A>>,
	accumulators: &mut HashMap<WindowKey<A>, A::Accumulator>,
	spans: &mut HashMap<WindowKey<A>, WindowSpan<Coord<A>>>,
	touched: &mut BTreeSet<WindowKey<A>>,
) where
	A: TumblingOperator,
{
	let Some((group, coord, contribution)) = extract_one(aggregate, row) else {
		return;
	};
	let span = aggregate.window_for(coord);
	let key = (group, span.start);
	if is_add {
		let survives = snapshot.get(&key.0).is_none_or(|hw| span.start >= *hw);
		if !survives {
			return;
		}
		spans.insert(key.clone(), span);
		let accumulator = accumulators.entry(key.clone()).or_insert_with(|| aggregate.new_accumulator());
		accumulator.add(&contribution);
		touched.insert(key);
	} else if let Some(accumulator) = accumulators.get_mut(&key)
		&& !accumulator.is_empty()
	{
		let survives = snapshot.get(&key.0).is_none_or(|hw| span.start >= *hw);
		if survives {
			accumulator.remove(&contribution);
		} else {
			accumulator.remove_if_present(&contribution);
		}
		spans.insert(key.clone(), span);
		touched.insert(key);
	}
}

#[allow(clippy::type_complexity)]
fn extract_one<A>(
	aggregate: &A,
	row: &CoreRow,
) -> Option<(Group<A>, Coord<A>, <A::Accumulator as WindowAccumulator>::Contribution)>
where
	A: TumblingOperator,
{
	let columns = Columns::from_row(row);
	let view = NativeColumnsView::new(&columns);
	let row_view = view.row(0)?;
	with_oracle_ctx(|ctx| aggregate.extract(ctx, &row_view))
}

fn materialize_outputs<O: Row>(
	outputs: impl Iterator<Item = O>,
	now_nanos: u64,
	output_key_columns: &[String],
) -> MaterializedTable {
	let mut sink = NativeRowSink::new(<O as Row>::COLUMNS).expect("output sink");
	let mut row_numbers: Vec<RowNumber> = Vec::new();
	let mut count = 0u64;
	for output in outputs {
		output.encode_into(&mut sink).expect("encode output");
		count += 1;
		row_numbers.push(RowNumber(count));
	}
	if count == 0 {
		return MaterializedTable::empty();
	}
	let columns = sink.finish(row_numbers, now_nanos).expect("finish sink");
	let change = Change::from_flow(
		FlowNodeId(0),
		CommitVersion(0),
		vec![Diff::insert(columns)],
		DateTime::from_nanos(now_nanos),
	);
	materialize_history(&[change], output_key_columns)
}

type RollingCoord<A> = <A as RollingOperator>::WindowCoord;
type RollingGroup<A> = <A as RollingOperator>::GroupKey;

type RollingContribution<A> = <<A as RollingOperator>::Accumulator as WindowAccumulator>::Contribution;
type RollingBuckets<A> = BTreeMap<(RollingGroup<A>, RollingCoord<A>), Vec<Leg<RollingContribution<A>>>>;

enum Leg<C> {
	Add(C),
	Remove(C),
}

fn bucket_rolling<A>(aggregate: &A, batch: &ChaosBatch) -> RollingBuckets<A>
where
	A: RollingOperator,
{
	let mut buckets: RollingBuckets<A> = BTreeMap::new();
	for event in &batch.events {
		match event {
			ChaosEvent::Insert {
				row,
				..
			} => push_rolling(aggregate, row, true, &mut buckets),
			ChaosEvent::Update {
				pre,
				post,
				..
			} => {
				push_rolling(aggregate, pre, false, &mut buckets);
				push_rolling(aggregate, post, true, &mut buckets);
			}
			ChaosEvent::Remove {
				row,
				..
			} => push_rolling(aggregate, row, false, &mut buckets),
		}
	}
	buckets
}

fn push_rolling<A>(aggregate: &A, row: &CoreRow, is_add: bool, buckets: &mut RollingBuckets<A>)
where
	A: RollingOperator,
{
	if let Some((group, coord, contribution)) = extract_rolling(aggregate, row) {
		let leg = if is_add {
			Leg::Add(contribution)
		} else {
			Leg::Remove(contribution)
		};
		buckets.entry((group, coord)).or_default().push(leg);
	}
}

#[allow(clippy::type_complexity)]
fn apply_rolling_buckets<A>(
	capacity: usize,
	snapshot: &HashMap<RollingGroup<A>, RollingCoord<A>>,
	buckets: RollingBuckets<A>,
	buffers: &mut HashMap<RollingGroup<A>, BTreeMap<RollingCoord<A>, A::Accumulator>>,
	high_water: &mut HashMap<RollingGroup<A>, RollingCoord<A>>,
) -> BTreeSet<RollingGroup<A>>
where
	A: RollingOperator,
{
	let mut touched: BTreeSet<RollingGroup<A>> = BTreeSet::new();
	for ((group, coord), legs) in buckets {
		let buffer = buffers.entry(group.clone()).or_default();

		let late = snapshot.get(&group).is_some_and(|hw| coord < *hw) && !buffer.contains_key(&coord);
		let mut accumulator = buffer.remove(&coord).unwrap_or_default();
		let mut changed = false;
		for leg in legs {
			match leg {
				Leg::Add(c) => {
					if late {
						continue;
					}
					accumulator.add(&c);
					changed = true;
				}
				Leg::Remove(c) => {
					if accumulator.is_empty() {
						continue;
					}
					accumulator.remove(&c);
					changed = true;
				}
			}
		}
		if !accumulator.is_empty() {
			buffer.insert(coord, accumulator);
		}

		if !changed {
			continue;
		}
		while buffer.len() > capacity {
			buffer.pop_first();
		}
		high_water
			.entry(group.clone())
			.and_modify(|hw| {
				if coord > *hw {
					*hw = coord;
				}
			})
			.or_insert(coord);
		touched.insert(group);
	}
	touched
}

pub fn rolling_accumulator_oracle<A>(
	aggregate: &A,
	ctx: &ChaosContext,
	batches: &[ChaosBatch],
	output_key_columns: &[String],
) -> MaterializedTable
where
	A: RollingOperator,
	A::Output: Row,
{
	let capacity = aggregate.capacity();
	let mut buffers: HashMap<RollingGroup<A>, BTreeMap<RollingCoord<A>, A::Accumulator>> = HashMap::new();
	let mut high_water: HashMap<RollingGroup<A>, RollingCoord<A>> = HashMap::new();
	let mut last_visible: HashMap<RollingGroup<A>, A::Output> = HashMap::new();

	for batch in batches {
		let snapshot = HashMap::new();
		let buckets = bucket_rolling(aggregate, batch);
		let touched = apply_rolling_buckets::<A>(capacity, &snapshot, buckets, &mut buffers, &mut high_water);
		for group in touched {
			match buffers.get(&group).and_then(|buffer| aggregate.combine(&group, buffer)) {
				Some(out) => {
					last_visible.insert(group, out);
				}
				None => {
					last_visible.remove(&group);
				}
			}
		}
	}

	materialize_outputs(last_visible.into_values(), ctx.now_nanos(), output_key_columns)
}

#[allow(clippy::type_complexity)]
fn extract_rolling<A>(
	aggregate: &A,
	row: &CoreRow,
) -> Option<(RollingGroup<A>, RollingCoord<A>, <A::Accumulator as WindowAccumulator>::Contribution)>
where
	A: RollingOperator,
{
	let columns = Columns::from_row(row);
	let view = NativeColumnsView::new(&columns);
	let row_view = view.row(0)?;
	with_oracle_ctx(|ctx| aggregate.extract(ctx, &row_view))
}

pub fn rolling_incremental_accumulator_oracle<A>(
	aggregate: &A,
	ctx: &ChaosContext,
	batches: &[ChaosBatch],
	output_key_columns: &[String],
) -> MaterializedTable
where
	A: RollingIncrementalOperator,
	A::Output: Row,
{
	let capacity = aggregate.capacity();
	let mut buffers: HashMap<RollingGroup<A>, BTreeMap<RollingCoord<A>, A::Accumulator>> = HashMap::new();
	let mut high_water: HashMap<RollingGroup<A>, RollingCoord<A>> = HashMap::new();
	let mut last_visible: HashMap<RollingGroup<A>, A::Output> = HashMap::new();

	for batch in batches {
		let snapshot = HashMap::new();
		let buckets = bucket_rolling(aggregate, batch);
		let touched = apply_rolling_buckets::<A>(capacity, &snapshot, buckets, &mut buffers, &mut high_water);
		for group in touched {
			let out = buffers.get(&group).and_then(|buffer| {
				let mut running = A::Running::default();
				for accumulator in buffer.values() {
					if let Some(value) = accumulator.finalize() {
						running.add(&aggregate.window_contribution(&value));
					}
				}
				let (coord, accumulator) = buffer.iter().next_back()?;
				let newest = accumulator.finalize()?;
				aggregate.combine_running(&group, &running, &newest, *coord)
			});
			match out {
				Some(out) => {
					last_visible.insert(group, out);
				}
				None => {
					last_visible.remove(&group);
				}
			}
		}
	}

	materialize_outputs(last_visible.into_values(), ctx.now_nanos(), output_key_columns)
}

type CarryCoord<A> = <A as TumblingCarryOperator>::WindowCoord;
type CarryGroup<A> = <A as TumblingCarryOperator>::GroupKey;
type CarryWindowKey<A> = (CarryGroup<A>, CarryCoord<A>);

type CarryContribution<A> = <<A as TumblingCarryOperator>::Accumulator as WindowAccumulator>::Contribution;
type CarryBuckets<A> = BTreeMap<CarryWindowKey<A>, (WindowSpan<CarryCoord<A>>, Vec<Leg<CarryContribution<A>>>)>;

struct CarryGroupState<C, Carry> {
	high_water: Option<C>,
	sealed_up_to: Option<C>,
	sealed_carry: Option<Carry>,
	windows: BTreeMap<C, Option<Carry>>,
}

impl<C, Carry> Default for CarryGroupState<C, Carry> {
	fn default() -> Self {
		Self {
			high_water: None,
			sealed_up_to: None,
			sealed_carry: None,
			windows: BTreeMap::new(),
		}
	}
}

fn bucket_carry<A>(aggregate: &A, batch: &ChaosBatch) -> CarryBuckets<A>
where
	A: TumblingCarryOperator,
{
	let mut buckets: CarryBuckets<A> = BTreeMap::new();
	for event in &batch.events {
		match event {
			ChaosEvent::Insert {
				row,
				..
			} => push_carry(aggregate, row, true, &mut buckets),
			ChaosEvent::Update {
				pre,
				post,
				..
			} => {
				push_carry(aggregate, pre, false, &mut buckets);
				push_carry(aggregate, post, true, &mut buckets);
			}
			ChaosEvent::Remove {
				row,
				..
			} => push_carry(aggregate, row, false, &mut buckets),
		}
	}
	buckets
}

fn push_carry<A>(aggregate: &A, row: &CoreRow, is_add: bool, buckets: &mut CarryBuckets<A>)
where
	A: TumblingCarryOperator,
{
	if let Some((group, coord, contribution)) = extract_carry(aggregate, row) {
		let span = aggregate.window_for(coord);
		let leg = if is_add {
			Leg::Add(contribution)
		} else {
			Leg::Remove(contribution)
		};
		buckets.entry((group, span.start)).or_insert_with(|| (span, Vec::new())).1.push(leg);
	}
}

pub fn tumbling_carry_accumulator_oracle<A>(
	aggregate: &A,
	ctx: &ChaosContext,
	batches: &[ChaosBatch],
	output_key_columns: &[String],
	retention: Option<<CarryCoord<A> as Slot>::Duration>,
) -> MaterializedTable
where
	A: TumblingCarryOperator,
	A::Output: Row,
{
	let mut accumulators: HashMap<CarryWindowKey<A>, A::Accumulator> = HashMap::new();
	let mut spans: HashMap<CarryWindowKey<A>, WindowSpan<CarryCoord<A>>> = HashMap::new();
	let mut metas: HashMap<CarryGroup<A>, CarryGroupState<CarryCoord<A>, A::Carry>> = HashMap::new();
	let mut last_visible: HashMap<CarryWindowKey<A>, A::Output> = HashMap::new();

	for batch in batches {
		let snapshot: HashMap<CarryGroup<A>, CarryCoord<A>> = HashMap::new();
		let buckets = bucket_carry(aggregate, batch);

		let mut earliest_affected: HashMap<CarryGroup<A>, CarryCoord<A>> = HashMap::new();
		for ((group, start), (span, legs)) in buckets {
			let meta = metas.entry(group.clone()).or_default();
			if matches!(meta.sealed_up_to, Some(s) if start <= s) {
				continue;
			}
			let snap_hw = snapshot.get(&group).copied();
			let tracked = meta.windows.contains_key(&start);
			let survives = snap_hw.is_none_or(|hw| start >= hw);
			if !tracked && !survives {
				continue;
			}
			let drop_adds = snap_hw.is_some_and(|hw| start < hw);
			let key = (group.clone(), start);
			let accumulator =
				accumulators.entry(key.clone()).or_insert_with(|| aggregate.new_accumulator());
			let mut changed = false;
			for leg in legs {
				match leg {
					Leg::Add(c) => {
						if drop_adds {
							continue;
						}
						accumulator.add(&c);
						changed = true;
					}
					Leg::Remove(c) => {
						if accumulator.is_empty() {
							continue;
						}
						accumulator.remove(&c);
						changed = true;
					}
				}
			}
			if !changed {
				continue;
			}
			spans.insert(key, span);
			meta.windows.entry(start).or_insert(None);
			if meta.high_water.is_none_or(|hw| start > hw) {
				meta.high_water = Some(start);
			}
			let e = earliest_affected.entry(group).or_insert(start);
			if start < *e {
				*e = start;
			}
		}

		for (group, start) in earliest_affected {
			let meta = metas.get_mut(&group).expect("affected group has meta");
			let mut prev_carry: Option<A::Carry> = match meta.windows.range(..start).next_back() {
				Some((_, c)) => c.clone(),
				None => meta.sealed_carry.clone(),
			};
			let coords: Vec<CarryCoord<A>> = meta.windows.range(start..).map(|(c, _)| *c).collect();
			let mut emptied: Vec<CarryCoord<A>> = Vec::new();
			for coord in coords {
				let key = (group.clone(), coord);
				let span = *spans.get(&key).expect("span recorded for tracked window");
				let value = accumulators.get(&key).and_then(|a| a.finalize());
				match value
					.as_ref()
					.and_then(|v| aggregate.build_output(&group, span, v, prev_carry.as_ref()))
				{
					Some(out) => {
						let new_carry = value
							.as_ref()
							.and_then(|v| aggregate.carry_forward(v, prev_carry.as_ref()));
						last_visible.insert(key, out);
						*meta.windows.get_mut(&coord).expect("window entry present") =
							new_carry.clone();
						if new_carry.is_some() {
							prev_carry = new_carry;
						}
					}
					None => {
						last_visible.remove(&key);
						emptied.push(coord);
					}
				}
			}
			for coord in emptied {
				meta.windows.remove(&coord);
			}

			if let (Some(retention), Some(hw)) = (retention, meta.high_water) {
				loop {
					let Some((&first, carry_out)) = meta.windows.iter().next() else {
						break;
					};
					if hw - first <= retention {
						break;
					}
					let carry_out = carry_out.clone();
					meta.windows.remove(&first);
					meta.sealed_up_to = Some(first);
					meta.sealed_carry = carry_out;
					accumulators.remove(&(group.clone(), first));
					spans.remove(&(group.clone(), first));
				}
			}
		}
	}

	materialize_outputs(last_visible.into_values(), ctx.now_nanos(), output_key_columns)
}

type MultiCoord<A> = <A as MultiRollingOperator>::WindowCoord;
type MultiGroup<A> = <A as MultiRollingOperator>::GroupKey;
type MultiContribution<A> = <<A as MultiRollingOperator>::Accumulator as WindowAccumulator>::Contribution;
type MultiBuckets<A> = BTreeMap<(MultiGroup<A>, MultiCoord<A>), Vec<Leg<MultiContribution<A>>>>;

fn bucket_multi<A>(aggregate: &A, batch: &ChaosBatch) -> MultiBuckets<A>
where
	A: MultiRollingOperator,
{
	let mut buckets: MultiBuckets<A> = BTreeMap::new();
	for event in &batch.events {
		match event {
			ChaosEvent::Insert {
				row,
				..
			} => push_multi(aggregate, row, true, &mut buckets),
			ChaosEvent::Update {
				pre,
				post,
				..
			} => {
				push_multi(aggregate, pre, false, &mut buckets);
				push_multi(aggregate, post, true, &mut buckets);
			}
			ChaosEvent::Remove {
				row,
				..
			} => push_multi(aggregate, row, false, &mut buckets),
		}
	}
	buckets
}

fn push_multi<A>(aggregate: &A, row: &CoreRow, is_add: bool, buckets: &mut MultiBuckets<A>)
where
	A: MultiRollingOperator,
{
	if let Some((group, coord, contribution)) = extract_multi(aggregate, row) {
		let leg = if is_add {
			Leg::Add(contribution)
		} else {
			Leg::Remove(contribution)
		};
		buckets.entry((group, coord)).or_default().push(leg);
	}
}

#[allow(clippy::type_complexity)]
fn apply_multi_buckets<A>(
	capacity: usize,
	snapshot: &HashMap<MultiGroup<A>, MultiCoord<A>>,
	buckets: MultiBuckets<A>,
	buffers: &mut HashMap<MultiGroup<A>, BTreeMap<MultiCoord<A>, A::Accumulator>>,
	high_water: &mut HashMap<MultiGroup<A>, MultiCoord<A>>,
) -> BTreeSet<MultiGroup<A>>
where
	A: MultiRollingOperator,
{
	let mut touched: BTreeSet<MultiGroup<A>> = BTreeSet::new();
	for ((group, coord), legs) in buckets {
		let buffer = buffers.entry(group.clone()).or_default();

		let late = snapshot.get(&group).is_some_and(|hw| coord < *hw) && !buffer.contains_key(&coord);
		let mut accumulator = buffer.remove(&coord).unwrap_or_default();
		let mut changed = false;
		for leg in legs {
			match leg {
				Leg::Add(c) => {
					if late {
						continue;
					}
					accumulator.add(&c);
					changed = true;
				}
				Leg::Remove(c) => {
					if accumulator.is_empty() {
						continue;
					}
					accumulator.remove(&c);
					changed = true;
				}
			}
		}
		if !accumulator.is_empty() {
			buffer.insert(coord, accumulator);
		}
		if !changed {
			continue;
		}
		while buffer.len() > capacity {
			buffer.pop_first();
		}
		high_water
			.entry(group.clone())
			.and_modify(|hw| {
				if coord > *hw {
					*hw = coord;
				}
			})
			.or_insert(coord);
		touched.insert(group);
	}
	touched
}

pub fn multi_rolling_accumulator_oracle<A>(
	aggregate: &A,
	ctx: &ChaosContext,
	batches: &[ChaosBatch],
	output_key_columns: &[String],
) -> MaterializedTable
where
	A: MultiRollingOperator,
	A::Output: Row,
{
	let capacity = aggregate.capacity();
	let mut buffers: HashMap<MultiGroup<A>, BTreeMap<MultiCoord<A>, A::Accumulator>> = HashMap::new();
	let mut high_water: HashMap<MultiGroup<A>, MultiCoord<A>> = HashMap::new();
	let mut last_visible: HashMap<MultiGroup<A>, Vec<A::Output>> = HashMap::new();

	for batch in batches {
		let snapshot = HashMap::new();
		let buckets = bucket_multi(aggregate, batch);
		let touched = apply_multi_buckets::<A>(capacity, &snapshot, buckets, &mut buffers, &mut high_water);
		for group in touched {
			if let Some(buffer) = buffers.get(&group) {
				let emit = aggregate.combine(&group, buffer);
				last_visible.insert(group, emit.into_values().collect());
			}
		}
	}

	let outputs: Vec<A::Output> = last_visible.into_values().flatten().collect();
	materialize_outputs(outputs.into_iter(), ctx.now_nanos(), output_key_columns)
}

#[allow(clippy::type_complexity)]
fn extract_multi<A>(
	aggregate: &A,
	row: &CoreRow,
) -> Option<(MultiGroup<A>, MultiCoord<A>, <A::Accumulator as WindowAccumulator>::Contribution)>
where
	A: MultiRollingOperator,
{
	let columns = Columns::from_row(row);
	let view = NativeColumnsView::new(&columns);
	let row_view = view.row(0)?;
	with_oracle_ctx(|ctx| aggregate.extract(ctx, &row_view))
}

#[allow(clippy::type_complexity)]
fn extract_carry<A>(
	aggregate: &A,
	row: &CoreRow,
) -> Option<(CarryGroup<A>, CarryCoord<A>, <A::Accumulator as WindowAccumulator>::Contribution)>
where
	A: TumblingCarryOperator,
{
	let columns = Columns::from_row(row);
	let view = NativeColumnsView::new(&columns);
	let row_view = view.row(0)?;
	with_oracle_ctx(|ctx| aggregate.extract(ctx, &row_view))
}