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
//! Aggregate expression plan.

use timely::dataflow::scopes::child::Iterative;
use timely::dataflow::Scope;
use timely::order::TotalOrder;
use timely::progress::Timestamp;

use differential_dataflow::difference::DiffPair;
use differential_dataflow::lattice::Lattice;
use differential_dataflow::operators::Join as JoinMap;
use differential_dataflow::operators::{Count, Reduce};

use crate::binding::{AsBinding, Binding};
use crate::plan::{Dependencies, ImplContext, Implementable};
use crate::{CollectionRelation, Relation, ShutdownHandle, Value, Var, VariableMap};

use num_rational::{Ratio, Rational32};

/// Permitted aggregation function.
#[derive(Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize)]
pub enum AggregationFn {
    /// Minimum
    MIN,
    /// Maximum
    MAX,
    /// MEDIAN
    MEDIAN,
    /// Count
    COUNT,
    /// Sum
    SUM,
    /// Average
    AVG,
    /// Variance
    VARIANCE,
    // /// Standard deviation
    // STDDEV,
}

/// [WIP] A plan stage applying the specified aggregation functions to
/// bindings for the specified variables. Given multiple aggregations
/// we iterate and n-1 joins are applied to the results.
#[derive(Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize)]
pub struct Aggregate<P: Implementable> {
    /// TODO
    pub variables: Vec<Var>,
    /// Plan for the data source.
    pub plan: Box<P>,
    /// Logical predicate to apply.
    pub aggregation_fns: Vec<AggregationFn>,
    /// Relation variables that determine the grouping.
    pub key_variables: Vec<Var>,
    /// Aggregation variables
    pub aggregation_variables: Vec<Var>,
    /// With variables
    pub with_variables: Vec<Var>,
}

