jhorstmann commented on code in PR #6252:
URL: https://github.com/apache/arrow-rs/pull/6252#discussion_r1724684929
##########
arrow-array/src/array/primitive_array.rs:
##########
@@ -1016,6 +1016,32 @@ impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
PrimitiveArray::new(values, Some(nulls))
}
+ /// Applies a unary infallible function to each value in an array,
producing a
+ /// new primitive array.
+ ///
+ /// # Null Handling
+ ///
+ /// Applies the function for all values, including those on null slots.
This
+ /// will often allow the compiler to generate faster vectorized code, but
+ /// requires that the operation must be infallible (not error/panic) for
any
+ /// value of the corresponding type or this function may panic.
+ pub fn from_unary<U: ArrayAccessor, F>(left: U, mut op: F) -> Self
+ where
+ F: FnMut(U::Item) -> T::Native,
+ {
+ let nulls = left.logical_nulls();
+ let mut values: Vec<T::Native> = vec![T::Native::default();
left.len()];
+
+ for (i, val) in values.iter_mut().enumerate().take(left.len()) {
Review Comment:
Nice!
Another alternative, guaranteed without UB, would have been using
`spare_capacity_mut` and calling `set_len` only afterwards:
```rust
let mut values: Vec<T::Native> = Vec::with_capacity(left.len());
for (i, val) in
values.spare_capacity_mut()[..left.len()].iter_mut().enumerate() {
*val = op(unsafe { left.value_unchecked(i) });
}
// Safety: values have been initialized
unsafe {
values.set_len(left.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]