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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
// 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 [protobuf] representation of [Apache Arrow] arrays.
//!
//! # Motivation
//!
//! Persist can store a small amount of data inline at the consensus layer.
//! Because we are space constrained, we take particular care to store only the
//! data that is necessary. Other Arrow serialization formats, e.g. [Parquet]
//! or [Arrow IPC], include data that we don't need and would be wasteful to
//! store.
//!
//! [protobuf]: https://protobuf.dev/
//! [Apache Arrow]: https://arrow.apache.org/
//! [Parquet]: https://parquet.apache.org/docs/
//! [Arrow IPC]: https://arrow.apache.org/docs/format/Columnar.html#serialization-and-interprocess-communication-ipc

use std::cmp::Ordering;
use std::sync::Arc;

use arrow::array::*;
use arrow::buffer::{BooleanBuffer, NullBuffer, OffsetBuffer};
use arrow::datatypes::{ArrowNativeType, DataType, Field, FieldRef, Fields};
use itertools::Itertools;
use mz_ore::cast::CastFrom;
use mz_ore::soft_assert_eq_no_log;
use mz_proto::{IntoRustIfSome, ProtoType, RustType, TryFromProtoError};
use prost::Message;

#[allow(missing_docs)]
mod proto {
    include!(concat!(env!("OUT_DIR"), "/mz_persist_types.arrow.rs"));
}
use crate::arrow::proto::data_type;
pub use proto::ProtoArrayData;

/// Extract the list of fields for our recursive datatypes.
pub fn fields_for_type(data_type: &DataType) -> &[FieldRef] {
    match data_type {
        DataType::Struct(fields) => fields,
        DataType::List(field) => std::slice::from_ref(field),
        DataType::Map(field, _) => std::slice::from_ref(field),
        DataType::Null
        | DataType::Boolean
        | DataType::Int8
        | DataType::Int16
        | DataType::Int32
        | DataType::Int64
        | DataType::UInt8
        | DataType::UInt16
        | DataType::UInt32
        | DataType::UInt64
        | DataType::Float16
        | DataType::Float32
        | DataType::Float64
        | DataType::Timestamp(_, _)
        | DataType::Date32
        | DataType::Date64
        | DataType::Time32(_)
        | DataType::Time64(_)
        | DataType::Duration(_)
        | DataType::Interval(_)
        | DataType::Binary
        | DataType::FixedSizeBinary(_)
        | DataType::LargeBinary
        | DataType::BinaryView
        | DataType::Utf8
        | DataType::LargeUtf8
        | DataType::Utf8View
        | DataType::Decimal128(_, _)
        | DataType::Decimal256(_, _) => &[],
        DataType::ListView(_)
        | DataType::FixedSizeList(_, _)
        | DataType::LargeList(_)
        | DataType::LargeListView(_)
        | DataType::Union(_, _)
        | DataType::Dictionary(_, _)
        | DataType::RunEndEncoded(_, _) => unimplemented!("not supported"),
    }
}

/// Encode the array into proto. If an expected data type is passed, that implies it is
/// encoded at some higher level, and we omit it from the data.
fn into_proto_with_type(data: &ArrayData, expected_type: Option<&DataType>) -> ProtoArrayData {
    let data_type = match expected_type {
        Some(expected) => {
            // Equality is recursive, and this function is itself called recursively,
            // skip the call in production to avoid a quadratic overhead.
            soft_assert_eq_no_log!(
                expected,
                data.data_type(),
                "actual type should match expected type"
            );
            None
        }
        None => Some(data.data_type().into_proto()),
    };

    ProtoArrayData {
        data_type,
        length: u64::cast_from(data.len()),
        offset: u64::cast_from(data.offset()),
        buffers: data.buffers().iter().map(|b| b.into_proto()).collect(),
        children: data
            .child_data()
            .iter()
            .zip_eq(fields_for_type(expected_type.unwrap_or(data.data_type())))
            .map(|(child, expect)| into_proto_with_type(child, Some(expect.data_type())))
            .collect(),
        nulls: data.nulls().map(|n| n.inner().into_proto()),
    }
}

