vortex_array/vtable/
validity.rs

1use vortex_error::VortexResult;
2use vortex_mask::Mask;
3
4use crate::Array;
5use crate::validity::Validity;
6use crate::vtable::VTable;
7
8pub trait ValidityVTable<V: VTable> {
9    fn is_valid(array: &V::Array, index: usize) -> VortexResult<bool>;
10
11    fn all_valid(array: &V::Array) -> VortexResult<bool>;
12
13    fn all_invalid(array: &V::Array) -> VortexResult<bool>;
14
15    /// Returns the number of valid elements in the array.
16    ///
17    /// ## Post-conditions
18    /// - The count is less than or equal to the length of the array.
19    fn valid_count(array: &V::Array) -> VortexResult<usize> {
20        Ok(Self::validity_mask(array)?.true_count())
21    }
22
23    /// Returns the number of invalid elements in the array.
24    ///
25    /// ## Post-conditions
26    /// - The count is less than or equal to the length of the array.
27    fn invalid_count(array: &V::Array) -> VortexResult<usize> {
28        Ok(Self::validity_mask(array)?.false_count())
29    }
30
31    fn validity_mask(array: &V::Array) -> VortexResult<Mask>;
32}
33
34/// An implementation of the [`ValidityVTable`] for arrays that hold validity as a child array.
35pub struct ValidityVTableFromValidityHelper;
36
37/// Expose validity held as a child array.
38pub trait ValidityHelper {
39    fn validity(&self) -> &Validity;
40}
41
42impl<V: VTable> ValidityVTable<V> for ValidityVTableFromValidityHelper
43where
44    V::Array: ValidityHelper,
45{
46    fn is_valid(array: &V::Array, index: usize) -> VortexResult<bool> {
47        array.validity().is_valid(index)
48    }
49
50    fn all_valid(array: &V::Array) -> VortexResult<bool> {
51        array.validity().all_valid()
52    }
53
54    fn all_invalid(array: &V::Array) -> VortexResult<bool> {
55        array.validity().all_invalid()
56    }
57
58    fn validity_mask(array: &V::Array) -> VortexResult<Mask> {
59        array.validity().to_mask(array.len())
60    }
61}
62
63/// An implementation of the [`ValidityVTable`] for arrays that delegate validity entirely
64/// to a child array.
65pub struct ValidityVTableFromChild;
66
67pub trait ValidityChild<V: VTable> {
68    fn validity_child(array: &V::Array) -> &dyn Array;
69}
70
71impl<V: VTable> ValidityVTable<V> for ValidityVTableFromChild
72where
73    V: ValidityChild<V>,
74{
75    fn is_valid(array: &V::Array, index: usize) -> VortexResult<bool> {
76        V::validity_child(array).is_valid(index)
77    }
78
79    fn all_valid(array: &V::Array) -> VortexResult<bool> {
80        V::validity_child(array).all_valid()
81    }
82
83    fn all_invalid(array: &V::Array) -> VortexResult<bool> {
84        V::validity_child(array).all_invalid()
85    }
86
87    fn validity_mask(array: &V::Array) -> VortexResult<Mask> {
88        V::validity_child(array).validity_mask()
89    }
90}