Skip to main content

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