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
use super::*;
use crate::prelude::*;
use crate::utils::get_iter_capacity;
use arrow::bitmap::{Bitmap, MutableBitmap};
use std::marker::PhantomData;
use std::sync::Arc;
pub struct ObjectChunkedBuilder<T> {
field: Field,
bitmask_builder: MutableBitmap,
values: Vec<T>,
}
impl<T> ObjectChunkedBuilder<T>
where
T: PolarsObject,
{
pub fn new(name: &str, capacity: usize) -> Self {
ObjectChunkedBuilder {
field: Field::new(name, DataType::Object(T::type_name())),
values: Vec::with_capacity(capacity),
bitmask_builder: MutableBitmap::with_capacity(capacity),
}
}
#[inline]
pub fn append_value(&mut self, v: T) {
self.values.push(v);
self.bitmask_builder.push(true);
}
#[inline]
pub fn append_null(&mut self) {
self.values.push(T::default());
self.bitmask_builder.push(false);
}
#[inline]
pub fn append_value_from_any(&mut self, v: &dyn Any) -> Result<()> {
match v.downcast_ref::<T>() {
None => Err(PolarsError::DataTypeMisMatch(
"cannot downcast any in ObjectBuilder".into(),
)),
Some(v) => {
self.append_value(v.clone());
Ok(())
}
}
}
#[inline]
pub fn append_option(&mut self, opt: Option<T>) {
match opt {
Some(s) => self.append_value(s),
None => self.append_null(),
}
}
pub fn finish(self) -> ObjectChunked<T> {
let null_bitmap: Option<Bitmap> = self.bitmask_builder.into();
let len = self.values.len();
let arr = Arc::new(ObjectArray {
values: Arc::new(self.values),
null_bitmap,
offset: 0,
len,
});
ChunkedArray {
field: Arc::new(self.field),
chunks: vec![arr],
phantom: PhantomData,
categorical_map: None,
..Default::default()
}
}
}
impl<T> Default for ObjectChunkedBuilder<T>
where
T: PolarsObject,
{
fn default() -> Self {
ObjectChunkedBuilder::new("", 0)
}
}
impl<T> NewChunkedArray<ObjectType<T>, T> for ObjectChunked<T>
where
T: PolarsObject,
{
fn new_from_slice(name: &str, v: &[T]) -> Self {
Self::new_from_iter(name, v.iter().cloned())
}
fn new_from_opt_slice(name: &str, opt_v: &[Option<T>]) -> Self {
let mut builder = ObjectChunkedBuilder::<T>::new(name, opt_v.len());
opt_v
.iter()
.cloned()
.for_each(|opt| builder.append_option(opt));
builder.finish()
}
fn new_from_opt_iter(name: &str, it: impl Iterator<Item = Option<T>>) -> ObjectChunked<T> {
let mut builder = ObjectChunkedBuilder::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>) -> ObjectChunked<T> {
let mut builder = ObjectChunkedBuilder::new(name, get_iter_capacity(&it));
it.for_each(|v| builder.append_value(v));
builder.finish()
}
}
impl<T> ObjectChunked<T>
where
T: PolarsObject,
{
pub fn new_from_vec(name: &str, v: Vec<T>) -> Self {
let field = Arc::new(Field::new(name, DataType::Object(T::type_name())));
let len = v.len();
let arr = Arc::new(ObjectArray {
values: Arc::new(v),
null_bitmap: None,
offset: 0,
len,
});
ObjectChunked {
field,
chunks: vec![arr],
phantom: PhantomData,
categorical_map: None,
..Default::default()
}
}
}