Skip to content

aggregation: allow aggregate to emit multiple values #492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions timely/src/dataflow/operators/aggregation/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub trait Aggregate<S: Scope, K: ExchangeData+Hash, V: ExchangeData> {
///
/// The `aggregate` method is implemented for streams of `(K, V)` data,
/// and takes functions `fold`, `emit`, and `hash`; used to combine new `V`
/// data with existing `D` state, to produce `R` output from `D` state, and
/// data with existing `D` state, to produce `I` output from `D` state, and
/// to route `K` keys, respectively.
///
/// Aggregation happens within each time, and results are produced once the
Expand All @@ -32,8 +32,9 @@ pub trait Aggregate<S: Scope, K: ExchangeData+Hash, V: ExchangeData> {
/// (0..10).to_stream(scope)
/// .map(|x| (x % 2, x))
/// .aggregate(
/// i32::default,
/// |_key, val, agg| { *agg += val; },
/// |key, agg: i32| (key, agg),
/// |key, agg| [(key, agg)],
/// |key| *key as u64
/// )
/// .inspect(|x| assert!(*x == (0, 20) || *x == (1, 25)));
Expand All @@ -52,28 +53,36 @@ pub trait Aggregate<S: Scope, K: ExchangeData+Hash, V: ExchangeData> {
///
/// (0..10).to_stream(scope)
/// .map(|x| (x % 2, x))
/// .aggregate::<_,Vec<i32>,_,_,_>(
/// .aggregate(
/// Vec::default,
/// |_key, val, agg| { agg.push(val); },
/// |key, agg| (key, agg.len()),
/// |key, agg| [(key, agg.len())],
/// |key| *key as u64
/// )
/// .inspect(|x| assert!(*x == (0, 5) || *x == (1, 5)));
/// });
/// ```
fn aggregate<R: Data, D: Default+'static, F: Fn(&K, V, &mut D)+'static, E: Fn(K, D)->R+'static, H: Fn(&K)->u64+'static>(
fn aggregate<I: IntoIterator, D: 'static, M: Fn() -> D+ 'static, F: Fn(&K, V, &mut D)+'static, E: Fn(K, D)->I+'static, H: Fn(&K)->u64+'static>(
&self,
make_default: M,
fold: F,
emit: E,
hash: H) -> Stream<S, R> where S::Timestamp: Eq;
hash: H) -> Stream<S, I::Item>
where S::Timestamp: Eq,
I::Item: Data;
}

impl<S: Scope, K: ExchangeData+Hash+Eq, V: ExchangeData> Aggregate<S, K, V> for Stream<S, (K, V)> {

fn aggregate<R: Data, D: Default+'static, F: Fn(&K, V, &mut D)+'static, E: Fn(K, D)->R+'static, H: Fn(&K)->u64+'static>(
fn aggregate<I: IntoIterator, D: 'static, M: Fn() -> D + 'static, F: Fn(&K, V, &mut D)+'static, E: Fn(K, D)->I+'static, H: Fn(&K)->u64+'static>(
&self,
make_default: M,
fold: F,
emit: E,
hash: H) -> Stream<S, R> where S::Timestamp: Eq {
hash: H) -> Stream<S, I::Item>
where S::Timestamp: Eq,
I::Item: Data
{

let mut aggregates = HashMap::new();
let mut vector = Vec::new();
Expand All @@ -84,7 +93,7 @@ impl<S: Scope, K: ExchangeData+Hash+Eq, V: ExchangeData> Aggregate<S, K, V> for
data.swap(&mut vector);
let agg_time = aggregates.entry(time.time().clone()).or_insert_with(HashMap::new);
for (key, val) in vector.drain(..) {
let agg = agg_time.entry(key.clone()).or_insert_with(Default::default);
let agg = agg_time.entry(key.clone()).or_insert_with(&make_default);
fold(&key, val, agg);
}
notificator.notify_at(time.retain());
Expand All @@ -95,7 +104,7 @@ impl<S: Scope, K: ExchangeData+Hash+Eq, V: ExchangeData> Aggregate<S, K, V> for
if let Some(aggs) = aggregates.remove(time.time()) {
let mut session = output.session(&time);
for (key, agg) in aggs {
session.give(emit(key, agg));
session.give_iterator(emit(key, agg).into_iter());
}
}
});
Expand Down