jorgecarleitao commented on a change in pull request #9454:
URL: https://github.com/apache/arrow/pull/9454#discussion_r577309425
##########
File path: rust/arrow/src/compute/kernels/arithmetic.rs
##########
@@ -256,6 +256,49 @@ where
Ok(PrimitiveArray::<T>::from(Arc::new(data)))
}
+/// Scalar-divisor version of `math_divide`.
+fn math_divide_scalar<T>(
+ array: &PrimitiveArray<T>,
+ divisor: T::Native,
+) -> Result<PrimitiveArray<T>>
+where
+ T: ArrowNumericType,
+ T::Native: Div<Output = T::Native> + Zero,
+{
+ if divisor.is_zero() {
+ return Err(ArrowError::DivideByZero);
+ }
+
+ let null_bit_buffer = array.data_ref().null_buffer().cloned();
+
+ let buffer = if let Some(b) = &null_bit_buffer {
+ let values = array.values().iter().enumerate().map(|(i, value)| {
+ let is_valid = unsafe { bit_util::get_bit_raw(b.as_ptr(), i) };
+ if is_valid {
+ *value / divisor
+ } else {
+ T::default_value()
+ }
+ });
+ unsafe { Buffer::from_trusted_len_iter(values) }
+ } else {
+ // no value is null
+ let values = array.values().iter().map(|value| *value / divisor);
+ unsafe { Buffer::from_trusted_len_iter(values) }
+ };
Review comment:
fwiw, I think that this can be simplified to
```rust
let values = array.values().iter().map(|value| *value / divisor);
unsafe { Buffer::from_trusted_len_iter(values) }
```
This is because since `x / y` is infalible for `y != 0`, even with nulls the
operation can proceed without caring about then, and the null bitmap handles
them. This should further improve performance on arrays with nulls, as there is
no branching and the compiler will likely make it as performant as the
non-nulls case.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]