impl<P: Implementable> Implementable for Aggregate<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);

        // We split the incoming tuples into their (key, value) parts.
        let tuples = relation.tuples_by_variables(&self.key_variables);

        // For each aggregation function that is to be applied, we
        // need to determine the index (into the value part of each
        // tuple) at which its argument is to be found.

        let mut value_offsets = Vec::new();
        let mut seen = Vec::new();

        for variable in self.aggregation_variables.iter() {
            if !seen.contains(variable) {
                seen.push(*variable);
                value_offsets.push(seen.len() - 1);
            } else {
                value_offsets.push(AsBinding::binds(&seen, *variable).unwrap());
            }
        }

        // Users can specify weird find clauses like [:find ?key1 (min ?v1) ?key2]
        // and we would like to avoid an extra projection. Thus, we pre-compute
        // the correct output offset for each aggregation.

        let mut variables = self.variables.clone();
        let mut output_offsets = Vec::new();

        for variable in self.aggregation_variables.iter() {
            let output_index = AsBinding::binds(&variables, *variable).unwrap();
            output_offsets.push(output_index);

            variables[output_index] = 0;
        }

        let mut collections = Vec::new();

        // We iterate over all aggregations and keep track of the
        // resulting collections, s.t. they can be joined afterwards.
        for (i, aggregation_fn) in self.aggregation_fns.iter().enumerate() {
            let value_offset = value_offsets[i];
            let with_length = self.with_variables.len();

            // Access the right value for the given iteration loop and extend possible with-values.
            let prepare_unary = move |(key, tuple): (Vec<Value>, Vec<Value>)| {
                let value = &tuple[value_offset];
                let mut v = vec![value.clone()];

                // With-variables are always the last elements in the
                // value part of each tuple, given they are specified.
                // We append these, s.t. we consolidate correctly.
                if with_length > 0 {
                    v.extend(tuple.iter().rev().take(with_length).cloned());
                }

                (key, v)
            };

            match aggregation_fn {
                AggregationFn::MIN => {
                    let tuples = tuples.map(prepare_unary).reduce(|_key, vals, output| {
                        let min = &vals[0].0[0];
                        output.push((vec![min.clone()], 1));
                    });
                    collections.push(tuples);
                }
                AggregationFn::MAX => {
                    let tuples = tuples.map(prepare_unary).reduce(|_key, vals, output| {
                        let max = &vals[vals.len() - 1].0[0];
                        output.push((vec![max.clone()], 1));
                    });
                    collections.push(tuples);
                }
                AggregationFn::MEDIAN => {
                    let tuples = tuples.map(prepare_unary).reduce(|_key, vals, output| {
                        let median = &vals[vals.len() / 2].0[0];
                        output.push((vec![median.clone()], 1));
                    });
                    collections.push(tuples);
                }
                AggregationFn::COUNT => {
                    let tuples = tuples.map(prepare_unary).reduce(|_key, input, output| {
                        let mut total_count = 0;
                        for (_, count) in input.iter() {
                            total_count += count;
                        }

                        output.push((vec![Value::Number(total_count as i64)], 1))
                    });
                    collections.push(tuples);
                }
                AggregationFn::SUM => {
                    let tuples = tuples
                        .map(prepare_unary)
                        .explode(|(key, val)| {
                            let v = match val[0] {
                                Value::Number(num) => num,
                                _ => panic!("SUM can only be applied on type Number."),
                            };
                            Some((key, v as isize))
                        })
                        .count()
                        .map(move |(key, count)| (key, vec![Value::Number(count as i64)]));
                    collections.push(tuples);
                }
                AggregationFn::AVG => {
                    let tuples = tuples
                        .map(prepare_unary)
                        .explode(move |(key, val)| {
                            let v = match val[0] {
                                Value::Number(num) => num,
                                _ => panic!("AVG can only be applied on type Number."),
                            };
                            Some((key, DiffPair::new(v as isize, 1)))
                        })
                        .count()
                        .map(move |(key, diff_pair)| {
                            (
                                key,
                                vec![Value::Rational32(Ratio::new(
                                    diff_pair.element1 as i32,
                                    diff_pair.element2 as i32,
                                ))],
                            )
                        });
                    collections.push(tuples);
                }
                AggregationFn::VARIANCE => {
                    let tuples = tuples
                        .map(prepare_unary)
                        .explode(move |(key, val)| {
                            let v = match val[0] {
                                Value::Number(num) => num,
                                _ => panic!("VARIANCE can only be applied on type Number."),
                            };
                            Some((
                                key,
                                DiffPair::new(
                                    DiffPair::new(v as isize * v as isize, v as isize),
                                    1,
                                ),
                            ))
                        })
                        .count()
                        .map(move |(key, diff_pair)| {
                            let sum_square = diff_pair.element1.element1 as i32;
                            let sum = diff_pair.element1.element2 as i32;
                            let c = diff_pair.element2 as i32;
                            (
                                key,
                                vec![Value::Rational32(
                                    Rational32::new(sum_square, c) - Rational32::new(sum, c).pow(2),
                                )],
                            )
                        });
                    collections.push(tuples);
                }
            };
        }

        if collections.len() == 1 {
            let output_index = output_offsets[0];
            let relation = CollectionRelation {
                variables: self.variables.to_vec(),
                tuples: collections[0].map(move |(key, val)| {
                    let mut k = key.clone();
                    let v = val[0].clone();
                    k.insert(output_index, v);
                    k
                }),
            };

            (relation, shutdown_handle)
        } else {
            // @TODO replace this with a join application
            let left = collections.remove(0);
            let tuples = collections.iter().fold(left, |coll, next| {
                coll.join_map(&next, |key, v1, v2| {
                    let mut val = v1.clone();
                    val.append(&mut v2.clone());
                    (key.clone(), val)
                })
            });

            let relation = CollectionRelation {
                variables: self.variables.to_vec(),
                tuples: tuples.map(move |(key, vals)| {
                    let mut v = key.clone();
                    for (i, val) in vals.iter().enumerate() {
                        v.insert(output_offsets[i], val.clone())
                    }
                    v
                }),
            };

            (relation, shutdown_handle)
        }
    }
}