vortex_vector/null/
vector.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Definition and implementation of [`NullVector`].
5
6use std::fmt::Debug;
7use std::ops::BitAnd;
8use std::ops::RangeBounds;
9
10use vortex_mask::Mask;
11
12use crate::VectorOps;
13use crate::null::NullScalar;
14use crate::null::NullVectorMut;
15
16/// An immutable vector of null values.
17///
18/// Since a "null" value does not require any data storage, the nulls are stored internally with a
19/// single `length` counter.
20///
21/// The mutable equivalent of this type is [`NullVectorMut`].
22#[derive(Debug, Clone, Eq)]
23pub struct NullVector {
24    /// The total number of nulls.
25    pub(super) len: usize,
26    /// The validity mask. We only store this in order to implement the
27    /// [`validity()`](Self::validity) method.
28    pub(super) validity: Mask,
29}
30
31impl PartialEq for NullVector {
32    fn eq(&self, other: &Self) -> bool {
33        // NullVectors are equal if they have the same length.
34        // All elements are null, so there's nothing to compare at valid positions.
35        self.len == other.len
36    }
37}
38
39impl NullVector {
40    /// Creates a new immutable vector of nulls with the given length.
41    pub fn new(len: usize) -> Self {
42        Self {
43            len,
44            validity: Mask::AllFalse(len),
45        }
46    }
47}
48
49impl VectorOps for NullVector {
50    type Mutable = NullVectorMut;
51    type Scalar = NullScalar;
52
53    fn len(&self) -> usize {
54        self.len
55    }
56
57    fn validity(&self) -> &Mask {
58        &self.validity
59    }
60
61    fn mask_validity(&mut self, mask: &Mask) {
62        self.validity = self.validity.bitand(mask);
63    }
64
65    fn scalar_at(&self, index: usize) -> NullScalar {
66        assert!(index < self.len, "Index out of bounds in `NullVector`");
67        NullScalar
68    }
69
70    fn slice(&self, range: impl RangeBounds<usize> + Clone + Debug) -> Self {
71        let len = crate::vector_ops::range_bounds_to_len(range, self.len());
72        Self::new(len)
73    }
74
75    fn clear(&mut self) {
76        self.len = 0;
77        self.validity = Mask::AllFalse(0);
78    }
79
80    fn try_into_mut(self) -> Result<NullVectorMut, Self> {
81        Ok(NullVectorMut::new(self.len))
82    }
83
84    fn into_mut(self) -> NullVectorMut {
85        NullVectorMut::new(self.len)
86    }
87}