alamb commented on code in PR #6252:
URL: https://github.com/apache/arrow-rs/pull/6252#discussion_r1725057031
##########
parquet/src/arrow/array_reader/primitive_array.rs:
##########
@@ -217,35 +217,19 @@ where
arrow_cast::cast(&a, target_type)?
}
ArrowType::Decimal128(p, s) => {
- // We can simply reuse the null buffer from `array` rather
than recomputing it
- // (as was the case when we simply used `collect` to produce
the new array).
- let nulls = array.nulls().cloned();
let array = match array.data_type() {
- ArrowType::Int32 => {
- let decimal = array
- .as_any()
- .downcast_ref::<Int32Array>()
- .unwrap()
- .iter()
- .map(|v| match v {
- Some(i) => i as i128,
- None => i128::default(),
- });
- Decimal128Array::from_iter_values_with_nulls(decimal,
nulls)
- }
-
- ArrowType::Int64 => {
- let decimal = array
- .as_any()
- .downcast_ref::<Int64Array>()
- .unwrap()
- .iter()
- .map(|v| match v {
- Some(i) => i as i128,
- None => i128::default(),
- });
- Decimal128Array::from_iter_values_with_nulls(decimal,
nulls)
- }
+ ArrowType::Int32 => array
Review Comment:
I suggest we add a comment explaining the rationale for not checking nulls
-- something like
```rust
// Apply conversion to all elements regardless of null slots as the
conversion
// to `i128` is infallible. This improves performance by avoiding a branch
in
// the inner loop
```
##########
arrow-array/src/array/primitive_array.rs:
##########
@@ -1016,6 +1016,36 @@ 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
+ ///
+ /// See [`Self::unary`] for more information on null handling.
+ ///
+ /// # Example: create an [`Int16Array`] from an [`ArrayAccessor`] with
item type `&[u8]`
+ /// ```
+ /// use arrow_array::{Array, FixedSizeBinaryArray, Int16Array};
+ /// let input_arg = vec![ vec![1, 0], vec![2, 0], vec![3, 0] ];
+ /// let arr =
FixedSizeBinaryArray::try_from_iter(input_arg.into_iter()).unwrap();
+ /// let c = Int16Array::from_unary(&arr, |x|
i16::from_le_bytes(x[..2].try_into().unwrap()));
+ /// assert_eq!(c, Int16Array::from(vec![Some(1i16), Some(2i16),
Some(3i16)]));
+ /// ```
+ pub fn from_unary<U: ArrayAccessor, F>(left: U, mut op: F) -> Self
Review Comment:
I like how this follows the model of `BooleanArray::from_unary`
https://docs.rs/arrow/latest/arrow/array/struct.BooleanArray.html#method.from_unary
--
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]