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
use timely::dataflow::scopes::child::Iterative;
use timely::dataflow::Scope;
use timely::order::TotalOrder;
use timely::progress::Timestamp;
use differential_dataflow::lattice::Lattice;
use crate::binding::{AsBinding, Binding};
use crate::plan::{Dependencies, ImplContext, Implementable};
use crate::{CollectionRelation, Relation, ShutdownHandle, Value, Var, VariableMap};
#[derive(Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize)]
pub enum Function {
TRUNCATE,
ADD,
SUBTRACT,
}
#[derive(Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize)]
pub struct Transform<P: Implementable> {
pub variables: Vec<Var>,
pub result_variable: Var,
pub plan: Box<P>,
pub function: Function,
pub constants: Vec<Option<Value>>,
}
impl<P: Implementable> Implementable for Transform<P> {
fn dependencies(&self) -> Dependencies {
self.plan.dependencies()
}
fn into_bindings(&self) -> Vec<Binding> {
self.plan.into_bindings()
}
fn implement<'b, T, I, S>(
&self,
nested: &mut Iterative<'b, S, u64>,
local_arrangements: &VariableMap<Iterative<'b, S, u64>>,
context: &mut I,
) -> (CollectionRelation<'b, S>, ShutdownHandle)
where
T: Timestamp + Lattice + TotalOrder,
I: ImplContext<T>,
S: Scope<Timestamp = T>,
{
let (relation, shutdown_handle) = self.plan.implement(nested, local_arrangements, context);
let key_offsets: Vec<usize> = self
.variables
.iter()
.map(|variable| relation.binds(*variable).expect("variable not found"))
.collect();
let mut variables = relation.variables();
variables.push(self.result_variable);
let constants_local = self.constants.clone();
let transformed = match self.function {
Function::TRUNCATE => CollectionRelation {
variables,
tuples: relation.tuples().map(move |tuple| {
let mut t = match tuple[key_offsets[0]] {
Value::Instant(inst) => inst as u64,
_ => panic!("TRUNCATE can only be applied to timestamps"),
};
let default_interval = String::from(":hour");
let interval_param = match constants_local[1].clone() {
Some(Value::String(interval)) => interval,
None => default_interval,
_ => panic!("Parameter for TRUNCATE must be a string"),
};
let mod_val = match interval_param.as_ref() {
":minute" => 60000,
":hour" => 3_600_000,
":day" => 86_400_000,
":week" => 604_800_000,
_ => panic!("Unknown interval for TRUNCATE"),
};
t = t - (t % mod_val);
let mut v = tuple.clone();
v.push(Value::Instant(t));
v
}),
},
Function::ADD => CollectionRelation {
variables,
tuples: relation.tuples().map(move |tuple| {
let mut result = 0;
for offset in &key_offsets {
let summand = match tuple[*offset] {
Value::Number(s) => s as i64,
_ => panic!("ADD can only be applied to numbers"),
};
result += summand;
}
for arg in &constants_local {
if let Some(constant) = arg {
let summand = match constant {
Value::Number(s) => *s as i64,
_ => panic!("ADD can only be applied to numbers"),
};
result += summand;
}
}
let mut v = tuple.clone();
v.push(Value::Number(result));
v
}),
},
Function::SUBTRACT => CollectionRelation {
variables,
tuples: relation.tuples().map(move |tuple| {
let mut result = match constants_local[0].clone() {
Some(constant) => match constant {
Value::Number(minuend) => minuend as i64,
_ => panic!("SUBTRACT can only be applied to numbers"),
},
None => match tuple[key_offsets[0]] {
Value::Number(minuend) => minuend as i64,
_ => panic!("SUBTRACT can only be applied to numbers"),
},
};
result = result + result;
for offset in &key_offsets {
let subtrahend = match tuple[*offset] {
Value::Number(s) => s as i64,
_ => panic!("SUBTRACT can only be applied to numbers"),
};
result -= subtrahend;
}
for arg in &constants_local {
if let Some(constant) = arg {
let subtrahend = match constant {
Value::Number(s) => *s as i64,
_ => panic!("SUBTRACT can only be applied to numbers"),
};
result -= subtrahend;
}
}
let mut v = tuple.clone();
v.push(Value::Number(result));
v
}),
},
};
(transformed, shutdown_handle)
}
}