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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// 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.

//! A columnar representation of one blob's worth of data

use arrow2::array::{Array, PrimitiveArray};
use arrow2::buffer::Buffer;
use arrow2::chunk::Chunk;
use arrow2::datatypes::{DataType as ArrowLogicalType, Field};
use arrow2::io::parquet::write::Encoding;

use crate::columnar::sealed::{ColumnMut, ColumnRef};
use crate::columnar::{PartEncoder, Schema};
use crate::dyn_col::DynColumnRef;
use crate::dyn_struct::{ColumnsRef, DynStructCfg, DynStructCol, DynStructMut, ValidityRef};
use crate::stats::StructStats;
use crate::Codec64;

/// A structured columnar representation of one blob's worth of data.
#[derive(Debug)]
pub struct Part {
    len: usize,
    key: DynStructCol,
    val: DynStructCol,
    ts: Buffer<i64>,
    diff: Buffer<i64>,
}

impl Part {
    /// The number of updates contained.
    pub fn len(&self) -> usize {
        debug_assert_eq!(self.validate(), Ok(()));
        self.len
    }

    /// Returns a [ColumnsRef] for the key columns.
    pub fn key_ref(&self) -> ColumnsRef {
        self.key.as_ref()
    }

    /// Returns a [ColumnsRef] for the val columns.
    pub fn val_ref(&self) -> ColumnsRef {
        self.val.as_ref()
    }

    /// Computes a [StructStats] for the key columns.
    pub fn key_stats(&self) -> Result<StructStats, String> {
        let stats = self.key.stats(ValidityRef(None))?;
        Ok(stats.some)
    }

    pub(crate) fn to_arrow(&self) -> (Vec<Field>, Vec<Vec<Encoding>>, Chunk<Box<dyn Array>>) {
        let (mut fields, mut encodings, mut arrays) =
            (Vec::new(), Vec::new(), Vec::<Box<dyn Array>>::new());

        {
            // arrow2 doesn't allow empty struct arrays. To make a future schema
            // migration for <no columns> <-> <one optional column> easier, we
            // model this as a missing column (rather than something like
            // NullArray). This also matches how we'd do the same for nested
            // structs.
            if let Some((key_array, key_encodings)) = self.key.to_arrow_struct() {
                fields.push(Field::new("k", key_array.data_type().clone(), false));
                encodings.push(key_encodings);
                arrays.push(Box::new(key_array));
            }
        }

        {
            // arrow2 doesn't allow empty struct arrays. To make a future schema
            // migration for <no columns> <-> <one optional column> easier, we
            // model this as a missing column (rather than something like
            // NullArray). This also matches how we'd do the same for nested
            // structs.
            if let Some((val_array, val_encodings)) = self.val.to_arrow_struct() {
                fields.push(Field::new("v", val_array.data_type().clone(), false));
                encodings.push(val_encodings);
                arrays.push(Box::new(val_array));
            }
        }

        {
            let ts = PrimitiveArray::new(ArrowLogicalType::Int64, self.ts.clone(), None);
            fields.push(Field::new("t", ts.data_type().clone(), false));
            encodings.push(vec![Encoding::Plain]);
            arrays.push(Box::new(ts));
        }

        {
            let diff = PrimitiveArray::new(ArrowLogicalType::Int64, self.diff.clone(), None);
            fields.push(Field::new("d", diff.data_type().clone(), false));
            encodings.push(vec![Encoding::Plain]);
            arrays.push(Box::new(diff));
        }

        (fields, encodings, Chunk::new(arrays))
    }

    pub(crate) fn from_arrow<K, KS: Schema<K>, V, VS: Schema<V>>(
        key_schema: &KS,
        val_schema: &VS,
        chunk: Chunk<Box<dyn Array>>,
    ) -> Result<Self, String> {
        let key_schema = key_schema.columns();
        let val_schema = val_schema.columns();

        let len = chunk.len();
        let mut chunk = chunk.arrays().iter();
        let key = if key_schema.cols.is_empty() {
            None
        } else {
            Some(
                chunk
                    .next()
                    .ok_or_else(|| "missing key column".to_owned())?,
            )
        };
        let val = if val_schema.cols.is_empty() {
            None
        } else {
            Some(
                chunk
                    .next()
                    .ok_or_else(|| "missing val column".to_owned())?,
            )
        };
        let ts = chunk.next().ok_or_else(|| "missing ts column".to_owned())?;
        let diff = chunk
            .next()
            .ok_or_else(|| "missing diff column".to_owned())?;
        if let Some(_) = chunk.next() {
            return Err("too many columns".to_owned());
        }

        let key = match key {
            None => DynStructCol::empty(key_schema),
            Some(key) => DynStructCol::from_arrow(key_schema, key)?,
        };

        let val = match val {
            None => DynStructCol::empty(val_schema),
            Some(val) => DynStructCol::from_arrow(val_schema, val)?,
        };

        let diff = diff
            .as_any()
            .downcast_ref::<PrimitiveArray<i64>>()
            .ok_or_else(|| {
                format!(
                    "expected diff to be PrimitiveArray<i64> got {:?}",
                    diff.data_type()
                )
            })?;
        assert!(diff.validity().is_none());
        let diff = diff.values().clone();

        let ts = ts
            .as_any()
            .downcast_ref::<PrimitiveArray<i64>>()
            .ok_or_else(|| {
                format!(
                    "expected ts to be PrimitiveArray<i64> got {:?}",
                    ts.data_type()
                )
            })?;
        assert!(ts.validity().is_none());
        let ts = ts.values().clone();

        let part = Part {
            len,
            key,
            val,
            ts,
            diff,
        };
        let () = part.validate()?;
        Ok(part)
    }

