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
use differential_dataflow::lattice::Lattice;
use differential_dataflow::operators::arrange::{Arranged, TraceAgent};
use differential_dataflow::operators::reduce::ReduceCore;
use differential_dataflow::operators::Consolidate;
use expr::MirScalarExpr;
use timely::dataflow::Scope;
use timely::progress::{timestamp::Refines, Timestamp};
use repr::{Diff, Row};
use crate::arrangement::manager::RowSpine;
use crate::render::context::CollectionBundle;
use crate::render::context::{ArrangementFlavor, Context};
use dataflow_types::plan::threshold::{
BasicThresholdPlan, RetractionsThresholdPlan, ThresholdPlan,
};
fn threshold_arrangement<G, T, R, L>(
arrangement: &R,
name: &str,
logic: L,
) -> Arranged<G, TraceAgent<RowSpine<Row, Row, G::Timestamp, Diff>>>
where
G: Scope,
G::Timestamp: Lattice + Refines<T>,
T: Timestamp + Lattice,
R: ReduceCore<G, Row, Row, Diff>,
L: Fn(&Diff) -> bool + 'static,
{
arrangement.reduce_abelian(name, move |_key, s, t| {
for (record, count) in s.iter() {
if logic(count) {
t.push(((*record).clone(), *count));
}
}
})
}
pub fn build_threshold_basic<G, T>(
input: CollectionBundle<G, Row, T>,
key: Vec<MirScalarExpr>,
) -> CollectionBundle<G, Row, T>
where
G: Scope,
G::Timestamp: Lattice + Refines<T>,
T: Timestamp + Lattice,
{
let arrangement = input
.arrangement(&key)
.expect("Arrangement ensured to exist");
match arrangement {
ArrangementFlavor::Local(oks, errs) => {
let oks = threshold_arrangement(&oks, "Threshold local", |count| *count > 0);
CollectionBundle::from_expressions(key, ArrangementFlavor::Local(oks, errs))
}
ArrangementFlavor::Trace(_, oks, errs) => {
let oks = threshold_arrangement(&oks, "Threshold trace", |count| *count > 0);
use differential_dataflow::operators::arrange::ArrangeBySelf;
let errs = errs.as_collection(|k, _| k.clone()).arrange_by_self();
CollectionBundle::from_expressions(key, ArrangementFlavor::Local(oks, errs))
}
}
}
pub fn build_threshold_retractions<G, T>(
input: CollectionBundle<G, Row, T>,
key: Vec<MirScalarExpr>,
) -> CollectionBundle<G, Row, T>
where
G: Scope,
G::Timestamp: Lattice + Refines<T>,
T: Timestamp + Lattice,
{
let arrangement = input
.arrangement(&key)
.expect("Arrangement ensured to exist");
let negatives = match &arrangement {
ArrangementFlavor::Local(oks, _) => {
threshold_arrangement(oks, "Threshold retractions local", |count| *count < 0)
}
ArrangementFlavor::Trace(_, oks, _) => {
threshold_arrangement(oks, "Threshold retractions trace", |count| *count < 0)
}
};
let (oks, errs) = arrangement.as_collection();
let oks = negatives
.as_collection(|k, _| k.clone())
.negate()
.concat(&oks)
.consolidate();
CollectionBundle::from_collections(oks, errs)
}
impl<G, T> Context<G, Row, T>
where
G: Scope,
G::Timestamp: Lattice + Refines<T>,
T: Timestamp + Lattice,
{
pub fn render_threshold(
&self,
input: CollectionBundle<G, Row, T>,
threshold_plan: ThresholdPlan,
) -> CollectionBundle<G, Row, T> {
match threshold_plan {
ThresholdPlan::Basic(BasicThresholdPlan { ensure_arrangement }) => {
build_threshold_basic(input, ensure_arrangement.0)
}
ThresholdPlan::Retractions(RetractionsThresholdPlan { ensure_arrangement }) => {
build_threshold_retractions(input, ensure_arrangement.0)
}
}
}
}