vortex_array/arrays/constant/compute/
mod.rs

1mod binary_numeric;
2mod boolean;
3mod cast;
4mod compare;
5mod invert;
6mod search_sorted;
7
8use vortex_error::VortexResult;
9use vortex_mask::Mask;
10use vortex_scalar::Scalar;
11
12use crate::arrays::ConstantEncoding;
13use crate::arrays::constant::ConstantArray;
14use crate::compute::{
15    BinaryBooleanFn, BinaryNumericFn, CastFn, CompareFn, FilterFn, InvertFn, ScalarAtFn,
16    SearchSortedFn, SliceFn, TakeFn, UncompressedSizeFn,
17};
18use crate::vtable::ComputeVTable;
19use crate::{Array, ArrayRef};
20
21impl ComputeVTable for ConstantEncoding {
22    fn binary_boolean_fn(&self) -> Option<&dyn BinaryBooleanFn<&dyn Array>> {
23        Some(self)
24    }
25
26    fn binary_numeric_fn(&self) -> Option<&dyn BinaryNumericFn<&dyn Array>> {
27        Some(self)
28    }
29
30    fn cast_fn(&self) -> Option<&dyn CastFn<&dyn Array>> {
31        Some(self)
32    }
33
34    fn compare_fn(&self) -> Option<&dyn CompareFn<&dyn Array>> {
35        Some(self)
36    }
37
38    fn filter_fn(&self) -> Option<&dyn FilterFn<&dyn Array>> {
39        Some(self)
40    }
41
42    fn invert_fn(&self) -> Option<&dyn InvertFn<&dyn Array>> {
43        Some(self)
44    }
45
46    fn scalar_at_fn(&self) -> Option<&dyn ScalarAtFn<&dyn Array>> {
47        Some(self)
48    }
49
50    fn search_sorted_fn(&self) -> Option<&dyn SearchSortedFn<&dyn Array>> {
51        Some(self)
52    }
53
54    fn slice_fn(&self) -> Option<&dyn SliceFn<&dyn Array>> {
55        Some(self)
56    }
57
58    fn take_fn(&self) -> Option<&dyn TakeFn<&dyn Array>> {
59        Some(self)
60    }
61
62    fn uncompressed_size_fn(&self) -> Option<&dyn UncompressedSizeFn<&dyn Array>> {
63        Some(self)
64    }
65}
66
67impl ScalarAtFn<&ConstantArray> for ConstantEncoding {
68    fn scalar_at(&self, array: &ConstantArray, _index: usize) -> VortexResult<Scalar> {
69        Ok(array.scalar().clone())
70    }
71}
72
73impl TakeFn<&ConstantArray> for ConstantEncoding {
74    fn take(&self, array: &ConstantArray, indices: &dyn Array) -> VortexResult<ArrayRef> {
75        Ok(ConstantArray::new(array.scalar().clone(), indices.len()).into_array())
76    }
77}
78
79impl SliceFn<&ConstantArray> for ConstantEncoding {
80    fn slice(&self, array: &ConstantArray, start: usize, stop: usize) -> VortexResult<ArrayRef> {
81        Ok(ConstantArray::new(array.scalar().clone(), stop - start).into_array())
82    }
83}
84
85impl FilterFn<&ConstantArray> for ConstantEncoding {
86    fn filter(&self, array: &ConstantArray, mask: &Mask) -> VortexResult<ArrayRef> {
87        Ok(ConstantArray::new(array.scalar().clone(), mask.true_count()).into_array())
88    }
89}
90
91impl UncompressedSizeFn<&ConstantArray> for ConstantEncoding {
92    fn uncompressed_size(&self, array: &ConstantArray) -> VortexResult<usize> {
93        let scalar = array.scalar();
94
95        let size = match scalar.as_bool_opt() {
96            Some(_) => array.len() / 8,
97            None => array.scalar().nbytes() * array.len(),
98        };
99        Ok(size)
100    }
101}
102
103#[cfg(test)]
104mod test {
105    use vortex_dtype::half::f16;
106    use vortex_scalar::Scalar;
107
108    use super::ConstantArray;
109    use crate::array::Array;
110    use crate::compute::test_harness::test_mask;
111
112    #[test]
113    fn test_mask_constant() {
114        test_mask(&ConstantArray::new(Scalar::null_typed::<i32>(), 5).into_array());
115        test_mask(&ConstantArray::new(Scalar::from(3u16), 5).into_array());
116        test_mask(&ConstantArray::new(Scalar::from(1.0f32 / 0.0f32), 5).into_array());
117        test_mask(&ConstantArray::new(Scalar::from(f16::from_f32(3.0f32)), 5).into_array());
118    }
119}