polars_core/series/implementations/
null.rs

1use std::any::Any;
2
3use polars_error::constants::LENGTH_LIMIT_MSG;
4
5use self::compare_inner::TotalOrdInner;
6use super::*;
7use crate::prelude::compare_inner::{IntoTotalEqInner, TotalEqInner};
8use crate::prelude::*;
9use crate::series::private::{PrivateSeries, PrivateSeriesNumeric};
10use crate::series::*;
11
12impl Series {
13    pub fn new_null(name: PlSmallStr, len: usize) -> Series {
14        NullChunked::new(name, len).into_series()
15    }
16}
17
18#[derive(Clone)]
19pub struct NullChunked {
20    pub(crate) name: PlSmallStr,
21    length: IdxSize,
22    // we still need chunks as many series consumers expect
23    // chunks to be there
24    chunks: Vec<ArrayRef>,
25}
26
27impl NullChunked {
28    pub(crate) fn new(name: PlSmallStr, len: usize) -> Self {
29        Self {
30            name,
31            length: len as IdxSize,
32            chunks: vec![Box::new(arrow::array::NullArray::new(
33                ArrowDataType::Null,
34                len,
35            ))],
36        }
37    }
38
39    pub fn len(&self) -> usize {
40        self.length as usize
41    }
42}
43impl PrivateSeriesNumeric for NullChunked {
44    fn bit_repr(&self) -> Option<BitRepr> {
45        Some(BitRepr::Small(UInt32Chunked::full_null(
46            self.name.clone(),
47            self.len(),
48        )))
49    }
50}
51
52impl PrivateSeries for NullChunked {
53    fn compute_len(&mut self) {
54        fn inner(chunks: &[ArrayRef]) -> usize {
55            match chunks.len() {
56                // fast path
57                1 => chunks[0].len(),
58                _ => chunks.iter().fold(0, |acc, arr| acc + arr.len()),
59            }
60        }
61        self.length = IdxSize::try_from(inner(&self.chunks)).expect(LENGTH_LIMIT_MSG);
62    }
63    fn _field(&self) -> Cow<Field> {
64        Cow::Owned(Field::new(self.name().clone(), DataType::Null))
65    }
66
67    #[allow(unused)]
68    fn _set_flags(&mut self, flags: StatisticsFlags) {}
69
70    fn _dtype(&self) -> &DataType {
71        &DataType::Null
72    }
73
74    #[cfg(feature = "zip_with")]
75    fn zip_with_same_type(&self, mask: &BooleanChunked, other: &Series) -> PolarsResult<Series> {
76        let len = match (self.len(), mask.len(), other.len()) {
77            (a, b, c) if a == b && b == c => a,
78            (1, a, b) | (a, 1, b) | (a, b, 1) if a == b => a,
79            (a, 1, 1) | (1, a, 1) | (1, 1, a) => a,
80            (_, 0, _) => 0,
81            _ => {
82                polars_bail!(ShapeMismatch: "shapes of `self`, `mask` and `other` are not suitable for `zip_with` operation")
83            },
84        };
85
86        Ok(Self::new(self.name().clone(), len).into_series())
87    }
88
89    fn into_total_eq_inner<'a>(&'a self) -> Box<dyn TotalEqInner + 'a> {
90        IntoTotalEqInner::into_total_eq_inner(self)
91    }
92    fn into_total_ord_inner<'a>(&'a self) -> Box<dyn TotalOrdInner + 'a> {
93        invalid_operation_panic!(into_total_ord_inner, self)
94    }
95
96    fn subtract(&self, _rhs: &Series) -> PolarsResult<Series> {
97        null_arithmetic(self, _rhs, "subtract")
98    }
99
100    fn add_to(&self, _rhs: &Series) -> PolarsResult<Series> {
101        null_arithmetic(self, _rhs, "add_to")
102    }
103    fn multiply(&self, _rhs: &Series) -> PolarsResult<Series> {
104        null_arithmetic(self, _rhs, "multiply")
105    }
106    fn divide(&self, _rhs: &Series) -> PolarsResult<Series> {
107        null_arithmetic(self, _rhs, "divide")
108    }
109    fn remainder(&self, _rhs: &Series) -> PolarsResult<Series> {
110        null_arithmetic(self, _rhs, "remainder")
111    }
112
113    #[cfg(feature = "algorithm_group_by")]
114    fn group_tuples(&self, _multithreaded: bool, _sorted: bool) -> PolarsResult<GroupsType> {
115        Ok(if self.is_empty() {
116            GroupsType::default()
117        } else {
118            GroupsType::Slice {
119                groups: vec![[0, self.length]],
120                rolling: false,
121            }
122        })
123    }
124
125    #[cfg(feature = "algorithm_group_by")]
126    unsafe fn agg_list(&self, groups: &GroupsType) -> Series {
127        AggList::agg_list(self, groups)
128    }
129
130    fn _get_flags(&self) -> StatisticsFlags {
131        StatisticsFlags::empty()
132    }
133
134    fn vec_hash(
135        &self,
136        random_state: PlSeedableRandomStateQuality,
137        buf: &mut Vec<u64>,
138    ) -> PolarsResult<()> {
139        VecHash::vec_hash(self, random_state, buf)?;
140        Ok(())
141    }
142
143    fn vec_hash_combine(
144        &self,
145        build_hasher: PlSeedableRandomStateQuality,
146        hashes: &mut [u64],
147    ) -> PolarsResult<()> {
148        VecHash::vec_hash_combine(self, build_hasher, hashes)?;
149        Ok(())
150    }
151}
152
153fn null_arithmetic(lhs: &NullChunked, rhs: &Series, op: &str) -> PolarsResult<Series> {
154    let output_len = match (lhs.len(), rhs.len()) {
155        (1, len_r) => len_r,
156        (len_l, 1) => len_l,
157        (len_l, len_r) if len_l == len_r => len_l,
158        _ => polars_bail!(ComputeError: "Cannot {:?} two series of different lengths.", op),
159    };
160    Ok(NullChunked::new(lhs.name().clone(), output_len).into_series())
161}
162
163impl SeriesTrait for NullChunked {
164    fn name(&self) -> &PlSmallStr {
165        &self.name
166    }
167
168    fn rename(&mut self, name: PlSmallStr) {
169        self.name = name
170    }
171
172    fn chunks(&self) -> &Vec<ArrayRef> {
173        &self.chunks
174    }
175    unsafe fn chunks_mut(&mut self) -> &mut Vec<ArrayRef> {
176        &mut self.chunks
177    }
178
179    fn chunk_lengths(&self) -> ChunkLenIter {
180        self.chunks.iter().map(|chunk| chunk.len())
181    }
182
183    fn take(&self, indices: &IdxCa) -> PolarsResult<Series> {
184        Ok(NullChunked::new(self.name.clone(), indices.len()).into_series())
185    }
186
187    unsafe fn take_unchecked(&self, indices: &IdxCa) -> Series {
188        NullChunked::new(self.name.clone(), indices.len()).into_series()
189    }
190
191    fn take_slice(&self, indices: &[IdxSize]) -> PolarsResult<Series> {
192        Ok(NullChunked::new(self.name.clone(), indices.len()).into_series())
193    }
194
195    unsafe fn take_slice_unchecked(&self, indices: &[IdxSize]) -> Series {
196        NullChunked::new(self.name.clone(), indices.len()).into_series()
197    }
198
199    fn len(&self) -> usize {
200        self.length as usize
201    }
202
203    fn has_nulls(&self) -> bool {
204        self.len() > 0
205    }
206
207    fn rechunk(&self) -> Series {
208        NullChunked::new(self.name.clone(), self.len()).into_series()
209    }
210
211    fn drop_nulls(&self) -> Series {
212        NullChunked::new(self.name.clone(), 0).into_series()
213    }
214
215    fn cast(&self, dtype: &DataType, _cast_options: CastOptions) -> PolarsResult<Series> {
216        Ok(Series::full_null(self.name.clone(), self.len(), dtype))
217    }
218
219    fn null_count(&self) -> usize {
220        self.len()
221    }
222
223    #[cfg(feature = "algorithm_group_by")]
224    fn unique(&self) -> PolarsResult<Series> {
225        let ca = NullChunked::new(self.name.clone(), self.n_unique().unwrap());
226        Ok(ca.into_series())
227    }
228
229    #[cfg(feature = "algorithm_group_by")]
230    fn n_unique(&self) -> PolarsResult<usize> {
231        let n = if self.is_empty() { 0 } else { 1 };
232        Ok(n)
233    }
234
235    #[cfg(feature = "algorithm_group_by")]
236    fn arg_unique(&self) -> PolarsResult<IdxCa> {
237        let idxs: Vec<IdxSize> = (0..self.n_unique().unwrap() as IdxSize).collect();
238        Ok(IdxCa::new(self.name().clone(), idxs))
239    }
240
241    fn new_from_index(&self, _index: usize, length: usize) -> Series {
242        NullChunked::new(self.name.clone(), length).into_series()
243    }
244
245    unsafe fn get_unchecked(&self, _index: usize) -> AnyValue {
246        AnyValue::Null
247    }
248
249    fn slice(&self, offset: i64, length: usize) -> Series {
250        let (chunks, len) = chunkops::slice(&self.chunks, offset, length, self.len());
251        NullChunked {
252            name: self.name.clone(),
253            length: len as IdxSize,
254            chunks,
255        }
256        .into_series()
257    }
258
259    fn split_at(&self, offset: i64) -> (Series, Series) {
260        let (l, r) = chunkops::split_at(self.chunks(), offset, self.len());
261        (
262            NullChunked {
263                name: self.name.clone(),
264                length: l.iter().map(|arr| arr.len() as IdxSize).sum(),
265                chunks: l,
266            }
267            .into_series(),
268            NullChunked {
269                name: self.name.clone(),
270                length: r.iter().map(|arr| arr.len() as IdxSize).sum(),
271                chunks: r,
272            }
273            .into_series(),
274        )
275    }
276
277    fn sort_with(&self, _options: SortOptions) -> PolarsResult<Series> {
278        Ok(self.clone().into_series())
279    }
280
281    fn arg_sort(&self, _options: SortOptions) -> IdxCa {
282        IdxCa::from_vec(self.name().clone(), (0..self.len() as IdxSize).collect())
283    }
284
285    fn is_null(&self) -> BooleanChunked {
286        BooleanChunked::full(self.name().clone(), true, self.len())
287    }
288
289    fn is_not_null(&self) -> BooleanChunked {
290        BooleanChunked::full(self.name().clone(), false, self.len())
291    }
292
293    fn reverse(&self) -> Series {
294        self.clone().into_series()
295    }
296
297    fn filter(&self, filter: &BooleanChunked) -> PolarsResult<Series> {
298        let len = if self.is_empty() {
299            // We still allow a length of `1` because it could be `lit(true)`.
300            polars_ensure!(filter.len() <= 1, ShapeMismatch: "filter's length: {} differs from that of the series: 0", filter.len());
301            0
302        } else if filter.len() == 1 {
303            return match filter.get(0) {
304                Some(true) => Ok(self.clone().into_series()),
305                None | Some(false) => Ok(NullChunked::new(self.name.clone(), 0).into_series()),
306            };
307        } else {
308            polars_ensure!(filter.len() == self.len(), ShapeMismatch: "filter's length: {} differs from that of the series: {}", filter.len(), self.len());
309            filter.sum().unwrap_or(0) as usize
310        };
311        Ok(NullChunked::new(self.name.clone(), len).into_series())
312    }
313
314    fn shift(&self, _periods: i64) -> Series {
315        self.clone().into_series()
316    }
317
318    fn append(&mut self, other: &Series) -> PolarsResult<()> {
319        polars_ensure!(other.dtype() == &DataType::Null, ComputeError: "expected null dtype");
320        // we don't create a new null array to keep probability of aligned chunks higher
321        self.length += other.len() as IdxSize;
322        self.chunks.extend(other.chunks().iter().cloned());
323        Ok(())
324    }
325    fn append_owned(&mut self, mut other: Series) -> PolarsResult<()> {
326        polars_ensure!(other.dtype() == &DataType::Null, ComputeError: "expected null dtype");
327        // we don't create a new null array to keep probability of aligned chunks higher
328        let other: &mut NullChunked = other._get_inner_mut().as_any_mut().downcast_mut().unwrap();
329        self.length += other.len() as IdxSize;
330        self.chunks.extend(std::mem::take(&mut other.chunks));
331        Ok(())
332    }
333
334    fn extend(&mut self, other: &Series) -> PolarsResult<()> {
335        *self = NullChunked::new(self.name.clone(), self.len() + other.len());
336        Ok(())
337    }
338
339    fn clone_inner(&self) -> Arc<dyn SeriesTrait> {
340        Arc::new(self.clone())
341    }
342
343    fn find_validity_mismatch(&self, other: &Series, idxs: &mut Vec<IdxSize>) {
344        ChunkNestingUtils::find_validity_mismatch(self, other, idxs)
345    }
346
347    fn as_any(&self) -> &dyn Any {
348        self
349    }
350
351    fn as_any_mut(&mut self) -> &mut dyn Any {
352        self
353    }
354
355    fn as_phys_any(&self) -> &dyn Any {
356        self
357    }
358
359    fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
360        self as _
361    }
362}
363
364unsafe impl IntoSeries for NullChunked {
365    fn into_series(self) -> Series
366    where
367        Self: Sized,
368    {
369        Series(Arc::new(self))
370    }
371}