vortex_array/arrays/varbin/
accessor.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::iter;
5
6use vortex_dtype::match_each_integer_ptype;
7use vortex_error::VortexResult;
8
9use crate::ToCanonical;
10use crate::accessor::ArrayAccessor;
11use crate::arrays::varbin::VarBinArray;
12use crate::validity::Validity;
13use crate::vtable::ValidityHelper;
14
15impl ArrayAccessor<[u8]> for VarBinArray {
16    fn with_iterator<F, R>(&self, f: F) -> VortexResult<R>
17    where
18        F: for<'a> FnOnce(&mut dyn Iterator<Item = Option<&'a [u8]>>) -> R,
19    {
20        let offsets = self.offsets().to_primitive()?;
21        let validity = self.validity();
22
23        let bytes = self.bytes();
24        let bytes = bytes.as_slice();
25
26        match_each_integer_ptype!(offsets.ptype(), |T| {
27            let offsets = offsets.as_slice::<T>();
28
29            #[allow(clippy::cast_possible_truncation)]
30            match validity {
31                Validity::NonNullable | Validity::AllValid => {
32                    let mut iter = offsets
33                        .windows(2)
34                        .map(|w| Some(&bytes[w[0] as usize..w[1] as usize]));
35                    Ok(f(&mut iter))
36                }
37                Validity::AllInvalid => Ok(f(&mut iter::repeat_n(None, self.len()))),
38                Validity::Array(v) => {
39                    let validity = v.to_bool()?;
40                    let mut iter = offsets
41                        .windows(2)
42                        .zip(validity.boolean_buffer())
43                        .map(|(w, valid)| valid.then(|| &bytes[w[0] as usize..w[1] as usize]));
44                    Ok(f(&mut iter))
45                }
46            }
47        })
48    }
49}