declarative_dataflow/plan/
project.rs

1//! Projection expression plan.
2
3use timely::dataflow::scopes::child::Iterative;
4use timely::dataflow::Scope;
5use timely::progress::Timestamp;
6
7use differential_dataflow::lattice::Lattice;
8
9use crate::binding::Binding;
10use crate::plan::{next_id, Dependencies, ImplContext, Implementable};
11use crate::{Aid, Eid, Value, Var};
12use crate::{CollectionRelation, Implemented, Relation, ShutdownHandle, VariableMap};
13
14/// A plan stage projecting its source to only the specified sequence
15/// of variables. Throws on unbound variables. Frontends are responsible
16/// for ensuring that the source binds all requested variables.
17#[derive(Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize)]
18pub struct Project<P: Implementable> {
19    /// TODO
20    pub variables: Vec<Var>,
21    /// Plan for the data source.
22    pub plan: Box<P>,
23}
24
25impl<P: Implementable> Implementable for Project<P> {
26    fn dependencies(&self) -> Dependencies {
27        self.plan.dependencies()
28    }
29
30    fn into_bindings(&self) -> Vec<Binding> {
31        self.plan.into_bindings()
32    }
33
34    fn datafy(&self) -> Vec<(Eid, Aid, Value)> {
35        let eid = next_id();
36        let mut data = self.plan.datafy();
37
38        if data.is_empty() {
39            Vec::new()
40        } else {
41            let child_eid = data[0].0;
42
43            data.push((eid, "df.project/binding".to_string(), Value::Eid(child_eid)));
44
45            data
46        }
47    }
48
49    fn implement<'b, T, I, S>(
50        &self,
51        nested: &mut Iterative<'b, S, u64>,
52        local_arrangements: &VariableMap<Iterative<'b, S, u64>>,
53        context: &mut I,
54    ) -> (Implemented<'b, S>, ShutdownHandle)
55    where
56        T: Timestamp + Lattice,
57        I: ImplContext<T>,
58        S: Scope<Timestamp = T>,
59    {
60        let (relation, mut shutdown_handle) =
61            self.plan.implement(nested, local_arrangements, context);
62        let tuples = {
63            let (projected, shutdown) = relation.projected(nested, context, &self.variables);
64            shutdown_handle.merge_with(shutdown);
65
66            projected
67        };
68
69        let projected = CollectionRelation {
70            variables: self.variables.to_vec(),
71            tuples,
72        };
73
74        (Implemented::Collection(projected), shutdown_handle)
75    }
76}