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 223
/
nested.rs
201 lines (168 loc) · 4.9 KB
/
nested.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
use std::sync::Arc;
use crate::array::*;
use crate::bitmap::*;
use crate::buffer::*;
use crate::datatypes::*;
use crate::error::*;
/// Auxiliary struct
#[derive(Debug)]
pub struct DynMutableListArray<O: Offset> {
data_type: DataType,
offsets: MutableBuffer<O>,
values: Box<dyn MutableArray>,
validity: Option<MutableBitmap>,
}
impl<O: Offset> DynMutableListArray<O> {
pub fn new_from(values: Box<dyn MutableArray>, data_type: DataType, capacity: usize) -> Self {
let mut offsets = MutableBuffer::<O>::with_capacity(capacity + 1);
offsets.push(O::default());
assert_eq!(values.len(), 0);
ListArray::<O>::get_child_field(&data_type);
Self {
data_type,
offsets,
values,
validity: None,
}
}
/// Creates a new [`MutableListArray`] from a [`MutableArray`] and capacity.
pub fn new_with_capacity(values: Box<dyn MutableArray>, capacity: usize) -> Self {
let data_type = ListArray::<O>::default_datatype(values.data_type().clone());
Self::new_from(values, data_type, capacity)
}
/// The values
pub fn mut_values(&mut self) -> &mut dyn MutableArray {
self.values.as_mut()
}
#[inline]
pub fn try_push_valid(&mut self) -> Result<()> {
let size = self.values.len();
let size = O::from_usize(size).ok_or(ArrowError::Overflow)?;
assert!(size >= *self.offsets.last().unwrap());
self.offsets.push(size);
if let Some(validity) = &mut self.validity {
validity.push(true)
}
Ok(())
}
#[inline]
fn push_null(&mut self) {
self.offsets.push(self.last_offset());
match &mut self.validity {
Some(validity) => validity.push(false),
None => self.init_validity(),
}
}
#[inline]
fn last_offset(&self) -> O {
*self.offsets.last().unwrap()
}
fn init_validity(&mut self) {
let len = self.offsets.len() - 1;
let mut validity = MutableBitmap::new();
validity.extend_constant(len, true);
validity.set(len - 1, false);
self.validity = Some(validity)
}
}
impl<O: Offset> MutableArray for DynMutableListArray<O> {
fn len(&self) -> usize {
self.offsets.len() - 1
}
fn validity(&self) -> Option<&MutableBitmap> {
self.validity.as_ref()
}
fn as_box(&mut self) -> Box<dyn Array> {
Box::new(ListArray::from_data(
self.data_type.clone(),
std::mem::take(&mut self.offsets).into(),
self.values.as_arc(),
std::mem::take(&mut self.validity).map(|x| x.into()),
))
}
fn as_arc(&mut self) -> Arc<dyn Array> {
Arc::new(ListArray::from_data(
self.data_type.clone(),
std::mem::take(&mut self.offsets).into(),
self.values.as_arc(),
std::mem::take(&mut self.validity).map(|x| x.into()),
))
}
fn data_type(&self) -> &DataType {
&self.data_type
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_mut_any(&mut self) -> &mut dyn std::any::Any {
self
}
#[inline]
fn push_null(&mut self) {
self.push_null()
}
fn shrink_to_fit(&mut self) {
todo!();
}
}
#[derive(Debug)]
pub struct FixedItemsUtf8Dictionary {
data_type: DataType,
keys: MutablePrimitiveArray<i32>,
values: Utf8Array<i32>,
}
impl FixedItemsUtf8Dictionary {
pub fn with_capacity(values: Utf8Array<i32>, capacity: usize) -> Self {
Self {
data_type: DataType::Dictionary(
IntegerType::Int32,
Box::new(values.data_type().clone()),
),
keys: MutablePrimitiveArray::<i32>::with_capacity(capacity),
values,
}
}
pub fn push_valid(&mut self, key: i32) {
self.keys.push(Some(key))
}
/// pushes a null value
pub fn push_null(&mut self) {
self.keys.push(None)
}
}
impl MutableArray for FixedItemsUtf8Dictionary {
fn len(&self) -> usize {
self.keys.len()
}
fn validity(&self) -> Option<&MutableBitmap> {
self.keys.validity()
}
fn as_box(&mut self) -> Box<dyn Array> {
Box::new(DictionaryArray::from_data(
std::mem::take(&mut self.keys).into(),
Arc::new(self.values.clone()),
))
}
fn as_arc(&mut self) -> Arc<dyn Array> {
Arc::new(DictionaryArray::from_data(
std::mem::take(&mut self.keys).into(),
Arc::new(self.values.clone()),
))
}
fn data_type(&self) -> &DataType {
&self.data_type
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_mut_any(&mut self) -> &mut dyn std::any::Any {
self
}
#[inline]
fn push_null(&mut self) {
self.push_null()
}
fn shrink_to_fit(&mut self) {
todo!();
}
}