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