/// Decode the array data.
/// If the data type is omitted from the proto, we decode it as the expected type.
fn from_proto_with_type(
    proto: ProtoArrayData,
    expected_type: Option<&DataType>,
) -> Result<ArrayData, TryFromProtoError> {
    let ProtoArrayData {
        data_type,
        length,
        offset,
        buffers,
        children,
        nulls,
    } = proto;
    let data_type: Option<DataType> = data_type.into_rust()?;
    let data_type = match (data_type, expected_type) {
        (Some(data_type), None) => data_type,
        (Some(data_type), Some(expected_type)) => {
            // Equality is recursive, and this function is itself called recursively,
            // skip the call in production to avoid a quadratic overhead.
            soft_assert_eq_no_log!(
                data_type,
                *expected_type,
                "expected type should match actual type"
            );
            data_type
        }
        (None, Some(expected_type)) => expected_type.clone(),
        (None, None) => {
            return Err(TryFromProtoError::MissingField(
                "ProtoArrayData::data_type".to_string(),
            ))
        }
    };
    let nulls = nulls
        .map(|n| n.into_rust())
        .transpose()?
        .map(NullBuffer::new);

    let mut builder = ArrayDataBuilder::new(data_type.clone())
        .len(usize::cast_from(length))
        .offset(usize::cast_from(offset))
        .nulls(nulls);

    for b in buffers.into_iter().map(|b| b.into_rust()) {
        builder = builder.add_buffer(b?);
    }
    for c in children
        .into_iter()
        .zip_eq(fields_for_type(&data_type))
        .map(|(c, field)| from_proto_with_type(c, Some(field.data_type())))
    {
        builder = builder.add_child_data(c?);
    }

    // Construct the builder which validates all inputs and aligns data.
    builder
        .build_aligned()
        .map_err(|e| TryFromProtoError::RowConversionError(e.to_string()))
}

impl RustType<ProtoArrayData> for arrow::array::ArrayData {
    fn into_proto(&self) -> ProtoArrayData {
        into_proto_with_type(self, None)
    }

    fn from_proto(proto: ProtoArrayData) -> Result<Self, TryFromProtoError> {
        from_proto_with_type(proto, None)
    }
}

impl RustType<proto::DataType> for arrow::datatypes::DataType {
    fn into_proto(&self) -> proto::DataType {
        let kind = match self {
            DataType::Null => proto::data_type::Kind::Null(()),
            DataType::Boolean => proto::data_type::Kind::Boolean(()),
            DataType::UInt8 => proto::data_type::Kind::Uint8(()),
            DataType::UInt16 => proto::data_type::Kind::Uint16(()),
            DataType::UInt32 => proto::data_type::Kind::Uint32(()),
            DataType::UInt64 => proto::data_type::Kind::Uint64(()),
            DataType::Int8 => proto::data_type::Kind::Int8(()),
            DataType::Int16 => proto::data_type::Kind::Int16(()),
            DataType::Int32 => proto::data_type::Kind::Int32(()),
            DataType::Int64 => proto::data_type::Kind::Int64(()),
            DataType::Float32 => proto::data_type::Kind::Float32(()),
            DataType::Float64 => proto::data_type::Kind::Float64(()),
            DataType::Utf8 => proto::data_type::Kind::String(()),
            DataType::Binary => proto::data_type::Kind::Binary(()),
            DataType::FixedSizeBinary(size) => proto::data_type::Kind::FixedBinary(*size),
            DataType::List(inner) => proto::data_type::Kind::List(Box::new(inner.into_proto())),
            DataType::Map(inner, sorted) => {
                let map = proto::data_type::Map {
                    value: Some(Box::new(inner.into_proto())),
                    sorted: *sorted,
                };
                proto::data_type::Kind::Map(Box::new(map))
            }
            DataType::Struct(children) => {
                let children = children.into_iter().map(|f| f.into_proto()).collect();
                proto::data_type::Kind::Struct(proto::data_type::Struct { children })
            }
            other => unimplemented!("unsupported data type {other:?}"),
        };

        proto::DataType { kind: Some(kind) }
    }

