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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// 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.

//! Implementations of [Codec] for stdlib types.

use std::collections::BTreeMap;
use std::fmt::Debug;
use std::marker::PhantomData;

use arrow::array::{
    Array, ArrayBuilder, BinaryArray, BinaryBuilder, NullArray, StringArray, StringBuilder,
    StructArray,
};
use bytes::{BufMut, Bytes};
use timely::order::Product;

use crate::columnar::{ColumnDecoder, ColumnEncoder, Schema2};
use crate::stats::{ColumnStatKinds, ColumnarStats, NoneStats, StructStats};
use crate::{Codec, Codec64, Opaque, ShardId};

/// An implementation of [Schema2] for [()].
#[derive(Debug, Default, PartialEq)]
pub struct UnitSchema;

impl Codec for () {
    type Storage = ();
    type Schema = UnitSchema;

    fn codec_name() -> String {
        "()".into()
    }

    fn encode<B>(&self, _buf: &mut B)
    where
        B: BufMut,
    {
        // No-op.
    }

    fn decode<'a>(buf: &'a [u8], _schema: &UnitSchema) -> Result<Self, String> {
        if !buf.is_empty() {
            return Err(format!("decode expected empty buf got {} bytes", buf.len()));
        }
        Ok(())
    }

    fn encode_schema(_schema: &Self::Schema) -> Bytes {
        Bytes::new()
    }

    fn decode_schema(buf: &Bytes) -> Self::Schema {
        assert_eq!(*buf, Bytes::new());
        UnitSchema
    }
}

/// An encoder and decoder for [`UnitSchema`].
#[derive(Debug)]
pub struct UnitColumnar {
    /// Number of entries in this column.
    len: usize,
}

impl UnitColumnar {
    /// Returns a new [`UnitColumnar`] with the number of entries specified.
    pub fn new(len: usize) -> Self {
        UnitColumnar { len }
    }
}

impl ColumnDecoder<()> for UnitColumnar {
    fn decode(&self, idx: usize, _val: &mut ()) {
        if idx >= self.len {
            panic!("index out of bounds, idx: {idx}, len: {}", self.len);
        }
    }

    fn is_null(&self, idx: usize) -> bool {
        if idx < self.len {
            true
        } else {
            panic!("index out of bounds, idx: {idx}, len: {}", self.len);
        }
    }

    fn stats(&self) -> StructStats {
        StructStats {
            len: self.len,
            cols: BTreeMap::new(),
        }
    }
}

impl ColumnEncoder<()> for UnitColumnar {
    type FinishedColumn = NullArray;

    fn goodbytes(&self) -> usize {
        0
    }

    fn append(&mut self, _val: &()) {
        self.len += 1;
    }

    fn append_null(&mut self) {
        self.len += 1;
    }

    fn finish(self) -> Self::FinishedColumn {
        NullArray::new(self.len)
    }
}

impl Schema2<()> for UnitSchema {
    type ArrowColumn = NullArray;
    type Statistics = NoneStats;

    type Decoder = UnitColumnar;
    type Encoder = UnitColumnar;

    fn decoder(&self, col: Self::ArrowColumn) -> Result<Self::Decoder, anyhow::Error> {
        Ok(UnitColumnar::new(col.len()))
    }

    fn encoder(&self) -> Result<Self::Encoder, anyhow::Error> {
        Ok(UnitColumnar::new(0))
    }
}

/// Simple type of data that can be columnar encoded.
pub trait SimpleColumnarData {
    /// Type of [`arrow`] builder that we encode data into.
    type ArrowBuilder: arrow::array::ArrayBuilder + Default;
    /// Type of [`arrow`] array the we decode data from.
    type ArrowColumn: arrow::array::Array + Clone + 'static;

    /// The number of actual data bytes this item represents.
    fn goodbytes(builder: &Self::ArrowBuilder) -> usize;

    /// Encode `self` into `builder`.
    fn push(&self, builder: &mut Self::ArrowBuilder);
    /// Encode a null value into `builder`.
    fn push_null(builder: &mut Self::ArrowBuilder);

    /// Decode an instance of `self` from `column`.
    fn read(&mut self, idx: usize, column: &Self::ArrowColumn);
}

impl SimpleColumnarData for String {
    type ArrowBuilder = StringBuilder;
    type ArrowColumn = StringArray;

    fn goodbytes(builder: &Self::ArrowBuilder) -> usize {
        builder.values_slice().len()
    }

