vortex_array/arrays/listview/compute/
is_constant.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::VortexResult;
5
6use crate::arrays::{ListViewArray, ListViewVTable};
7use crate::compute::{IsConstantKernel, IsConstantKernelAdapter, IsConstantOpts};
8use crate::register_kernel;
9
10impl IsConstantKernel for ListViewVTable {
11    fn is_constant(
12        &self,
13        array: &ListViewArray,
14        opts: &IsConstantOpts,
15    ) -> VortexResult<Option<bool>> {
16        // At this point, we're guaranteed:
17        // - Array has at least 2 elements
18        // - All elements are valid (no nulls)
19
20        // First check if all list sizes are constant.
21        if !array.sizes().is_constant_opts(opts.cost) {
22            return Ok(Some(false));
23        }
24
25        // If checking is too expensive, return None early.
26        if opts.is_negligible_cost() {
27            return Ok(None);
28        }
29
30        // Get the first scalar for comparison.
31        debug_assert!(
32            array.len() > 1,
33            "precondition for `is_constant` is incorrect"
34        );
35        let first_scalar = array.scalar_at(0);
36
37        // Compare all other scalars to the first.
38        for i in 1..array.len() {
39            if array.scalar_at(i) != first_scalar {
40                return Ok(Some(false));
41            }
42        }
43
44        Ok(Some(true))
45    }
46}
47
48register_kernel!(IsConstantKernelAdapter(ListViewVTable).lift());