    fn from_proto(proto: proto::DataType) -> Result<Self, TryFromProtoError> {
        let data_type = proto
            .kind
            .ok_or_else(|| TryFromProtoError::missing_field("kind"))?;
        let data_type = match data_type {
            proto::data_type::Kind::Null(()) => DataType::Null,
            proto::data_type::Kind::Boolean(()) => DataType::Boolean,
            proto::data_type::Kind::Uint8(()) => DataType::UInt8,
            proto::data_type::Kind::Uint16(()) => DataType::UInt16,
            proto::data_type::Kind::Uint32(()) => DataType::UInt32,
            proto::data_type::Kind::Uint64(()) => DataType::UInt64,
            proto::data_type::Kind::Int8(()) => DataType::Int8,
            proto::data_type::Kind::Int16(()) => DataType::Int16,
            proto::data_type::Kind::Int32(()) => DataType::Int32,
            proto::data_type::Kind::Int64(()) => DataType::Int64,
            proto::data_type::Kind::Float32(()) => DataType::Float32,
            proto::data_type::Kind::Float64(()) => DataType::Float64,
            proto::data_type::Kind::String(()) => DataType::Utf8,
            proto::data_type::Kind::Binary(()) => DataType::Binary,
            proto::data_type::Kind::FixedBinary(size) => DataType::FixedSizeBinary(size),
            proto::data_type::Kind::List(inner) => DataType::List(Arc::new((*inner).into_rust()?)),
            proto::data_type::Kind::Map(inner) => {
                let value = inner
                    .value
                    .ok_or_else(|| TryFromProtoError::missing_field("map.value"))?;
                DataType::Map(Arc::new((*value).into_rust()?), inner.sorted)
            }
            proto::data_type::Kind::Struct(inner) => {
                let children: Vec<Field> = inner
                    .children
                    .into_iter()
                    .map(|c| c.into_rust())
                    .collect::<Result<_, _>>()?;
                DataType::Struct(Fields::from(children))
            }
        };

        Ok(data_type)
    }
}

impl RustType<proto::Field> for arrow::datatypes::Field {
    fn into_proto(&self) -> proto::Field {
        proto::Field {
            name: self.name().clone(),
            nullable: self.is_nullable(),
            data_type: Some(Box::new(self.data_type().into_proto())),
        }
    }

    fn from_proto(proto: proto::Field) -> Result<Self, TryFromProtoError> {
        let proto::Field {
            name,
            nullable,
            data_type,
        } = proto;
        let data_type =
            data_type.ok_or_else(|| TryFromProtoError::missing_field("field.data_type"))?;
        let data_type = (*data_type).into_rust()?;

        Ok(Field::new(name, data_type, nullable))
    }
}

impl RustType<proto::Buffer> for arrow::buffer::Buffer {
    fn into_proto(&self) -> proto::Buffer {
        // TODO(parkmycar): There is probably something better we can do here.
        proto::Buffer {
            data: bytes::Bytes::copy_from_slice(self.as_slice()),
        }
    }

    fn from_proto(proto: proto::Buffer) -> Result<Self, TryFromProtoError> {
        Ok(arrow::buffer::Buffer::from_bytes(proto.data.into()))
    }
}

impl RustType<proto::BooleanBuffer> for arrow::buffer::BooleanBuffer {
    fn into_proto(&self) -> proto::BooleanBuffer {
        proto::BooleanBuffer {
            buffer: Some(self.sliced().into_proto()),
            length: u64::cast_from(self.len()),
        }
    }

    fn from_proto(proto: proto::BooleanBuffer) -> Result<Self, TryFromProtoError> {
        let proto::BooleanBuffer { buffer, length } = proto;
        let buffer = buffer.into_rust_if_some("buffer")?;
        Ok(BooleanBuffer::new(buffer, 0, usize::cast_from(length)))
    }
}