    fn push(&self, builder: &mut Self::ArrowBuilder) {
        builder.append_value(self.as_str())
    }
    fn push_null(builder: &mut Self::ArrowBuilder) {
        builder.append_null()
    }
    fn read(&mut self, idx: usize, column: &Self::ArrowColumn) {
        self.clear();
        self.push_str(column.value(idx));
    }
}

impl SimpleColumnarData for Vec<u8> {
    type ArrowBuilder = BinaryBuilder;
    type ArrowColumn = BinaryArray;

    fn goodbytes(builder: &Self::ArrowBuilder) -> usize {
        builder.values_slice().len()
    }

    fn push(&self, builder: &mut Self::ArrowBuilder) {
        builder.append_value(self.as_slice())
    }
    fn push_null(builder: &mut Self::ArrowBuilder) {
        builder.append_null()
    }
    fn read(&mut self, idx: usize, column: &Self::ArrowColumn) {
        self.clear();
        self.extend(column.value(idx));
    }
}

impl SimpleColumnarData for ShardId {
    type ArrowBuilder = StringBuilder;
    type ArrowColumn = StringArray;

    fn goodbytes(builder: &Self::ArrowBuilder) -> usize {
        builder.values_slice().len()
    }

    fn push(&self, builder: &mut Self::ArrowBuilder) {
        builder.append_value(&self.to_string());
    }
    fn push_null(builder: &mut Self::ArrowBuilder) {
        builder.append_null();
    }
    fn read(&mut self, idx: usize, column: &Self::ArrowColumn) {
        *self = column.value(idx).parse().expect("should be valid ShardId");
    }
}

/// A type that implements [`ColumnEncoder`] for [`SimpleColumnarData`].
#[derive(Debug, Default)]
pub struct SimpleColumnarEncoder<T: SimpleColumnarData>(T::ArrowBuilder);

impl<T: SimpleColumnarData> ColumnEncoder<T> for SimpleColumnarEncoder<T> {
    type FinishedColumn = T::ArrowColumn;

    fn goodbytes(&self) -> usize {
        T::goodbytes(&self.0)
    }

    fn append(&mut self, val: &T) {
        T::push(val, &mut self.0);
    }
    fn append_null(&mut self) {
        T::push_null(&mut self.0)
    }
    fn finish(mut self) -> Self::FinishedColumn {
        let array = ArrayBuilder::finish(&mut self.0);
        let array = array
            .as_any()
            .downcast_ref::<T::ArrowColumn>()
            .expect("created using StringBuilder")
            .clone();

        array
    }
}

/// A type that implements [`ColumnDecoder`] for [`SimpleColumnarData`].
#[derive(Debug)]
pub struct SimpleColumnarDecoder<T: SimpleColumnarData>(T::ArrowColumn);

impl<T: SimpleColumnarData> SimpleColumnarDecoder<T> {
    /// Returns a new [`SimpleColumnarDecoder`] with the provided column.
    pub fn new(col: T::ArrowColumn) -> Self {
        SimpleColumnarDecoder(col)
    }
}

impl<T: SimpleColumnarData> ColumnDecoder<T> for SimpleColumnarDecoder<T> {
    fn decode(&self, idx: usize, val: &mut T) {
        T::read(val, idx, &self.0)
    }
    fn is_null(&self, idx: usize) -> bool {
        self.0.is_null(idx)
    }

    fn stats(&self) -> StructStats {
        ColumnarStats::one_column_struct(self.0.len(), ColumnStatKinds::None)
    }
}

/// An implementation of [Schema2] for [String].
#[derive(Debug, Clone, Default, PartialEq)]
pub struct StringSchema;

impl Schema2<String> for StringSchema {
    type ArrowColumn = StringArray;
    type Statistics = NoneStats;

    type Decoder = SimpleColumnarDecoder<String>;
    type Encoder = SimpleColumnarEncoder<String>;

    fn encoder(&self) -> Result<Self::Encoder, anyhow::Error> {
        Ok(SimpleColumnarEncoder::default())
    }

    fn decoder(&self, col: Self::ArrowColumn) -> Result<Self::Decoder, anyhow::Error> {
        Ok(SimpleColumnarDecoder::new(col))
    }
}

impl Codec for String {
    type Storage = ();
    type Schema = StringSchema;

    fn codec_name() -> String {
        "String".into()
    }

    fn encode<B>(&self, buf: &mut B)
    where
        B: BufMut,
    {
        buf.put(self.as_bytes())
    }

    fn decode<'a>(buf: &'a [u8], _schema: &StringSchema) -> Result<Self, String> {
        String::from_utf8(buf.to_owned()).map_err(|err| err.to_string())
    }

    fn encode_schema(_schema: &Self::Schema) -> Bytes {
        Bytes::new()
    }

    fn decode_schema(buf: &Bytes) -> Self::Schema {
        assert_eq!(*buf, Bytes::new());
        StringSchema
    }
}

