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 columnation::Columnation;
33    use differential_dataflow::trace::implementations::ord_neu::{
34        OrdKeyBatch, OrdKeyBuilder, OrdValBatch, OrdValBuilder,
35    };
36    use differential_dataflow::trace::implementations::spine_fueled::Spine;
37    use differential_dataflow::trace::implementations::{Layout, Update};
38    use mz_timely_util::columnation::ColumnationStack;
39
40    use mz_row_spine::{ArcBatch, ArcBuilder, OffsetOptimized};
41
42    use crate::typedefs::{KeyBatcher, KeyValBatcher};
43
44    /// A spine for generic keys and values.
45    pub type ColValSpine<K, V, T, R> = Spine<ArcBatch<OrdValBatch<MzStack<((K, V), T, R)>>>>;
46    pub type ColValBatcher<K, V, T, R> = KeyValBatcher<K, V, T, R>;
47    pub type ColValBuilder<K, V, T, R> =
48        ArcBuilder<OrdValBuilder<MzStack<((K, V), T, R)>, ColumnationStack<((K, V), T, R)>>>;
49
50    /// A spine for generic keys
51    pub type ColKeySpine<K, T, R> = Spine<ArcBatch<OrdKeyBatch<MzStack<((K, ()), T, R)>>>>;
52    pub type ColKeyBatcher<K, T, R> = KeyBatcher<K, T, R>;
53    pub type ColKeyBuilder<K, T, R> =
54        ArcBuilder<OrdKeyBuilder<MzStack<((K, ()), T, R)>, ColumnationStack<((K, ()), T, R)>>>;
55
56    /// A layout based on chunked timely stacks
57    pub struct MzStack<U: Update> {
58        phantom: std::marker::PhantomData<U>,
59    }
60
61    impl<U: Update> Layout for MzStack<U>
62    where
63        U::Key: Columnation + 'static,
64        U::Val: Columnation + 'static,
65        U::Time: Columnation,
66        U::Diff: Columnation,
67    {
68        type KeyContainer = ColumnationStack<U::Key>;
69        type ValContainer = ColumnationStack<U::Val>;
70        type TimeContainer = ColumnationStack<U::Time>;
71        type DiffContainer = ColumnationStack<U::Diff>;
72        type OffsetContainer = OffsetOptimized;
73    }
74}
75
76// Spines are data structures that collect and maintain updates.
77// Agents are wrappers around spines that allow shared read access.
78
79// Fully generic spines and agents.
80pub type KeyValSpine<K, V, T, R> = ColValSpine<K, V, T, R>;
81pub type KeyValAgent<K, V, T, R> = TraceAgent<KeyValSpine<K, V, T, R>>;
82pub type KeyValEnter<K, V, T, R, TEnter> =
83    TraceEnter<TraceFrontier<KeyValAgent<K, V, T, R>>, TEnter>;
84
85// Fully generic key-only spines and agents
86pub type KeySpine<K, T, R> = ColKeySpine<K, T, R>;
87pub type KeyAgent<K, T, R> = TraceAgent<KeySpine<K, T, R>>;
88pub type KeyEnter<K, T, R, TEnter> = TraceEnter<TraceFrontier<KeyAgent<K, T, R>>, TEnter>;
89
90// Row specialized spines and agents.
91pub type RowValAgent<V, T, R> = TraceAgent<RowValSpine<V, T, R>>;
92pub type RowValArrangement<'scope, T, V> = Arranged<'scope, RowValAgent<V, T, Diff>>;
93pub type RowValEnter<V, T, R, TEnter> = TraceEnter<TraceFrontier<RowValAgent<V, T, R>>, TEnter>;
94// Row specialized spines and agents.
95pub type RowRowAgent<T, R> = TraceAgent<RowRowSpine<T, R>>;
96pub type RowRowArrangement<'scope, T> = Arranged<'scope, RowRowAgent<T, Diff>>;
97pub type RowRowEnter<T, R, TEnter> = TraceEnter<TraceFrontier<RowRowAgent<T, R>>, TEnter>;
98// Row specialized spines and agents.
99pub type RowAgent<T, R> = TraceAgent<RowSpine<T, R>>;
100pub type RowArrangement<'scope, T> = Arranged<'scope, RowAgent<T, Diff>>;
101pub type RowEnter<T, R, TEnter> = TraceEnter<TraceFrontier<RowAgent<T, R>>, TEnter>;
102
103// Error specialized spines and agents.
104pub type ErrSpine<T, R> = ColKeySpine<DataflowErrorSer, T, R>;
105pub type ErrBatcher<T, R> = ColKeyBatcher<DataflowErrorSer, T, R>;
106pub type ErrBuilder<T, R> = ColKeyBuilder<DataflowErrorSer, T, R>;
107
108pub type ErrAgent<T, R> = TraceAgent<ErrSpine<T, R>>;
109pub type ErrEnter<T, TEnter> = TraceEnter<TraceFrontier<ErrAgent<T, Diff>>, TEnter>;
110
111pub type KeyErrSpine<K, T, R> = ColValSpine<K, DataflowErrorSer, T, R>;
112pub type KeyErrBatcher<K, T, R> = ColValBatcher<K, DataflowErrorSer, T, R>;
113pub type KeyErrBuilder<K, T, R> = ColValBuilder<K, DataflowErrorSer, T, R>;
114
115pub type RowErrSpine<T, R> = RowValSpine<DataflowErrorSer, T, R>;
116pub type RowErrBatcher<T, R> = RowValBatcher<DataflowErrorSer, T, R>;
117pub type RowErrBuilder<T, R> = RowValBuilder<DataflowErrorSer, T, R>;
118
119// Batchers for consolidation
120pub type KeyBatcher<K, T, D> = KeyValBatcher<K, (), T, D>;
121pub type KeyValBatcher<K, V, T, D> = MergeBatcher<ColInternalMerger<(K, V), T, D>>;
122
123/// Timestamp trait for rendering, constraint to support [`MzData`] and [timely::progress::Timestamp].
124pub trait MzTimestamp:
125    MzData + timely::progress::Timestamp + differential_dataflow::lattice::Lattice + std::hash::Hash
126{
127}
128
129impl<T> MzTimestamp for T
130where
131    T: MzData,
132    T: timely::progress::Timestamp,
133    T: differential_dataflow::lattice::Lattice + std::hash::Hash,
134{
135}
136
137/// Trait for data types that can be used in Materialize's dataflow, supporting both columnar and
138/// columnation.
139pub trait MzData:
140    columnation::Columnation
141    + for<'a> columnar::Columnar<Container: Container<Ref<'a>: Copy + Ord> + Clone + Send>
142{
143}
144
145impl<T> MzData for T
146where
147    T: columnation::Columnation,
148    T: for<'a> columnar::Columnar<Container: Clone + Send>,
149    for<'a> Ref<'a, T>: Copy + Ord,
150{
151}
152
153pub trait MzArrangeData: columnation::Columnation {}
154impl<T> MzArrangeData for T where T: columnation::Columnation {}