/// Wraps a single arrow array, downcasted to a specific type.
#[derive(Clone, Debug)]
pub enum ArrayOrd {
    /// Wraps a `NullArray`.
    Null(NullArray),
    /// Wraps a `Bool` array.
    Bool(BooleanArray),
    /// Wraps a `Int8` array.
    Int8(Int8Array),
    /// Wraps a `Int16` array.
    Int16(Int16Array),
    /// Wraps a `Int32` array.
    Int32(Int32Array),
    /// Wraps a `Int64` array.
    Int64(Int64Array),
    /// Wraps a `UInt8` array.
    UInt8(UInt8Array),
    /// Wraps a `UInt16` array.
    UInt16(UInt16Array),
    /// Wraps a `UInt32` array.
    UInt32(UInt32Array),
    /// Wraps a `UInt64` array.
    UInt64(UInt64Array),
    /// Wraps a `Float32` array.
    Float32(Float32Array),
    /// Wraps a `Float64` array.
    Float64(Float64Array),
    /// Wraps a `String` array.
    String(StringArray),
    /// Wraps a `Binary` array.
    Binary(BinaryArray),
    /// Wraps a `FixedSizeBinary` array.
    FixedSizeBinary(FixedSizeBinaryArray),
    /// Wraps a `List` array.
    List(Option<NullBuffer>, OffsetBuffer<i32>, Box<ArrayOrd>),
    /// Wraps a `Struct` array.
    Struct(Option<NullBuffer>, Vec<ArrayOrd>),
}

impl ArrayOrd {
    /// Downcast the provided array to a specific type in our enum.
    pub fn new(array: &dyn Array) -> Self {
        match array.data_type() {
            DataType::Null => ArrayOrd::Null(NullArray::from(array.to_data())),
            DataType::Boolean => ArrayOrd::Bool(array.as_boolean().clone()),
            DataType::Int8 => ArrayOrd::Int8(array.as_primitive().clone()),
            DataType::Int16 => ArrayOrd::Int16(array.as_primitive().clone()),
            DataType::Int32 => ArrayOrd::Int32(array.as_primitive().clone()),
            DataType::Int64 => ArrayOrd::Int64(array.as_primitive().clone()),
            DataType::UInt8 => ArrayOrd::UInt8(array.as_primitive().clone()),
            DataType::UInt16 => ArrayOrd::UInt16(array.as_primitive().clone()),
            DataType::UInt32 => ArrayOrd::UInt32(array.as_primitive().clone()),
            DataType::UInt64 => ArrayOrd::UInt64(array.as_primitive().clone()),
            DataType::Float32 => ArrayOrd::Float32(array.as_primitive().clone()),
            DataType::Float64 => ArrayOrd::Float64(array.as_primitive().clone()),
            DataType::Binary => ArrayOrd::Binary(array.as_binary().clone()),
            DataType::Utf8 => ArrayOrd::String(array.as_string().clone()),
            DataType::FixedSizeBinary(_) => {
                ArrayOrd::FixedSizeBinary(array.as_fixed_size_binary().clone())
            }
            DataType::List(_) => {
                let list_array = array.as_list();
                ArrayOrd::List(
                    list_array.nulls().cloned(),
                    list_array.offsets().clone(),
                    Box::new(ArrayOrd::new(list_array.values())),
                )
            }
            DataType::Struct(_) => {
                let struct_array = array.as_struct();
                let nulls = array.nulls().cloned();
                let columns: Vec<_> = struct_array
                    .columns()
                    .iter()
                    .map(|a| ArrayOrd::new(a))
                    .collect();
                ArrayOrd::Struct(nulls, columns)
            }
            data_type => unimplemented!("array type {data_type:?} not yet supported"),
        }
    }

    /// Returns the rough amount of space required for the data in this array in bytes.
    /// (Not counting nulls, dictionary encoding, or other space optimizations.)
    pub fn goodbytes(&self) -> usize {
        match self {
            ArrayOrd::Null(_) => 0,
            // This is, strictly speaking, wrong - but consistent with `ArrayIdx::goodbytes`,
            // which counts one byte per bool.
            ArrayOrd::Bool(b) => b.len(),
            ArrayOrd::Int8(a) => a.values().inner().len(),
            ArrayOrd::Int16(a) => a.values().inner().len(),
            ArrayOrd::Int32(a) => a.values().inner().len(),
            ArrayOrd::Int64(a) => a.values().inner().len(),
            ArrayOrd::UInt8(a) => a.values().inner().len(),
            ArrayOrd::UInt16(a) => a.values().inner().len(),
            ArrayOrd::UInt32(a) => a.values().inner().len(),
            ArrayOrd::UInt64(a) => a.values().inner().len(),
            ArrayOrd::Float32(a) => a.values().inner().len(),
            ArrayOrd::Float64(a) => a.values().inner().len(),
            ArrayOrd::String(a) => a.values().len(),
            ArrayOrd::Binary(a) => a.values().len(),
            ArrayOrd::FixedSizeBinary(a) => a.values().len(),
            ArrayOrd::List(_, _, nested) => nested.goodbytes(),
            ArrayOrd::Struct(_, nested) => nested.iter().map(|a| a.goodbytes()).sum(),
        }
    }