/// An implementation of [Schema2] for [`Vec<u8>`].
#[derive(Debug, Clone, Default, PartialEq)]
pub struct VecU8Schema;

impl Schema2<Vec<u8>> for VecU8Schema {
    type ArrowColumn = BinaryArray;
    type Statistics = NoneStats;

    type Decoder = SimpleColumnarDecoder<Vec<u8>>;
    type Encoder = SimpleColumnarEncoder<Vec<u8>>;

    fn encoder(&self) -> Result<Self::Encoder, anyhow::Error> {
        Ok(SimpleColumnarEncoder::default())
    }

    fn decoder(&self, col: Self::ArrowColumn) -> Result<Self::Decoder, anyhow::Error> {
        Ok(SimpleColumnarDecoder::new(col))
    }
}

impl Codec for Vec<u8> {
    type Storage = ();
    type Schema = VecU8Schema;

    fn codec_name() -> String {
        "Vec<u8>".into()
    }

    fn encode<B>(&self, buf: &mut B)
    where
        B: BufMut,
    {
        buf.put(self.as_slice())
    }

    fn decode<'a>(buf: &'a [u8], _schema: &VecU8Schema) -> Result<Self, String> {
        Ok(buf.to_owned())
    }

    fn encode_schema(_schema: &Self::Schema) -> Bytes {
        Bytes::new()
    }

    fn decode_schema(buf: &Bytes) -> Self::Schema {
        assert_eq!(*buf, Bytes::new());
        VecU8Schema
    }
}

impl Codec for ShardId {
    type Storage = ();
    type Schema = ShardIdSchema;
    fn codec_name() -> String {
        "ShardId".into()
    }
    fn encode<B: BufMut>(&self, buf: &mut B) {
        buf.put(self.to_string().as_bytes())
    }
    fn decode<'a>(buf: &'a [u8], _schema: &ShardIdSchema) -> Result<Self, String> {
        let shard_id = String::from_utf8(buf.to_owned()).map_err(|err| err.to_string())?;
        shard_id.parse()
    }
    fn encode_schema(_schema: &Self::Schema) -> Bytes {
        Bytes::new()
    }
    fn decode_schema(buf: &Bytes) -> Self::Schema {
        assert_eq!(*buf, Bytes::new());
        ShardIdSchema
    }
}

/// An implementation of [Schema2] for [ShardId].
#[derive(Debug, PartialEq)]
pub struct ShardIdSchema;

impl Schema2<ShardId> for ShardIdSchema {
    type ArrowColumn = StringArray;
    type Statistics = NoneStats;

    type Decoder = SimpleColumnarDecoder<ShardId>;
    type Encoder = SimpleColumnarEncoder<ShardId>;

    fn encoder(&self) -> Result<Self::Encoder, anyhow::Error> {
        Ok(SimpleColumnarEncoder::default())
    }

    fn decoder(&self, col: Self::ArrowColumn) -> Result<Self::Decoder, anyhow::Error> {
        Ok(SimpleColumnarDecoder::new(col))
    }
}

impl Codec64 for i64 {
    fn codec_name() -> String {
        "i64".to_owned()
    }

    fn encode(&self) -> [u8; 8] {
        self.to_le_bytes()
    }

    fn decode(buf: [u8; 8]) -> Self {
        i64::from_le_bytes(buf)
    }
}

impl Codec64 for u64 {
    fn codec_name() -> String {
        "u64".to_owned()
    }

    fn encode(&self) -> [u8; 8] {
        self.to_le_bytes()
    }

    fn decode(buf: [u8; 8]) -> Self {
        u64::from_le_bytes(buf)
    }
}

impl Opaque for u64 {
    fn initial() -> Self {
        u64::MIN
    }
}

impl Codec64 for Product<u32, u32> {
    fn codec_name() -> String {
        "Product<u32, u32>".to_owned()
    }

    fn encode(&self) -> [u8; 8] {
        let o = self.outer.to_le_bytes();
        let i = self.inner.to_le_bytes();
        [o[0], o[1], o[2], o[3], i[0], i[1], i[2], i[3]]
    }

    fn decode(buf: [u8; 8]) -> Self {
        let outer = [buf[0], buf[1], buf[2], buf[3]];
        let inner = [buf[4], buf[5], buf[6], buf[7]];
        Product::new(u32::from_le_bytes(outer), u32::from_le_bytes(inner))
    }
}

