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
//! Deserialization of [`Frame`]s from the JSON format.
use std::{collections::BTreeMap, fmt, marker::PhantomData};

use arrow2::{
    array::{
        Array, MutableArray, MutableBooleanArray, MutablePrimitiveArray, MutableUtf8Array, TryPush,
    },
    datatypes::{DataType, TimeUnit},
    types::{NativeType, Offset},
};
use num_traits::Float;
use serde::{
    de::{Deserializer, Error, MapAccess, SeqAccess, Visitor},
    Deserialize,
};
use serde_json::from_str;

use crate::data::{
    field::{FieldConfig, SimpleType, TypeInfo, TypeInfoType},
    frame::ser::Entities,
    Frame, Metadata,
};

// Deserialization from the JSON representation for [`Frame`]s.
//
// This follows the [Manually deserializing a struct](https://serde.rs/deserialize-struct.html)
// example in the [`serde`] docs, and uses [`RawValue`] to avoid intermediate buffering
// of arrays during deserialization.
impl<'de> Deserialize<'de> for Frame {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(field_identifier, rename_all = "lowercase")]
        enum Field {
            Schema,
            Data,
        }

        struct FrameVisitor;

        impl<'de> Visitor<'de> for FrameVisitor {
            type Value = Frame;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("stuct Frame")
            }

            fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
            where
                V: MapAccess<'de>,
            {
                let mut schema: Option<Schema> = None;
                let mut raw_data: Option<RawData> = None;

                while let Some(key) = map.next_key()? {
                    match key {
                        Field::Schema => {
                            if schema.is_some() {
                                return Err(Error::duplicate_field("schema"));
                            }
                            schema = Some(map.next_value()?);
                        }
                        Field::Data => {
                            if raw_data.is_some() {
                                return Err(Error::duplicate_field("data"));
                            }
                            raw_data = Some(map.next_value()?);
                        }
                    }
                }
                let schema = schema.ok_or_else(|| Error::missing_field("schema"))?;
                let raw_data = raw_data.ok_or_else(|| Error::missing_field("data"))?;
                let data: Data = (&schema, raw_data)
                    .try_into()
                    .map_err(|e| Error::custom(format!("invalid values: {}", e)))?;

                Ok(Frame {
                    name: schema.name,
                    ref_id: Some(schema.ref_id),
                    meta: schema.meta,
                    fields: schema
                        .fields
                        .into_iter()
                        .zip(data.values)
                        .map(|(f, values)| crate::data::field::Field {
                            name: f.name,
                            labels: f.labels,
                            config: f.config,
                            values,
                            type_info: f.type_info,
                        })
                        .collect(),
                })
            }
        }

        const FIELDS: &[&str] = &["schema", "data"];
        deserializer.deserialize_struct("Frame", FIELDS, FrameVisitor)
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Field {
    #[serde(default)]
    name: String,
    #[serde(default)]
    labels: BTreeMap<String, String>,
    #[serde(default)]
    config: Option<FieldConfig>,
    #[serde(rename = "type")]
    _type: SimpleType,
    type_info: TypeInfo,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Schema {
    name: String,
    ref_id: String,
    #[serde(default)]
    fields: Vec<Field>,
    #[serde(default)]
    meta: Option<Metadata>,
}

#[derive(Debug, Deserialize)]
struct RawData<'a> {
    #[serde(borrow, default)]
    values: Vec<&'a serde_json::value::RawValue>,
    #[serde(default)]
    entities: Option<Vec<Option<Entities>>>,
}

#[derive(Debug)]
struct Data {
    values: Vec<Box<dyn Array>>,
}

impl TryFrom<(&'_ Schema, RawData<'_>)> for Data {
    type Error = serde_json::Error;

