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
mod boolean;
mod from;
mod list;
mod primitive;
mod utf8;
use crate::{
prelude::*,
utils::{get_iter_capacity, NoNull},
};
use arrow::{array::*, bitmap::Bitmap};
use std::borrow::Cow;
use std::iter::FromIterator;
use std::marker::PhantomData;
use std::sync::Arc;
pub use {boolean::*, list::*, primitive::*, utf8::*};
pub trait ChunkedBuilder<N, T> {
fn append_value(&mut self, val: N);
fn append_null(&mut self);
fn append_option(&mut self, opt_val: Option<N>) {
match opt_val {
Some(v) => self.append_value(v),
None => self.append_null(),
}
}
fn finish(self) -> ChunkedArray<T>;
fn shrink_to_fit(&mut self);
}
impl<T> FromIterator<(AlignedVec<T::Native>, Option<Bitmap>)> for ChunkedArray<T>
where
T: PolarsNumericType,
{
fn from_iter<I: IntoIterator<Item = (AlignedVec<T::Native>, Option<Bitmap>)>>(iter: I) -> Self {
let mut chunks = vec![];
for (values, opt_buffer) in iter {
chunks.push(to_array::<T>(values, opt_buffer))
}
ChunkedArray::new_from_chunks("from_iter", chunks)
}
}
pub trait NewChunkedArray<T, N> {
fn new_from_slice(name: &str, v: &[N]) -> Self;
fn new_from_opt_slice(name: &str, opt_v: &[Option<N>]) -> Self;
fn new_from_opt_iter(name: &str, it: impl Iterator<Item = Option<N>>) -> Self;
fn new_from_iter(name: &str, it: impl Iterator<Item = N>) -> Self;
}
impl<T> NewChunkedArray<T, T::Native> for ChunkedArray<T>
where
T: PolarsNumericType,
{
fn new_from_slice(name: &str, v: &[T::Native]) -> Self {
let arr = PrimitiveArray::<T::Native>::from_slice(v).to(T::get_dtype().to_arrow());
ChunkedArray::new_from_chunks(name, vec![Arc::new(arr)])
}
fn new_from_opt_slice(name: &str, opt_v: &[Option<T::Native>]) -> Self {
Self::new_from_opt_iter(name, opt_v.iter().copied())
}
fn new_from_opt_iter(
name: &str,
it: impl Iterator<Item = Option<T::Native>>,
) -> ChunkedArray<T> {
let mut builder = PrimitiveChunkedBuilder::new(name, get_iter_capacity(&it));
it.for_each(|opt| builder.append_option(opt));
builder.finish()
}
fn new_from_iter(name: &str, it: impl Iterator<Item = T::Native>) -> ChunkedArray<T> {
let ca: NoNull<ChunkedArray<_>> = it.collect();
let mut ca = ca.into_inner();
ca.rename(name);
ca
}
}
impl NewChunkedArray<BooleanType, bool> for BooleanChunked {
fn new_from_slice(name: &str, v: &[bool]) -> Self {
Self::new_from_iter(name, v.iter().copied())
}
fn new_from_opt_slice(name: &str, opt_v: &[Option<bool>]) -> Self {
Self::new_from_opt_iter(name, opt_v.iter().copied())
}
fn new_from_opt_iter(
name: &str,
it: impl Iterator<Item = Option<bool>>,
) -> ChunkedArray<BooleanType> {
let mut builder = BooleanChunkedBuilder::new(name, get_iter_capacity(&it));
it.for_each(|opt| builder.append_option(opt));
builder.finish()
}
fn new_from_iter(name: &str, it: impl Iterator<Item = bool>) -> ChunkedArray<BooleanType> {
let mut ca: ChunkedArray<_> = it.collect();
ca.rename(name);
ca
}
}
impl<S> NewChunkedArray<Utf8Type, S> for Utf8Chunked
where
S: AsRef<str>,
{
fn new_from_slice(name: &str, v: &[S]) -> Self {
let values_size = v.iter().fold(0, |acc, s| acc + s.as_ref().len());
let mut builder = MutableUtf8Array::<i64>::with_capacities(v.len(), values_size);
v.iter().for_each(|val| {
builder.push(Some(val.as_ref()));
});
let field = Arc::new(Field::new(name, DataType::Utf8));
ChunkedArray {
field,
chunks: vec![builder.into_arc()],
phantom: PhantomData,
categorical_map: None,
..Default::default()
}
}
fn new_from_opt_slice(name: &str, opt_v: &[Option<S>]) -> Self {
let values_size = opt_v.iter().fold(0, |acc, s| match s {
Some(s) => acc + s.as_ref().len(),
None => acc,
});
let mut builder = Utf8ChunkedBuilder::new(name, opt_v.len(), values_size);
opt_v.iter().for_each(|opt| match opt {
Some(v) => builder.append_value(v.as_ref()),
None => builder.append_null(),
});
builder.finish()
}
fn new_from_opt_iter(name: &str, it: impl Iterator<Item = Option<S>>) -> Self {
let cap = get_iter_capacity(&it);
let mut builder = Utf8ChunkedBuilder::new(name, cap, cap * 5);
it.for_each(|opt| builder.append_option(opt));
builder.finish()
}
fn new_from_iter(name: &str, it: impl Iterator<Item = S>) -> Self {
let cap = get_iter_capacity(&it);
let mut builder = Utf8ChunkedBuilder::new(name, cap, cap * 5);
it.for_each(|v| builder.append_value(v));
builder.finish()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_primitive_builder() {
let mut builder = PrimitiveChunkedBuilder::<UInt32Type>::new("foo", 6);
let values = &[Some(1), None, Some(2), Some(3), None, Some(4)];
for val in values {
builder.append_option(*val);
}
let ca = builder.finish();
assert_eq!(Vec::from(&ca), values);
}
#[test]
fn test_list_builder() {
let mut builder = ListPrimitiveChunkedBuilder::<Int32Type>::new("a", 10, 5);
let mut s1 = Int32Chunked::new_from_slice("a", &[1, 2, 3]).into_series();
let s2 = Int32Chunked::new_from_slice("b", &[4, 5, 6]).into_series();
s1.append(&s2).unwrap();
builder.append_series(&s1);
builder.append_series(&s2);
let ls = builder.finish();
if let AnyValue::List(s) = ls.get_any_value(0) {
assert_eq!(s.len(), 6)
} else {
panic!()
}
if let AnyValue::List(s) = ls.get_any_value(1) {
assert_eq!(s.len(), 3)
} else {
panic!()
}
let out = [&s1, &s2].iter().copied().collect::<ListChunked>();
assert_eq!(out.get(0).unwrap().len(), 6);
assert_eq!(out.get(1).unwrap().len(), 3);
let mut builder = ListPrimitiveChunkedBuilder::<Int32Type>::new("a", 10, 5);
builder.append_series(&s1);
builder.append_null();
let out = builder.finish();
let out = out.explode().unwrap();
assert_eq!(out.len(), 7);
assert_eq!(out.get(6), AnyValue::Null);
}
#[test]
fn test_list_str_builder() {
let mut builder = ListUtf8ChunkedBuilder::new("a", 10, 10);
builder.append_series(&Series::new("", &["foo", "bar"]));
let ca = builder.finish();
dbg!(ca);
}
}