vortex_array/arrays/decimal/compute/
is_constant.rs

1use vortex_error::VortexResult;
2
3use crate::arrays::{DecimalArray, DecimalEncoding, NativeDecimalType};
4use crate::compute::{IsConstantFn, IsConstantOpts};
5use crate::match_each_decimal_value_type;
6
7impl IsConstantFn<&DecimalArray> for DecimalEncoding {
8    fn is_constant(
9        &self,
10        array: &DecimalArray,
11        _opts: &IsConstantOpts,
12    ) -> VortexResult<Option<bool>> {
13        let constant = match_each_decimal_value_type!(array.values_type, |$S| {
14           compute_is_constant(&array.buffer::<$S>())
15        });
16        Ok(Some(constant))
17    }
18}
19
20fn compute_is_constant<T: NativeDecimalType>(values: &[T]) -> bool {
21    // We know that the top-level `is_constant` ensures that the array is all_valid or non-null.
22    let first_value = values[0];
23
24    for &value in &values[1..] {
25        if value != first_value {
26            return false;
27        }
28    }
29
30    true
31}
32
33#[cfg(test)]
34mod tests {
35    use vortex_buffer::buffer;
36    use vortex_dtype::DecimalDType;
37
38    use crate::arrays::DecimalArray;
39    use crate::compute::is_constant;
40    use crate::validity::Validity;
41
42    #[test]
43    fn test_is_constant() {
44        let array = DecimalArray::new(
45            buffer![0i128, 1i128, 2i128],
46            DecimalDType::new(19, 0),
47            Validity::NonNullable,
48        );
49
50        assert!(!is_constant(&array).unwrap());
51
52        let array = DecimalArray::new(
53            buffer![100i128, 100i128, 100i128],
54            DecimalDType::new(19, 0),
55            Validity::NonNullable,
56        );
57
58        assert!(is_constant(&array).unwrap());
59    }
60}