vortex_array/arrays/decimal/compute/
is_constant.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use itertools::Itertools;
5use vortex_dtype::match_each_decimal_value_type;
6use vortex_error::VortexResult;
7
8use crate::arrays::DecimalArray;
9use crate::arrays::DecimalVTable;
10use crate::compute::IsConstantKernel;
11use crate::compute::IsConstantKernelAdapter;
12use crate::compute::IsConstantOpts;
13use crate::register_kernel;
14
15impl IsConstantKernel for DecimalVTable {
16    fn is_constant(
17        &self,
18        array: &DecimalArray,
19        _opts: &IsConstantOpts,
20    ) -> VortexResult<Option<bool>> {
21        let constant = match_each_decimal_value_type!(array.values_type, |S| {
22            array.buffer::<S>().iter().all_equal()
23        });
24        Ok(Some(constant))
25    }
26}
27
28register_kernel!(IsConstantKernelAdapter(DecimalVTable).lift());
29
30#[cfg(test)]
31mod tests {
32    use vortex_buffer::buffer;
33    use vortex_dtype::DecimalDType;
34
35    use crate::arrays::DecimalArray;
36    use crate::compute::is_constant;
37    use crate::validity::Validity;
38
39    #[test]
40    fn test_is_constant() {
41        let array = DecimalArray::new(
42            buffer![0i128, 1i128, 2i128],
43            DecimalDType::new(19, 0),
44            Validity::NonNullable,
45        );
46
47        assert!(!is_constant(array.as_ref()).unwrap().unwrap());
48
49        let array = DecimalArray::new(
50            buffer![100i128, 100i128, 100i128],
51            DecimalDType::new(19, 0),
52            Validity::NonNullable,
53        );
54
55        assert!(is_constant(array.as_ref()).unwrap().unwrap());
56    }
57}