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
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Threshold execution logic.
//!
//! Consult [ThresholdPlan] documentation for details.

use differential_dataflow::lattice::Lattice;
use differential_dataflow::operators::arrange::{Arranged, TraceAgent};
use differential_dataflow::trace::cursor::IntoOwned;
use differential_dataflow::trace::{Batch, Builder, Trace, TraceReader};
use differential_dataflow::Data;
use mz_compute_types::plan::threshold::{BasicThresholdPlan, ThresholdPlan};
use mz_expr::MirScalarExpr;
use mz_repr::Diff;
use timely::container::columnation::{Columnation, TimelyStack};
use timely::dataflow::Scope;
use timely::progress::timestamp::Refines;
use timely::progress::Timestamp;

use crate::extensions::arrange::{ArrangementSize, KeyCollection, MzArrange};
use crate::extensions::reduce::MzReduce;
use crate::render::context::{
    ArrangementFlavor, CollectionBundle, Context, MzArrangement, MzArrangementImport,
};

/// Shared function to compute an arrangement of values matching `logic`.
fn threshold_arrangement<G, K, V, T1, T2, L>(
    arrangement: &Arranged<G, T1>,
    name: &str,
    logic: L,
) -> Arranged<G, TraceAgent<T2>>
where
    G: Scope,
    G::Timestamp: Lattice + Columnation,
    V: Data + Columnation,
    T1: TraceReader<Time = G::Timestamp, Diff = Diff> + Clone + 'static,
    for<'a> T1::Key<'a>: IntoOwned<'a, Owned = K>,
    for<'a> T1::Val<'a>: IntoOwned<'a, Owned = V>,
    K: Columnation + Data,
    T2: for<'a> Trace<
            Key<'a> = T1::Key<'a>,
            Val<'a> = T1::Val<'a>,
            Time = G::Timestamp,
            Diff = Diff,
        > + 'static,
    T2::Batch: Batch,
    for<'a> T2::Builder: Builder<Input = TimelyStack<((K, V), G::Timestamp, Diff)>>,
    L: Fn(&Diff) -> bool + 'static,
    Arranged<G, TraceAgent<T2>>: ArrangementSize,
{
    arrangement.mz_reduce_abelian(name, move |_key, s, t| {
        for (record, count) in s.iter() {
            if logic(count) {
                t.push(((*record).into_owned(), *count));
            }
        }
    })
}

/// Dispatches according to existing type-specialization to an appropriate threshold computation
/// resulting in another type-specialized arrangement.
fn dispatch_threshold_arrangement_local<G, L>(
    oks: &MzArrangement<G>,
    name: &str,
    logic: L,
) -> MzArrangement<G>
where
    G: Scope,
    G::Timestamp: Lattice + Columnation,
    L: Fn(&Diff) -> bool + 'static,
{
    match oks {
        MzArrangement::RowRow(inner) => {
            let oks = threshold_arrangement(inner, name, logic);
            MzArrangement::RowRow(oks)
        }
    }
}

/// Dispatches threshold computation for a trace, similarly to `dispatch_threshold_arrangement_local`.
fn dispatch_threshold_arrangement_trace<G, T, L>(
    oks: &MzArrangementImport<G, T>,
    name: &str,
    logic: L,
) -> MzArrangement<G>
where
    G: Scope,
    T: Timestamp + Lattice + Columnation,
    G::Timestamp: Lattice + Refines<T> + Columnation,
    L: Fn(&Diff) -> bool + 'static,
{
    match oks {
        MzArrangementImport::RowRow(inner) => {
            let oks = threshold_arrangement(inner, name, logic);
            MzArrangement::RowRow(oks)
        }
    }
}

/// Build a dataflow to threshold the input data.
///
/// This implementation maintains rows in the output, i.e. all rows that have a count greater than
/// zero. It returns a [CollectionBundle] populated from a local arrangement.
pub fn build_threshold_basic<G, T>(
    input: CollectionBundle<G, T>,
    key: Vec<MirScalarExpr>,
) -> CollectionBundle<G, T>
where
    G: Scope,
    G::Timestamp: Lattice + Refines<T> + Columnation,
    T: Timestamp + Lattice + Columnation,
{
    let arrangement = input
        .arrangement(&key)
        .expect("Arrangement ensured to exist");
    match arrangement {
        ArrangementFlavor::Local(oks, errs) => {
            let oks =
                dispatch_threshold_arrangement_local(&oks, "Threshold local", |count| *count > 0);
            CollectionBundle::from_expressions(key, ArrangementFlavor::Local(oks, errs))
        }
        ArrangementFlavor::Trace(_, oks, errs) => {
            let oks =
                dispatch_threshold_arrangement_trace(&oks, "Threshold trace", |count| *count > 0);
            let errs: KeyCollection<_, _, _> = errs.as_collection(|k, _| k.clone()).into();
            let errs = errs.mz_arrange("Arrange threshold basic err");
            CollectionBundle::from_expressions(key, ArrangementFlavor::Local(oks, errs))
        }
    }
}

impl<G, T> Context<G, T>
where
    G: Scope,
    G::Timestamp: Lattice + Refines<T> + Columnation,
    T: Timestamp + Lattice + Columnation,
{
    pub(crate) fn render_threshold(
        &self,
        input: CollectionBundle<G, T>,
        threshold_plan: ThresholdPlan,
    ) -> CollectionBundle<G, T> {
        match threshold_plan {
            ThresholdPlan::Basic(BasicThresholdPlan {
                ensure_arrangement: (key, _, _),
            }) => {
                // We do not need to apply the permutation here,
                // since threshold doesn't inspect the values, but only
                // their counts.
                build_threshold_basic(input, key)
            }
        }
    }
}