|
| 1 | +use std::cmp::Ordering; |
| 2 | + |
| 3 | +use vortex_array::Array; |
| 4 | +use vortex_array::compute::{SearchResult, SearchSortedFn, SearchSortedSide, SearchSortedUsizeFn}; |
| 5 | +use vortex_error::{VortexExpect, VortexResult}; |
| 6 | +use vortex_scalar::Scalar; |
| 7 | + |
| 8 | +use crate::{SparseArray, SparseEncoding}; |
| 9 | + |
| 10 | +impl SearchSortedFn<&SparseArray> for SparseEncoding { |
| 11 | + fn search_sorted( |
| 12 | + &self, |
| 13 | + array: &SparseArray, |
| 14 | + value: &Scalar, |
| 15 | + side: SearchSortedSide, |
| 16 | + ) -> VortexResult<SearchResult> { |
| 17 | + let min_index = array.patches().min_index()?; |
| 18 | + // For a sorted array the patches can be either at the beginning or at the end of the array |
| 19 | + if min_index == 0 { |
| 20 | + match value |
| 21 | + .partial_cmp(array.fill_scalar()) |
| 22 | + .vortex_expect("value and fill scalar must have same dtype") |
| 23 | + { |
| 24 | + Ordering::Less => array.patches().search_sorted(value.clone(), side), |
| 25 | + Ordering::Equal => Ok(SearchResult::Found(if side == SearchSortedSide::Left { |
| 26 | + // In case of patches being at the beginning we want the first index after the end of patches |
| 27 | + array.patches().indices().len() |
| 28 | + } else { |
| 29 | + array.len() |
| 30 | + })), |
| 31 | + Ordering::Greater => Ok(SearchResult::NotFound(array.len())), |
| 32 | + } |
| 33 | + } else { |
| 34 | + match value |
| 35 | + .partial_cmp(array.fill_scalar()) |
| 36 | + .vortex_expect("value and fill scalar must have same dtype") |
| 37 | + { |
| 38 | + Ordering::Less => Ok(SearchResult::NotFound(0)), |
| 39 | + Ordering::Equal => Ok(SearchResult::Found(if side == SearchSortedSide::Left { |
| 40 | + 0 |
| 41 | + } else { |
| 42 | + // Searching from right the min_index is one value after the last fill value |
| 43 | + min_index |
| 44 | + })), |
| 45 | + Ordering::Greater => array.patches().search_sorted(value.clone(), side), |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl SearchSortedUsizeFn<&SparseArray> for SparseEncoding { |
| 52 | + fn search_sorted_usize( |
| 53 | + &self, |
| 54 | + array: &SparseArray, |
| 55 | + value: usize, |
| 56 | + side: SearchSortedSide, |
| 57 | + ) -> VortexResult<SearchResult> { |
| 58 | + let Ok(target) = Scalar::from(value).cast(array.dtype()) else { |
| 59 | + // If the downcast fails, then the target is too large for the dtype. |
| 60 | + return Ok(SearchResult::NotFound(array.len())); |
| 61 | + }; |
| 62 | + SearchSortedFn::search_sorted(self, array, &target, side) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[cfg(test)] |
| 67 | +mod tests { |
| 68 | + use rstest::rstest; |
| 69 | + use vortex_array::arrays::PrimitiveArray; |
| 70 | + use vortex_array::compute::{SearchResult, SearchSortedSide, search_sorted}; |
| 71 | + use vortex_array::validity::Validity; |
| 72 | + use vortex_array::{Array, ArrayRef, IntoArray}; |
| 73 | + use vortex_buffer::buffer; |
| 74 | + use vortex_dtype::Nullability; |
| 75 | + use vortex_scalar::Scalar; |
| 76 | + |
| 77 | + use crate::SparseArray; |
| 78 | + |
| 79 | + fn sparse_high_null_fill() -> ArrayRef { |
| 80 | + SparseArray::try_new( |
| 81 | + buffer![17u64, 18, 19].into_array(), |
| 82 | + PrimitiveArray::new(buffer![33_i32, 44, 55], Validity::AllValid).into_array(), |
| 83 | + 20, |
| 84 | + Scalar::null_typed::<i32>(), |
| 85 | + ) |
| 86 | + .unwrap() |
| 87 | + .into_array() |
| 88 | + } |
| 89 | + |
| 90 | + fn sparse_high_non_null_fill() -> ArrayRef { |
| 91 | + SparseArray::try_new( |
| 92 | + buffer![17u64, 18, 19].into_array(), |
| 93 | + buffer![33_i32, 44, 55].into_array(), |
| 94 | + 20, |
| 95 | + Scalar::primitive(22, Nullability::NonNullable), |
| 96 | + ) |
| 97 | + .unwrap() |
| 98 | + .into_array() |
| 99 | + } |
| 100 | + |
| 101 | + fn sparse_low() -> ArrayRef { |
| 102 | + SparseArray::try_new( |
| 103 | + buffer![0u64, 1, 2].into_array(), |
| 104 | + buffer![33_i32, 44, 55].into_array(), |
| 105 | + 20, |
| 106 | + Scalar::primitive(60, Nullability::NonNullable), |
| 107 | + ) |
| 108 | + .unwrap() |
| 109 | + .into_array() |
| 110 | + } |
| 111 | + |
| 112 | + #[rstest] |
| 113 | + #[case(sparse_high_null_fill(), SearchResult::NotFound(20))] |
| 114 | + #[case(sparse_high_non_null_fill(), SearchResult::NotFound(20))] |
| 115 | + #[case(sparse_low(), SearchResult::NotFound(20))] |
| 116 | + fn search_larger_than_left(#[case] array: ArrayRef, #[case] expected: SearchResult) { |
| 117 | + let res = search_sorted(&array, 66, SearchSortedSide::Left).unwrap(); |
| 118 | + assert_eq!(res, expected); |
| 119 | + } |
| 120 | + |
| 121 | + #[rstest] |
| 122 | + #[case(sparse_high_null_fill(), SearchResult::NotFound(20))] |
| 123 | + #[case(sparse_high_non_null_fill(), SearchResult::NotFound(20))] |
| 124 | + #[case(sparse_low(), SearchResult::NotFound(20))] |
| 125 | + fn search_larger_than_right(#[case] array: ArrayRef, #[case] expected: SearchResult) { |
| 126 | + let res = search_sorted(&array, 66, SearchSortedSide::Right).unwrap(); |
| 127 | + assert_eq!(res, expected); |
| 128 | + } |
| 129 | + |
| 130 | + #[rstest] |
| 131 | + #[case(sparse_high_null_fill(), SearchResult::NotFound(17))] |
| 132 | + #[case(sparse_high_non_null_fill(), SearchResult::NotFound(0))] |
| 133 | + #[case(sparse_low(), SearchResult::NotFound(0))] |
| 134 | + fn search_less_than_left(#[case] array: ArrayRef, #[case] expected: SearchResult) { |
| 135 | + let res = search_sorted(&array, 21, SearchSortedSide::Left).unwrap(); |
| 136 | + assert_eq!(res, expected); |
| 137 | + } |
| 138 | + |
| 139 | + #[rstest] |
| 140 | + #[case(sparse_high_null_fill(), SearchResult::NotFound(17))] |
| 141 | + #[case(sparse_high_non_null_fill(), SearchResult::NotFound(0))] |
| 142 | + #[case(sparse_low(), SearchResult::NotFound(0))] |
| 143 | + fn search_less_than_right(#[case] array: ArrayRef, #[case] expected: SearchResult) { |
| 144 | + let res = search_sorted(&array, 21, SearchSortedSide::Right).unwrap(); |
| 145 | + assert_eq!(res, expected); |
| 146 | + } |
| 147 | + |
| 148 | + #[rstest] |
| 149 | + #[case(sparse_high_null_fill(), SearchResult::Found(18))] |
| 150 | + #[case(sparse_high_non_null_fill(), SearchResult::Found(18))] |
| 151 | + #[case(sparse_low(), SearchResult::Found(1))] |
| 152 | + fn search_patches_found_left(#[case] array: ArrayRef, #[case] expected: SearchResult) { |
| 153 | + let res = search_sorted(&array, 44, SearchSortedSide::Left).unwrap(); |
| 154 | + assert_eq!(res, expected); |
| 155 | + } |
| 156 | + |
| 157 | + #[rstest] |
| 158 | + #[case(sparse_high_null_fill(), SearchResult::Found(19))] |
| 159 | + #[case(sparse_high_non_null_fill(), SearchResult::Found(19))] |
| 160 | + #[case(sparse_low(), SearchResult::Found(2))] |
| 161 | + fn search_patches_found_right(#[case] array: ArrayRef, #[case] expected: SearchResult) { |
| 162 | + let res = search_sorted(&array, 44, SearchSortedSide::Right).unwrap(); |
| 163 | + assert_eq!(res, expected); |
| 164 | + } |
| 165 | + |
| 166 | + #[rstest] |
| 167 | + #[should_panic] |
| 168 | + #[case(sparse_high_null_fill(), Scalar::null_typed::<i32>(), SearchResult::Found(18))] |
| 169 | + #[case( |
| 170 | + sparse_high_non_null_fill(), |
| 171 | + Scalar::primitive(22, Nullability::NonNullable), |
| 172 | + SearchResult::Found(0) |
| 173 | + )] |
| 174 | + #[case( |
| 175 | + sparse_low(), |
| 176 | + Scalar::primitive(60, Nullability::NonNullable), |
| 177 | + SearchResult::Found(3) |
| 178 | + )] |
| 179 | + fn search_fill_left( |
| 180 | + #[case] array: ArrayRef, |
| 181 | + #[case] search: Scalar, |
| 182 | + #[case] expected: SearchResult, |
| 183 | + ) { |
| 184 | + let res = search_sorted(&array, search, SearchSortedSide::Left).unwrap(); |
| 185 | + assert_eq!(res, expected); |
| 186 | + } |
| 187 | + |
| 188 | + #[rstest] |
| 189 | + #[should_panic] |
| 190 | + #[case(sparse_high_null_fill(), Scalar::null_typed::<i32>(), SearchResult::Found(18))] |
| 191 | + #[case( |
| 192 | + sparse_high_non_null_fill(), |
| 193 | + Scalar::primitive(22, Nullability::NonNullable), |
| 194 | + SearchResult::Found(17) |
| 195 | + )] |
| 196 | + #[case( |
| 197 | + sparse_low(), |
| 198 | + Scalar::primitive(60, Nullability::NonNullable), |
| 199 | + SearchResult::Found(20) |
| 200 | + )] |
| 201 | + fn search_fill_right( |
| 202 | + #[case] array: ArrayRef, |
| 203 | + #[case] search: Scalar, |
| 204 | + #[case] expected: SearchResult, |
| 205 | + ) { |
| 206 | + let res = search_sorted(&array, search, SearchSortedSide::Right).unwrap(); |
| 207 | + assert_eq!(res, expected); |
| 208 | + } |
| 209 | +} |
0 commit comments