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
// 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.

use std::fmt::Debug;

use mz_proto::{IntoRustIfSome, ProtoType, RustType, TryFromProtoError};
use proptest::strategy::{Just, Strategy, Union};
use serde::Serialize;

use crate::stats::json::{any_json_stats, JsonStats};
use crate::stats::primitive::{any_primitive_vec_u8_stats, PrimitiveStats};
use crate::stats::{
    proto_bytes_stats, proto_fixed_size_bytes_stats, ColumnStatKinds, ColumnStats, ColumnarStats,
    DynStats, OptionStats, ProtoAtomicBytesStats, ProtoBytesStats, ProtoFixedSizeBytesStats,
    TrimStats,
};

/// `PrimitiveStats<Vec<u8>>` that cannot safely be trimmed.
#[derive(Debug, Clone)]
pub struct AtomicBytesStats {
    /// See [PrimitiveStats::lower]
    pub lower: Vec<u8>,
    /// See [PrimitiveStats::upper]
    pub upper: Vec<u8>,
}

impl AtomicBytesStats {
    fn debug_json(&self) -> serde_json::Value {
        serde_json::json!({
            "lower": hex::encode(&self.lower),
            "upper": hex::encode(&self.upper),
        })
    }
}

impl RustType<ProtoAtomicBytesStats> for AtomicBytesStats {
    fn into_proto(&self) -> ProtoAtomicBytesStats {
        ProtoAtomicBytesStats {
            lower: self.lower.into_proto(),
            upper: self.upper.into_proto(),
        }
    }

    fn from_proto(proto: ProtoAtomicBytesStats) -> Result<Self, TryFromProtoError> {
        Ok(AtomicBytesStats {
            lower: proto.lower.into_rust()?,
            upper: proto.upper.into_rust()?,
        })
    }
}

/// `PrimitiveStats<Vec<u8>>` for types that implement [`FixedSizeCodec`] and
/// cannot safely be trimmed.
///
/// [`FixedSizeCodec`]: crate::columnar::FixedSizeCodec
#[derive(Debug, Clone)]
pub struct FixedSizeBytesStats {
    /// See [PrimitiveStats::lower]
    pub lower: Vec<u8>,
    /// See [PrimitiveStats::upper]
    pub upper: Vec<u8>,
    /// The kind of data these stats represent.
    pub kind: FixedSizeBytesStatsKind,
}

impl FixedSizeBytesStats {
    fn debug_json(&self) -> serde_json::Value {
        serde_json::json!({
            "lower": hex::encode(&self.lower),
            "upper": hex::encode(&self.upper),
            "kind": self.kind,
        })
    }
}

impl RustType<ProtoFixedSizeBytesStats> for FixedSizeBytesStats {
    fn into_proto(&self) -> ProtoFixedSizeBytesStats {
        ProtoFixedSizeBytesStats {
            lower: self.lower.into_proto(),
            upper: self.upper.into_proto(),
            kind: Some(self.kind.into_proto()),
        }
    }

    fn from_proto(proto: ProtoFixedSizeBytesStats) -> Result<Self, TryFromProtoError> {
        Ok(FixedSizeBytesStats {
            lower: proto.lower.into_rust()?,
            upper: proto.upper.into_rust()?,
            kind: proto
                .kind
                .into_rust_if_some("missing field ProtoFixedSizeBytesStats::kind")?,
        })
    }
}

/// The type of data encoded in an [`FixedSizeBytesStats`].
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum FixedSizeBytesStatsKind {
    PackedTime,
    PackedDateTime,
    PackedInterval,
    PackedNumeric,
    Uuid,
}

impl RustType<proto_fixed_size_bytes_stats::Kind> for FixedSizeBytesStatsKind {
    fn into_proto(&self) -> proto_fixed_size_bytes_stats::Kind {
        match self {
            FixedSizeBytesStatsKind::PackedTime => {
                proto_fixed_size_bytes_stats::Kind::PackedTime(())
            }
            FixedSizeBytesStatsKind::PackedDateTime => {
                proto_fixed_size_bytes_stats::Kind::PackedDateTime(())
            }
            FixedSizeBytesStatsKind::PackedInterval => {
                proto_fixed_size_bytes_stats::Kind::PackedInterval(())
            }
            FixedSizeBytesStatsKind::PackedNumeric => {
                proto_fixed_size_bytes_stats::Kind::PackedNumeric(())
            }
            FixedSizeBytesStatsKind::Uuid => proto_fixed_size_bytes_stats::Kind::Uuid(()),
        }
    }

    fn from_proto(proto: proto_fixed_size_bytes_stats::Kind) -> Result<Self, TryFromProtoError> {
        let kind = match proto {
            proto_fixed_size_bytes_stats::Kind::PackedTime(_) => {
                FixedSizeBytesStatsKind::PackedTime
            }
            proto_fixed_size_bytes_stats::Kind::PackedDateTime(_) => {
                FixedSizeBytesStatsKind::PackedDateTime
            }
            proto_fixed_size_bytes_stats::Kind::PackedInterval(_) => {
                FixedSizeBytesStatsKind::PackedInterval
            }
            proto_fixed_size_bytes_stats::Kind::PackedNumeric(_) => {
                FixedSizeBytesStatsKind::PackedNumeric
            }
            proto_fixed_size_bytes_stats::Kind::Uuid(_) => FixedSizeBytesStatsKind::Uuid,
        };
        Ok(kind)
    }
}

