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::RangeBounds;
8
9use vortex_mask::Mask;
10
11use crate::null::{NullScalar, NullVectorMut};
12use crate::{Scalar, VectorOps};
13
14/// An immutable vector of null values.
15///
16/// Since a "null" value does not require any data storage, the nulls are stored internally with a
17/// single `length` counter.
18///
19/// The mutable equivalent of this type is [`NullVectorMut`].
20#[derive(Debug, Clone)]
21pub struct NullVector {
22    /// The total number of nulls.
23    pub(super) len: usize,
24    /// The validity mask. We only store this in order to implement the
25    /// [`validity()`](Self::validity) method.
26    pub(super) validity: Mask,
27}
28
29impl NullVector {
30    /// Creates a new immutable vector of nulls with the given length.
31    pub fn new(len: usize) -> Self {
32        Self {
33            len,
34            validity: Mask::AllFalse(len),
35        }
36    }
37}
38
39impl VectorOps for NullVector {
40    type Mutable = NullVectorMut;
41
42    fn len(&self) -> usize {
43        self.len
44    }
45
46    fn validity(&self) -> &Mask {
47        &self.validity
48    }
49
50    fn scalar_at(&self, index: usize) -> Scalar {
51        assert!(index < self.len, "Index out of bounds in `NullVector`");
52        NullScalar.into()
53    }
54
55    fn slice(&self, range: impl RangeBounds<usize> + Clone + Debug) -> Self {
56        let len = crate::vector_ops::range_bounds_to_len(range, self.len());
57        Self::new(len)
58    }
59
60    fn try_into_mut(self) -> Result<NullVectorMut, Self> {
61        Ok(NullVectorMut::new(self.len))
62    }
63
64    fn into_mut(self) -> NullVectorMut {
65        NullVectorMut::new(self.len)
66    }
67}