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
use crate::{
    array::{Array, PrimitiveArray},
    chunk::Chunk,
    datatypes::DataType,
};

use super::super::{ArrowJsonBatch, ArrowJsonColumn};

/// Serializes a [`Chunk`] to [`ArrowJsonBatch`].
pub fn serialize_chunk<A: ToString>(
    columns: &Chunk<Box<dyn Array>>,
    names: &[A],
) -> ArrowJsonBatch {
    let count = columns.len();

    let columns = columns
        .arrays()
        .iter()
        .zip(names.iter())
        .map(|(array, name)| match array.data_type() {
            DataType::Int8 => {
                let array = array.as_any().downcast_ref::<PrimitiveArray<i8>>().unwrap();

                let (validity, data) = array
                    .iter()
                    .map(|x| (x.is_some() as u8, x.copied().unwrap_or_default().into()))
                    .unzip();

                ArrowJsonColumn {
                    name: name.to_string(),
                    count: array.len(),
                    validity: Some(validity),
                    data: Some(data),
                    offset: None,
                    type_id: None,
                    children: None,
                }
            }
            _ => ArrowJsonColumn {
                name: name.to_string(),
                count: array.len(),
                validity: None,
                data: None,
                offset: None,
                type_id: None,
                children: None,
            },
        })
        .collect();

    ArrowJsonBatch { count, columns }
}