Skip to main content

mz_compute/
typedefs.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Convience typedefs for differential types.
11
12#![allow(dead_code, missing_docs)]
13
14use columnar::{Container, Ref};
15use differential_dataflow::operators::arrange::Arranged;
16use differential_dataflow::operators::arrange::TraceAgent;
17use differential_dataflow::trace::implementations::merge_batcher::MergeBatcher;
18use differential_dataflow::trace::wrappers::enter::TraceEnter;
19use differential_dataflow::trace::wrappers::frontier::TraceFrontier;
20use mz_repr::Diff;
21use mz_timely_util::columnation::ColInternalMerger;
22
23use mz_row_spine::RowValBuilder;
24
25use crate::render::errors::DataflowErrorSer;
26use crate::typedefs::spines::{ColKeyBatcher, ColKeyBuilder, ColValBatcher, ColValBuilder};
27
28pub use crate::typedefs::spines::{ColKeySpine, ColValSpine};
29pub use mz_row_spine::{RowRowSpine, RowSpine, RowValBatcher, RowValSpine};
30
31pub(crate) mod spines {
32    use std::rc::Rc;
33
34    use columnation::Columnation;
35    use differential_dataflow::trace::implementations::ord_neu::{
36        OrdKeyBatch, OrdKeyBuilder, OrdValBatch, OrdValBuilder,
37    };
38    use differential_dataflow::trace::implementations::spine_fueled::Spine;
39    use differential_dataflow::trace::implementations::{Layout, Update};
40    use differential_dataflow::trace::rc_blanket_impls::RcBuilder;
41    use mz_timely_util::columnation::ColumnationStack;
42
43    use mz_row_spine::OffsetOptimized;
44
45    use crate::typedefs::{KeyBatcher, KeyValBatcher};
46
47    /// A spine for generic keys and values.
48    pub type ColValSpine<K, V, T, R> = Spine<Rc<OrdValBatch<MzStack<((K, V), T, R)>>>>;
49    pub type ColValBatcher<K, V, T, R> = KeyValBatcher<K, V, T, R>;
50    pub type ColValBuilder<K, V, T, R> =
51        RcBuilder<OrdValBuilder<MzStack<((K, V), T, R)>, ColumnationStack<((K, V), T, R)>>>;
52
53    /// A spine for generic keys
54    pub type ColKeySpine<K, T, R> = Spine<Rc<OrdKeyBatch<MzStack<((K, ()), T, R)>>>>;
55    pub type ColKeyBatcher<K, T, R> = KeyBatcher<K, T, R>;
56    pub type ColKeyBuilder<K, T, R> =
57        RcBuilder<OrdKeyBuilder<MzStack<((K, ()), T, R)>, ColumnationStack<((K, ()), T, R)>>>;
58
59    /// A layout based on chunked timely stacks
60    pub struct MzStack<U: Update> {
61        phantom: std::marker::PhantomData<U>,
62    }
63
64    impl<U: Update> Layout for MzStack<U>
65    where
66        U::Key: Columnation + 'static,
67        U::Val: Columnation + 'static,
68        U::Time: Columnation,
69        U::Diff: Columnation,
70    {
71        type KeyContainer = ColumnationStack<U::Key>;
72        type ValContainer = ColumnationStack<U::Val>;
73        type TimeContainer = ColumnationStack<U::Time>;
74        type DiffContainer = ColumnationStack<U::Diff>;
75        type OffsetContainer = OffsetOptimized;
76    }
77}
78
79// Spines are data structures that collect and maintain updates.
80// Agents are wrappers around spines that allow shared read access.
81
82// Fully generic spines and agents.
83pub type KeyValSpine<K, V, T, R> = ColValSpine<K, V, T, R>;
84pub type KeyValAgent<K, V, T, R> = TraceAgent<KeyValSpine<K, V, T, R>>;
85pub type KeyValEnter<K, V, T, R, TEnter> =
86    TraceEnter<TraceFrontier<KeyValAgent<K, V, T, R>>, TEnter>;
87
88// Fully generic key-only spines and agents
89pub type KeySpine<K, T, R> = ColKeySpine<K, T, R>;
90pub type KeyAgent<K, T, R> = TraceAgent<KeySpine<K, T, R>>;
91pub type KeyEnter<K, T, R, TEnter> = TraceEnter<TraceFrontier<KeyAgent<K, T, R>>, TEnter>;
92
93// Row specialized spines and agents.
94pub type RowValAgent<V, T, R> = TraceAgent<RowValSpine<V, T, R>>;
95pub type RowValArrangement<'scope, T, V> = Arranged<'scope, RowValAgent<V, T, Diff>>;
96pub type RowValEnter<V, T, R, TEnter> = TraceEnter<TraceFrontier<RowValAgent<V, T, R>>, TEnter>;
97// Row specialized spines and agents.
98pub type RowRowAgent<T, R> = TraceAgent<RowRowSpine<T, R>>;
99pub type RowRowArrangement<'scope, T> = Arranged<'scope, RowRowAgent<T, Diff>>;
100pub type RowRowEnter<T, R, TEnter> = TraceEnter<TraceFrontier<RowRowAgent<T, R>>, TEnter>;
101// Row specialized spines and agents.
102pub type RowAgent<T, R> = TraceAgent<RowSpine<T, R>>;
103pub type RowArrangement<'scope, T> = Arranged<'scope, RowAgent<T, Diff>>;
104pub type RowEnter<T, R, TEnter> = TraceEnter<TraceFrontier<RowAgent<T, R>>, TEnter>;
105
106// Error specialized spines and agents.
107pub type ErrSpine<T, R> = ColKeySpine<DataflowErrorSer, T, R>;
108pub type ErrBatcher<T, R> = ColKeyBatcher<DataflowErrorSer, T, R>;
109pub type ErrBuilder<T, R> = ColKeyBuilder<DataflowErrorSer, T, R>;
110
111pub type ErrAgent<T, R> = TraceAgent<ErrSpine<T, R>>;
112pub type ErrEnter<T, TEnter> = TraceEnter<TraceFrontier<ErrAgent<T, Diff>>, TEnter>;
113
114pub type KeyErrSpine<K, T, R> = ColValSpine<K, DataflowErrorSer, T, R>;
115pub type KeyErrBatcher<K, T, R> = ColValBatcher<K, DataflowErrorSer, T, R>;
116pub type KeyErrBuilder<K, T, R> = ColValBuilder<K, DataflowErrorSer, T, R>;
117
118pub type RowErrSpine<T, R> = RowValSpine<DataflowErrorSer, T, R>;
119pub type RowErrBatcher<T, R> = RowValBatcher<DataflowErrorSer, T, R>;
120pub type RowErrBuilder<T, R> = RowValBuilder<DataflowErrorSer, T, R>;
121
122// Batchers for consolidation
123pub type KeyBatcher<K, T, D> = KeyValBatcher<K, (), T, D>;
124pub type KeyValBatcher<K, V, T, D> = MergeBatcher<ColInternalMerger<(K, V), T, D>>;
125
126/// Timestamp trait for rendering, constraint to support [`MzData`] and [timely::progress::Timestamp].
127pub trait MzTimestamp:
128    MzData + timely::progress::Timestamp + differential_dataflow::lattice::Lattice + std::hash::Hash
129{
130}
131
132impl<T> MzTimestamp for T
133where
134    T: MzData,
135    T: timely::progress::Timestamp,
136    T: differential_dataflow::lattice::Lattice + std::hash::Hash,
137{
138}
139
140/// Trait for data types that can be used in Materialize's dataflow, supporting both columnar and
141/// columnation.
142pub trait MzData:
143    columnation::Columnation
144    + for<'a> columnar::Columnar<Container: Container<Ref<'a>: Copy + Ord> + Clone + Send>
145{
146}
147
148impl<T> MzData for T
149where
150    T: columnation::Columnation,
151    T: for<'a> columnar::Columnar<Container: Clone + Send>,
152    for<'a> Ref<'a, T>: Copy + Ord,
153{
154}
155
156pub trait MzArrangeData: columnation::Columnation {}
157impl<T> MzArrangeData for T where T: columnation::Columnation {}