dbsp 0.287.0

Continuous streaming analytics engine
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
use crate::{
    Circuit, NestedCircuit, Position, RootCircuit, Stream,
    algebra::{IndexedZSet, ZBatch},
    circuit::{
        metadata::{BatchSizeStats, INPUT_BATCHES_STATS, OUTPUT_BATCHES_STATS, OperatorMeta},
        operator_traits::Operator,
        splitter_output_chunk_size,
    },
    dynamic::Erase,
    operator::{
        async_stream_operators::{StreamingNaryOperator, StreamingNaryWrapper},
        dynamic::{MonoIndexedZSet, MonoIndexedZSetFactories, MonoZSet, MonoZSetFactories},
    },
    trace::{
        BatchReader, BatchReaderFactories, Builder, Cursor, Spine, SpineSnapshot, WithSnapshot,
        cursor::{CursorList, CursorWithPolarity},
    },
};
use async_stream::stream;
use std::{
    borrow::Cow,
    cell::{Cell, RefCell},
    marker::PhantomData,
};

impl RootCircuit {
    /// See [`RootCircuit::accumulate_concat_indexed_zsets`].
    pub fn dyn_accumulate_concat_indexed_zsets(
        &self,
        factories: &MonoIndexedZSetFactories,
        streams: &[(Stream<Self, MonoIndexedZSet>, bool)],
    ) -> Stream<Self, MonoIndexedZSet> {
        dyn_accumulate_concat(factories, streams.iter().cloned())
    }

    /// See [`RootCircuit::accumulate_concat_zsets`].
    pub fn dyn_accumulate_concat_zsets(
        &self,
        factories: &MonoZSetFactories,
        streams: &[(Stream<Self, MonoZSet>, bool)],
    ) -> Stream<Self, MonoZSet> {
        dyn_accumulate_concat(factories, streams.iter().cloned())
    }
}

impl NestedCircuit {
    /// See [`NestedCircuit::accumulate_concat_indexed_zsets`].
    pub fn dyn_acumulate_concat_indexed_zsets(
        &self,
        factories: &MonoIndexedZSetFactories,
        streams: &[(Stream<Self, MonoIndexedZSet>, bool)],
    ) -> Stream<Self, MonoIndexedZSet> {
        dyn_accumulate_concat(factories, streams.iter().cloned())
    }

    /// See [`NestedCircuit::accumulate_concat_zsets`].
    pub fn dyn_accumulate_concat_zsets(
        &self,
        factories: &MonoZSetFactories,
        streams: &[(Stream<Self, MonoZSet>, bool)],
    ) -> Stream<Self, MonoZSet> {
        dyn_accumulate_concat(factories, streams.iter().cloned())
    }
}

pub fn dyn_accumulate_concat<C, Z, I>(input_factories: &Z::Factories, streams: I) -> Stream<C, Z>
where
    C: Circuit,
    Z: IndexedZSet,
    I: IntoIterator<Item = (Stream<C, Z>, bool)>,
{
    let (streams, polarities): (Vec<_>, Vec<_>) = streams.into_iter().unzip();

    let streams: Vec<Stream<C, Z>> = streams
        .into_iter()
        .map(|stream| stream.try_sharded_version())
        .collect();
    assert!(!streams.is_empty());

    let sharded = streams.iter().all(|stream| stream.is_sharded());

    let accumulated_streams = streams
        .iter()
        .map(|stream| stream.dyn_accumulate(input_factories))
        .collect::<Vec<_>>();

    let circuit = streams[0].circuit();

    let result = circuit.add_nary_operator(
        StreamingNaryWrapper::new(AccumulateConcatZSets::new(input_factories, &polarities)),
        &accumulated_streams,
    );

    if sharded {
        result.mark_sharded();
    }

    result
}

struct AccumulateConcatZSets<Z>
where
    Z: ZBatch,
{
    factories: Z::Factories,
    snapshots: RefCell<Vec<Option<SpineSnapshot<Z>>>>,
    input_batch_stats: RefCell<Vec<BatchSizeStats>>,
    output_batch_stats: RefCell<BatchSizeStats>,
    polarity: Vec<bool>,
    flush: Cell<bool>,
    phantom: PhantomData<fn(&Z)>,
}