    /// Return a struct representing the value at a particular index in this array.
    pub fn at(&self, idx: usize) -> ArrayIdx {
        ArrayIdx { idx, array: self }
    }
}

/// A struct representing a particular entry in a particular array. Most useful for its `Ord`
/// implementation, which can compare entire rows across similarly-typed arrays.
///
/// It is an error to compare indices from arrays with different types, with one exception:
/// it is valid to compare two `StructArray`s, one of which is a prefix of the other...
/// in which case we'll compare the values on that subset of the fields, and the shorter
/// of the two structs will compare less if they're otherwise equal.
#[derive(Clone, Copy, Debug)]
pub struct ArrayIdx<'a> {
    /// An index into a particular array.
    pub idx: usize,
    /// The particular array.
    pub array: &'a ArrayOrd,
}

#[inline]
fn list_range<'a>(
    offsets: &OffsetBuffer<i32>,
    values: &'a ArrayOrd,
    idx: usize,
) -> impl Iterator<Item = ArrayIdx<'a>> {
    let offsets = offsets.inner();
    let from = offsets[idx].as_usize();
    let to = offsets[idx + 1].as_usize();
    (from..to).map(|i| values.at(i))
}

impl<'a> ArrayIdx<'a> {
    /// Returns the rough amount of space required for this entry in bytes.
    /// (Not counting nulls, dictionary encoding, or other space optimizations.)
    pub fn goodbytes(&self) -> usize {
        match self.array {
            ArrayOrd::Null(_) => 0,
            ArrayOrd::Bool(_) => size_of::<bool>(),
            ArrayOrd::Int8(_) => size_of::<i8>(),
            ArrayOrd::Int16(_) => size_of::<i16>(),
            ArrayOrd::Int32(_) => size_of::<i32>(),
            ArrayOrd::Int64(_) => size_of::<i64>(),
            ArrayOrd::UInt8(_) => size_of::<u8>(),
            ArrayOrd::UInt16(_) => size_of::<u16>(),
            ArrayOrd::UInt32(_) => size_of::<u32>(),
            ArrayOrd::UInt64(_) => size_of::<u64>(),
            ArrayOrd::Float32(_) => size_of::<f32>(),
            ArrayOrd::Float64(_) => size_of::<f64>(),
            ArrayOrd::String(a) => a.value(self.idx).len(),
            ArrayOrd::Binary(a) => a.value(self.idx).len(),
            ArrayOrd::FixedSizeBinary(a) => a.value_length().as_usize(),
            ArrayOrd::List(_, offsets, nested) => {
                // Range over the list, summing up the bytes for each entry.
                list_range(offsets, nested, self.idx)
                    .map(|a| a.goodbytes())
                    .sum()
            }
            ArrayOrd::Struct(_, nested) => nested.iter().map(|a| a.at(self.idx).goodbytes()).sum(),
        }
    }
}

