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.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Sep 22, 2021
1 parent cdbc958 commit 682f545
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/compute/arithmetics/basic/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
array::{Array, PrimitiveArray},
compute::{
arithmetics::{ArrayCheckedDiv, ArrayDiv, NotI128},
arity::{binary, binary_checked, unary, unary_checked},
arity::{binary_checked, unary, unary_checked},
},
error::{ArrowError, Result},
types::NativeType,
Expand All @@ -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,12 @@ where
));
}

binary(lhs, rhs, lhs.data_type().clone(), |a, b| a / b)
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 682f545

Please sign in to comment.