From 682f54520363f63aecfd629727caf3a5fd4c24e9 Mon Sep 17 00:00:00 2001 From: "Jorge C. Leitao" Date: Wed, 22 Sep 2021 16:08:20 +0000 Subject: [PATCH] Fixed panic in division using nulls. --- src/compute/arithmetics/basic/div.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/compute/arithmetics/basic/div.rs b/src/compute/arithmetics/basic/div.rs index c776292c544..b00ac47847d 100644 --- a/src/compute/arithmetics/basic/div.rs +++ b/src/compute/arithmetics/basic/div.rs @@ -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, @@ -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(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> @@ -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