Skip to main content

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_error::VortexResult;
6
7use crate::arrays::DecimalArray;
8use crate::arrays::DecimalVTable;
9use crate::compute::IsConstantKernel;
10use crate::compute::IsConstantKernelAdapter;
11use crate::compute::IsConstantOpts;
12use crate::match_each_decimal_value_type;
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
34    use crate::IntoArray;
35    use crate::arrays::DecimalArray;
36    use crate::compute::is_constant;
37    use crate::dtype::DecimalDType;
38    use crate::validity::Validity;
39
40    #[test]
41    fn test_is_constant() {
42        let array = DecimalArray::new(
43            buffer![0i128, 1i128, 2i128],
44            DecimalDType::new(19, 0),
45            Validity::NonNullable,
46        );
47
48        assert!(!is_constant(&array.into_array()).unwrap().unwrap());
49
50        let array = DecimalArray::new(
51            buffer![100i128, 100i128, 100i128],
52            DecimalDType::new(19, 0),
53            Validity::NonNullable,
54        );
55
56        assert!(is_constant(&array.into_array()).unwrap().unwrap());
57    }
58}