    /// Create `Data` from the schema and data objects found in the JSON representation.
    ///
    /// This handles deserializing each of the arrays in `values` with the correct datatypes,
    /// replacing any 'entities' (`NaN`, `Inf`, `-Inf`) in those arrays, and converting any
    /// which require it (e.g. timestamps).
    fn try_from((schema, data): (&'_ Schema, RawData<'_>)) -> Result<Self, Self::Error> {
        // Handle entity replacement, return values.
        let fields = schema.fields.iter();
        let values = data.values.into_iter();
        let entities = data
            .entities
            .unwrap_or_else(|| vec![None; fields.len()])
            .into_iter();
        Ok(Self {
            values: fields
                .zip(values)
                .zip(entities)
                .map(|((f, v), e)| {
                    let s = v.get();
                    let arr: Box<dyn Array> = match f.type_info.frame {
                        TypeInfoType::Int8 => {
                            parse_array::<MutablePrimitiveArray<i8>, i8, ()>(s)?.as_box()
                        }
                        TypeInfoType::Int16 => {
                            parse_array::<MutablePrimitiveArray<i16>, i16, ()>(s)?.as_box()
                        }
                        TypeInfoType::Int32 => {
                            parse_array::<MutablePrimitiveArray<i32>, i32, ()>(s)?.as_box()
                        }
                        TypeInfoType::Int64 => {
                            parse_array::<MutablePrimitiveArray<i64>, i64, ()>(s)?.as_box()
                        }
                        TypeInfoType::UInt8 => {
                            parse_array::<MutablePrimitiveArray<u8>, u8, ()>(s)?.as_box()
                        }
                        TypeInfoType::UInt16 => {
                            parse_array::<MutablePrimitiveArray<u16>, u16, ()>(s)?.as_box()
                        }
                        TypeInfoType::UInt32 => {
                            parse_array::<MutablePrimitiveArray<u32>, u32, ()>(s)?.as_box()
                        }
                        TypeInfoType::UInt64 => {
                            parse_array::<MutablePrimitiveArray<u64>, u64, ()>(s)?.as_box()
                        }
                        TypeInfoType::Float32 => {
                            parse_array_with_entities::<MutablePrimitiveArray<f32>, f32>(s, e)?
                                .as_box()
                        }
                        TypeInfoType::Float64 => {
                            parse_array_with_entities::<MutablePrimitiveArray<f64>, f64>(s, e)?
                                .as_box()
                        }
                        TypeInfoType::String => {
                            parse_array::<MutableUtf8Array<i32>, String, ()>(s)?.as_box()
                        }
                        TypeInfoType::Bool => {
                            parse_array::<MutableBooleanArray, bool, ()>(s)?.as_box()
                        }
                        TypeInfoType::Time => {
                            parse_array::<MutablePrimitiveArray<i64>, i64, TimestampProcessor>(s)?
                                .to(DataType::Timestamp(TimeUnit::Nanosecond, None))
                                .as_box()
                        }
                    };
                    Ok(arr)
                })
                .collect::<Result<Vec<_>, _>>()?,
        })
    }
}

/// Parse a JSON array containing elements of `U` into a mutable Arrow array `T`.
///
/// # Errors
///
/// Returns an error if the string is invalid JSON, if the elements of
/// the array are not of type `U`,  or if the Arrow buffer could not be
/// created.
fn parse_array<'de, T, U, V>(s: &'de str) -> Result<T, serde_json::Error>
where
    T: Default + MutableArray + TryPush<Option<U>> + WithCapacity,
    U: Deserialize<'de>,
    V: ElementProcessor<U>,
{
    Ok(from_str::<DeArray<T, U, V>>(s)?.array)
}

/// Parse a JSON array containing elements of `U` into a mutable Arrow array `T`,
/// then substitutes any sentinel `entities`.
///
/// # Errors
///
/// Returns an error if the string is invalid JSON, if the elements of
/// the array are not of type `U`,  or if the Arrow buffer could not be
/// created.
///
/// # Panics
///
/// Panics if any of the indexes in `entities` are greater than the length
/// of the parsed array.
fn parse_array_with_entities<'de, T, U>(
    s: &'de str,
    entities: Option<Entities>,
) -> Result<T, serde_json::Error>
where
    T: Default + MutableArray + SetArray<Option<U>> + TryPush<Option<U>> + WithCapacity,
    U: Deserialize<'de> + Float,
{
    let mut arr = from_str::<DeArray<T, U>>(s)?.array;
    if let Some(e) = entities {
        e.nan.iter().for_each(|idx| arr.set(*idx, Some(U::nan())));
        e.inf
            .iter()
            .for_each(|idx| arr.set(*idx, Some(U::infinity())));
        e.neg_inf
            .iter()
            .for_each(|idx| arr.set(*idx, Some(U::neg_infinity())));
    }
    Ok(arr)
}

