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