From c3f870fde2110852dc3f505ee7c842969482755d Mon Sep 17 00:00:00 2001 From: Connor Tsui Date: Tue, 4 Nov 2025 14:07:50 -0500 Subject: [PATCH] add immutable `validity` for mutable vectors Signed-off-by: Connor Tsui --- vortex-vector/src/binaryview/vector_mut.rs | 4 ++++ vortex-vector/src/bool/vector_mut.rs | 4 ++++ vortex-vector/src/decimal/generic_mut.rs | 4 ++++ vortex-vector/src/decimal/vector_mut.rs | 5 +++++ .../src/fixed_size_list/vector_mut.rs | 4 ++++ vortex-vector/src/null/vector_mut.rs | 21 ++++++++++++++++--- vortex-vector/src/ops.rs | 9 +++++++- vortex-vector/src/primitive/generic_mut.rs | 4 ++++ .../src/primitive/generic_mut_impl.rs | 7 ------- vortex-vector/src/primitive/vector_mut.rs | 5 +++++ vortex-vector/src/struct_/vector_mut.rs | 4 ++++ vortex-vector/src/vector_mut.rs | 5 +++++ 12 files changed, 65 insertions(+), 11 deletions(-) diff --git a/vortex-vector/src/binaryview/vector_mut.rs b/vortex-vector/src/binaryview/vector_mut.rs index df42ac8eadd..4f4424c8686 100644 --- a/vortex-vector/src/binaryview/vector_mut.rs +++ b/vortex-vector/src/binaryview/vector_mut.rs @@ -158,6 +158,10 @@ impl VectorMutOps for BinaryViewVectorMut { self.views.len() } + fn validity(&self) -> &MaskMut { + &self.validity + } + fn capacity(&self) -> usize { self.views.capacity() } diff --git a/vortex-vector/src/bool/vector_mut.rs b/vortex-vector/src/bool/vector_mut.rs index 67ab329ee74..cee04c96942 100644 --- a/vortex-vector/src/bool/vector_mut.rs +++ b/vortex-vector/src/bool/vector_mut.rs @@ -84,6 +84,10 @@ impl VectorMutOps for BoolVectorMut { self.bits.len() } + fn validity(&self) -> &MaskMut { + &self.validity + } + fn capacity(&self) -> usize { self.bits.capacity() } diff --git a/vortex-vector/src/decimal/generic_mut.rs b/vortex-vector/src/decimal/generic_mut.rs index 548cd9f60b0..2d7a894be23 100644 --- a/vortex-vector/src/decimal/generic_mut.rs +++ b/vortex-vector/src/decimal/generic_mut.rs @@ -192,6 +192,10 @@ impl VectorMutOps for DVectorMut { self.elements.len() } + fn validity(&self) -> &MaskMut { + &self.validity + } + fn capacity(&self) -> usize { self.elements.capacity() } diff --git a/vortex-vector/src/decimal/vector_mut.rs b/vortex-vector/src/decimal/vector_mut.rs index 6581aa264c7..e4a3ef5c1a0 100644 --- a/vortex-vector/src/decimal/vector_mut.rs +++ b/vortex-vector/src/decimal/vector_mut.rs @@ -7,6 +7,7 @@ use vortex_dtype::{ DecimalDType, DecimalType, DecimalTypeDowncast, DecimalTypeUpcast, NativeDecimalType, i256, }; use vortex_error::vortex_panic; +use vortex_mask::MaskMut; use crate::decimal::{DVectorMut, DecimalVector}; use crate::{VectorMutOps, match_each_dvector_mut}; @@ -63,6 +64,10 @@ impl VectorMutOps for DecimalVectorMut { match_each_dvector_mut!(self, |d| { d.len() }) } + fn validity(&self) -> &MaskMut { + match_each_dvector_mut!(self, |d| { d.validity() }) + } + fn capacity(&self) -> usize { match_each_dvector_mut!(self, |d| { d.capacity() }) } diff --git a/vortex-vector/src/fixed_size_list/vector_mut.rs b/vortex-vector/src/fixed_size_list/vector_mut.rs index 966eaf2131e..4bf2be164a3 100644 --- a/vortex-vector/src/fixed_size_list/vector_mut.rs +++ b/vortex-vector/src/fixed_size_list/vector_mut.rs @@ -172,6 +172,10 @@ impl VectorMutOps for FixedSizeListVectorMut { self.len } + fn validity(&self) -> &MaskMut { + &self.validity + } + /// In the case that `list_size == 0`, the capacity of the vector is infinite because it will /// never take up any space. fn capacity(&self) -> usize { diff --git a/vortex-vector/src/null/vector_mut.rs b/vortex-vector/src/null/vector_mut.rs index 29347974e5e..b3431063fc1 100644 --- a/vortex-vector/src/null/vector_mut.rs +++ b/vortex-vector/src/null/vector_mut.rs @@ -3,6 +3,8 @@ //! Definition and implementation of [`NullVectorMut`]. +use vortex_mask::MaskMut; + use crate::VectorMutOps; use crate::null::NullVector; @@ -12,16 +14,22 @@ use crate::null::NullVector; /// single `length` counter. /// /// The immutable equivalent of this type is [`NullVector`]. -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone)] pub struct NullVectorMut { /// The total number of nulls. pub(super) len: usize, + /// The validity mask. We only store this in order to implement the + /// [`validity()`](Self::validity) method. + pub(super) validity: MaskMut, } impl NullVectorMut { /// Creates a new mutable vector of nulls with the given length. pub fn new(len: usize) -> Self { - Self { len } + Self { + len, + validity: MaskMut::new_false(len), + } } } @@ -32,6 +40,10 @@ impl VectorMutOps for NullVectorMut { self.len } + fn validity(&self) -> &MaskMut { + &self.validity + } + fn capacity(&self) -> usize { usize::MAX } @@ -62,7 +74,10 @@ impl VectorMutOps for NullVectorMut { let new_len = self.len.saturating_sub(at); self.len = std::cmp::min(self.len, at); - NullVectorMut { len: new_len } + NullVectorMut { + len: new_len, + validity: MaskMut::new_false(new_len), + } } fn unsplit(&mut self, other: Self) { diff --git a/vortex-vector/src/ops.rs b/vortex-vector/src/ops.rs index fa253f6cb23..e6ef13671af 100644 --- a/vortex-vector/src/ops.rs +++ b/vortex-vector/src/ops.rs @@ -4,7 +4,7 @@ //! Definition and implementation of [`VectorOps`] and [`VectorMutOps`] for [`Vector`] and //! [`VectorMut`], respectively. -use vortex_mask::Mask; +use vortex_mask::{Mask, MaskMut}; use crate::{Vector, VectorMut, private}; @@ -56,6 +56,13 @@ pub trait VectorMutOps: private::Sealed + Into { self.len() == 0 } + /// Returns the validity mask of the vector, where `true` represents a _valid_ element and + /// `false` represents a `null` element. + /// + /// Note that while this returns a [`MaskMut`] (which is typically an owned type), the caller is + /// only allowed to inspect it via the shared reference. + fn validity(&self) -> &MaskMut; + /// Returns the total number of elements the vector can hold without reallocating. fn capacity(&self) -> usize; diff --git a/vortex-vector/src/primitive/generic_mut.rs b/vortex-vector/src/primitive/generic_mut.rs index 106fec06f99..dc92de5dbb7 100644 --- a/vortex-vector/src/primitive/generic_mut.rs +++ b/vortex-vector/src/primitive/generic_mut.rs @@ -86,6 +86,10 @@ impl VectorMutOps for PVectorMut { self.elements.len() } + fn validity(&self) -> &MaskMut { + &self.validity + } + fn capacity(&self) -> usize { self.elements.capacity() } diff --git a/vortex-vector/src/primitive/generic_mut_impl.rs b/vortex-vector/src/primitive/generic_mut_impl.rs index e636b1db863..ab3e9f9457a 100644 --- a/vortex-vector/src/primitive/generic_mut_impl.rs +++ b/vortex-vector/src/primitive/generic_mut_impl.rs @@ -5,7 +5,6 @@ use vortex_buffer::BufferMut; use vortex_dtype::NativePType; -use vortex_mask::MaskMut; use crate::VectorMutOps; use crate::primitive::PVectorMut; @@ -106,12 +105,6 @@ impl PVectorMut { &self.elements } - /// Returns the validity of the [`PVectorMut`]. - #[inline] - pub fn validity(&self) -> &MaskMut { - &self.validity - } - /// Resizes the `Vec` in-place so that `len` is equal to `new_len`. /// /// If `new_len` is greater than `len`, the `Vec` is extended by the difference, with each diff --git a/vortex-vector/src/primitive/vector_mut.rs b/vortex-vector/src/primitive/vector_mut.rs index ebe89fc8595..7b082894791 100644 --- a/vortex-vector/src/primitive/vector_mut.rs +++ b/vortex-vector/src/primitive/vector_mut.rs @@ -6,6 +6,7 @@ use vortex_dtype::half::f16; use vortex_dtype::{NativePType, PType, PTypeDowncast, PTypeUpcast}; use vortex_error::vortex_panic; +use vortex_mask::MaskMut; use crate::primitive::{PVectorMut, PrimitiveVector}; use crate::{VectorMutOps, match_each_pvector_mut}; @@ -87,6 +88,10 @@ impl VectorMutOps for PrimitiveVectorMut { match_each_pvector_mut!(self, |v| { v.len() }) } + fn validity(&self) -> &MaskMut { + match_each_pvector_mut!(self, |v| { v.validity() }) + } + fn capacity(&self) -> usize { match_each_pvector_mut!(self, |v| { v.capacity() }) } diff --git a/vortex-vector/src/struct_/vector_mut.rs b/vortex-vector/src/struct_/vector_mut.rs index aeacb564c94..6ca5bf0338b 100644 --- a/vortex-vector/src/struct_/vector_mut.rs +++ b/vortex-vector/src/struct_/vector_mut.rs @@ -147,6 +147,10 @@ impl VectorMutOps for StructVectorMut { self.len } + fn validity(&self) -> &MaskMut { + &self.validity + } + fn capacity(&self) -> usize { self.minimum_capacity() } diff --git a/vortex-vector/src/vector_mut.rs b/vortex-vector/src/vector_mut.rs index f5b348f48eb..8e9ac42c6fc 100644 --- a/vortex-vector/src/vector_mut.rs +++ b/vortex-vector/src/vector_mut.rs @@ -8,6 +8,7 @@ use vortex_dtype::DType; use vortex_error::vortex_panic; +use vortex_mask::MaskMut; use crate::binaryview::{BinaryVectorMut, StringVectorMut}; use crate::bool::BoolVectorMut; @@ -90,6 +91,10 @@ impl VectorMutOps for VectorMut { match_each_vector_mut!(self, |v| { v.len() }) } + fn validity(&self) -> &MaskMut { + match_each_vector_mut!(self, |v| { v.validity() }) + } + fn capacity(&self) -> usize { match_each_vector_mut!(self, |v| { v.capacity() }) }