    fn validate(&self) -> Result<(), String> {
        let () = self.key.validate()?;
        if !self.key.cols.is_empty() && self.len != self.key.len() {
            return Err(format!(
                "key len {} didn't match part len {}",
                self.key.len(),
                self.len
            ));
        }
        let () = self.val.validate()?;
        if !self.val.cols.is_empty() && self.len != self.val.len() {
            return Err(format!(
                "val len {} didn't match part len {}",
                self.val.len(),
                self.len
            ));
        }
        if self.len != self.ts.len() {
            return Err(format!(
                "ts col len {} didn't match part len {}",
                self.ts.len(),
                self.len
            ));
        }
        if self.len != self.diff.len() {
            return Err(format!(
                "diff col len {} didn't match part len {}",
                self.diff.len(),
                self.len
            ));
        }
        // TODO: Also validate the col types match schema.
        Ok(())
    }
}

/// An in-progress columnar constructor for one blob's worth of data.
#[derive(Debug)]
pub struct PartBuilder<K, KS: Schema<K>, V, VS: Schema<V>> {
    /// Configuration for the `key` column.
    key_cfg: DynStructCfg,
    /// Encoder for the `key` column.
    key_encoder: KS::Encoder,
    /// Configuration for the `val` column.
    val_cfg: DynStructCfg,
    /// Encoder for the val column.
    val_encoder: VS::Encoder,

    /// The ts column.
    ts: Codec64Mut,
    /// The diff column.
    diff: Codec64Mut,
}

impl<K, KS: Schema<K>, V, VS: Schema<V>> PartBuilder<K, KS, V, VS> {
    /// Returns a new [`PartBuilder`] that can be used to build a [`Part`].
    pub fn new(key_schema: &KS, val_schema: &VS) -> Result<Self, String> {
        let key = DynStructMut::new(&key_schema.columns());
        let key_cfg = key.cfg().clone();
        let key_encoder = key_schema.encoder(key.as_mut())?;

        let val = DynStructMut::new(&val_schema.columns());
        let val_cfg = val.cfg().clone();
        let val_encoder = val_schema.encoder(val.as_mut())?;

        let ts = Codec64Mut(Vec::new());
        let diff = Codec64Mut(Vec::new());

        let builder = PartBuilder {
            key_cfg,
            key_encoder,
            val_cfg,
            val_encoder,
            ts,
            diff,
        };

        Ok(builder)
    }

    /// Push a new row onto this [`PartBuilder`].
    pub fn push<T: Codec64, D: Codec64>(&mut self, k: &K, v: &V, t: T, d: D) {
        self.key_encoder.encode(k);
        self.val_encoder.encode(v);
        self.ts.push(t);
        self.diff.push(d);
    }

    /// Consumes self returning a [`Part`].
    pub fn finish(self) -> Part {
        let Self {
            key_cfg,
            key_encoder,
            val_cfg,
            val_encoder,
            ts,
            diff,
        } = self;

        let (key_len, key_cols) = key_encoder.finish();
        let (val_len, val_cols) = val_encoder.finish();

        assert!(key_len == val_len);
        assert!(key_len == ts.len());
        assert!(key_len == diff.len());

        let key = DynStructCol {
            len: key_len,
            cfg: key_cfg,
            validity: None,
            cols: key_cols.into_iter().map(DynColumnRef::from).collect(),
        };

        let val = DynStructCol {
            len: val_len,
            cfg: val_cfg,
            validity: None,
            cols: val_cols.into_iter().map(DynColumnRef::from).collect(),
        };

        Part {
            len: key_len,
            key,
            val,
            ts: Buffer::from(ts.0),
            diff: Buffer::from(diff.0),
        }
    }
}

/// Mutable access to a column of a [`Codec64`] implementor.
#[derive(Debug)]
pub struct Codec64Mut(Vec<i64>);

impl Codec64Mut {
    /// Returns the length of the column.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Pushes the given value into this column.
    pub fn push<X: Codec64>(&mut self, val: X) {
        self.0.push(i64::from_le_bytes(Codec64::encode(&val)));
    }
}

#[cfg(test)]
mod tests {
    use std::marker::PhantomData;

    use super::*;
    use crate::codec_impls::UnitSchema;

    // Make sure that the API structs are Sync + Send, so that they can be used in async tasks.
    // NOTE: This is a compile-time only test. If it compiles, we're good.
    #[allow(unused)]
    fn sync_send() {
        fn is_send_sync<T: Send + Sync>(_: PhantomData<T>) -> bool {
            true
        }

        assert!(is_send_sync::<Part>(PhantomData));
        assert!(is_send_sync::<PartBuilder<(), UnitSchema, (), UnitSchema>>(
            PhantomData
        ));
    }
}