/// Statistics about a column of `Vec<u8>`.
#[derive(Clone)]
pub enum BytesStats {
    Primitive(PrimitiveStats<Vec<u8>>),
    Json(JsonStats),
    Atomic(AtomicBytesStats),
    FixedSize(FixedSizeBytesStats),
}

impl Debug for BytesStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Debug::fmt(&self.debug_json(), f)
    }
}

impl DynStats for BytesStats {
    fn debug_json(&self) -> serde_json::Value {
        match self {
            BytesStats::Primitive(x) => x.debug_json(),
            BytesStats::Json(x) => x.debug_json(),
            BytesStats::Atomic(x) => x.debug_json(),
            BytesStats::FixedSize(x) => x.debug_json(),
        }
    }

    fn into_columnar_stats(self) -> ColumnarStats {
        ColumnarStats {
            nulls: None,
            values: ColumnStatKinds::Bytes(self),
        }
    }
}

impl ColumnStats for BytesStats {
    type Ref<'a> = &'a [u8];

    fn lower<'a>(&'a self) -> Option<Self::Ref<'a>> {
        match self {
            BytesStats::Primitive(x) => Some(x.lower.as_slice()),
            BytesStats::Json(_) => None,
            BytesStats::Atomic(x) => Some(&x.lower),
            BytesStats::FixedSize(x) => Some(&x.lower),
        }
    }
    fn upper<'a>(&'a self) -> Option<Self::Ref<'a>> {
        match self {
            BytesStats::Primitive(x) => Some(x.upper.as_slice()),
            BytesStats::Json(_) => None,
            BytesStats::Atomic(x) => Some(&x.upper),
            BytesStats::FixedSize(x) => Some(&x.upper),
        }
    }
    fn none_count(&self) -> usize {
        0
    }
}

impl ColumnStats for OptionStats<BytesStats> {
    type Ref<'a> = Option<&'a [u8]>;

    fn lower<'a>(&'a self) -> Option<Self::Ref<'a>> {
        self.some.lower().map(Some)
    }
    fn upper<'a>(&'a self) -> Option<Self::Ref<'a>> {
        self.some.upper().map(Some)
    }
    fn none_count(&self) -> usize {
        self.none
    }
}

impl RustType<ProtoBytesStats> for BytesStats {
    fn into_proto(&self) -> ProtoBytesStats {
        let kind = match self {
            BytesStats::Primitive(x) => proto_bytes_stats::Kind::Primitive(RustType::into_proto(x)),
            BytesStats::Json(x) => proto_bytes_stats::Kind::Json(RustType::into_proto(x)),
            BytesStats::Atomic(x) => proto_bytes_stats::Kind::Atomic(RustType::into_proto(x)),
            BytesStats::FixedSize(x) => proto_bytes_stats::Kind::FixedSize(RustType::into_proto(x)),
        };
        ProtoBytesStats { kind: Some(kind) }
    }

    fn from_proto(proto: ProtoBytesStats) -> Result<Self, TryFromProtoError> {
        match proto.kind {
            Some(proto_bytes_stats::Kind::Primitive(x)) => Ok(BytesStats::Primitive(
                PrimitiveStats::<Vec<u8>>::from_proto(x)?,
            )),
            Some(proto_bytes_stats::Kind::Json(x)) => {
                Ok(BytesStats::Json(JsonStats::from_proto(x)?))
            }
            Some(proto_bytes_stats::Kind::Atomic(x)) => {
                Ok(BytesStats::Atomic(AtomicBytesStats::from_proto(x)?))
            }
            Some(proto_bytes_stats::Kind::FixedSize(x)) => {
                Ok(BytesStats::FixedSize(FixedSizeBytesStats::from_proto(x)?))
            }
            None => Err(TryFromProtoError::missing_field("ProtoBytesStats::kind")),
        }
    }
}

impl TrimStats for ProtoBytesStats {
    fn trim(&mut self) {
        use proto_bytes_stats::*;
        match &mut self.kind {
            Some(Kind::Primitive(stats)) => stats.trim(),
            Some(Kind::Json(stats)) => stats.trim(),
            // We explicitly don't trim atomic stats!
            Some(Kind::Atomic(_)) => {}
            // We explicitly don't trim fixed size stats!
            Some(Kind::FixedSize(_)) => {}
            None => {}
        }
    }
}

/// Returns a [`Strategy`] for generating arbitrary [`BytesStats`].
pub(crate) fn any_bytes_stats() -> impl Strategy<Value = BytesStats> {
    let kind_of_packed = Union::new(vec![
        Just(FixedSizeBytesStatsKind::PackedTime),
        Just(FixedSizeBytesStatsKind::PackedInterval),
        Just(FixedSizeBytesStatsKind::PackedNumeric),
        Just(FixedSizeBytesStatsKind::Uuid),
    ]);

    Union::new(vec![
        any_primitive_vec_u8_stats()
            .prop_map(BytesStats::Primitive)
            .boxed(),
        any_json_stats().prop_map(BytesStats::Json).boxed(),
        any_primitive_vec_u8_stats()
            .prop_map(|x| {
                BytesStats::Atomic(AtomicBytesStats {
                    lower: x.lower,
                    upper: x.upper,
                })
            })
            .boxed(),
        (any_primitive_vec_u8_stats(), kind_of_packed)
            .prop_map(|(x, kind)| {
                BytesStats::FixedSize(FixedSizeBytesStats {
                    lower: x.lower,
                    upper: x.upper,
                    kind,
                })
            })
            .boxed(),
    ])
}