impl<Z> AccumulateConcatZSets<Z>
where
    Z: ZBatch,
{
    pub fn new(factories: &Z::Factories, polarity: &[bool]) -> Self {
        Self {
            factories: factories.clone(),
            snapshots: RefCell::new(Vec::new()),
            input_batch_stats: RefCell::new(Vec::new()),
            output_batch_stats: RefCell::new(BatchSizeStats::new()),
            polarity: polarity.to_vec(),
            flush: Cell::new(false),
            phantom: PhantomData,
        }
    }
}

impl<Z> Operator for AccumulateConcatZSets<Z>
where
    Z: IndexedZSet,
{
    fn name(&self) -> std::borrow::Cow<'static, str> {
        Cow::Borrowed("AccumulateConcat")
    }

    fn metadata(&self, meta: &mut OperatorMeta) {
        self.input_batch_stats
            .borrow()
            .iter()
            .enumerate()
            .for_each(|(i, stats)| {
                meta.insert(
                    INPUT_BATCHES_STATS,
                    vec![(Cow::Borrowed("input"), Cow::Owned(format!("{i}")))],
                    stats.metadata(),
                )
            });
        meta.extend(metadata! {
            OUTPUT_BATCHES_STATS => self.output_batch_stats.borrow().metadata(),
        });
    }

    fn fixedpoint(&self, _scope: crate::circuit::Scope) -> bool {
        true
    }

    fn flush(&mut self) {
        self.flush.set(true);
    }
}

impl<Z: IndexedZSet> StreamingNaryOperator<Option<Spine<Z>>, Z> for AccumulateConcatZSets<Z> {
    fn eval<'a, Iter>(
        self: std::rc::Rc<Self>,
        inputs: Iter,
    ) -> impl futures::Stream<Item = (Z, bool, Option<Position>)> + 'static
    where
        Iter: Iterator<Item = Cow<'a, Option<Spine<Z>>>>,
    {
        let inputs = inputs.into_iter().collect::<Vec<_>>();
        assert_eq!(inputs.len(), self.polarity.len());

        let mut snapshots = self.snapshots.borrow_mut();

        snapshots.resize(inputs.len(), None);

        // Latch final output of each input stream.
        for (i, input) in inputs.iter().enumerate() {
            let snapshot = input.as_ref().as_ref().map(|spine| spine.ro_snapshot());

            if snapshot.is_some() && snapshots[i].is_some() {
                panic!("received input {i} twice");
            }

            if let Some(snapshot) = snapshot {
                let mut input_batch_stats = self.input_batch_stats.borrow_mut();
                input_batch_stats.resize_with(inputs.len(), BatchSizeStats::new);
                input_batch_stats[i].add_batch(snapshot.len());

                snapshots[i] = Some(snapshot);
            }
        }
        drop(snapshots);

        let flush = self.flush.replace(false);
        let factories = self.factories.clone();

        let snapshots = if flush {
            self.snapshots
                .borrow_mut()
                .iter_mut()
                .enumerate()
                .map(|(i, snapshot)| {
                    if snapshot.is_none() {
                        panic!("input {i} is empty on flush");
                    }
                    snapshot.take().unwrap()
                })
                .collect::<Vec<_>>()
        } else {
            Vec::new()
        };

        stream! {
            if !flush {
                yield (Z::dyn_empty(&factories), true, None);
                return;
            }

            let chunk_size = splitter_output_chunk_size();
            let cursors = snapshots.into_iter().enumerate().map(|(i, snapshot)| CursorWithPolarity::new(snapshot.cursor(), self.polarity[i])).collect::<Vec<_>>();

            let mut builder = Z::Builder::with_capacity(&factories, chunk_size, chunk_size);
            let mut cursor = CursorList::new(factories.weight_factory(), cursors);

            let mut has_val = false;

            while cursor.key_valid() {
                while cursor.val_valid() {
                    let w = **cursor.weight();
                    builder.push_val_diff(cursor.val(), w.erase());
                    has_val=true;

                    if builder.num_tuples() >= chunk_size {
                        builder.push_key(cursor.key());
                        has_val = false;

                        let result = builder.done();
                        self.output_batch_stats.borrow_mut().add_batch(result.len());
                        yield (result, false, cursor.position());
                        builder = Z::Builder::with_capacity(&factories, chunk_size, chunk_size);
                    }
                    cursor.step_val();
                }
                if has_val{
                    builder.push_key(cursor.key());
                }
                cursor.step_key();
            }

            let result = builder.done();
            self.output_batch_stats.borrow_mut().add_batch(result.len());

            yield (result, true, cursor.position())
        }
    }
}

