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
231 lines (223 loc) · 8.48 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
231
use avro_schema::Schema as AvroSchema;
use crate::datatypes::{IntervalUnit, PhysicalType, PrimitiveType};
use crate::types::months_days_ns;
use crate::{array::*, datatypes::DataType};
use super::super::super::iterator::*;
use super::util;
/// A type alias for a boxed [`StreamingIterator`], used to write arrays into avro rows
/// (i.e. a column -> row transposition of types known at run-time)
pub type BoxSerializer<'a> = Box<dyn StreamingIterator<Item = [u8]> + 'a + Send + Sync>;
/// Creates a [`StreamingIterator`] trait object that presents items from `array`
/// encoded according to `schema`.
/// # Panic
/// This function panics iff the `data_type` is not supported (use [`can_serialize`] to check)
/// # Implementation
/// This function performs minimal CPU work: it dynamically dispatches based on the schema
/// and arrow type.
pub fn new_serializer<'a>(array: &'a dyn Array, schema: &AvroSchema) -> BoxSerializer<'a> {
let data_type = array.data_type().to_physical_type();
match (data_type, schema) {
(PhysicalType::Boolean, AvroSchema::Boolean) => {
let values = array.as_any().downcast_ref::<BooleanArray>().unwrap();
Box::new(BufStreamingIterator::new(
values.values_iter(),
|x, buf| {
buf.push(x as u8);
},
vec![],
))
}
(PhysicalType::Boolean, AvroSchema::Union(_)) => {
let values = array.as_any().downcast_ref::<BooleanArray>().unwrap();
Box::new(BufStreamingIterator::new(
values.iter(),
|x, buf| {
util::zigzag_encode(x.is_some() as i64, buf).unwrap();
if let Some(x) = x {
buf.push(x as u8);
}
},
vec![],
))
}
(PhysicalType::Utf8, AvroSchema::Union(_)) => {
let values = array.as_any().downcast_ref::<Utf8Array<i32>>().unwrap();
Box::new(BufStreamingIterator::new(
values.iter(),
|x, buf| {
util::zigzag_encode(x.is_some() as i64, buf).unwrap();
if let Some(x) = x {
util::zigzag_encode(x.len() as i64, buf).unwrap();
buf.extend_from_slice(x.as_bytes());
}
},
vec![],
))
}
(PhysicalType::Utf8, AvroSchema::String(_)) => {
let values = array.as_any().downcast_ref::<Utf8Array<i32>>().unwrap();
Box::new(BufStreamingIterator::new(
values.values_iter(),
|x, buf| {
util::zigzag_encode(x.len() as i64, buf).unwrap();
buf.extend_from_slice(x.as_bytes());
},
vec![],
))
}
(PhysicalType::Binary, AvroSchema::Union(_)) => {
let values = array.as_any().downcast_ref::<BinaryArray<i32>>().unwrap();
Box::new(BufStreamingIterator::new(
values.iter(),
|x, buf| {
util::zigzag_encode(x.is_some() as i64, buf).unwrap();
if let Some(x) = x {
util::zigzag_encode(x.len() as i64, buf).unwrap();
buf.extend_from_slice(x);
}
},
vec![],
))
}
(PhysicalType::Binary, AvroSchema::Bytes(_)) => {
let values = array.as_any().downcast_ref::<BinaryArray<i32>>().unwrap();
Box::new(BufStreamingIterator::new(
values.values_iter(),
|x, buf| {
util::zigzag_encode(x.len() as i64, buf).unwrap();
buf.extend_from_slice(x);
},
vec![],
))
}
(PhysicalType::Primitive(PrimitiveType::Int32), AvroSchema::Union(_)) => {
let values = array
.as_any()
.downcast_ref::<PrimitiveArray<i32>>()
.unwrap();
Box::new(BufStreamingIterator::new(
values.iter(),
|x, buf| {
util::zigzag_encode(x.is_some() as i64, buf).unwrap();
if let Some(x) = x {
util::zigzag_encode(*x as i64, buf).unwrap();
}
},
vec![],
))
}
(PhysicalType::Primitive(PrimitiveType::Int32), AvroSchema::Int(_)) => {
let values = array
.as_any()
.downcast_ref::<PrimitiveArray<i32>>()
.unwrap();
Box::new(BufStreamingIterator::new(
values.values().iter(),
|x, buf| {
util::zigzag_encode(*x as i64, buf).unwrap();
},
vec![],
))
}
(PhysicalType::Primitive(PrimitiveType::Int64), AvroSchema::Union(_)) => {
let values = array
.as_any()
.downcast_ref::<PrimitiveArray<i64>>()
.unwrap();
Box::new(BufStreamingIterator::new(
values.iter(),
|x, buf| {
util::zigzag_encode(x.is_some() as i64, buf).unwrap();
if let Some(x) = x {
util::zigzag_encode(*x, buf).unwrap();
}
},
vec![],
))
}
(PhysicalType::Primitive(PrimitiveType::Int64), AvroSchema::Long(_)) => {
let values = array
.as_any()
.downcast_ref::<PrimitiveArray<i64>>()
.unwrap();
Box::new(BufStreamingIterator::new(
values.values().iter(),
|x, buf| {
util::zigzag_encode(*x, buf).unwrap();
},
vec![],
))
}
(PhysicalType::Primitive(PrimitiveType::Float32), AvroSchema::Float) => {
let values = array
.as_any()
.downcast_ref::<PrimitiveArray<f32>>()
.unwrap();
Box::new(BufStreamingIterator::new(
values.values().iter(),
|x, buf| {
buf.extend_from_slice(&x.to_le_bytes());
},
vec![],
))
}
(PhysicalType::Primitive(PrimitiveType::Float64), AvroSchema::Double) => {
let values = array
.as_any()
.downcast_ref::<PrimitiveArray<f64>>()
.unwrap();
Box::new(BufStreamingIterator::new(
values.values().iter(),
|x, buf| {
buf.extend_from_slice(&x.to_le_bytes());
},
vec![],
))
}
(PhysicalType::Primitive(PrimitiveType::MonthDayNano), AvroSchema::Fixed(_)) => {
let values = array
.as_any()
.downcast_ref::<PrimitiveArray<months_days_ns>>()
.unwrap();
Box::new(BufStreamingIterator::new(
values.values().iter(),
interval_write,
vec![],
))
}
(PhysicalType::Primitive(PrimitiveType::MonthDayNano), AvroSchema::Union(_)) => {
let values = array
.as_any()
.downcast_ref::<PrimitiveArray<months_days_ns>>()
.unwrap();
Box::new(BufStreamingIterator::new(
values.iter(),
|x, buf| {
util::zigzag_encode(x.is_some() as i64, buf).unwrap();
if let Some(x) = x {
interval_write(x, buf)
}
},
vec![],
))
}
(a, b) => todo!("{:?} -> {:?} not supported", a, b),
}
}
/// Whether [`new_serializer`] supports `data_type`.
pub fn can_serialize(data_type: &DataType) -> bool {
use DataType::*;
matches!(
data_type,
Boolean | Int32 | Int64 | Utf8 | Binary | Interval(IntervalUnit::MonthDayNano)
)
}
#[inline]
fn interval_write(x: &months_days_ns, buf: &mut Vec<u8>) {
// https://avro.apache.org/docs/current/spec.html#Duration
// 12 bytes, months, days, millis in LE
buf.reserve(12);
buf.extend(x.months().to_le_bytes());
buf.extend(x.days().to_le_bytes());
buf.extend(((x.ns() / 1_000_000) as i32).to_le_bytes());
}