trait ElementProcessor<T> {
    fn process_element(el: T) -> T {
        el
    }
}

impl<T> ElementProcessor<T> for () {}

struct TimestampProcessor;
impl ElementProcessor<i64> for TimestampProcessor {
    fn process_element(el: i64) -> i64 {
        el * 1_000_000
    }
}

/// Helper struct used to deserialize a sequence into an Arrow `Array`.
#[derive(Debug)]
struct DeArray<T, U, V = ()> {
    array: T,
    u: PhantomData<U>,
    v: PhantomData<V>,
}

// Deserialization for mutable Arrow arrays.
//
// Constructs a mutable array from a sequence of valid values.
// All of the `Mutable` variants of Arrow arrays implement `TryPush<Option<U>>`
// for some relevant `U`, and here we just impose that the `U` is `Deserialize`
// and gradually build up the array.
impl<'de, T, U, V> Deserialize<'de> for DeArray<T, U, V>
where
    T: Default + TryPush<Option<U>> + WithCapacity,
    U: Deserialize<'de>,
    V: ElementProcessor<U>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct ArrayVisitor<T, U, V>(PhantomData<T>, PhantomData<U>, PhantomData<V>);

        impl<'de, T, U, V> Visitor<'de> for ArrayVisitor<T, U, V>
        where
            T: Default + TryPush<Option<U>> + WithCapacity,
            U: Deserialize<'de>,
            V: ElementProcessor<U>,
        {
            type Value = DeArray<T, U, V>;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("a heterogeneous array of compatible values")
            }

            fn visit_seq<S>(self, mut seq: S) -> Result<Self::Value, S::Error>
            where
                S: SeqAccess<'de>,
            {
                let mut array = seq.size_hint().map_or_else(T::default, T::with_capacity);
                while let Some(x) = seq.next_element::<Option<U>>()? {
                    array.try_push(x.map(V::process_element)).map_err(|e| {
                        S::Error::custom(format!("could not push to Arrow array: {}", e))
                    })?;
                }
                Ok(Self::Value {
                    array,
                    u: PhantomData,
                    v: PhantomData,
                })
            }
        }

        deserializer.deserialize_seq(ArrayVisitor(PhantomData, PhantomData, PhantomData))
    }
}

/// Indicates that an array can have its elements mutated.
trait SetArray<T> {
    /// Set the value at `index` to `value`.
    ///
    /// Note that if it is the first time a null appears in this array,
    /// this initializes the validity bitmap (`O(N)`).
    ///
    /// # Panics
    ///
    /// Panics iff index is larger than `self.len()`.
    fn set(&mut self, index: usize, value: T);
}

impl<T: NativeType> SetArray<Option<T>> for MutablePrimitiveArray<T> {
    fn set(&mut self, index: usize, value: Option<T>) {
        self.set(index, value);
    }
}

/// Indicates that an array can be created with a specified capacity.
trait WithCapacity {
    /// Create `Self` with the given `capacity` preallocated.
    ///
    /// This generally just delegates to the `with_capacity` method on
    /// individual arrays.
    fn with_capacity(capacity: usize) -> Self;
}

impl<T: NativeType> WithCapacity for MutablePrimitiveArray<T> {
    fn with_capacity(capacity: usize) -> Self {
        Self::with_capacity(capacity)
    }
}

impl WithCapacity for MutableBooleanArray {
    fn with_capacity(capacity: usize) -> Self {
        Self::with_capacity(capacity)
    }
}

impl<T: Offset> WithCapacity for MutableUtf8Array<T> {
    fn with_capacity(capacity: usize) -> Self {
        Self::with_capacity(capacity)
    }
}

#[cfg(test)]
mod test {
    use crate::data::Frame;

    #[test]
    fn deserialize_golden() {
        let jdoc = include_str!("golden.json");
        let _: Frame = serde_json::from_str(jdoc).unwrap();
    }
}