Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-core/src/series/implementations/extension.rs
7884 views
1
use super::*;
2
use crate::prelude::*;
3
4
unsafe impl IntoSeries for ExtensionChunked {
5
fn into_series(self) -> Series {
6
Series(Arc::new(SeriesWrap(self)))
7
}
8
}
9
10
impl SeriesWrap<ExtensionChunked> {
11
fn apply_on_storage<F>(&self, apply: F) -> Series
12
where
13
F: Fn(&Series) -> Series,
14
{
15
apply(self.0.storage()).into_extension(self.0.extension_type().clone())
16
}
17
18
fn try_apply_on_storage<F>(&self, apply: F) -> PolarsResult<Series>
19
where
20
F: Fn(&Series) -> PolarsResult<Series>,
21
{
22
Ok(apply(self.0.storage())?.into_extension(self.0.extension_type().clone()))
23
}
24
}
25
26
impl private::PrivateSeries for SeriesWrap<ExtensionChunked> {
27
fn _field(&self) -> Cow<'_, Field> {
28
Cow::Owned(self.0.field())
29
}
30
31
fn _dtype(&self) -> &DataType {
32
self.0.dtype()
33
}
34
35
fn compute_len(&mut self) {
36
self.0.storage_mut().compute_len();
37
}
38
39
fn _get_flags(&self) -> StatisticsFlags {
40
self.0.storage().get_flags()
41
}
42
43
fn _set_flags(&mut self, flags: StatisticsFlags) {
44
self.0.storage_mut().set_flags(flags)
45
}
46
47
fn into_total_eq_inner<'a>(&'a self) -> Box<dyn TotalEqInner + 'a> {
48
self.0.storage().into_total_eq_inner()
49
}
50
51
fn into_total_ord_inner<'a>(&'a self) -> Box<dyn TotalOrdInner + 'a> {
52
self.0.storage().into_total_ord_inner()
53
}
54
55
fn vec_hash(
56
&self,
57
build_hasher: PlSeedableRandomStateQuality,
58
buf: &mut Vec<u64>,
59
) -> PolarsResult<()> {
60
self.0.storage().vec_hash(build_hasher, buf)
61
}
62
63
fn vec_hash_combine(
64
&self,
65
build_hasher: PlSeedableRandomStateQuality,
66
hashes: &mut [u64],
67
) -> PolarsResult<()> {
68
self.0.storage().vec_hash_combine(build_hasher, hashes)
69
}
70
71
fn group_tuples(&self, multithreaded: bool, sorted: bool) -> PolarsResult<GroupsType> {
72
self.0.storage().group_tuples(multithreaded, sorted)
73
}
74
75
fn zip_with_same_type(&self, mask: &BooleanChunked, other: &Series) -> PolarsResult<Series> {
76
assert!(self._dtype() == other.dtype());
77
self.try_apply_on_storage(|s| s.zip_with_same_type(mask, other.ext()?.storage()))
78
}
79
80
#[cfg(feature = "algorithm_group_by")]
81
unsafe fn agg_list(&self, groups: &GroupsType) -> Series {
82
let list = self.0.storage().agg_list(groups);
83
let mut list = list.list().unwrap().clone();
84
unsafe { list.to_logical(self.dtype().clone()) };
85
list.into_series()
86
}
87
88
fn arg_sort_multiple(
89
&self,
90
by: &[Column],
91
options: &SortMultipleOptions,
92
) -> PolarsResult<IdxCa> {
93
self.0.storage().arg_sort_multiple(by, options)
94
}
95
}
96
97
impl private::PrivateSeriesNumeric for SeriesWrap<ExtensionChunked> {
98
fn bit_repr(&self) -> Option<BitRepr> {
99
self.0.storage().bit_repr()
100
}
101
}
102
103
impl SeriesTrait for SeriesWrap<ExtensionChunked> {
104
fn rename(&mut self, name: PlSmallStr) {
105
self.0.rename(name);
106
}
107
108
fn chunk_lengths(&self) -> ChunkLenIter<'_> {
109
self.0.storage().chunk_lengths()
110
}
111
112
fn name(&self) -> &PlSmallStr {
113
self.0.name()
114
}
115
116
fn chunks(&self) -> &Vec<ArrayRef> {
117
self.0.storage().chunks()
118
}
119
120
unsafe fn chunks_mut(&mut self) -> &mut Vec<ArrayRef> {
121
self.0.storage_mut().chunks_mut()
122
}
123
124
fn slice(&self, offset: i64, length: usize) -> Series {
125
self.0
126
.storage()
127
.slice(offset, length)
128
.into_extension(self.0.extension_type().clone())
129
}
130
131
fn split_at(&self, offset: i64) -> (Series, Series) {
132
let (left, right) = self.0.storage().split_at(offset);
133
(
134
left.into_extension(self.0.extension_type().clone()),
135
right.into_extension(self.0.extension_type().clone()),
136
)
137
}
138
139
fn append(&mut self, other: &Series) -> PolarsResult<()> {
140
assert!(self.0.dtype() == other.dtype());
141
self.0.storage_mut().append(other.ext()?.storage())?;
142
Ok(())
143
}
144
145
fn append_owned(&mut self, mut other: Series) -> PolarsResult<()> {
146
assert!(self.0.dtype() == other.dtype());
147
self.0.storage_mut().append_owned(std::mem::take(
148
other
149
._get_inner_mut()
150
.as_any_mut()
151
.downcast_mut::<ExtensionChunked>()
152
.unwrap()
153
.storage_mut(),
154
))?;
155
Ok(())
156
}
157
158
fn extend(&mut self, other: &Series) -> PolarsResult<()> {
159
assert!(self.0.dtype() == other.dtype());
160
self.0.storage_mut().extend(other.ext()?.storage())?;
161
Ok(())
162
}
163
164
fn filter(&self, filter: &BooleanChunked) -> PolarsResult<Series> {
165
self.try_apply_on_storage(|s| s.filter(filter))
166
}
167
168
fn take(&self, indices: &IdxCa) -> PolarsResult<Series> {
169
self.try_apply_on_storage(|s| s.take(indices))
170
}
171
172
unsafe fn take_unchecked(&self, idx: &IdxCa) -> Series {
173
self.apply_on_storage(|s| s.take_unchecked(idx))
174
}
175
176
fn take_slice(&self, indices: &[IdxSize]) -> PolarsResult<Series> {
177
self.try_apply_on_storage(|s| s.take_slice(indices))
178
}
179
180
unsafe fn take_slice_unchecked(&self, idx: &[IdxSize]) -> Series {
181
self.apply_on_storage(|s| s.take_slice_unchecked(idx))
182
}
183
184
fn len(&self) -> usize {
185
self.0.storage().len()
186
}
187
188
fn rechunk(&self) -> Series {
189
self.apply_on_storage(|s| s.rechunk())
190
}
191
192
fn new_from_index(&self, index: usize, length: usize) -> Series {
193
self.apply_on_storage(|s| s.new_from_index(index, length))
194
}
195
196
fn deposit(&self, validity: &Bitmap) -> Series {
197
self.apply_on_storage(|s| s.deposit(validity))
198
}
199
200
fn find_validity_mismatch(&self, other: &Series, idxs: &mut Vec<IdxSize>) {
201
assert!(self.0.dtype() == other.dtype());
202
self.0
203
.storage()
204
.find_validity_mismatch(other.ext().unwrap().storage(), idxs)
205
}
206
207
fn cast(&self, dtype: &DataType, options: CastOptions) -> PolarsResult<Series> {
208
self.0.cast_with_options(dtype, options)
209
}
210
211
unsafe fn get_unchecked(&self, index: usize) -> AnyValue<'_> {
212
self.0.storage().get_unchecked(index)
213
}
214
215
fn null_count(&self) -> usize {
216
self.0.storage().null_count()
217
}
218
219
fn has_nulls(&self) -> bool {
220
self.0.storage().has_nulls()
221
}
222
223
fn is_null(&self) -> BooleanChunked {
224
self.0.storage().is_null()
225
}
226
227
fn is_not_null(&self) -> BooleanChunked {
228
self.0.storage().is_not_null()
229
}
230
231
fn reverse(&self) -> Series {
232
self.apply_on_storage(|s| s.reverse())
233
}
234
235
fn shift(&self, periods: i64) -> Series {
236
self.apply_on_storage(|s| s.shift(periods))
237
}
238
239
fn clone_inner(&self) -> Arc<dyn SeriesTrait> {
240
Arc::new(SeriesWrap(Clone::clone(&self.0)))
241
}
242
243
fn as_any(&self) -> &dyn Any {
244
&self.0
245
}
246
247
fn as_any_mut(&mut self) -> &mut dyn Any {
248
&mut self.0
249
}
250
251
fn as_phys_any(&self) -> &dyn Any {
252
self.0.storage().as_phys_any()
253
}
254
255
fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
256
self
257
}
258
259
fn field(&self) -> Cow<'_, Field> {
260
Cow::Owned(self.0.field())
261
}
262
263
fn dtype(&self) -> &DataType {
264
self.0.dtype()
265
}
266
267
fn n_chunks(&self) -> usize {
268
self.0.storage().n_chunks()
269
}
270
271
fn shrink_to_fit(&mut self) {
272
// no-op
273
}
274
275
fn trim_lists_to_normalized_offsets(&self) -> Option<Series> {
276
let trimmed = self.0.storage().trim_lists_to_normalized_offsets()?;
277
Some(trimmed.into_extension(self.0.extension_type().clone()))
278
}
279
280
fn propagate_nulls(&self) -> Option<Series> {
281
let propagated = self.0.storage().propagate_nulls()?;
282
Some(propagated.into_extension(self.0.extension_type().clone()))
283
}
284
285
fn sort_with(&self, options: SortOptions) -> PolarsResult<Series> {
286
self.try_apply_on_storage(|s| s.sort_with(options))
287
}
288
289
fn arg_sort(&self, options: SortOptions) -> IdxCa {
290
self.0.storage().arg_sort(options)
291
}
292
293
fn unique(&self) -> PolarsResult<Series> {
294
self.try_apply_on_storage(|s| s.unique())
295
}
296
297
fn n_unique(&self) -> PolarsResult<usize> {
298
self.0.storage().n_unique()
299
}
300
301
fn arg_unique(&self) -> PolarsResult<IdxCa> {
302
self.0.storage().arg_unique()
303
}
304
305
fn unique_id(&self) -> PolarsResult<(IdxSize, Vec<IdxSize>)> {
306
self.0.storage().unique_id()
307
}
308
309
fn as_single_ptr(&mut self) -> PolarsResult<usize> {
310
self.0.storage_mut().as_single_ptr()
311
}
312
313
#[cfg(feature = "approx_unique")]
314
fn approx_n_unique(&self) -> PolarsResult<IdxSize> {
315
self.0.storage().approx_n_unique()
316
}
317
}
318
319