#[cfg(test)]
mod test {

    use proptest::{collection::vec, prelude::*};

    use crate::{DBData, Runtime, ZWeight, algebra::NegByRef, circuit::CircuitConfig, utils::Tup2};

    fn test_zset<K: DBData>(inputs: Vec<(Vec<Vec<Tup2<K, ZWeight>>>, bool)>, transaction: bool) {
        let (mut inputs, polarities): (Vec<_>, Vec<_>) = inputs.into_iter().unzip();

        let (mut dbsp, (input_handles, output_handle, expected_output_handle)) =
            Runtime::init_circuit(
                CircuitConfig::from(4).with_splitter_chunk_size_records(2),
                move |circuit| {
                    let mut streams = Vec::new();
                    let mut input_handles = Vec::new();

                    for polarity in polarities.iter() {
                        let (stream, handle) = circuit.add_input_zset::<K>();
                        streams.push((stream, *polarity));
                        input_handles.push(handle)
                    }

                    let output_handle = circuit
                        .accumulate_concat_zsets(&streams)
                        .accumulate_output();

                    let streams_with_polarities = streams
                        .iter()
                        .map(|(stream, polarity)| {
                            if *polarity {
                                stream.clone()
                            } else {
                                stream.apply(|batch| batch.neg_by_ref())
                            }
                        })
                        .collect::<Vec<_>>();

                    let expected_output_handle = streams_with_polarities[0]
                        .sum(streams_with_polarities[1..].iter())
                        .accumulate_output();

                    Ok((input_handles, output_handle, expected_output_handle))
                },
            )
            .unwrap();

        if transaction {
            dbsp.start_transaction().unwrap();
        }

        for step in 0..inputs[0].len() {
            for i in 0..input_handles.len() {
                input_handles[i].append(&mut inputs[i][step]);
            }
            if !transaction {
                dbsp.transaction().unwrap();
                assert_eq!(
                    output_handle.concat().consolidate(),
                    expected_output_handle.concat().consolidate()
                )
            } else {
                dbsp.step().unwrap();
            }
        }

        if transaction {
            dbsp.commit_transaction().unwrap();
            assert_eq!(
                output_handle.concat().consolidate(),
                expected_output_handle.concat().consolidate()
            )
        }
    }

    fn test_indexed_zset<K: DBData, V: DBData>(
        inputs: Vec<(Vec<Vec<Tup2<K, Tup2<V, ZWeight>>>>, bool)>,
        transaction: bool,
    ) {
        let (mut inputs, polarities): (Vec<_>, Vec<_>) = inputs.into_iter().unzip();

        let (mut dbsp, (input_handles, output_handle, expected_output_handle)) =
            Runtime::init_circuit(
                CircuitConfig::from(4).with_splitter_chunk_size_records(2),
                move |circuit| {
                    let mut streams = Vec::new();
                    let mut input_handles = Vec::new();

                    for polarity in polarities.iter() {
                        let (stream, handle) = circuit.add_input_indexed_zset::<K, V>();
                        streams.push((stream, *polarity));
                        input_handles.push(handle)
                    }

                    let output_handle = circuit
                        .accumulate_concat_indexed_zsets(&streams)
                        .accumulate_output();

                    let streams_with_polarities = streams
                        .iter()
                        .map(|(stream, polarity)| {
                            if *polarity {
                                stream.clone()
                            } else {
                                stream.apply(|batch| batch.neg_by_ref())
                            }
                        })
                        .collect::<Vec<_>>();

                    let expected_output_handle = streams_with_polarities[0]
                        .sum(streams_with_polarities[1..].iter())
                        .accumulate_output();

                    Ok((input_handles, output_handle, expected_output_handle))
                },
            )
            .unwrap();

        if transaction {
            dbsp.start_transaction().unwrap();
        }

        for step in 0..inputs[0].len() {
            for i in 0..input_handles.len() {
                input_handles[i].append(&mut inputs[i][step]);
            }
            if !transaction {
                dbsp.transaction().unwrap();
                assert_eq!(
                    output_handle.concat().consolidate(),
                    expected_output_handle.concat().consolidate()
                )
            } else {
                dbsp.step().unwrap();
            }
        }

        if transaction {
            dbsp.commit_transaction().unwrap();
            assert_eq!(
                output_handle.concat().consolidate(),
                expected_output_handle.concat().consolidate()
            )
        }
    }