impl<'a> Ord for ArrayIdx<'a> {
    fn cmp(&self, other: &Self) -> Ordering {
        #[inline]
        fn is_null(buffer: &Option<NullBuffer>, idx: usize) -> bool {
            buffer.as_ref().map_or(false, |b| b.is_null(idx))
        }
        #[inline]
        fn cmp<A: ArrayAccessor>(
            left: A,
            left_idx: usize,
            right: A,
            right_idx: usize,
            cmp: fn(&A::Item, &A::Item) -> Ordering,
        ) -> Ordering {
            // NB: nulls sort last, conveniently matching psql / mz_repr
            match (left.is_null(left_idx), right.is_null(right_idx)) {
                (false, true) => Ordering::Less,
                (true, true) => Ordering::Equal,
                (true, false) => Ordering::Greater,
                (false, false) => cmp(&left.value(left_idx), &right.value(right_idx)),
            }
        }
        match (&self.array, &other.array) {
            (ArrayOrd::Null(s), ArrayOrd::Null(o)) => {
                debug_assert!(
                    self.idx < s.len() && other.idx < o.len(),
                    "null array indices in bounds"
                );
                Ordering::Equal
            }
            // For arrays with "simple" value types, we fetch and compare the underlying values directly.
            (ArrayOrd::Bool(s), ArrayOrd::Bool(o)) => cmp(s, self.idx, o, other.idx, Ord::cmp),
            (ArrayOrd::Int8(s), ArrayOrd::Int8(o)) => cmp(s, self.idx, o, other.idx, Ord::cmp),
            (ArrayOrd::Int16(s), ArrayOrd::Int16(o)) => cmp(s, self.idx, o, other.idx, Ord::cmp),
            (ArrayOrd::Int32(s), ArrayOrd::Int32(o)) => cmp(s, self.idx, o, other.idx, Ord::cmp),
            (ArrayOrd::Int64(s), ArrayOrd::Int64(o)) => cmp(s, self.idx, o, other.idx, Ord::cmp),
            (ArrayOrd::UInt8(s), ArrayOrd::UInt8(o)) => cmp(s, self.idx, o, other.idx, Ord::cmp),
            (ArrayOrd::UInt16(s), ArrayOrd::UInt16(o)) => cmp(s, self.idx, o, other.idx, Ord::cmp),
            (ArrayOrd::UInt32(s), ArrayOrd::UInt32(o)) => cmp(s, self.idx, o, other.idx, Ord::cmp),
            (ArrayOrd::UInt64(s), ArrayOrd::UInt64(o)) => cmp(s, self.idx, o, other.idx, Ord::cmp),
            (ArrayOrd::Float32(s), ArrayOrd::Float32(o)) => {
                cmp(s, self.idx, o, other.idx, f32::total_cmp)
            }
            (ArrayOrd::Float64(s), ArrayOrd::Float64(o)) => {
                cmp(s, self.idx, o, other.idx, f64::total_cmp)
            }
            (ArrayOrd::String(s), ArrayOrd::String(o)) => cmp(s, self.idx, o, other.idx, Ord::cmp),
            (ArrayOrd::Binary(s), ArrayOrd::Binary(o)) => cmp(s, self.idx, o, other.idx, Ord::cmp),
            (ArrayOrd::FixedSizeBinary(s), ArrayOrd::FixedSizeBinary(o)) => {
                cmp(s, self.idx, o, other.idx, Ord::cmp)
            }
            // For lists, we generate an iterator for each side that ranges over the correct
            // indices into the value buffer, then compare them lexicographically.
            (
                ArrayOrd::List(s_nulls, s_offset, s_values),
                ArrayOrd::List(o_nulls, o_offset, o_values),
            ) => match (is_null(s_nulls, self.idx), is_null(o_nulls, other.idx)) {
                (false, true) => Ordering::Less,
                (true, true) => Ordering::Equal,
                (true, false) => Ordering::Greater,
                (false, false) => list_range(s_offset, s_values, self.idx)
                    .cmp(list_range(o_offset, o_values, other.idx)),
            },
            // For structs, we iterate over the same index in each field for each input,
            // comparing them lexicographically in order.
            (ArrayOrd::Struct(s_nulls, s_cols), ArrayOrd::Struct(o_nulls, o_cols)) => {
                match (is_null(s_nulls, self.idx), is_null(o_nulls, other.idx)) {
                    (false, true) => Ordering::Less,
                    (true, true) => Ordering::Equal,
                    (true, false) => Ordering::Greater,
                    (false, false) => {
                        let s = s_cols.iter().map(|array| array.at(self.idx));
                        let o = o_cols.iter().map(|array| array.at(other.idx));
                        s.cmp(o)
                    }
                }
            }
            (_, _) => panic!("array types did not match"),
        }
    }
}