// TODO: Remove this once we wrap coord epochs in an `Epoch` struct and impl
// Opaque on `Epoch` instead.
impl Opaque for i64 {
    fn initial() -> Self {
        i64::MIN
    }
}

/// A placeholder for a [Codec] impl that hasn't yet gotten a real [Schema2].
#[derive(Debug)]
pub struct TodoSchema<T>(PhantomData<T>);

impl<T> Default for TodoSchema<T> {
    fn default() -> Self {
        Self(Default::default())
    }
}

impl<T> PartialEq for TodoSchema<T> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl<T: Debug + Send + Sync> Schema2<T> for TodoSchema<T> {
    type ArrowColumn = StructArray;
    type Statistics = NoneStats;

    type Decoder = TodoColumnarDecoder<T>;
    type Encoder = TodoColumnarEncoder<T>;

    fn decoder(&self, _col: Self::ArrowColumn) -> Result<Self::Decoder, anyhow::Error> {
        panic!("TODO")
    }

    fn encoder(&self) -> Result<Self::Encoder, anyhow::Error> {
        panic!("TODO")
    }
}

/// A [`ColumnEncoder`] that has no implementation.
#[derive(Debug)]
pub struct TodoColumnarEncoder<T>(PhantomData<T>);

impl<T> ColumnEncoder<T> for TodoColumnarEncoder<T> {
    type FinishedColumn = StructArray;

    fn goodbytes(&self) -> usize {
        panic!("TODO")
    }

    fn append(&mut self, _val: &T) {
        panic!("TODO")
    }

    fn append_null(&mut self) {
        panic!("TODO")
    }

    fn finish(self) -> Self::FinishedColumn {
        panic!("TODO")
    }
}

/// A [`ColumnDecoder`] that has no implementation.
#[derive(Debug)]
pub struct TodoColumnarDecoder<T>(PhantomData<T>);

impl<T> ColumnDecoder<T> for TodoColumnarDecoder<T> {
    fn decode(&self, _idx: usize, _val: &mut T) {
        panic!("TODO")
    }

    fn is_null(&self, _idx: usize) -> bool {
        panic!("TODO")
    }

    fn stats(&self) -> StructStats {
        panic!("TODO")
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use serde::{Deserialize, Serialize};
    use serde_json::json;

    use super::*;

    #[mz_ore::test]
    fn fmt_ids() {
        assert_eq!(
            format!("{}", ShardId([0u8; 16])),
            "s00000000-0000-0000-0000-000000000000"
        );
        assert_eq!(
            format!("{:?}", ShardId([0u8; 16])),
            "ShardId(00000000-0000-0000-0000-000000000000)"
        );

        // ShardId can be parsed back from its Display/to_string format.
        assert_eq!(
            ShardId::from_str("s00000000-0000-0000-0000-000000000000"),
            Ok(ShardId([0u8; 16]))
        );
        assert_eq!(
            ShardId::from_str("x00000000-0000-0000-0000-000000000000"),
            Err(
                "invalid ShardId x00000000-0000-0000-0000-000000000000: incorrect prefix"
                    .to_string()
            )
        );
        assert_eq!(
            ShardId::from_str("s0"),
            Err(
                "invalid ShardId s0: invalid length: expected length 32 for simple format, found 1"
                    .to_string()
            )
        );
        assert_eq!(
            ShardId::from_str("s00000000-0000-0000-0000-000000000000FOO"),
            Err("invalid ShardId s00000000-0000-0000-0000-000000000000FOO: invalid character: expected an optional prefix of `urn:uuid:` followed by [0-9a-fA-F-], found `O` at 38".to_string())
        );
    }

    #[mz_ore::test]
    fn shard_id_human_readable_serde() {
        #[derive(Debug, Serialize, Deserialize)]
        struct ShardIdContainer {
            shard_id: ShardId,
        }

        // roundtrip id through json
        let id =
            ShardId::from_str("s00000000-1234-5678-0000-000000000000").expect("valid shard id");
        assert_eq!(
            id,
            serde_json::from_value(serde_json::to_value(id).expect("serializable"))
                .expect("deserializable")
        );

        // deserialize a serialized string directly
        assert_eq!(
            id,
            serde_json::from_str("\"s00000000-1234-5678-0000-000000000000\"")
                .expect("deserializable")
        );

        // roundtrip shard id through a container type
        let json = json!({ "shard_id": id });
        assert_eq!(
            "{\"shard_id\":\"s00000000-1234-5678-0000-000000000000\"}",
            &json.to_string()
        );
        let container: ShardIdContainer = serde_json::from_value(json).expect("deserializable");
        assert_eq!(container.shard_id, id);
    }
}