Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Fixed panic in division using nulls. (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao authored Sep 22, 2021
1 parent cdbc958 commit 93c56d7
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/compute/arithmetics/basic/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ use strength_reduce::{
/// use arrow2::compute::arithmetics::basic::div::div;
/// use arrow2::array::Int32Array;
///
/// let a = Int32Array::from(&[Some(10), Some(6)]);
/// let b = Int32Array::from(&[Some(5), Some(6)]);
/// let a = Int32Array::from(&[Some(10), Some(1), Some(6)]);
/// let b = Int32Array::from(&[Some(5), None, Some(6)]);
/// let result = div(&a, &b).unwrap();
/// let expected = Int32Array::from(&[Some(2), Some(1)]);
/// let expected = Int32Array::from(&[Some(2), None, Some(1)]);
/// assert_eq!(result, expected)
/// ```
pub fn div<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> Result<PrimitiveArray<T>>
Expand All @@ -41,7 +41,21 @@ where
));
}

binary(lhs, rhs, lhs.data_type().clone(), |a, b| a / b)
if rhs.null_count() == 0 {
binary(lhs, rhs, lhs.data_type().clone(), |a, b| a / b)
} else {
if lhs.len() != rhs.len() {
return Err(ArrowError::InvalidArgumentError(
"Arrays must have the same length".to_string(),
));
}
let values = lhs.iter().zip(rhs.iter()).map(|(l, r)| match (l, r) {
(Some(l), Some(r)) => Some(*l / *r),
_ => None,
});

Ok(PrimitiveArray::from_trusted_len_iter(values).to(lhs.data_type().clone()))
}
}

/// Checked division of two primitive arrays. If the result from the division
Expand Down

0 comments on commit 93c56d7

Please sign in to comment.