    fn generate_test_zset(
        max_key: i32,
        max_weight: ZWeight,
        max_tuples: usize,
    ) -> BoxedStrategy<Vec<Tup2<i32, ZWeight>>> {
        vec(
            (0..max_key, -max_weight..max_weight).prop_map(|(x, y)| Tup2(x, y)),
            0..max_tuples,
        )
        .boxed()
    }

    fn generate_test_zsets(
        max_key: i32,
        max_weight: ZWeight,
        max_tuples: usize,
    ) -> BoxedStrategy<(
        Vec<Vec<Tup2<i32, ZWeight>>>,
        Vec<Vec<Tup2<i32, ZWeight>>>,
        Vec<Vec<Tup2<i32, ZWeight>>>,
    )> {
        (
            vec(generate_test_zset(max_key, max_weight, max_tuples), 10),
            vec(generate_test_zset(max_key, max_weight, max_tuples), 10),
            vec(generate_test_zset(max_key, max_weight, max_tuples), 10),
        )
            .boxed()
    }

    fn generate_test_indexed_zset(
        max_key: i32,
        max_val: i32,
        max_weight: ZWeight,
        max_tuples: usize,
    ) -> BoxedStrategy<Vec<Tup2<i32, Tup2<i32, ZWeight>>>> {
        vec(
            (0..max_key, 0..max_val, -max_weight..max_weight)
                .prop_map(|(x, y, z)| Tup2(x, Tup2(y, z))),
            0..max_tuples,
        )
        .boxed()
    }

    fn generate_test_indexed_zsets(
        max_key: i32,
        max_val: i32,
        max_weight: ZWeight,
        max_tuples: usize,
    ) -> BoxedStrategy<(
        Vec<Vec<Tup2<i32, Tup2<i32, ZWeight>>>>,
        Vec<Vec<Tup2<i32, Tup2<i32, ZWeight>>>>,
        Vec<Vec<Tup2<i32, Tup2<i32, ZWeight>>>>,
    )> {
        (
            vec(
                generate_test_indexed_zset(max_key, max_val, max_weight, max_tuples),
                10,
            ),
            vec(
                generate_test_indexed_zset(max_key, max_val, max_weight, max_tuples),
                10,
            ),
            vec(
                generate_test_indexed_zset(max_key, max_val, max_weight, max_tuples),
                10,
            ),
        )
            .boxed()
    }

    proptest! {
        #[test]
        fn proptest_concat_zset_big_step(inputs in generate_test_zsets(10, 3, 100)) {
            let (inputs1, inputs2, inputs3) = inputs;
            test_zset(vec![(inputs1, true), (inputs2, false), (inputs3, true)], true);
        }

        #[test]
        fn proptest_concat_zset_small_step(inputs in generate_test_zsets(10, 3, 100)) {
            let (inputs1, inputs2, inputs3) = inputs;
            test_zset(vec![(inputs1, true), (inputs2, false), (inputs3, true)], false);
        }

        #[test]
        fn proptest_concat_indexed_zset_big_step(inputs in generate_test_indexed_zsets(10, 5, 3, 100)) {
            let (inputs1, inputs2, inputs3) = inputs;
            test_indexed_zset(vec![(inputs1, true), (inputs2, false), (inputs3, true)], true);
        }

        #[test]
        fn proptest_concat_indexed_zset_small_step(inputs in generate_test_indexed_zsets(10, 5, 3, 100)) {
            let (inputs1, inputs2, inputs3) = inputs;
            test_indexed_zset(vec![(inputs1, true), (inputs2, false), (inputs3, true)], false);
        }
    }
}