polars_core/series/implementations/
object.rs1use std::any::Any;
2use std::borrow::Cow;
3
4use self::compare_inner::TotalOrdInner;
5use super::{BitRepr, StatisticsFlags, *};
6use crate::chunked_array::cast::CastOptions;
7use crate::chunked_array::object::PolarsObjectSafe;
8use crate::chunked_array::ops::compare_inner::{IntoTotalEqInner, TotalEqInner};
9use crate::prelude::*;
10use crate::series::implementations::SeriesWrap;
11use crate::series::private::{PrivateSeries, PrivateSeriesNumeric};
12
13impl<T: PolarsObject> PrivateSeriesNumeric for SeriesWrap<ObjectChunked<T>> {
14 fn bit_repr(&self) -> Option<BitRepr> {
15 None
16 }
17}
18
19impl<T> PrivateSeries for SeriesWrap<ObjectChunked<T>>
20where
21 T: PolarsObject,
22{
23 fn get_list_builder(
24 &self,
25 _name: PlSmallStr,
26 _values_capacity: usize,
27 _list_capacity: usize,
28 ) -> Box<dyn ListBuilderTrait> {
29 ObjectChunked::<T>::get_list_builder(_name, _values_capacity, _list_capacity)
30 }
31
32 fn compute_len(&mut self) {
33 self.0.compute_len()
34 }
35
36 fn _field(&self) -> Cow<'_, Field> {
37 Cow::Borrowed(self.0.ref_field())
38 }
39
40 fn _dtype(&self) -> &DataType {
41 self.0.dtype()
42 }
43
44 fn _set_flags(&mut self, flags: StatisticsFlags) {
45 self.0.set_flags(flags)
46 }
47 fn _get_flags(&self) -> StatisticsFlags {
48 self.0.get_flags()
49 }
50 unsafe fn agg_list(&self, groups: &GroupsType) -> Series {
51 self.0.agg_list(groups)
52 }
53
54 fn into_total_eq_inner<'a>(&'a self) -> Box<dyn TotalEqInner + 'a> {
55 (&self.0).into_total_eq_inner()
56 }
57 fn into_total_ord_inner<'a>(&'a self) -> Box<dyn TotalOrdInner + 'a> {
58 invalid_operation_panic!(into_total_ord_inner, self)
59 }
60
61 fn vec_hash(
62 &self,
63 random_state: PlSeedableRandomStateQuality,
64 buf: &mut Vec<u64>,
65 ) -> PolarsResult<()> {
66 self.0.vec_hash(random_state, buf)?;
67 Ok(())
68 }
69
70 fn vec_hash_combine(
71 &self,
72 build_hasher: PlSeedableRandomStateQuality,
73 hashes: &mut [u64],
74 ) -> PolarsResult<()> {
75 self.0.vec_hash_combine(build_hasher, hashes)?;
76 Ok(())
77 }
78
79 #[cfg(feature = "algorithm_group_by")]
80 fn group_tuples(&self, multithreaded: bool, sorted: bool) -> PolarsResult<GroupsType> {
81 IntoGroupsType::group_tuples(&self.0, multithreaded, sorted)
82 }
83 #[cfg(feature = "zip_with")]
84 fn zip_with_same_type(&self, mask: &BooleanChunked, other: &Series) -> PolarsResult<Series> {
85 self.0
86 .zip_with(mask, other.as_ref().as_ref())
87 .map(|ca| ca.into_series())
88 }
89}
90impl<T> SeriesTrait for SeriesWrap<ObjectChunked<T>>
91where
92 T: PolarsObject,
93{
94 fn rename(&mut self, name: PlSmallStr) {
95 ObjectChunked::rename(&mut self.0, name)
96 }
97
98 fn chunk_lengths(&self) -> ChunkLenIter<'_> {
99 ObjectChunked::chunk_lengths(&self.0)
100 }
101
102 fn name(&self) -> &PlSmallStr {
103 ObjectChunked::name(&self.0)
104 }
105
106 fn dtype(&self) -> &DataType {
107 ObjectChunked::dtype(&self.0)
108 }
109
110 fn chunks(&self) -> &Vec<ArrayRef> {
111 ObjectChunked::chunks(&self.0)
112 }
113 unsafe fn chunks_mut(&mut self) -> &mut Vec<ArrayRef> {
114 self.0.chunks_mut()
115 }
116
117 fn slice(&self, offset: i64, length: usize) -> Series {
118 ObjectChunked::slice(&self.0, offset, length).into_series()
119 }
120
121 fn split_at(&self, offset: i64) -> (Series, Series) {
122 let (a, b) = ObjectChunked::split_at(&self.0, offset);
123 (a.into_series(), b.into_series())
124 }
125
126 fn append(&mut self, other: &Series) -> PolarsResult<()> {
127 polars_ensure!(self.dtype() == other.dtype(), append);
128 ObjectChunked::append(&mut self.0, other.as_ref().as_ref())
129 }
130 fn append_owned(&mut self, other: Series) -> PolarsResult<()> {
131 polars_ensure!(self.dtype() == other.dtype(), append);
132 ObjectChunked::append_owned(&mut self.0, other.take_inner())
133 }
134
135 fn extend(&mut self, _other: &Series) -> PolarsResult<()> {
136 polars_bail!(opq = extend, self.dtype());
137 }
138
139 fn filter(&self, filter: &BooleanChunked) -> PolarsResult<Series> {
140 ChunkFilter::filter(&self.0, filter).map(|ca| ca.into_series())
141 }
142
143 fn take(&self, indices: &IdxCa) -> PolarsResult<Series> {
144 let ca = self.rechunk_object();
145 Ok(ca.take(indices)?.into_series())
146 }
147
148 unsafe fn take_unchecked(&self, indices: &IdxCa) -> Series {
149 let ca = self.rechunk_object();
150 ca.take_unchecked(indices).into_series()
151 }
152
153 fn take_slice(&self, indices: &[IdxSize]) -> PolarsResult<Series> {
154 Ok(self.0.take(indices)?.into_series())
155 }
156
157 unsafe fn take_slice_unchecked(&self, indices: &[IdxSize]) -> Series {
158 self.0.take_unchecked(indices).into_series()
159 }
160
161 fn deposit(&self, validity: &Bitmap) -> Series {
162 self.0.deposit(validity).into_series()
163 }
164
165 fn len(&self) -> usize {
166 ObjectChunked::len(&self.0)
167 }
168
169 fn rechunk(&self) -> Series {
170 self.rechunk_object().into_series()
172 }
173
174 fn new_from_index(&self, index: usize, length: usize) -> Series {
175 ChunkExpandAtIndex::new_from_index(&self.0, index, length).into_series()
176 }
177
178 fn cast(&self, dtype: &DataType, _cast_options: CastOptions) -> PolarsResult<Series> {
179 if matches!(dtype, DataType::Object(_)) {
180 Ok(self.0.clone().into_series())
181 } else {
182 Err(PolarsError::ComputeError(
183 "cannot cast 'Object' type".into(),
184 ))
185 }
186 }
187
188 fn get(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
189 ObjectChunked::get_any_value(&self.0, index)
190 }
191 unsafe fn get_unchecked(&self, index: usize) -> AnyValue<'_> {
192 ObjectChunked::get_any_value_unchecked(&self.0, index)
193 }
194 fn null_count(&self) -> usize {
195 ObjectChunked::null_count(&self.0)
196 }
197
198 fn has_nulls(&self) -> bool {
199 ObjectChunked::has_nulls(&self.0)
200 }
201
202 fn unique(&self) -> PolarsResult<Series> {
203 ChunkUnique::unique(&self.0).map(|ca| ca.into_series())
204 }
205
206 fn n_unique(&self) -> PolarsResult<usize> {
207 ChunkUnique::n_unique(&self.0)
208 }
209
210 fn arg_unique(&self) -> PolarsResult<IdxCa> {
211 ChunkUnique::arg_unique(&self.0)
212 }
213
214 fn unique_id(&self) -> PolarsResult<(IdxSize, Vec<IdxSize>)> {
215 polars_bail!(opq = unique_id, self.dtype());
216 }
217
218 fn is_null(&self) -> BooleanChunked {
219 ObjectChunked::is_null(&self.0)
220 }
221
222 fn is_not_null(&self) -> BooleanChunked {
223 ObjectChunked::is_not_null(&self.0)
224 }
225
226 fn reverse(&self) -> Series {
227 ChunkReverse::reverse(&self.0).into_series()
228 }
229
230 fn shift(&self, periods: i64) -> Series {
231 ChunkShift::shift(&self.0, periods).into_series()
232 }
233
234 fn clone_inner(&self) -> Arc<dyn SeriesTrait> {
235 Arc::new(SeriesWrap(Clone::clone(&self.0)))
236 }
237
238 fn get_object(&self, index: usize) -> Option<&dyn PolarsObjectSafe> {
239 ObjectChunked::<T>::get_object(&self.0, index)
240 }
241
242 unsafe fn get_object_chunked_unchecked(
243 &self,
244 chunk: usize,
245 index: usize,
246 ) -> Option<&dyn PolarsObjectSafe> {
247 ObjectChunked::<T>::get_object_chunked_unchecked(&self.0, chunk, index)
248 }
249
250 fn find_validity_mismatch(&self, other: &Series, idxs: &mut Vec<IdxSize>) {
251 self.0.find_validity_mismatch(other, idxs)
252 }
253
254 fn as_any(&self) -> &dyn Any {
255 &self.0
256 }
257
258 fn as_any_mut(&mut self) -> &mut dyn Any {
259 &mut self.0
260 }
261
262 fn as_phys_any(&self) -> &dyn Any {
263 self
264 }
265
266 fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
267 self as _
268 }
269}
270
271#[cfg(test)]
272mod test {
273 use super::*;
274
275 #[test]
276 fn test_downcast_object() -> PolarsResult<()> {
277 #[allow(non_local_definitions)]
278 impl PolarsObject for i32 {
279 fn type_name() -> &'static str {
280 "i32"
281 }
282 }
283
284 let ca = ObjectChunked::new_from_vec("a".into(), vec![0i32, 1, 2]);
285 let s = ca.into_series();
286
287 let ca = s.as_any().downcast_ref::<ObjectChunked<i32>>().unwrap();
288 assert_eq!(*ca.get(0).unwrap(), 0);
289 assert_eq!(*ca.get(1).unwrap(), 1);
290 assert_eq!(*ca.get(2).unwrap(), 2);
291
292 Ok(())
293 }
294}