vortex_array/arrays/varbinview/compute/
is_constant.rs

1use vortex_error::{VortexExpect, VortexResult};
2
3use crate::arrays::{Ref, VarBinViewArray, VarBinViewEncoding};
4use crate::compute::{IsConstantFn, IsConstantOpts};
5
6impl IsConstantFn<&VarBinViewArray> for VarBinViewEncoding {
7    fn is_constant(
8        &self,
9        array: &VarBinViewArray,
10        _opts: &IsConstantOpts,
11    ) -> VortexResult<Option<bool>> {
12        let mut views_iter = array.views().iter();
13        let first_value = views_iter
14            .next()
15            .vortex_expect("Must have at least one value");
16
17        // For the array to be constant, all views must be of the same type
18        if first_value.is_inlined() {
19            let first_value = first_value.as_inlined();
20
21            for view in views_iter {
22                // Short circuit if the view is of the wrong type, then if both are inlined they must be equal.
23                if !view.is_inlined() || view.as_inlined() != first_value {
24                    return Ok(Some(false));
25                }
26            }
27        } else {
28            // Directly fetch the values for a `Ref`
29            let ref_bytes = |view_ref: &Ref| {
30                &array.buffer(view_ref.buffer_index() as usize).as_slice()[view_ref.to_range()]
31            };
32
33            let first_view_ref = first_value.as_view();
34            let first_value_bytes = ref_bytes(first_view_ref);
35
36            for view in views_iter {
37                // Short circuit if the view is of the wrong type
38                if view.is_inlined() || view.len() != first_value.len() {
39                    return Ok(Some(false));
40                }
41
42                let view_ref = view.as_view();
43                let value = ref_bytes(view_ref);
44                if value != first_value_bytes {
45                    return Ok(Some(false));
46                }
47            }
48        }
49
50        Ok(Some(true))
51    }
52}