impl<'a> PartialOrd for ArrayIdx<'a> {
    fn partial_cmp(&self, other: &ArrayIdx) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<'a> PartialEq for ArrayIdx<'a> {
    fn eq(&self, other: &ArrayIdx) -> bool {
        self.cmp(other) == Ordering::Equal
    }
}

impl<'a> Eq for ArrayIdx<'a> {}

/// An array with precisely one entry, for use as a lower bound.
#[derive(Debug, Clone)]
pub struct ArrayBound {
    raw: ArrayRef,
    ord: ArrayOrd,
    index: usize,
}

impl PartialEq for ArrayBound {
    fn eq(&self, other: &Self) -> bool {
        self.get().eq(&other.get())
    }
}

impl Eq for ArrayBound {}

impl ArrayBound {
    /// Create a new `ArrayBound` for this array, with the bound at the provided index.
    pub fn new(array: ArrayRef, index: usize) -> Self {
        Self {
            ord: ArrayOrd::new(array.as_ref()),
            raw: array,
            index,
        }
    }

    /// Get the value of the bound.
    pub fn get(&self) -> ArrayIdx {
        self.ord.at(self.index)
    }

    /// Convert to an array-data proto, respecting a maximum data size. The resulting proto will
    /// decode to a single-row array, such that `ArrayBound::new(decoded, 0).get() <= self.get()`,
    /// which makes it suitable as a lower bound.
    pub fn to_proto_lower(&self, max_len: usize) -> Option<ProtoArrayData> {
        // Use `take` instead of slice to make sure we encode just the relevant row to proto,
        // instead of some larger buffer with an offset.
        let indices = UInt64Array::from_value(u64::usize_as(self.index), 1);
        let taken = arrow::compute::take(self.raw.as_ref(), &indices, None).ok()?;
        let array_data = taken.into_data();

        let mut proto = array_data.into_proto();
        let original_len = proto.encoded_len();
        if original_len <= max_len {
            return Some(proto);
        }

        let mut data_type = proto.data_type.take()?;
        maybe_trim_proto(&mut data_type, &mut proto, max_len);
        proto.data_type = Some(data_type);

        if cfg!(debug_assertions) {
            let array: ArrayData = proto
                .clone()
                .into_rust()
                .expect("trimmed array data can still be decoded");
            assert_eq!(array.len(), 1);
            let new_bound = Self::new(make_array(array), 0);
            assert!(
                new_bound.get() <= self.get(),
                "trimmed bound should be comparable to and no larger than the original data"
            )
        }

        if proto.encoded_len() <= max_len {
            Some(proto)
        } else {
            None
        }
    }
}

/// Makes a best effort to shrink the proto while preserving the ordering.
/// (The proto might not be smaller after this method is called, but it should always
/// be a valid lower bound.)
///
/// Note that we pass in the data type and the array data separately, since we only keep
/// type info at the top level. If a caller does have a top-level `ArrayData` instance,
/// they should take that type and pass it in separately.
fn maybe_trim_proto(data_type: &mut proto::DataType, body: &mut ProtoArrayData, max_len: usize) {
    assert!(body.data_type.is_none(), "expected separate data type");
    // TODO: consider adding cases for strings and byte arrays
    let encoded_len = data_type.encoded_len() + body.encoded_len();
    match &mut data_type.kind {
        Some(data_type::Kind::Struct(data_type::Struct { children: fields })) => {
            // Pop off fields one by one, keeping an estimate of the encoded length.
            let mut struct_len = encoded_len;
            while struct_len > max_len {
                let Some(mut child) = body.children.pop() else {
                    break;
                };
                let Some(mut field) = fields.pop() else { break };

                struct_len -= field.encoded_len() + child.encoded_len();
                if let Some(remaining_len) = max_len.checked_sub(struct_len) {
                    // We're under budget after removing this field! See if we can
                    // shrink it to fit, but exit the loop regardless.
                    let Some(field_type) = field.data_type.as_mut() else {
                        break;
                    };
                    maybe_trim_proto(field_type, &mut child, remaining_len);
                    if field.encoded_len() + child.encoded_len() <= remaining_len {
                        fields.push(field);
                        body.children.push(child);
                    }
                    break;
                }
            }
        }
        _ => {}
    };
}

#[cfg(test)]
mod tests {
    use crate::arrow::{ArrayBound, ArrayOrd};
    use arrow::array::{
        make_array, ArrayRef, AsArray, BooleanArray, StringArray, StructArray, UInt64Array,
    };
    use arrow::datatypes::{DataType, Field, Fields};
    use mz_ore::assert_none;
    use mz_proto::ProtoType;
    use std::sync::Arc;

