forked from rerun-io/rerun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrow_util.rs
305 lines (275 loc) · 11.5 KB
/
arrow_util.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
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
use arrow2::{
array::{
growable::make_growable, Array, FixedSizeListArray, ListArray, StructArray, UnionArray,
},
bitmap::Bitmap,
datatypes::{DataType, Field, UnionMode},
offset::Offsets,
};
use itertools::Itertools;
// ---
pub trait ArrayExt: Array {
/// Returns the length of the child array at the given index.
///
/// * Panics if `self` is not a `ListArray<i32>`.
/// * Panics if `child_nr` is out of bounds.
fn get_child_length(&self, child_nr: usize) -> usize;
/// Create a new `Array` which avoids problematic types for polars.
///
/// This does the following conversion:
/// - `FixedSizeList` -> `List`
/// - `Union` -> `Struct`
///
/// Nested types are expanded and cleaned recursively
fn clean_for_polars(&self) -> Box<dyn Array>;
}
impl ArrayExt for dyn Array {
/// Return the length of the first child.
///
/// ## Panics
///
/// Panics if `Self` is not a `ListArray<i32>`, or if the array is empty (no children).
fn get_child_length(&self, child_nr: usize) -> usize {
self.as_any()
.downcast_ref::<ListArray<i32>>()
.unwrap()
.offsets()
.lengths()
.nth(child_nr)
.unwrap()
}
/// Create a new `Array` which avoids problematic types for polars.
///
/// This does the following conversion:
/// - `FixedSizeList` -> `List`
/// - `Union` -> `Struct`
///
/// Nested types are expanded and cleaned recursively
fn clean_for_polars(&self) -> Box<dyn Array> {
let datatype = self.data_type();
let datatype = if let DataType::Extension(_, inner, _) = datatype {
(**inner).clone()
} else {
datatype.clone()
};
match &datatype {
DataType::List(field) => {
// Recursively clean the contents
let typed_arr = self.as_any().downcast_ref::<ListArray<i32>>().unwrap();
let clean_vals = typed_arr.values().as_ref().clean_for_polars();
let clean_data = DataType::List(Box::new(Field::new(
&field.name,
clean_vals.data_type().clone(),
field.is_nullable,
)));
ListArray::<i32>::try_new(
clean_data,
typed_arr.offsets().clone(),
clean_vals,
typed_arr.validity().cloned(),
)
.unwrap()
.boxed()
}
DataType::LargeList(field) => {
// Recursively clean the contents
let typed_arr = self.as_any().downcast_ref::<ListArray<i64>>().unwrap();
let clean_vals = typed_arr.values().as_ref().clean_for_polars();
let clean_data = DataType::LargeList(Box::new(Field::new(
&field.name,
clean_vals.data_type().clone(),
field.is_nullable,
)));
ListArray::<i64>::try_new(
clean_data,
typed_arr.offsets().clone(),
clean_vals,
typed_arr.validity().cloned(),
)
.unwrap()
.boxed()
}
DataType::FixedSizeList(field, len) => {
// Recursively clean the contents and convert `FixedSizeListArray` -> `ListArray`
let typed_arr = self.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
let clean_vals = typed_arr.values().as_ref().clean_for_polars();
let clean_data = DataType::List(Box::new(Field::new(
&field.name,
clean_vals.data_type().clone(),
field.is_nullable,
)));
let lengths = std::iter::repeat(len).take(typed_arr.len()).cloned();
let offsets = Offsets::try_from_lengths(lengths).unwrap();
ListArray::<i32>::try_new(
clean_data,
offsets.into(),
clean_vals,
typed_arr.validity().cloned(),
)
.unwrap()
.boxed()
}
DataType::Struct(fields) => {
// Recursively clean the contents
let typed_arr = self.as_any().downcast_ref::<StructArray>().unwrap();
let clean_vals = typed_arr
.values()
.iter()
.map(|v| v.as_ref().clean_for_polars())
.collect_vec();
let clean_fields = itertools::izip!(fields, &clean_vals)
.map(|(f, v)| Field::new(&f.name, v.data_type().clone(), f.is_nullable))
.collect_vec();
let clean_data = DataType::Struct(clean_fields);
StructArray::try_new(clean_data, clean_vals, typed_arr.validity().cloned())
.unwrap()
.boxed()
}
DataType::Union(fields, ids, UnionMode::Dense) => {
// Recursively clean the contents and convert `UnionArray` -> `StructArray`
let typed_arr = self.as_any().downcast_ref::<UnionArray>().unwrap();
// Note: Union calls its stored value-arrays "fields"
let clean_vals = typed_arr
.fields()
.iter()
.map(|v| v.as_ref().clean_for_polars())
.collect_vec();
let ids = ids
.clone()
.unwrap_or_else(|| (0i32..(clean_vals.len() as i32)).collect_vec());
// For Dense Unions, the value-arrays need to be padded to the
// correct length, which we do by growing using the existing type
// table.
let padded_vals = itertools::izip!(&clean_vals, &ids)
.map(|(dense, id)| {
let mut next = 0;
let mut grow = make_growable(&[dense.as_ref()], true, self.len());
typed_arr.types().iter().for_each(|t| {
if *t == *id as i8 {
grow.extend(0, next, 1);
next += 1;
} else {
grow.extend_validity(1);
}
});
grow.as_box()
})
.collect_vec();
let clean_field_types = itertools::izip!(fields, &clean_vals)
.map(|(f, v)| Field::new(&f.name, v.data_type().clone(), f.is_nullable))
.collect_vec();
// The new type will be a struct
let clean_data = DataType::Struct(clean_field_types);
StructArray::try_new(clean_data, padded_vals, typed_arr.validity().cloned())
.unwrap()
.boxed()
}
DataType::Union(fields, ids, UnionMode::Sparse) => {
// Recursively clean the contents and convert `UnionArray` -> `StructArray`
let typed_arr = self.as_any().downcast_ref::<UnionArray>().unwrap();
// Note: Union calls its stored value-arrays "fields"
let clean_vals = typed_arr
.fields()
.iter()
.map(|v| v.as_ref().clean_for_polars())
.collect_vec();
let ids = ids
.clone()
.unwrap_or_else(|| (0i32..(clean_vals.len() as i32)).collect_vec());
// For Sparse Unions, the value-arrays is already the right
// correct length, but should have a validity derived from the types array.
let padded_vals = itertools::izip!(&clean_vals, &ids)
.map(|(sparse, id)| {
let validity = Bitmap::from(
typed_arr
.types()
.iter()
.map(|t| *t == *id as i8)
.collect_vec(),
);
sparse.with_validity(Some(validity))
})
.collect_vec();
let clean_field_types = itertools::izip!(fields, &clean_vals)
.map(|(f, v)| Field::new(&f.name, v.data_type().clone(), f.is_nullable))
.collect_vec();
// The new type will be a struct
let clean_data = DataType::Struct(clean_field_types);
StructArray::try_new(clean_data, padded_vals, typed_arr.validity().cloned())
.unwrap()
.boxed()
}
_ => self.to_boxed(),
}
}
}
#[test]
fn test_clean_for_polars_nomodify() {
use re_components::datagen::build_some_colors;
use re_log_types::DataCell;
// Colors don't need polars cleaning
let cell: DataCell = build_some_colors(5).try_into().unwrap();
let cleaned = cell.as_arrow_ref().clean_for_polars();
assert_eq!(cell.as_arrow_ref(), &*cleaned);
}
#[cfg(test)]
mod tests {
use arrow2::datatypes::{DataType, Field, UnionMode};
use arrow2_convert::{ArrowDeserialize, ArrowField, ArrowSerialize};
use re_components::FixedSizeArrayField;
use re_log_types::DataCell;
use crate::ArrayExt;
#[derive(
Copy, Clone, Debug, Default, PartialEq, ArrowField, ArrowSerialize, ArrowDeserialize,
)]
#[arrow_field(transparent)]
pub struct Vec3D(#[arrow_field(type = "FixedSizeArrayField<f32,3>")] pub [f32; 3]);
#[derive(Clone, Copy, Debug, PartialEq, ArrowField, ArrowSerialize, ArrowDeserialize)]
#[arrow_field(type = "dense")]
enum TestComponentWithUnionAndFixedSizeList {
Bool(bool),
Vec3D(Vec3D),
}
re_log_types::component_legacy_shim!(TestComponentWithUnionAndFixedSizeList);
impl re_log_types::LegacyComponent for TestComponentWithUnionAndFixedSizeList {
fn legacy_name() -> re_log_types::ComponentName {
"test_component_with_union_and_fixed_size_list".into()
}
}
#[test]
fn test_clean_for_polars_modify() {
// Pick a type with both Unions and FixedSizeLists
let elements = vec![TestComponentWithUnionAndFixedSizeList::Bool(false)];
let cell: DataCell = elements.try_into().unwrap();
assert_eq!(
*cell.datatype(),
DataType::Union(
vec![
Field::new("Bool", DataType::Boolean, false),
Field::new(
"Vec3D",
DataType::FixedSizeList(
Box::new(Field::new("item", DataType::Float32, false)),
3
),
false
)
],
None,
UnionMode::Dense
)
);
let cleaned = cell.as_arrow_ref().clean_for_polars();
assert_eq!(
*cleaned.data_type(),
DataType::Struct(vec![
Field::new("Bool", DataType::Boolean, false),
Field::new(
"Vec3D",
DataType::List(Box::new(Field::new("item", DataType::Float32, false))),
false
)
],)
);
}
}