From f240164405a294be22073a2d25ac32cffd55cdf5 Mon Sep 17 00:00:00 2001 From: digicoder Date: Sun, 22 Nov 2026 12:15:56 +0000 Subject: [PATCH] reduce: pend joins of history times advanced past upper With partially ordered times, a reduce advances its input and output history by the meet of the times it will visit, and that can move a history time from before the retired interval to beyond it. Such a time is not interesting, as no batch or pending time lies below it, but its joins with this interval's batch times and current interesting times are. Both tactics skipped every time in advance of `upper` without forming those joins, so the joins were neither evaluated nor carried forward. No later interval forms them either: there both partners are history, which does not seed interesting times. The output then misses corrections at those joins. The cursor tactic now runs the determination step for an uninteresting time even when it is in advance of `upper_limit`; its joins are also in advance of `upper_limit` and so are deferred. The proxy tactic now closes an unreached time against the reached times even out of the interval; `close` pends the joins. Under a total order an uninteresting time has no such joins, so only partially ordered times take the new path. Regression tests: `distinct` over a three-dimensional product order, the same through `iterate` (a transitive closure, as a randomized test found it), and a retire-level test of the proxy tactic. --- .../src/operators/cursor/reduce.rs | 8 +- .../src/operators/int_proxy/reduce.rs | 16 ++- differential-dataflow/tests/int_proxy.rs | 40 +++++++ differential-dataflow/tests/reduce.rs | 109 ++++++++++++++++++ 4 files changed, 166 insertions(+), 7 deletions(-) diff --git a/differential-dataflow/src/operators/cursor/reduce.rs b/differential-dataflow/src/operators/cursor/reduce.rs index 0f11a39f4..11a83853d 100644 --- a/differential-dataflow/src/operators/cursor/reduce.rs +++ b/differential-dataflow/src/operators/cursor/reduce.rs @@ -412,7 +412,13 @@ mod history_replay { // We have no particular guarantee that known times will not be in advance of `upper_limit`. // We may have the guarantee that synthetic times will not be, as we test against the limit // before we add the time to `synth_times`. - if !upper_limit.less_equal(&next_time) { + // + // An uninteresting time in advance of `upper_limit` still needs its synthetic times. With + // partially ordered times, advancing history by `meet` can move a time from before the + // interval to beyond it. Its joins with batch times and `times_current` are interesting, + // and no later interval finds them, as it seeds only from its own batch and pending times. + // The joins are also in advance of `upper_limit`, so the determination below defers them. + if !interesting || !upper_limit.less_equal(&next_time) { // DETERMINATION (times only). Determine synthetic interesting times. // diff --git a/differential-dataflow/src/operators/int_proxy/reduce.rs b/differential-dataflow/src/operators/int_proxy/reduce.rs index 67a38a25a..47dd7fc14 100644 --- a/differential-dataflow/src/operators/int_proxy/reduce.rs +++ b/differential-dataflow/src/operators/int_proxy/reduce.rs @@ -599,14 +599,18 @@ impl, pended: &mut Vec) -> Tick { let Some(at) = self.frontier() else { return Tick::Done }; let reached = self.absorb(&at); - if upper.less_equal(&at) { - // Out of the interval: nothing can be emitted here, so there is nothing to close - // against either — a join with `at` is at or beyond `at`, hence also out of interval, - // and will be rediscovered from `at` in the round that admits it. + if reached && upper.less_equal(&at) { + // Out of the interval: nothing can be emitted here. A join with `at` is at or beyond + // `at`, hence also out of interval, and will be rediscovered from `at` in the round + // that admits it. self.settle(); - if reached { pended.push(at); return Tick::Pended; } - return Tick::Passed; + pended.push(at); + return Tick::Pended; } + // An unreached time is closed against the reached times even out of the interval. With + // partially ordered times, advancing the prior times by the meet can move one from before + // the interval to beyond it, and no later round rediscovers its joins with this round's + // seeds: there both are prior. The joins are at or beyond `at`, so `close` pends them. self.close(&at, reached, upper, pended); if reached { return Tick::Crossing(at); } self.settle(); diff --git a/differential-dataflow/tests/int_proxy.rs b/differential-dataflow/tests/int_proxy.rs index 412d53318..6db443e98 100644 --- a/differential-dataflow/tests/int_proxy.rs +++ b/differential-dataflow/tests/int_proxy.rs @@ -609,6 +609,46 @@ fn reduce_prior_advanced_past_novel() { assert!(pending.is_empty()); } +/// Advancing a prior record by the meet of the seeds can move it past `upper` while its join with +/// a seed is still owed. Prior `(0,1,0)` advances by the meet `(0,0,1)` of seeds `(0,0,2)` and +/// `(1,0,1)` to `(0,1,1)`, which is past `upper`. Its join with `(0,0,2)`, at `(0,1,2)`, must be +/// pended, as the value is present twice there. Two dimensions cannot produce this case. +#[test] +fn reduce_unreached_time_past_upper_pends_joins() { + type T = Product>; + let time = |a, b, c| -> T { Product::new(a, Product::new(b, c)) }; + let updates = vec![((7u64, 3u64), time(0, 1, 0), 1i64), ((7, 3), time(0, 0, 2), 1), ((7, 3), time(1, 0, 1), 1)]; + let prior = hbatch(updates[.. 1].to_vec(), time(0, 0, 0), time(0, 0, 1)); + let novel = hbatch(updates[1 ..].to_vec(), time(0, 0, 1), time(0, 1, 1)); + let frontier = Antichain::from_elem; + let mut tactic = ProxyReduceTactic::new(VecReduceBackend::new(max_logic)); + let (first, pending) = tactic.retire( + vec![], vec![], vec![prior.clone()], + &frontier(time(0, 0, 0)), &frontier(time(0, 0, 1)), &frontier(time(0, 0, 0)), + ); + assert!(pending.is_empty()); + let mut outputs: Vec<_> = first.into_iter().filter_map(|s| s.inner).collect(); + let (second, pending) = tactic.retire( + vec![prior.clone()], outputs.clone(), vec![novel.clone()], + &frontier(time(0, 0, 1)), &frontier(time(0, 1, 1)), &frontier(time(0, 0, 1)), + ); + outputs.extend(second.into_iter().filter_map(|s| s.inner)); + // No new records: the rest of the work is the pending times. + let (third, pending) = tactic.retire( + vec![prior, novel], outputs.clone(), vec![], + &frontier(time(0, 1, 1)), &frontier(time(3, 3, 3)), &pending, + ); + assert!(pending.is_empty()); + outputs.extend(third.into_iter().filter_map(|s| s.inner)); + let outputs = hread(&outputs); + for (a, b, c) in (0 .. 3).flat_map(|a| (0 .. 3).flat_map(move |b| (0 .. 3).map(move |c| (a, b, c)))) { + let at = time(a, b, c); + let count: i64 = updates.iter().filter(|(_, t, _)| timely::PartialOrder::less_equal(t, &at)).map(|(_, _, d)| d).sum(); + let output: i64 = outputs.iter().filter(|(_, t, _)| timely::PartialOrder::less_equal(t, &at)).map(|(_, _, d)| d).sum(); + assert_eq!(output, if count > 0 { 1 } else { 0 }, "at {at:?}"); + } +} + // ===== Reading as of a time ===== // // A trace's accumulations are correct only at or beyond its compaction frontier: compaction can diff --git a/differential-dataflow/tests/reduce.rs b/differential-dataflow/tests/reduce.rs index 419e2c2aa..114cd3bad 100644 --- a/differential-dataflow/tests/reduce.rs +++ b/differential-dataflow/tests/reduce.rs @@ -1,6 +1,7 @@ use timely::dataflow::operators::{ToStream, Capture, vec::Map}; use timely::dataflow::operators::capture::Extract; use differential_dataflow::AsCollection; +use differential_dataflow::operators::Iterate; use differential_dataflow::input::InputSession; use differential_dataflow::trace::implementations::{ValBuilder, ValSpine}; @@ -84,3 +85,111 @@ fn reduce_frontier_ahead_of_trace() { let updates: Vec<_> = recv.extract().into_iter().flat_map(|(_, data)| data).collect(); assert_eq!(updates, vec![((0, 1), 7, 1)]); } + +/// A time in a three-dimensional product order. The case below needs three dimensions. +type Time3 = timely::order::Product>; +fn time3(a: u64, b: u64, c: u64) -> Time3 { timely::order::Product::new(a, timely::order::Product::new(b, c)) } + +/// Feeds `rounds` to `logic` through an unordered input whose frontier starts at `initial`. Each +/// round gives its updates, advances the input frontier to the round's frontier, and runs the +/// dataflow until the output frontier catches up. Returns every output update. +fn run_rounds(initial: Vec, rounds: Vec<(Vec<(D, Time3, isize)>, Vec)>, logic: F) -> Vec<(D, Time3, isize)> +where + D: differential_dataflow::Data + Send + Sync, + F: for<'scope> FnOnce(differential_dataflow::VecCollection<'scope, Time3, D, isize>) -> differential_dataflow::VecCollection<'scope, Time3, D, isize> + Send + Sync + 'static, +{ + use timely::PartialOrder; + use timely::dataflow::operators::vec::unordered_input::UnorderedInput; + + let (send, recv) = std::sync::mpsc::channel(); + timely::execute_directly(move |worker| { + let (mut input, root, probe) = worker.dataflow::(|outer| { + outer.scoped::("Product", |scope| { + let ((input, root), stream) = scope.new_unordered_input::<(D, Time3, isize)>(); + // `inspect` rather than `capture`: the output can carry multi-capability stamps. + let (probe, _) = logic(stream.as_collection()).inspect(move |update| send.send(update.clone()).unwrap()).probe(); + (input, root, probe) + }) + }); + let mut caps: Vec<_> = initial.iter().map(|t| root.delayed(t)).collect(); + drop(root); + for (updates, frontier) in rounds { + for (data, time, diff) in updates { + let cap = caps.iter().find(|c| c.time().less_equal(&time)).expect("update behind the frontier"); + input.activate().session(cap).give((data, time, diff)); + } + caps = frontier.iter().map(|t| caps.iter().find(|c| c.time().less_equal(t)).expect("frontier regresses").delayed(t)).collect(); + worker.step_while(|| probe.with_frontier(|f| f.iter().any(|p| !caps.iter().any(|c| c.time().less_equal(p))))); + } + }); + recv.try_iter().collect() +} + +/// The accumulation of `updates` at `time`, without zeros. +fn accumulate(updates: &[(D, Time3, isize)], time: &Time3) -> std::collections::BTreeMap { + use timely::PartialOrder; + let mut acc = std::collections::BTreeMap::new(); + for (data, t, diff) in updates { + if t.less_equal(time) { *acc.entry(data.clone()).or_insert(0) += diff; } + } + acc.retain(|_, diff| *diff != 0); + acc +} + +/// Every time in `[0, bound)^3`. +fn grid(bound: u64) -> impl Iterator { + (0 .. bound).flat_map(move |a| (0 .. bound).flat_map(move |b| (0 .. bound).map(move |c| time3(a, b, c)))) +} + +/// A history time can need re-evaluation at its join with a new time even though both lie outside +/// the interval the reduce retires. Here `(0,1,0)` arrives first; `(0,0,2)` and `(1,0,1)` follow in +/// `[(0,0,1), (0,1,1))`. The reduce advances the history time by the meet of the new times, to +/// `(0,1,1)`, past the interval. Its join with `(0,0,2)`, at `(0,1,2)`, must still come back as a +/// pending time, where `distinct` retracts the second copy of the record. +#[test] +fn distinct_join_with_history_past_upper() { + let rounds = vec![ + (vec![(0u64, time3(0, 1, 0), 1)], vec![time3(0, 0, 1)]), + (vec![(0, time3(0, 0, 2), 1), (0, time3(1, 0, 1), 1)], vec![time3(0, 1, 1)]), + ]; + let input: Vec<_> = rounds.iter().flat_map(|(updates, _)| updates.clone()).collect(); + let output = run_rounds(vec![time3(0, 0, 0)], rounds, |c| c.distinct()); + for time in grid(4) { + let expected = accumulate(&input, &time).into_keys().map(|k| (k, 1)).collect(); + assert_eq!(accumulate(&output, &time), expected, "at {time:?}"); + } +} + +/// The case above, as a randomized test found it (seed 219): a transitive closure by `iterate` and +/// `distinct`, over edges that arrive at incomparable times in two rounds. The edge `1 -> 6` is +/// derived at `(0,1,2)` (`1 -> 2 -> 6`) and at `(2,1,0)` (`1 -> 2 -> 5 -> 6`), and at their join +/// `(2,1,2)` the closure must still hold it once. +#[test] +fn iterate_distinct_join_with_history_past_upper() { + let rounds = vec![ + (vec![((5u8, 6u8), time3(1, 0, 0), 1), ((1, 2), time3(0, 1, 0), 1), ((2, 5), time3(2, 0, 0), 1), ((1, 6), time3(3, 0, 0), 1)], + vec![time3(4, 0, 0), time3(0, 2, 0), time3(0, 0, 1)]), + (vec![((1, 1), time3(0, 0, 1), 1), ((2, 6), time3(0, 0, 2), 1)], + vec![time3(4, 0, 0), time3(0, 2, 0), time3(0, 0, 3)]), + ]; + let input: Vec<_> = rounds.iter().flat_map(|(updates, _)| updates.clone()).collect(); + let initial = vec![time3(1, 0, 0), time3(0, 1, 0), time3(0, 0, 1)]; + let output = run_rounds(initial, rounds, |edges| { + let links = edges.clone(); + edges.iterate(move |inner, reach| { + let links = links.enter(inner); + reach.map(|(a, b)| (b, a)).join_map(links.clone(), |_b, a, c| (*a, *c)).concat(links).distinct() + }) + }); + for time in grid(4) { + let mut expected = accumulate(&input, &time); + loop { + let paths: Vec<_> = expected.keys().flat_map(|&(a, b)| expected.keys().filter(move |&&(c, _)| c == b).map(move |&(_, d)| (a, d))).collect(); + let size = expected.len(); + expected.extend(paths.into_iter().map(|path| (path, 1))); + if expected.len() == size { break; } + } + let expected = expected.into_keys().map(|path| (path, 1)).collect(); + assert_eq!(accumulate(&output, &time), expected, "at {time:?}"); + } +}