|
| 1 | +use arrow2::array::PrimitiveArray; |
| 2 | +use common_error::{DaftError, DaftResult}; |
| 3 | +use num_traits::{clamp, clamp_max, clamp_min}; |
| 4 | + |
| 5 | +use crate::{array::DataArray, datatypes::DaftNumericType, prelude::AsArrow}; |
| 6 | + |
| 7 | +impl<T> DataArray<T> |
| 8 | +where |
| 9 | + T: DaftNumericType, |
| 10 | + T::Native: PartialOrd, |
| 11 | +{ |
| 12 | + /// Clips the values in the array to the provided left and right bounds. |
| 13 | + /// |
| 14 | + /// # Arguments |
| 15 | + /// |
| 16 | + /// * `left_bound` - The lower bound for clipping. |
| 17 | + /// * `right_bound` - The upper bound for clipping. |
| 18 | + /// |
| 19 | + /// # Returns |
| 20 | + /// |
| 21 | + /// * `DaftResult<Self>` - The clipped DataArray. |
| 22 | + pub fn clip(&self, left_bound: &Self, right_bound: &Self) -> DaftResult<Self> { |
| 23 | + match (self.len(), left_bound.len(), right_bound.len()) { |
| 24 | + // Case where all arrays have the same length |
| 25 | + (array_size, lbound_size, rbound_size) |
| 26 | + if array_size == lbound_size && array_size == rbound_size => |
| 27 | + { |
| 28 | + let result = self |
| 29 | + .as_arrow() |
| 30 | + .values_iter() // Fine to use values_iter since we will apply the validity later, saves us 1 branch. |
| 31 | + .zip(left_bound.as_arrow().iter()) |
| 32 | + .zip(right_bound.as_arrow().iter()) |
| 33 | + .map(|((value, left), right)| match (left, right) { |
| 34 | + (Some(l), Some(r)) => Some(clamp(*value, *l, *r)), |
| 35 | + (Some(l), None) => Some(clamp_min(*value, *l)), |
| 36 | + (None, Some(r)) => Some(clamp_max(*value, *r)), |
| 37 | + (None, None) => Some(*value), |
| 38 | + }); |
| 39 | + let result = PrimitiveArray::<T::Native>::from_trusted_len_iter(result); |
| 40 | + let data_array = Self::from((self.name(), Box::new(result))) |
| 41 | + .with_validity(self.validity().cloned())?; |
| 42 | + Ok(data_array) |
| 43 | + } |
| 44 | + // Case where left_bound has the same length as self and right_bound has length 1 |
| 45 | + (array_size, lbound_size, 1) if array_size == lbound_size => { |
| 46 | + let right = right_bound.get(0); |
| 47 | + // We check the validity of right_bound here, since it has length 1. |
| 48 | + // This avoids a validity check in the clamp function |
| 49 | + match right { |
| 50 | + Some(r) => { |
| 51 | + // Right is valid, so we just clamp/clamp_max the values depending on the left bound |
| 52 | + let result = self |
| 53 | + .as_arrow() |
| 54 | + .values_iter() |
| 55 | + .zip(left_bound.as_arrow().iter()) |
| 56 | + .map(move |(value, left)| match left { |
| 57 | + Some(l) => Some(clamp(*value, *l, r)), |
| 58 | + None => Some(clamp_max(*value, r)), // If left is null, we can just clamp_max |
| 59 | + }); |
| 60 | + let result = PrimitiveArray::<T::Native>::from_trusted_len_iter(result); |
| 61 | + let data_array = Self::from((self.name(), Box::new(result))) |
| 62 | + .with_validity(self.validity().cloned())?; |
| 63 | + Ok(data_array) |
| 64 | + } |
| 65 | + None => { |
| 66 | + // In this case, right_bound is null, so we can just do a simple clamp_min |
| 67 | + let result = self |
| 68 | + .as_arrow() |
| 69 | + .values_iter() |
| 70 | + .zip(left_bound.as_arrow().iter()) |
| 71 | + .map(|(value, left)| match left { |
| 72 | + Some(l) => Some(clamp_min(*value, *l)), |
| 73 | + None => Some(*value), // Left null, and right null, so we just don't do anything |
| 74 | + }); |
| 75 | + let result = PrimitiveArray::<T::Native>::from_trusted_len_iter(result); |
| 76 | + let data_array = Self::from((self.name(), Box::new(result))) |
| 77 | + .with_validity(self.validity().cloned())?; |
| 78 | + Ok(data_array) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + // Case where right_bound has the same length as self and left_bound has length 1 |
| 83 | + (array_size, 1, rbound_size) if array_size == rbound_size => { |
| 84 | + let left = left_bound.get(0); |
| 85 | + match left { |
| 86 | + Some(l) => { |
| 87 | + let result = self |
| 88 | + .as_arrow() |
| 89 | + .values_iter() |
| 90 | + .zip(right_bound.as_arrow().iter()) |
| 91 | + .map(move |(value, right)| match right { |
| 92 | + Some(r) => Some(clamp(*value, l, *r)), |
| 93 | + None => Some(clamp_min(*value, l)), // Right null, so we can just clamp_min |
| 94 | + }); |
| 95 | + let result = PrimitiveArray::<T::Native>::from_trusted_len_iter(result); |
| 96 | + let data_array = Self::from((self.name(), Box::new(result))) |
| 97 | + .with_validity(self.validity().cloned())?; |
| 98 | + Ok(data_array) |
| 99 | + } |
| 100 | + None => { |
| 101 | + let result = self |
| 102 | + .as_arrow() |
| 103 | + .values_iter() |
| 104 | + .zip(right_bound.as_arrow().iter()) |
| 105 | + .map(|(value, right)| match right { |
| 106 | + Some(r) => Some(clamp_max(*value, *r)), |
| 107 | + None => Some(*value), |
| 108 | + }); |
| 109 | + let result = PrimitiveArray::<T::Native>::from_trusted_len_iter(result); |
| 110 | + let data_array = Self::from((self.name(), Box::new(result))) |
| 111 | + .with_validity(self.validity().cloned())?; |
| 112 | + Ok(data_array) |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + // Case where both left_bound and right_bound have length 1 |
| 117 | + (_, 1, 1) => { |
| 118 | + let left = left_bound.get(0); |
| 119 | + let right = right_bound.get(0); |
| 120 | + match (left, right) { |
| 121 | + (Some(l), Some(r)) => self.apply(|value| clamp(value, l, r)), |
| 122 | + (Some(l), None) => self.apply(|value| clamp_min(value, l)), |
| 123 | + (None, Some(r)) => self.apply(|value| clamp_max(value, r)), |
| 124 | + (None, None) => { |
| 125 | + // Not doing anything here, so we can just return self |
| 126 | + Ok(self.clone()) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + // Handle incompatible lengths |
| 131 | + _ => Err(DaftError::ValueError(format!( |
| 132 | + "Unable to clip incompatible length arrays: {}: {}, {}: {}, {}: {}", |
| 133 | + self.name(), |
| 134 | + self.len(), |
| 135 | + left_bound.name(), |
| 136 | + left_bound.len(), |
| 137 | + right_bound.name(), |
| 138 | + right_bound.len() |
| 139 | + ))), |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments