This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
serialize.rs
230 lines (215 loc) · 7.67 KB
/
serialize.rs
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
use lexical_core::ToLexical;
use serde_json::Value;
use streaming_iterator::StreamingIterator;
use crate::bitmap::utils::zip_validity;
use crate::chunk::Chunk;
use crate::io::iterator::BufStreamingIterator;
use crate::util::lexical_to_bytes_mut;
use crate::{array::*, datatypes::DataType, types::NativeType};
use super::{JsonArray, JsonFormat};
fn boolean_serializer<'a>(
array: &'a BooleanArray,
) -> Box<dyn StreamingIterator<Item = [u8]> + 'a + Send + Sync> {
Box::new(BufStreamingIterator::new(
array.iter(),
|x, buf| match x {
Some(true) => buf.extend_from_slice(b"true"),
Some(false) => buf.extend_from_slice(b"false"),
None => buf.extend_from_slice(b"null"),
},
vec![],
))
}
fn primitive_serializer<'a, T: NativeType + ToLexical>(
array: &'a PrimitiveArray<T>,
) -> Box<dyn StreamingIterator<Item = [u8]> + 'a + Send + Sync> {
Box::new(BufStreamingIterator::new(
array.iter(),
|x, buf| {
if let Some(x) = x {
lexical_to_bytes_mut(*x, buf)
} else {
buf.extend(b"null")
}
},
vec![],
))
}
fn utf8_serializer<'a, O: Offset>(
array: &'a Utf8Array<O>,
) -> Box<dyn StreamingIterator<Item = [u8]> + 'a + Send + Sync> {
Box::new(BufStreamingIterator::new(
array.iter(),
|x, buf| {
if let Some(x) = x {
utf8_serialize(x, buf)
} else {
buf.extend_from_slice(b"null")
}
},
vec![],
))
}
fn struct_serializer<'a>(
array: &'a StructArray,
) -> Box<dyn StreamingIterator<Item = [u8]> + 'a + Send + Sync> {
// {"a": [1, 2, 3], "b": [a, b, c], "c": {"a": [1, 2, 3]}}
// [
// {"a": 1, "b": a, "c": {"a": 1}},
// {"a": 2, "b": b, "c": {"a": 2}},
// {"a": 3, "b": c, "c": {"a": 3}},
// ]
//
let mut serializers = array
.values()
.iter()
.map(|x| x.as_ref())
.map(new_serializer)
.collect::<Vec<_>>();
let names = array.fields().iter().map(|f| f.name.as_str());
Box::new(BufStreamingIterator::new(
zip_validity(0..array.len(), array.validity().map(|x| x.iter())),
move |maybe, buf| {
if maybe.is_some() {
let names = names.clone();
let mut record: Vec<(&str, &[u8])> = Default::default();
serializers
.iter_mut()
.zip(names)
// `unwrap` is infalible because `array.len()` equals `len` on `Chunk`
.for_each(|(iter, name)| {
let item = iter.next().unwrap();
record.push((name, item));
});
serialize_item(buf, &record, JsonArray::default(), true);
} else {
serializers.iter_mut().for_each(|iter| {
let _ = iter.next();
});
buf.extend(b"null");
}
},
vec![],
))
}
fn list_serializer<'a, O: Offset>(
array: &'a ListArray<O>,
) -> Box<dyn StreamingIterator<Item = [u8]> + 'a + Send + Sync> {
// [[1, 2], [3]]
// [
// [1, 2],
// [3]
// ]
//
let mut serializer = new_serializer(array.values().as_ref());
Box::new(BufStreamingIterator::new(
zip_validity(
array.offsets().windows(2),
array.validity().map(|x| x.iter()),
),
move |offset, buf| {
if let Some(offset) = offset {
let length = (offset[1] - offset[0]).to_usize();
buf.push(b'[');
let mut is_first_row = true;
for _ in 0..length {
if !is_first_row {
buf.push(b',');
}
is_first_row = false;
buf.extend(serializer.next().unwrap());
}
buf.push(b']');
} else {
buf.extend(b"null");
}
},
vec![],
))
}
#[inline]
fn utf8_serialize(value: &str, buf: &mut Vec<u8>) {
if value.as_bytes().is_ascii() && !value.contains('"') {
buf.reserve(value.len() + 2);
buf.push(b'"');
buf.extend_from_slice(value.as_bytes());
buf.push(b'"');
} else {
// it may contain reserved keywords: perform roundtrip for
// todo: avoid this roundtrip over serde_json
serde_json::to_writer(buf, value).unwrap();
}
}
fn new_serializer<'a>(
array: &'a dyn Array,
) -> Box<dyn StreamingIterator<Item = [u8]> + 'a + Send + Sync> {
match array.data_type().to_logical_type() {
DataType::Boolean => boolean_serializer(array.as_any().downcast_ref().unwrap()),
DataType::Int8 => primitive_serializer::<i8>(array.as_any().downcast_ref().unwrap()),
DataType::Int16 => primitive_serializer::<i16>(array.as_any().downcast_ref().unwrap()),
DataType::Int32 => primitive_serializer::<i32>(array.as_any().downcast_ref().unwrap()),
DataType::Int64 => primitive_serializer::<i64>(array.as_any().downcast_ref().unwrap()),
DataType::UInt8 => primitive_serializer::<u8>(array.as_any().downcast_ref().unwrap()),
DataType::UInt16 => primitive_serializer::<u16>(array.as_any().downcast_ref().unwrap()),
DataType::UInt32 => primitive_serializer::<u32>(array.as_any().downcast_ref().unwrap()),
DataType::UInt64 => primitive_serializer::<u64>(array.as_any().downcast_ref().unwrap()),
DataType::Float32 => primitive_serializer::<f32>(array.as_any().downcast_ref().unwrap()),
DataType::Float64 => primitive_serializer::<f64>(array.as_any().downcast_ref().unwrap()),
DataType::Utf8 => utf8_serializer::<i32>(array.as_any().downcast_ref().unwrap()),
DataType::LargeUtf8 => utf8_serializer::<i64>(array.as_any().downcast_ref().unwrap()),
DataType::Struct(_) => struct_serializer(array.as_any().downcast_ref().unwrap()),
DataType::List(_) => list_serializer::<i32>(array.as_any().downcast_ref().unwrap()),
DataType::LargeList(_) => list_serializer::<i64>(array.as_any().downcast_ref().unwrap()),
other => todo!("Writing {:?} to JSON", other),
}
}
fn serialize_item<F: JsonFormat>(
buffer: &mut Vec<u8>,
record: &[(&str, &[u8])],
format: F,
is_first_row: bool,
) {
format.start_row(buffer, is_first_row).unwrap();
buffer.push(b'{');
let mut first_item = true;
for (key, value) in record {
if !first_item {
buffer.push(b',');
}
first_item = false;
utf8_serialize(key, buffer);
buffer.push(b':');
buffer.extend(*value);
}
buffer.push(b'}');
format.end_row(buffer).unwrap();
}
/// Serializes a (name, array) to a valid JSON to `buffer`
/// This is CPU-bounded
pub fn serialize<N, A, F>(names: &[N], columns: &Chunk<A>, format: F, buffer: &mut Vec<u8>)
where
N: AsRef<str>,
A: AsRef<dyn Array>,
F: JsonFormat,
{
let num_rows = columns.len();
let mut serializers: Vec<_> = columns
.arrays()
.iter()
.map(|array| new_serializer(array.as_ref()))
.collect();
let mut is_first_row = true;
(0..num_rows).for_each(|_| {
let mut record: Vec<(&str, &[u8])> = Default::default();
serializers
.iter_mut()
.zip(names.iter())
// `unwrap` is infalible because `array.len()` equals `len` on `Chunk`
.for_each(|(iter, name)| {
let item = iter.next().unwrap();
record.push((name.as_ref(), item));
});
serialize_item(buffer, &record, format, is_first_row);
is_first_row = false;
})
}