    #[mz_ore::test]
    fn trim_proto() {
        let nested_fields: Fields = vec![Field::new("a", DataType::UInt64, true)].into();
        let array: ArrayRef = Arc::new(StructArray::new(
            vec![
                Field::new("a", DataType::UInt64, true),
                Field::new("b", DataType::Utf8, true),
                Field::new_struct("c", nested_fields.clone(), true),
            ]
            .into(),
            vec![
                Arc::new(UInt64Array::from_iter_values([1])),
                Arc::new(StringArray::from_iter_values(["large".repeat(50)])),
                Arc::new(StructArray::new_null(nested_fields, 1)),
            ],
            None,
        ));
        let bound = ArrayBound::new(array, 0);

        assert_none!(bound.to_proto_lower(0));
        assert_none!(bound.to_proto_lower(1));

        let proto = bound
            .to_proto_lower(100)
            .expect("can fit something in less than 100 bytes");
        let array = make_array(proto.into_rust().expect("valid proto"));
        assert_eq!(
            array.as_struct().column_names().as_slice(),
            &["a"],
            "only the first column should fit"
        );

        let proto = bound
            .to_proto_lower(1000)
            .expect("can fit everything in less than 1000 bytes");
        let array = make_array(proto.into_rust().expect("valid proto"));
        assert_eq!(
            array.as_struct().column_names().as_slice(),
            &["a", "b", "c"],
            "all columns should fit"
        )
    }

    #[mz_ore::test]
    fn struct_ord() {
        let prefix = StructArray::new(
            vec![Field::new("a", DataType::UInt64, true)].into(),
            vec![Arc::new(UInt64Array::from_iter_values([1, 3, 5]))],
            None,
        );
        let full = StructArray::new(
            vec![
                Field::new("a", DataType::UInt64, true),
                Field::new("b", DataType::Utf8, true),
            ]
            .into(),
            vec![
                Arc::new(UInt64Array::from_iter_values([2, 3, 4])),
                Arc::new(StringArray::from_iter_values(["a", "b", "c"])),
            ],
            None,
        );
        let prefix_ord = ArrayOrd::new(&prefix);
        let full_ord = ArrayOrd::new(&full);

        // Comparison works as normal over the shared columns... but when those columns are identical,
        // the shorter struct is always smaller.
        assert!(prefix_ord.at(0) < full_ord.at(0), "(1) < (2, 'a')");
        assert!(prefix_ord.at(1) < full_ord.at(1), "(3) < (3, 'b')");
        assert!(prefix_ord.at(2) > full_ord.at(2), "(5) < (4, 'c')");
    }

    #[mz_ore::test]
    #[should_panic(expected = "array types did not match")]
    fn struct_ord_incompat() {
        // This test is descriptive, not prescriptive: we declare it is an error to compare
        // structs like this, but not what the result of comparing them is.
        let string = StructArray::new(
            vec![
                Field::new("a", DataType::UInt64, true),
                Field::new("b", DataType::Utf8, true),
            ]
            .into(),
            vec![
                Arc::new(UInt64Array::from_iter_values([1])),
                Arc::new(StringArray::from_iter_values(["a"])),
            ],
            None,
        );
        let boolean = StructArray::new(
            vec![
                Field::new("a", DataType::UInt64, true),
                Field::new("b", DataType::Boolean, true),
            ]
            .into(),
            vec![
                Arc::new(UInt64Array::from_iter_values([1])),
                Arc::new(BooleanArray::from_iter([Some(true)])),
            ],
            None,
        );
        let string_ord = ArrayOrd::new(&string);
        let bool_ord = ArrayOrd::new(&boolean);

        // Despite the matching first column, this will panic with a type mismatch.
        assert!(string_ord.at(0) < bool_ord.at(0));
    }
}