tustvold commented on code in PR #3115:
URL: https://github.com/apache/arrow-rs/pull/3115#discussion_r1023450806
##########
arrow-array/src/array/primitive_array.rs:
##########
@@ -397,6 +397,61 @@ impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
unsafe { build_primitive_array(len, buffer, null_count, null_buffer) }
}
+ /// Applies an unary and infallible function to a mutable primitive array.
+ /// Mutable primitive array means that the buffer is not shared with other
arrays.
+ /// As a result, this mutates the buffer directly without allocating new
buffer.
+ ///
+ /// # Implementation
+ ///
+ /// This will apply the function for all values, including those on null
slots.
+ /// This implies that the operation must be infallible for any value of
the corresponding type
+ /// or this function may panic.
+ /// # Example
+ /// ```rust
+ /// # use arrow_array::{Int32Array, types::Int32Type};
+ /// # fn main() {
+ /// let array = Int32Array::from(vec![Some(5), Some(7), None]);
+ /// let c = array.unary_mut(|x| x * 2 + 1).unwrap();
+ /// assert_eq!(c, Int32Array::from(vec![Some(11), Some(15), None]));
+ /// # }
+ /// ```
+ pub fn unary_mut<F>(self, op: F) -> Result<PrimitiveArray<T>,
PrimitiveArray<T>>
+ where
+ F: Fn(T::Native) -> T::Native,
+ {
+ let data = self.data();
+ let len = self.len();
+ let null_count = self.null_count();
+ let null_buffer = data.null_buffer().map(|b|
b.bit_slice(data.offset(), len));
+
+ let buffer = self.data.buffers()[0].clone();
Review Comment:
I think we need call `slice_with_length` to apply the offset, something like
```
let element_len = std::mem::size_of::<T::Native>();
let buffer = self.data.buffers()[0].slice_with_length(data.offset() *
element_len, len * element_len)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]