From 083d9559d5ff12c1a7b487d76c2beb10104d12c2 Mon Sep 17 00:00:00 2001 From: "Jorge C. Leitao" Date: Sun, 14 Nov 2021 21:26:33 +0000 Subject: [PATCH 1/3] Removed Result from arithmetics. --- src/compute/arithmetics/basic/add.rs | 58 ++++++++-------------- src/compute/arithmetics/basic/div.rs | 31 +++++------- src/compute/arithmetics/basic/mul.rs | 69 ++++++++++---------------- src/compute/arithmetics/basic/rem.rs | 24 ++++----- src/compute/arithmetics/basic/sub.rs | 66 +++++++++--------------- src/compute/arithmetics/decimal/add.rs | 27 +++++----- src/compute/arithmetics/decimal/div.rs | 27 +++++----- src/compute/arithmetics/decimal/mul.rs | 35 ++++++------- src/compute/arithmetics/decimal/sub.rs | 21 ++++---- src/compute/arithmetics/mod.rs | 48 +++++++++--------- src/compute/arithmetics/time.rs | 18 +++---- src/compute/arity.rs | 20 ++++---- src/compute/bitwise.rs | 23 ++++----- 13 files changed, 193 insertions(+), 274 deletions(-) diff --git a/src/compute/arithmetics/basic/add.rs b/src/compute/arithmetics/basic/add.rs index 7bddf42ef8e..3edf52b7f06 100644 --- a/src/compute/arithmetics/basic/add.rs +++ b/src/compute/arithmetics/basic/add.rs @@ -3,20 +3,18 @@ use std::ops::Add; use num_traits::{ops::overflowing::OverflowingAdd, CheckedAdd, SaturatingAdd, WrappingAdd, Zero}; -use crate::compute::arithmetics::basic::check_same_type; -use crate::compute::arithmetics::ArrayWrappingAdd; use crate::{ array::{Array, PrimitiveArray}, bitmap::Bitmap, compute::{ arithmetics::{ - ArrayAdd, ArrayCheckedAdd, ArrayOverflowingAdd, ArraySaturatingAdd, NativeArithmetics, + basic::check_same_type, ArrayAdd, ArrayCheckedAdd, ArrayOverflowingAdd, + ArraySaturatingAdd, ArrayWrappingAdd, NativeArithmetics, }, arity::{ binary, binary_checked, binary_with_bitmap, unary, unary_checked, unary_with_bitmap, }, }, - error::Result, types::NativeType, }; @@ -34,12 +32,10 @@ use crate::{ /// let expected = PrimitiveArray::from([None, None, None, Some(12)]); /// assert_eq!(result, expected) /// ``` -pub fn add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> +pub fn add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + Add, { - check_same_type(lhs, rhs)?; - binary(lhs, rhs, lhs.data_type().clone(), |a, b| a + b) } @@ -57,15 +53,10 @@ where /// let expected = PrimitiveArray::from([Some(-100i8), Some(-56i8), Some(100i8)]); /// assert_eq!(result, expected); /// ``` -pub fn wrapping_add( - lhs: &PrimitiveArray, - rhs: &PrimitiveArray, -) -> Result> +pub fn wrapping_add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + WrappingAdd, { - check_same_type(lhs, rhs)?; - let op = move |a: T, b: T| a.wrapping_add(&b); binary(lhs, rhs, lhs.data_type().clone(), op) @@ -85,11 +76,11 @@ where /// let expected = PrimitiveArray::from([Some(100i8), None, Some(100i8)]); /// assert_eq!(result, expected); /// ``` -pub fn checked_add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> +pub fn checked_add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + CheckedAdd, { - check_same_type(lhs, rhs)?; + check_same_type(lhs, rhs).unwrap(); let op = move |a: T, b: T| a.checked_add(&b); @@ -111,15 +102,10 @@ where /// let expected = PrimitiveArray::from([Some(127)]); /// assert_eq!(result, expected); /// ``` -pub fn saturating_add( - lhs: &PrimitiveArray, - rhs: &PrimitiveArray, -) -> Result> +pub fn saturating_add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + SaturatingAdd, { - check_same_type(lhs, rhs)?; - let op = move |a: T, b: T| a.saturating_add(&b); binary(lhs, rhs, lhs.data_type().clone(), op) @@ -144,12 +130,10 @@ where pub fn overflowing_add( lhs: &PrimitiveArray, rhs: &PrimitiveArray, -) -> Result<(PrimitiveArray, Bitmap)> +) -> (PrimitiveArray, Bitmap) where T: NativeType + OverflowingAdd, { - check_same_type(lhs, rhs)?; - let op = move |a: T, b: T| a.overflowing_add(&b); binary_with_bitmap(lhs, rhs, lhs.data_type().clone(), op) @@ -160,7 +144,7 @@ impl ArrayAdd> for PrimitiveArray where T: NativeArithmetics + Add, { - fn add(&self, rhs: &PrimitiveArray) -> Result { + fn add(&self, rhs: &PrimitiveArray) -> Self { add(self, rhs) } } @@ -169,7 +153,7 @@ impl ArrayWrappingAdd> for PrimitiveArray where T: NativeArithmetics + WrappingAdd, { - fn wrapping_add(&self, rhs: &PrimitiveArray) -> Result { + fn wrapping_add(&self, rhs: &PrimitiveArray) -> Self { wrapping_add(self, rhs) } } @@ -179,7 +163,7 @@ impl ArrayCheckedAdd> for PrimitiveArray where T: NativeArithmetics + CheckedAdd, { - fn checked_add(&self, rhs: &PrimitiveArray) -> Result { + fn checked_add(&self, rhs: &PrimitiveArray) -> Self { checked_add(self, rhs) } } @@ -189,7 +173,7 @@ impl ArraySaturatingAdd> for PrimitiveArray where T: NativeArithmetics + SaturatingAdd, { - fn saturating_add(&self, rhs: &PrimitiveArray) -> Result { + fn saturating_add(&self, rhs: &PrimitiveArray) -> Self { saturating_add(self, rhs) } } @@ -199,7 +183,7 @@ impl ArrayOverflowingAdd> for PrimitiveArray where T: NativeArithmetics + OverflowingAdd, { - fn overflowing_add(&self, rhs: &PrimitiveArray) -> Result<(Self, Bitmap)> { + fn overflowing_add(&self, rhs: &PrimitiveArray) -> (Self, Bitmap) { overflowing_add(self, rhs) } } @@ -323,8 +307,8 @@ impl ArrayAdd for PrimitiveArray where T: NativeArithmetics + Add, { - fn add(&self, rhs: &T) -> Result { - Ok(add_scalar(self, rhs)) + fn add(&self, rhs: &T) -> Self { + add_scalar(self, rhs) } } @@ -333,8 +317,8 @@ impl ArrayCheckedAdd for PrimitiveArray where T: NativeArithmetics + CheckedAdd + Zero, { - fn checked_add(&self, rhs: &T) -> Result { - Ok(checked_add_scalar(self, rhs)) + fn checked_add(&self, rhs: &T) -> Self { + checked_add_scalar(self, rhs) } } @@ -343,8 +327,8 @@ impl ArraySaturatingAdd for PrimitiveArray where T: NativeArithmetics + SaturatingAdd, { - fn saturating_add(&self, rhs: &T) -> Result { - Ok(saturating_add_scalar(self, rhs)) + fn saturating_add(&self, rhs: &T) -> Self { + saturating_add_scalar(self, rhs) } } @@ -353,7 +337,7 @@ impl ArrayOverflowingAdd for PrimitiveArray where T: NativeArithmetics + OverflowingAdd, { - fn overflowing_add(&self, rhs: &T) -> Result<(Self, Bitmap)> { - Ok(overflowing_add_scalar(self, rhs)) + fn overflowing_add(&self, rhs: &T) -> (Self, Bitmap) { + overflowing_add_scalar(self, rhs) } } diff --git a/src/compute/arithmetics/basic/div.rs b/src/compute/arithmetics/basic/div.rs index 77eb7f73e41..aa41b6b42c0 100644 --- a/src/compute/arithmetics/basic/div.rs +++ b/src/compute/arithmetics/basic/div.rs @@ -3,7 +3,7 @@ use std::ops::Div; use num_traits::{CheckedDiv, NumCast, Zero}; -use crate::compute::arithmetics::basic::{check_same_len, check_same_type}; +use crate::compute::arithmetics::basic::check_same_len; use crate::datatypes::DataType; use crate::{ array::{Array, PrimitiveArray}, @@ -11,7 +11,6 @@ use crate::{ arithmetics::{ArrayCheckedDiv, ArrayDiv, NativeArithmetics}, arity::{binary, binary_checked, unary, unary_checked}, }, - error::Result, types::NativeType, }; use strength_reduce::{ @@ -28,26 +27,24 @@ use strength_reduce::{ /// /// 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 result = div(&a, &b); /// let expected = Int32Array::from(&[Some(2), None, Some(1)]); /// assert_eq!(result, expected) /// ``` -pub fn div(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> +pub fn div(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + Div, { - check_same_type(lhs, rhs)?; - if rhs.null_count() == 0 { binary(lhs, rhs, lhs.data_type().clone(), |a, b| a / b) } else { - check_same_len(lhs, rhs)?; + check_same_len(lhs, rhs).unwrap(); 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())) + PrimitiveArray::from_trusted_len_iter(values).to(lhs.data_type().clone()) } } @@ -62,16 +59,14 @@ where /// /// let a = Int8Array::from(&[Some(-100i8), Some(10i8)]); /// let b = Int8Array::from(&[Some(100i8), Some(0i8)]); -/// let result = checked_div(&a, &b).unwrap(); +/// let result = checked_div(&a, &b); /// let expected = Int8Array::from(&[Some(-1i8), None]); /// assert_eq!(result, expected); /// ``` -pub fn checked_div(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> +pub fn checked_div(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeArithmetics + CheckedDiv, { - check_same_type(lhs, rhs)?; - let op = move |a: T, b: T| a.checked_div(&b); binary_checked(lhs, rhs, lhs.data_type().clone(), op) @@ -82,7 +77,7 @@ impl ArrayDiv> for PrimitiveArray where T: NativeArithmetics + Div, { - fn div(&self, rhs: &PrimitiveArray) -> Result { + fn div(&self, rhs: &PrimitiveArray) -> Self { div(self, rhs) } } @@ -92,7 +87,7 @@ impl ArrayCheckedDiv> for PrimitiveArray where T: NativeArithmetics + CheckedDiv, { - fn checked_div(&self, rhs: &PrimitiveArray) -> Result { + fn checked_div(&self, rhs: &PrimitiveArray) -> Self { checked_div(self, rhs) } } @@ -208,8 +203,8 @@ impl ArrayDiv for PrimitiveArray where T: NativeType + Div + NativeArithmetics + NumCast, { - fn div(&self, rhs: &T) -> Result { - Ok(div_scalar(self, rhs)) + fn div(&self, rhs: &T) -> Self { + div_scalar(self, rhs) } } @@ -218,7 +213,7 @@ impl ArrayCheckedDiv for PrimitiveArray where T: NativeType + CheckedDiv + Zero + NativeArithmetics, { - fn checked_div(&self, rhs: &T) -> Result { - Ok(checked_div_scalar(self, rhs)) + fn checked_div(&self, rhs: &T) -> Self { + checked_div_scalar(self, rhs) } } diff --git a/src/compute/arithmetics/basic/mul.rs b/src/compute/arithmetics/basic/mul.rs index 27145afe00b..ff2b4fb1dc0 100644 --- a/src/compute/arithmetics/basic/mul.rs +++ b/src/compute/arithmetics/basic/mul.rs @@ -3,20 +3,18 @@ use std::ops::Mul; use num_traits::{ops::overflowing::OverflowingMul, CheckedMul, SaturatingMul, WrappingMul}; -use crate::compute::arithmetics::basic::check_same_type; -use crate::compute::arithmetics::ArrayWrappingMul; use crate::{ array::{Array, PrimitiveArray}, bitmap::Bitmap, compute::{ arithmetics::{ - ArrayCheckedMul, ArrayMul, ArrayOverflowingMul, ArraySaturatingMul, NativeArithmetics, + ArrayCheckedMul, ArrayMul, ArrayOverflowingMul, ArraySaturatingMul, ArrayWrappingMul, + NativeArithmetics, }, arity::{ binary, binary_checked, binary_with_bitmap, unary, unary_checked, unary_with_bitmap, }, }, - error::Result, types::NativeType, }; @@ -30,16 +28,14 @@ use crate::{ /// /// let a = Int32Array::from(&[None, Some(6), None, Some(6)]); /// let b = Int32Array::from(&[Some(5), None, None, Some(6)]); -/// let result = mul(&a, &b).unwrap(); +/// let result = mul(&a, &b); /// let expected = Int32Array::from(&[None, None, None, Some(36)]); /// assert_eq!(result, expected) /// ``` -pub fn mul(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> +pub fn mul(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + Mul, { - check_same_type(lhs, rhs)?; - binary(lhs, rhs, lhs.data_type().clone(), |a, b| a * b) } @@ -53,19 +49,14 @@ where /// /// let a = PrimitiveArray::from([Some(100i8), Some(0x10i8), Some(100i8)]); /// let b = PrimitiveArray::from([Some(0i8), Some(0x10i8), Some(0i8)]); -/// let result = wrapping_mul(&a, &b).unwrap(); +/// let result = wrapping_mul(&a, &b); /// let expected = PrimitiveArray::from([Some(0), Some(0), Some(0)]); /// assert_eq!(result, expected); /// ``` -pub fn wrapping_mul( - lhs: &PrimitiveArray, - rhs: &PrimitiveArray, -) -> Result> +pub fn wrapping_mul(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + WrappingMul, { - check_same_type(lhs, rhs)?; - let op = move |a: T, b: T| a.wrapping_mul(&b); binary(lhs, rhs, lhs.data_type().clone(), op) @@ -82,16 +73,14 @@ where /// /// let a = Int8Array::from(&[Some(100i8), Some(100i8), Some(100i8)]); /// let b = Int8Array::from(&[Some(1i8), Some(100i8), Some(1i8)]); -/// let result = checked_mul(&a, &b).unwrap(); +/// let result = checked_mul(&a, &b); /// let expected = Int8Array::from(&[Some(100i8), None, Some(100i8)]); /// assert_eq!(result, expected); /// ``` -pub fn checked_mul(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> +pub fn checked_mul(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + CheckedMul, { - check_same_type(lhs, rhs)?; - let op = move |a: T, b: T| a.checked_mul(&b); binary_checked(lhs, rhs, lhs.data_type().clone(), op) @@ -108,19 +97,14 @@ where /// /// let a = Int8Array::from(&[Some(-100i8)]); /// let b = Int8Array::from(&[Some(100i8)]); -/// let result = saturating_mul(&a, &b).unwrap(); +/// let result = saturating_mul(&a, &b); /// let expected = Int8Array::from(&[Some(-128)]); /// assert_eq!(result, expected); /// ``` -pub fn saturating_mul( - lhs: &PrimitiveArray, - rhs: &PrimitiveArray, -) -> Result> +pub fn saturating_mul(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + SaturatingMul, { - check_same_type(lhs, rhs)?; - let op = move |a: T, b: T| a.saturating_mul(&b); binary(lhs, rhs, lhs.data_type().clone(), op) @@ -138,19 +122,17 @@ where /// /// let a = Int8Array::from(&[Some(1i8), Some(-100i8)]); /// let b = Int8Array::from(&[Some(1i8), Some(100i8)]); -/// let (result, overflow) = overflowing_mul(&a, &b).unwrap(); +/// let (result, overflow) = overflowing_mul(&a, &b); /// let expected = Int8Array::from(&[Some(1i8), Some(-16i8)]); /// assert_eq!(result, expected); /// ``` pub fn overflowing_mul( lhs: &PrimitiveArray, rhs: &PrimitiveArray, -) -> Result<(PrimitiveArray, Bitmap)> +) -> (PrimitiveArray, Bitmap) where T: NativeType + OverflowingMul, { - check_same_type(lhs, rhs)?; - let op = move |a: T, b: T| a.overflowing_mul(&b); binary_with_bitmap(lhs, rhs, lhs.data_type().clone(), op) @@ -161,7 +143,7 @@ impl ArrayMul> for PrimitiveArray where T: NativeArithmetics + Mul, { - fn mul(&self, rhs: &PrimitiveArray) -> Result { + fn mul(&self, rhs: &PrimitiveArray) -> Self { mul(self, rhs) } } @@ -170,7 +152,7 @@ impl ArrayWrappingMul> for PrimitiveArray where T: NativeArithmetics + WrappingMul, { - fn wrapping_mul(&self, rhs: &PrimitiveArray) -> Result { + fn wrapping_mul(&self, rhs: &PrimitiveArray) -> Self { wrapping_mul(self, rhs) } } @@ -180,7 +162,7 @@ impl ArrayCheckedMul> for PrimitiveArray where T: NativeArithmetics + CheckedMul, { - fn checked_mul(&self, rhs: &PrimitiveArray) -> Result { + fn checked_mul(&self, rhs: &PrimitiveArray) -> Self { checked_mul(self, rhs) } } @@ -190,7 +172,7 @@ impl ArraySaturatingMul> for PrimitiveArray where T: NativeArithmetics + SaturatingMul, { - fn saturating_mul(&self, rhs: &PrimitiveArray) -> Result { + fn saturating_mul(&self, rhs: &PrimitiveArray) -> Self { saturating_mul(self, rhs) } } @@ -200,10 +182,11 @@ impl ArrayOverflowingMul> for PrimitiveArray where T: NativeArithmetics + OverflowingMul, { - fn overflowing_mul(&self, rhs: &PrimitiveArray) -> Result<(Self, Bitmap)> { + fn overflowing_mul(&self, rhs: &PrimitiveArray) -> (Self, Bitmap) { overflowing_mul(self, rhs) } } + /// Multiply a scalar T to a primitive array of type T. /// Panics if the multiplication of the values overflows. /// @@ -323,8 +306,8 @@ impl ArrayMul for PrimitiveArray where T: NativeType + Mul + NativeArithmetics, { - fn mul(&self, rhs: &T) -> Result { - Ok(mul_scalar(self, rhs)) + fn mul(&self, rhs: &T) -> Self { + mul_scalar(self, rhs) } } @@ -333,8 +316,8 @@ impl ArrayCheckedMul for PrimitiveArray where T: NativeArithmetics + CheckedMul, { - fn checked_mul(&self, rhs: &T) -> Result { - Ok(checked_mul_scalar(self, rhs)) + fn checked_mul(&self, rhs: &T) -> Self { + checked_mul_scalar(self, rhs) } } @@ -343,8 +326,8 @@ impl ArraySaturatingMul for PrimitiveArray where T: NativeArithmetics + SaturatingMul, { - fn saturating_mul(&self, rhs: &T) -> Result { - Ok(saturating_mul_scalar(self, rhs)) + fn saturating_mul(&self, rhs: &T) -> Self { + saturating_mul_scalar(self, rhs) } } @@ -353,7 +336,7 @@ impl ArrayOverflowingMul for PrimitiveArray where T: NativeArithmetics + OverflowingMul, { - fn overflowing_mul(&self, rhs: &T) -> Result<(Self, Bitmap)> { - Ok(overflowing_mul_scalar(self, rhs)) + fn overflowing_mul(&self, rhs: &T) -> (Self, Bitmap) { + overflowing_mul_scalar(self, rhs) } } diff --git a/src/compute/arithmetics/basic/rem.rs b/src/compute/arithmetics/basic/rem.rs index c39fb6359d7..8210fe05ca2 100644 --- a/src/compute/arithmetics/basic/rem.rs +++ b/src/compute/arithmetics/basic/rem.rs @@ -2,7 +2,6 @@ use std::ops::Rem; use num_traits::{CheckedRem, NumCast}; -use crate::compute::arithmetics::basic::check_same_type; use crate::datatypes::DataType; use crate::{ array::{Array, PrimitiveArray}, @@ -10,7 +9,6 @@ use crate::{ arithmetics::{ArrayCheckedRem, ArrayRem, NativeArithmetics}, arity::{binary, binary_checked, unary, unary_checked}, }, - error::Result, types::NativeType, }; use strength_reduce::{ @@ -27,16 +25,14 @@ use strength_reduce::{ /// /// let a = Int32Array::from(&[Some(10), Some(7)]); /// let b = Int32Array::from(&[Some(5), Some(6)]); -/// let result = rem(&a, &b).unwrap(); +/// let result = rem(&a, &b); /// let expected = Int32Array::from(&[Some(0), Some(1)]); /// assert_eq!(result, expected) /// ``` -pub fn rem(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> +pub fn rem(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + Rem, { - check_same_type(lhs, rhs)?; - binary(lhs, rhs, lhs.data_type().clone(), |a, b| a % b) } @@ -55,12 +51,10 @@ where /// let expected = Int8Array::from(&[Some(-0i8), None]); /// assert_eq!(result, expected); /// ``` -pub fn checked_rem(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> +pub fn checked_rem(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + CheckedRem, { - check_same_type(lhs, rhs)?; - let op = move |a: T, b: T| a.checked_rem(&b); binary_checked(lhs, rhs, lhs.data_type().clone(), op) @@ -70,7 +64,7 @@ impl ArrayRem> for PrimitiveArray where T: NativeArithmetics + Rem, { - fn rem(&self, rhs: &PrimitiveArray) -> Result { + fn rem(&self, rhs: &PrimitiveArray) -> Self { rem(self, rhs) } } @@ -79,7 +73,7 @@ impl ArrayCheckedRem> for PrimitiveArray where T: NativeArithmetics + CheckedRem, { - fn checked_rem(&self, rhs: &PrimitiveArray) -> Result { + fn checked_rem(&self, rhs: &PrimitiveArray) -> Self { checked_rem(self, rhs) } } @@ -195,8 +189,8 @@ impl ArrayRem for PrimitiveArray where T: NativeArithmetics + Rem + NumCast, { - fn rem(&self, rhs: &T) -> Result { - Ok(rem_scalar(self, rhs)) + fn rem(&self, rhs: &T) -> Self { + rem_scalar(self, rhs) } } @@ -204,7 +198,7 @@ impl ArrayCheckedRem for PrimitiveArray where T: NativeArithmetics + CheckedRem, { - fn checked_rem(&self, rhs: &T) -> Result { - Ok(checked_rem_scalar(self, rhs)) + fn checked_rem(&self, rhs: &T) -> Self { + checked_rem_scalar(self, rhs) } } diff --git a/src/compute/arithmetics/basic/sub.rs b/src/compute/arithmetics/basic/sub.rs index 2982410508d..b5e5ff843a0 100644 --- a/src/compute/arithmetics/basic/sub.rs +++ b/src/compute/arithmetics/basic/sub.rs @@ -3,20 +3,18 @@ use std::ops::Sub; use num_traits::{ops::overflowing::OverflowingSub, CheckedSub, SaturatingSub, WrappingSub}; -use crate::compute::arithmetics::basic::check_same_type; -use crate::compute::arithmetics::ArrayWrappingSub; use crate::{ array::{Array, PrimitiveArray}, bitmap::Bitmap, compute::{ arithmetics::{ - ArrayCheckedSub, ArrayOverflowingSub, ArraySaturatingSub, ArraySub, NativeArithmetics, + ArrayCheckedSub, ArrayOverflowingSub, ArraySaturatingSub, ArraySub, ArrayWrappingSub, + NativeArithmetics, }, arity::{ binary, binary_checked, binary_with_bitmap, unary, unary_checked, unary_with_bitmap, }, }, - error::Result, types::NativeType, }; @@ -30,16 +28,14 @@ use crate::{ /// /// let a = Int32Array::from(&[None, Some(6), None, Some(6)]); /// let b = Int32Array::from(&[Some(5), None, None, Some(6)]); -/// let result = sub(&a, &b).unwrap(); +/// let result = sub(&a, &b); /// let expected = Int32Array::from(&[None, None, None, Some(0)]); /// assert_eq!(result, expected) /// ``` -pub fn sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> +pub fn sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + Sub, { - check_same_type(lhs, rhs)?; - binary(lhs, rhs, lhs.data_type().clone(), |a, b| a - b) } @@ -53,19 +49,14 @@ where /// /// let a = PrimitiveArray::from([Some(-100i8), Some(-100i8), Some(100i8)]); /// let b = PrimitiveArray::from([Some(0i8), Some(100i8), Some(0i8)]); -/// let result = wrapping_sub(&a, &b).unwrap(); +/// let result = wrapping_sub(&a, &b); /// let expected = PrimitiveArray::from([Some(-100i8), Some(56i8), Some(100i8)]); /// assert_eq!(result, expected); /// ``` -pub fn wrapping_sub( - lhs: &PrimitiveArray, - rhs: &PrimitiveArray, -) -> Result> +pub fn wrapping_sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + WrappingSub, { - check_same_type(lhs, rhs)?; - let op = move |a: T, b: T| a.wrapping_sub(&b); binary(lhs, rhs, lhs.data_type().clone(), op) @@ -85,12 +76,10 @@ where /// let expected = Int8Array::from(&[Some(99i8), None, Some(100i8)]); /// assert_eq!(result, expected); /// ``` -pub fn checked_sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> +pub fn checked_sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + CheckedSub, { - check_same_type(lhs, rhs)?; - let op = move |a: T, b: T| a.checked_sub(&b); binary_checked(lhs, rhs, lhs.data_type().clone(), op) @@ -107,19 +96,14 @@ where /// /// let a = Int8Array::from(&[Some(-100i8)]); /// let b = Int8Array::from(&[Some(100i8)]); -/// let result = saturating_sub(&a, &b).unwrap(); +/// let result = saturating_sub(&a, &b); /// let expected = Int8Array::from(&[Some(-128)]); /// assert_eq!(result, expected); /// ``` -pub fn saturating_sub( - lhs: &PrimitiveArray, - rhs: &PrimitiveArray, -) -> Result> +pub fn saturating_sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + SaturatingSub, { - check_same_type(lhs, rhs)?; - let op = move |a: T, b: T| a.saturating_sub(&b); binary(lhs, rhs, lhs.data_type().clone(), op) @@ -137,19 +121,17 @@ where /// /// let a = Int8Array::from(&[Some(1i8), Some(-100i8)]); /// let b = Int8Array::from(&[Some(1i8), Some(100i8)]); -/// let (result, overflow) = overflowing_sub(&a, &b).unwrap(); +/// let (result, overflow) = overflowing_sub(&a, &b); /// let expected = Int8Array::from(&[Some(0i8), Some(56i8)]); /// assert_eq!(result, expected); /// ``` pub fn overflowing_sub( lhs: &PrimitiveArray, rhs: &PrimitiveArray, -) -> Result<(PrimitiveArray, Bitmap)> +) -> (PrimitiveArray, Bitmap) where T: NativeType + OverflowingSub, { - check_same_type(lhs, rhs)?; - let op = move |a: T, b: T| a.overflowing_sub(&b); binary_with_bitmap(lhs, rhs, lhs.data_type().clone(), op) @@ -160,7 +142,7 @@ impl ArraySub> for PrimitiveArray where T: NativeArithmetics + Sub, { - fn sub(&self, rhs: &PrimitiveArray) -> Result { + fn sub(&self, rhs: &PrimitiveArray) -> Self { sub(self, rhs) } } @@ -169,7 +151,7 @@ impl ArrayWrappingSub> for PrimitiveArray where T: NativeArithmetics + WrappingSub, { - fn wrapping_sub(&self, rhs: &PrimitiveArray) -> Result { + fn wrapping_sub(&self, rhs: &PrimitiveArray) -> Self { wrapping_sub(self, rhs) } } @@ -179,7 +161,7 @@ impl ArrayCheckedSub> for PrimitiveArray where T: NativeArithmetics + CheckedSub, { - fn checked_sub(&self, rhs: &PrimitiveArray) -> Result { + fn checked_sub(&self, rhs: &PrimitiveArray) -> Self { checked_sub(self, rhs) } } @@ -189,7 +171,7 @@ impl ArraySaturatingSub> for PrimitiveArray where T: NativeArithmetics + SaturatingSub, { - fn saturating_sub(&self, rhs: &PrimitiveArray) -> Result { + fn saturating_sub(&self, rhs: &PrimitiveArray) -> Self { saturating_sub(self, rhs) } } @@ -199,7 +181,7 @@ impl ArrayOverflowingSub> for PrimitiveArray where T: NativeArithmetics + OverflowingSub, { - fn overflowing_sub(&self, rhs: &PrimitiveArray) -> Result<(Self, Bitmap)> { + fn overflowing_sub(&self, rhs: &PrimitiveArray) -> (Self, Bitmap) { overflowing_sub(self, rhs) } } @@ -323,8 +305,8 @@ impl ArraySub for PrimitiveArray where T: NativeArithmetics + Sub, { - fn sub(&self, rhs: &T) -> Result { - Ok(sub_scalar(self, rhs)) + fn sub(&self, rhs: &T) -> Self { + sub_scalar(self, rhs) } } @@ -333,8 +315,8 @@ impl ArrayCheckedSub for PrimitiveArray where T: NativeArithmetics + CheckedSub, { - fn checked_sub(&self, rhs: &T) -> Result { - Ok(checked_sub_scalar(self, rhs)) + fn checked_sub(&self, rhs: &T) -> Self { + checked_sub_scalar(self, rhs) } } @@ -343,8 +325,8 @@ impl ArraySaturatingSub for PrimitiveArray where T: NativeArithmetics + SaturatingSub, { - fn saturating_sub(&self, rhs: &T) -> Result { - Ok(saturating_sub_scalar(self, rhs)) + fn saturating_sub(&self, rhs: &T) -> Self { + saturating_sub_scalar(self, rhs) } } @@ -353,7 +335,7 @@ impl ArrayOverflowingSub for PrimitiveArray where T: NativeArithmetics + OverflowingSub, { - fn overflowing_sub(&self, rhs: &T) -> Result<(Self, Bitmap)> { - Ok(overflowing_sub_scalar(self, rhs)) + fn overflowing_sub(&self, rhs: &T) -> (Self, Bitmap) { + overflowing_sub_scalar(self, rhs) } } diff --git a/src/compute/arithmetics/decimal/add.rs b/src/compute/arithmetics/decimal/add.rs index 078316f7cf9..98ccbd07dc6 100644 --- a/src/compute/arithmetics/decimal/add.rs +++ b/src/compute/arithmetics/decimal/add.rs @@ -32,13 +32,13 @@ use super::{adjusted_precision_scale, get_parameters, max_value, number_digits}; /// let a = PrimitiveArray::from([Some(1i128), Some(1i128), None, Some(2i128)]).to(DataType::Decimal(5, 2)); /// let b = PrimitiveArray::from([Some(1i128), Some(2i128), None, Some(2i128)]).to(DataType::Decimal(5, 2)); /// -/// let result = add(&a, &b).unwrap(); +/// let result = add(&a, &b); /// let expected = PrimitiveArray::from([Some(2i128), Some(3i128), None, Some(4i128)]).to(DataType::Decimal(5, 2)); /// /// assert_eq!(result, expected); /// ``` -pub fn add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> { - let (precision, _) = get_parameters(lhs.data_type(), rhs.data_type())?; +pub fn add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray { + let (precision, _) = get_parameters(lhs.data_type(), rhs.data_type()).unwrap(); let max = max_value(precision); let op = move |a, b| { @@ -79,8 +79,8 @@ pub fn add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result, rhs: &PrimitiveArray, -) -> Result> { - let (precision, _) = get_parameters(lhs.data_type(), rhs.data_type())?; +) -> PrimitiveArray { + let (precision, _) = get_parameters(lhs.data_type(), rhs.data_type()).unwrap(); let max = max_value(precision); let op = move |a, b| { @@ -115,16 +115,13 @@ pub fn saturating_add( /// let a = PrimitiveArray::from([Some(99000i128), Some(11100i128), None, Some(22200i128)]).to(DataType::Decimal(5, 2)); /// let b = PrimitiveArray::from([Some(01000i128), Some(22200i128), None, Some(11100i128)]).to(DataType::Decimal(5, 2)); /// -/// let result = checked_add(&a, &b).unwrap(); +/// let result = checked_add(&a, &b); /// let expected = PrimitiveArray::from([None, Some(33300i128), None, Some(33300i128)]).to(DataType::Decimal(5, 2)); /// /// assert_eq!(result, expected); /// ``` -pub fn checked_add( - lhs: &PrimitiveArray, - rhs: &PrimitiveArray, -) -> Result> { - let (precision, _) = get_parameters(lhs.data_type(), rhs.data_type())?; +pub fn checked_add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray { + let (precision, _) = get_parameters(lhs.data_type(), rhs.data_type()).unwrap(); let max = max_value(precision); let op = move |a, b| { @@ -142,21 +139,21 @@ pub fn checked_add( // Implementation of ArrayAdd trait for PrimitiveArrays impl ArrayAdd> for PrimitiveArray { - fn add(&self, rhs: &PrimitiveArray) -> Result { + fn add(&self, rhs: &PrimitiveArray) -> Self { add(self, rhs) } } // Implementation of ArrayCheckedAdd trait for PrimitiveArrays impl ArrayCheckedAdd> for PrimitiveArray { - fn checked_add(&self, rhs: &PrimitiveArray) -> Result { + fn checked_add(&self, rhs: &PrimitiveArray) -> Self { checked_add(self, rhs) } } // Implementation of ArraySaturatingAdd trait for PrimitiveArrays impl ArraySaturatingAdd> for PrimitiveArray { - fn saturating_add(&self, rhs: &PrimitiveArray) -> Result { + fn saturating_add(&self, rhs: &PrimitiveArray) -> Self { saturating_add(self, rhs) } } @@ -181,7 +178,7 @@ impl ArraySaturatingAdd> for PrimitiveArray { /// /// let a = PrimitiveArray::from([Some(11111_11i128)]).to(DataType::Decimal(7, 2)); /// let b = PrimitiveArray::from([Some(11111_111i128)]).to(DataType::Decimal(8, 3)); -/// let result = adaptive_add(&a, &b).unwrap(); +/// let result = adaptive_add(&a, &b); /// let expected = PrimitiveArray::from([Some(22222_221i128)]).to(DataType::Decimal(8, 3)); /// /// assert_eq!(result, expected); diff --git a/src/compute/arithmetics/decimal/div.rs b/src/compute/arithmetics/decimal/div.rs index 515aced7977..92c02602040 100644 --- a/src/compute/arithmetics/decimal/div.rs +++ b/src/compute/arithmetics/decimal/div.rs @@ -31,13 +31,13 @@ use super::{adjusted_precision_scale, get_parameters, max_value, number_digits}; /// let a = PrimitiveArray::from([Some(1_00i128), Some(4_00i128), Some(6_00i128)]).to(DataType::Decimal(5, 2)); /// let b = PrimitiveArray::from([Some(1_00i128), Some(2_00i128), Some(2_00i128)]).to(DataType::Decimal(5, 2)); /// -/// let result = div(&a, &b).unwrap(); +/// let result = div(&a, &b); /// let expected = PrimitiveArray::from([Some(1_00i128), Some(2_00i128), Some(3_00i128)]).to(DataType::Decimal(5, 2)); /// /// assert_eq!(result, expected); /// ``` -pub fn div(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> { - let (precision, scale) = get_parameters(lhs.data_type(), rhs.data_type())?; +pub fn div(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray { + let (precision, scale) = get_parameters(lhs.data_type(), rhs.data_type()).unwrap(); let scale = 10i128.pow(scale as u32); let max = max_value(precision); @@ -84,7 +84,7 @@ pub fn div(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result, rhs: &PrimitiveArray) -> Result, rhs: &PrimitiveArray, -) -> Result> { - let (precision, scale) = get_parameters(lhs.data_type(), rhs.data_type())?; +) -> PrimitiveArray { + let (precision, scale) = get_parameters(lhs.data_type(), rhs.data_type()).unwrap(); let scale = 10i128.pow(scale as u32); let max = max_value(precision); @@ -133,16 +133,13 @@ pub fn saturating_div( /// let a = PrimitiveArray::from([Some(1_00i128), Some(4_00i128), Some(6_00i128)]).to(DataType::Decimal(5, 2)); /// let b = PrimitiveArray::from([Some(000_00i128), None, Some(2_00i128)]).to(DataType::Decimal(5, 2)); /// -/// let result = checked_div(&a, &b).unwrap(); +/// let result = checked_div(&a, &b); /// let expected = PrimitiveArray::from([None, None, Some(3_00i128)]).to(DataType::Decimal(5, 2)); /// /// assert_eq!(result, expected); /// ``` -pub fn checked_div( - lhs: &PrimitiveArray, - rhs: &PrimitiveArray, -) -> Result> { - let (precision, scale) = get_parameters(lhs.data_type(), rhs.data_type())?; +pub fn checked_div(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray { + let (precision, scale) = get_parameters(lhs.data_type(), rhs.data_type()).unwrap(); let scale = 10i128.pow(scale as u32); let max = max_value(precision); @@ -164,14 +161,14 @@ pub fn checked_div( // Implementation of ArrayDiv trait for PrimitiveArrays impl ArrayDiv> for PrimitiveArray { - fn div(&self, rhs: &PrimitiveArray) -> Result { + fn div(&self, rhs: &PrimitiveArray) -> Self { div(self, rhs) } } // Implementation of ArrayCheckedDiv trait for PrimitiveArrays impl ArrayCheckedDiv> for PrimitiveArray { - fn checked_div(&self, rhs: &PrimitiveArray) -> Result { + fn checked_div(&self, rhs: &PrimitiveArray) -> Self { checked_div(self, rhs) } } @@ -197,7 +194,7 @@ impl ArrayCheckedDiv> for PrimitiveArray { /// /// let a = PrimitiveArray::from([Some(1000_00i128)]).to(DataType::Decimal(7, 2)); /// let b = PrimitiveArray::from([Some(10_0000i128)]).to(DataType::Decimal(6, 4)); -/// let result = adaptive_div(&a, &b).unwrap(); +/// let result = adaptive_div(&a, &b); /// let expected = PrimitiveArray::from([Some(100_0000i128)]).to(DataType::Decimal(9, 4)); /// /// assert_eq!(result, expected); diff --git a/src/compute/arithmetics/decimal/mul.rs b/src/compute/arithmetics/decimal/mul.rs index eab858099d2..8ccf5c7d9dd 100644 --- a/src/compute/arithmetics/decimal/mul.rs +++ b/src/compute/arithmetics/decimal/mul.rs @@ -30,13 +30,13 @@ use super::{adjusted_precision_scale, get_parameters, max_value, number_digits}; /// let a = PrimitiveArray::from([Some(1_00i128), Some(1_00i128), None, Some(2_00i128)]).to(DataType::Decimal(5, 2)); /// let b = PrimitiveArray::from([Some(1_00i128), Some(2_00i128), None, Some(2_00i128)]).to(DataType::Decimal(5, 2)); /// -/// let result = mul(&a, &b).unwrap(); +/// let result = mul(&a, &b); /// let expected = PrimitiveArray::from([Some(1_00i128), Some(2_00i128), None, Some(4_00i128)]).to(DataType::Decimal(5, 2)); /// /// assert_eq!(result, expected); /// ``` -pub fn mul(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> { - let (precision, scale) = get_parameters(lhs.data_type(), rhs.data_type())?; +pub fn mul(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray { + let (precision, scale) = get_parameters(lhs.data_type(), rhs.data_type()).unwrap(); let scale = 10i128.pow(scale as u32); let max = max_value(precision); @@ -85,7 +85,7 @@ pub fn mul(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result, rhs: &PrimitiveArray) -> Result, rhs: &PrimitiveArray, -) -> Result> { - let (precision, scale) = get_parameters(lhs.data_type(), rhs.data_type())?; +) -> PrimitiveArray { + let (precision, scale) = get_parameters(lhs.data_type(), rhs.data_type()).unwrap(); let scale = 10i128.pow(scale as u32); let max = max_value(precision); @@ -135,16 +135,13 @@ pub fn saturating_mul( /// let a = PrimitiveArray::from([Some(999_99i128), Some(1_00i128), None, Some(2_00i128)]).to(DataType::Decimal(5, 2)); /// let b = PrimitiveArray::from([Some(10_00i128), Some(2_00i128), None, Some(2_00i128)]).to(DataType::Decimal(5, 2)); /// -/// let result = checked_mul(&a, &b).unwrap(); +/// let result = checked_mul(&a, &b); /// let expected = PrimitiveArray::from([None, Some(2_00i128), None, Some(4_00i128)]).to(DataType::Decimal(5, 2)); /// /// assert_eq!(result, expected); /// ``` -pub fn checked_mul( - lhs: &PrimitiveArray, - rhs: &PrimitiveArray, -) -> Result> { - let (precision, scale) = get_parameters(lhs.data_type(), rhs.data_type())?; +pub fn checked_mul(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray { + let (precision, scale) = get_parameters(lhs.data_type(), rhs.data_type()).unwrap(); let scale = 10i128.pow(scale as u32); let max = max_value(precision); @@ -166,21 +163,21 @@ pub fn checked_mul( // Implementation of ArrayMul trait for PrimitiveArrays impl ArrayMul> for PrimitiveArray { - fn mul(&self, rhs: &PrimitiveArray) -> Result { + fn mul(&self, rhs: &PrimitiveArray) -> Self { mul(self, rhs) } } // Implementation of ArrayCheckedMul trait for PrimitiveArrays impl ArrayCheckedMul> for PrimitiveArray { - fn checked_mul(&self, rhs: &PrimitiveArray) -> Result { + fn checked_mul(&self, rhs: &PrimitiveArray) -> Self { checked_mul(self, rhs) } } // Implementation of ArraySaturatingMul trait for PrimitiveArrays impl ArraySaturatingMul> for PrimitiveArray { - fn saturating_mul(&self, rhs: &PrimitiveArray) -> Result { + fn saturating_mul(&self, rhs: &PrimitiveArray) -> Self { saturating_mul(self, rhs) } } @@ -233,12 +230,10 @@ pub fn adaptive_mul( // to the left to match the final scale let res = if lhs_s > rhs_s { l.checked_mul(r * shift) - .expect("Mayor overflow for multiplication") } else { - (l * shift) - .checked_mul(*r) - .expect("Mayor overflow for multiplication") - }; + (l * shift).checked_mul(*r) + } + .expect("Mayor overflow for multiplication"); let res = res / shift_1; diff --git a/src/compute/arithmetics/decimal/sub.rs b/src/compute/arithmetics/decimal/sub.rs index a81add11653..973bc11e4c0 100644 --- a/src/compute/arithmetics/decimal/sub.rs +++ b/src/compute/arithmetics/decimal/sub.rs @@ -34,8 +34,8 @@ use super::{adjusted_precision_scale, get_parameters, max_value, number_digits}; /// /// assert_eq!(result, expected); /// ``` -pub fn sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> { - let (precision, _) = get_parameters(lhs.data_type(), rhs.data_type())?; +pub fn sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray { + let (precision, _) = get_parameters(lhs.data_type(), rhs.data_type()).unwrap(); let max = max_value(precision); @@ -77,8 +77,8 @@ pub fn sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result, rhs: &PrimitiveArray, -) -> Result> { - let (precision, _) = get_parameters(lhs.data_type(), rhs.data_type())?; +) -> PrimitiveArray { + let (precision, _) = get_parameters(lhs.data_type(), rhs.data_type()).unwrap(); let max = max_value(precision); @@ -102,21 +102,21 @@ pub fn saturating_sub( // Implementation of ArraySub trait for PrimitiveArrays impl ArraySub> for PrimitiveArray { - fn sub(&self, rhs: &PrimitiveArray) -> Result { + fn sub(&self, rhs: &PrimitiveArray) -> Self { sub(self, rhs) } } // Implementation of ArrayCheckedSub trait for PrimitiveArrays impl ArrayCheckedSub> for PrimitiveArray { - fn checked_sub(&self, rhs: &PrimitiveArray) -> Result { + fn checked_sub(&self, rhs: &PrimitiveArray) -> Self { checked_sub(self, rhs) } } // Implementation of ArraySaturatingSub trait for PrimitiveArrays impl ArraySaturatingSub> for PrimitiveArray { - fn saturating_sub(&self, rhs: &PrimitiveArray) -> Result { + fn saturating_sub(&self, rhs: &PrimitiveArray) -> Self { saturating_sub(self, rhs) } } @@ -140,11 +140,8 @@ impl ArraySaturatingSub> for PrimitiveArray { /// /// assert_eq!(result, expected); /// ``` -pub fn checked_sub( - lhs: &PrimitiveArray, - rhs: &PrimitiveArray, -) -> Result> { - let (precision, _) = get_parameters(lhs.data_type(), rhs.data_type())?; +pub fn checked_sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray { + let (precision, _) = get_parameters(lhs.data_type(), rhs.data_type()).unwrap(); let max = max_value(precision); diff --git a/src/compute/arithmetics/mod.rs b/src/compute/arithmetics/mod.rs index d1e9fc34717..e75c604d20d 100644 --- a/src/compute/arithmetics/mod.rs +++ b/src/compute/arithmetics/mod.rs @@ -79,9 +79,9 @@ macro_rules! primitive { ($lhs: expr, $rhs: expr, $op: expr, $array_type: ty) => {{ let res_lhs = $lhs.as_any().downcast_ref().unwrap(); let res_rhs = $rhs.as_any().downcast_ref().unwrap(); - arithmetic_primitive::<$array_type>(res_lhs, $op, res_rhs) - .map(Box::new) - .map(|x| x as Box) + + let res = arithmetic_primitive::<$array_type>(res_lhs, $op, res_rhs); + Ok(Box::new(res) as Box) }}; } @@ -121,7 +121,7 @@ pub fn arithmetic(lhs: &dyn Array, op: Operator, rhs: &dyn Array) -> Result) + Ok(Box::new(res) as Box) } (Time32(TimeUnit::Second), Add, Duration(_)) | (Time32(TimeUnit::Millisecond), Add, Duration(_)) @@ -245,7 +245,7 @@ pub fn arithmetic_primitive( lhs: &PrimitiveArray, op: Operator, rhs: &PrimitiveArray, -) -> Result> +) -> PrimitiveArray where T: NativeType + Div @@ -350,115 +350,115 @@ where /// Defines basic addition operation for primitive arrays pub trait ArrayAdd: Sized { /// Adds itself to `rhs` - fn add(&self, rhs: &Rhs) -> Result; + fn add(&self, rhs: &Rhs) -> Self; } /// Defines wrapping addition operation for primitive arrays pub trait ArrayWrappingAdd: Sized { /// Adds itself to `rhs` using wrapping addition - fn wrapping_add(&self, rhs: &Rhs) -> Result; + fn wrapping_add(&self, rhs: &Rhs) -> Self; } /// Defines checked addition operation for primitive arrays pub trait ArrayCheckedAdd: Sized { /// Checked add - fn checked_add(&self, rhs: &Rhs) -> Result; + fn checked_add(&self, rhs: &Rhs) -> Self; } /// Defines saturating addition operation for primitive arrays pub trait ArraySaturatingAdd: Sized { /// Saturating add - fn saturating_add(&self, rhs: &Rhs) -> Result; + fn saturating_add(&self, rhs: &Rhs) -> Self; } /// Defines Overflowing addition operation for primitive arrays pub trait ArrayOverflowingAdd: Sized { /// Overflowing add - fn overflowing_add(&self, rhs: &Rhs) -> Result<(Self, Bitmap)>; + fn overflowing_add(&self, rhs: &Rhs) -> (Self, Bitmap); } /// Defines basic subtraction operation for primitive arrays pub trait ArraySub: Sized { /// subtraction - fn sub(&self, rhs: &Rhs) -> Result; + fn sub(&self, rhs: &Rhs) -> Self; } /// Defines wrapping subtraction operation for primitive arrays pub trait ArrayWrappingSub: Sized { /// wrapping subtraction - fn wrapping_sub(&self, rhs: &Rhs) -> Result; + fn wrapping_sub(&self, rhs: &Rhs) -> Self; } /// Defines checked subtraction operation for primitive arrays pub trait ArrayCheckedSub: Sized { /// checked subtraction - fn checked_sub(&self, rhs: &Rhs) -> Result; + fn checked_sub(&self, rhs: &Rhs) -> Self; } /// Defines saturating subtraction operation for primitive arrays pub trait ArraySaturatingSub: Sized { /// saturarting subtraction - fn saturating_sub(&self, rhs: &Rhs) -> Result; + fn saturating_sub(&self, rhs: &Rhs) -> Self; } /// Defines Overflowing subtraction operation for primitive arrays pub trait ArrayOverflowingSub: Sized { /// overflowing subtraction - fn overflowing_sub(&self, rhs: &Rhs) -> Result<(Self, Bitmap)>; + fn overflowing_sub(&self, rhs: &Rhs) -> (Self, Bitmap); } /// Defines basic multiplication operation for primitive arrays pub trait ArrayMul: Sized { /// multiplication - fn mul(&self, rhs: &Rhs) -> Result; + fn mul(&self, rhs: &Rhs) -> Self; } /// Defines wrapping multiplication operation for primitive arrays pub trait ArrayWrappingMul: Sized { /// wrapping multiplication - fn wrapping_mul(&self, rhs: &Rhs) -> Result; + fn wrapping_mul(&self, rhs: &Rhs) -> Self; } /// Defines checked multiplication operation for primitive arrays pub trait ArrayCheckedMul: Sized { /// checked multiplication - fn checked_mul(&self, rhs: &Rhs) -> Result; + fn checked_mul(&self, rhs: &Rhs) -> Self; } /// Defines saturating multiplication operation for primitive arrays pub trait ArraySaturatingMul: Sized { /// saturating multiplication - fn saturating_mul(&self, rhs: &Rhs) -> Result; + fn saturating_mul(&self, rhs: &Rhs) -> Self; } /// Defines Overflowing multiplication operation for primitive arrays pub trait ArrayOverflowingMul: Sized { /// overflowing multiplication - fn overflowing_mul(&self, rhs: &Rhs) -> Result<(Self, Bitmap)>; + fn overflowing_mul(&self, rhs: &Rhs) -> (Self, Bitmap); } /// Defines basic division operation for primitive arrays pub trait ArrayDiv: Sized { /// division - fn div(&self, rhs: &Rhs) -> Result; + fn div(&self, rhs: &Rhs) -> Self; } /// Defines checked division operation for primitive arrays pub trait ArrayCheckedDiv: Sized { /// checked division - fn checked_div(&self, rhs: &Rhs) -> Result; + fn checked_div(&self, rhs: &Rhs) -> Self; } /// Defines basic reminder operation for primitive arrays pub trait ArrayRem: Sized { /// remainder - fn rem(&self, rhs: &Rhs) -> Result; + fn rem(&self, rhs: &Rhs) -> Self; } /// Defines checked reminder operation for primitive arrays pub trait ArrayCheckedRem: Sized { /// checked remainder - fn checked_rem(&self, rhs: &Rhs) -> Result; + fn checked_rem(&self, rhs: &Rhs) -> Self; } /// Trait describing a [`NativeType`] whose semantics of arithmetic in Arrow equals diff --git a/src/compute/arithmetics/time.rs b/src/compute/arithmetics/time.rs index 1b64c4e0038..1d468853427 100644 --- a/src/compute/arithmetics/time.rs +++ b/src/compute/arithmetics/time.rs @@ -114,7 +114,7 @@ where // required to add a duration to the timestamp array. let op = move |a: T, b: i64| a + (b as f64 * scale).as_(); - binary(time, duration, time.data_type().clone(), op) + Ok(binary(time, duration, time.data_type().clone(), op)) } /// Subtract a duration to a time array (Timestamp, Time and Date). The timeunit @@ -170,7 +170,7 @@ where // required to add a duration to the timestamp array. let op = move |a: T, b: i64| a - (b as f64 * scale).as_(); - binary(time, duration, time.data_type().clone(), op) + Ok(binary(time, duration, time.data_type().clone(), op)) } /// Calculates the difference between two timestamps returning an array of type @@ -220,7 +220,7 @@ pub fn subtract_timestamps( let scale = temporal_conversions::timeunit_scale(*timeunit_a, *timeunit_b); let op = move |a, b| a - (b as f64 * scale) as i64; - binary(lhs, rhs, DataType::Duration(*timeunit_a), op) + Ok(binary(lhs, rhs, DataType::Duration(*timeunit_a), op)) } _ => Err(ArrowError::InvalidArgumentError( "Incorrect data type for the arguments".to_string(), @@ -238,7 +238,7 @@ pub fn add_interval( let time_unit = *time_unit; let timezone = temporal_conversions::parse_offset(timezone_str); match timezone { - Ok(timezone) => binary( + Ok(timezone) => Ok(binary( timestamp, interval, timestamp.data_type().clone(), @@ -247,11 +247,11 @@ pub fn add_interval( timestamp, time_unit, interval, &timezone, ) }, - ), + )), #[cfg(feature = "chrono-tz")] Err(_) => { let timezone = temporal_conversions::parse_offset_tz(timezone_str)?; - binary( + Ok(binary( timestamp, interval, timestamp.data_type().clone(), @@ -260,7 +260,7 @@ pub fn add_interval( timestamp, time_unit, interval, &timezone, ) }, - ) + )) } #[cfg(not(feature = "chrono-tz"))] _ => Err(ArrowError::InvalidArgumentError(format!( @@ -271,14 +271,14 @@ pub fn add_interval( } DataType::Timestamp(time_unit, None) => { let time_unit = *time_unit; - binary( + Ok(binary( timestamp, interval, timestamp.data_type().clone(), |timestamp, interval| { temporal_conversions::add_naive_interval(timestamp, time_unit, interval) }, - ) + )) } _ => Err(ArrowError::InvalidArgumentError( "Adding an interval is only supported for `DataType::Timestamp`".to_string(), diff --git a/src/compute/arity.rs b/src/compute/arity.rs index 362181c731e..fec79e9d4cd 100644 --- a/src/compute/arity.rs +++ b/src/compute/arity.rs @@ -140,13 +140,13 @@ pub fn binary( rhs: &PrimitiveArray, data_type: DataType, op: F, -) -> Result> +) -> PrimitiveArray where T: NativeType, D: NativeType, F: Fn(T, D) -> T, { - check_same_len(lhs, rhs)?; + check_same_len(lhs, rhs).unwrap(); let validity = combine_validities(lhs.validity(), rhs.validity()); @@ -157,7 +157,7 @@ where .map(|(l, r)| op(*l, *r)); let values = Buffer::from_trusted_len_iter(values); - Ok(PrimitiveArray::::from_data(data_type, values, validity)) + PrimitiveArray::::from_data(data_type, values, validity) } /// Version of binary that checks for errors in the closure used to create the @@ -195,13 +195,13 @@ pub fn binary_with_bitmap( rhs: &PrimitiveArray, data_type: DataType, op: F, -) -> Result<(PrimitiveArray, Bitmap)> +) -> (PrimitiveArray, Bitmap) where T: NativeType, D: NativeType, F: Fn(T, D) -> (T, bool), { - check_same_len(lhs, rhs)?; + check_same_len(lhs, rhs).unwrap(); let validity = combine_validities(lhs.validity(), rhs.validity()); @@ -215,10 +215,10 @@ where let values = Buffer::from_trusted_len_iter(values); - Ok(( + ( PrimitiveArray::::from_data(data_type, values, validity), mut_bitmap.into(), - )) + ) } /// Version of binary that creates a mutable bitmap that is used to keep track @@ -229,13 +229,13 @@ pub fn binary_checked( rhs: &PrimitiveArray, data_type: DataType, op: F, -) -> Result> +) -> PrimitiveArray where T: NativeType, D: NativeType, F: Fn(T, D) -> Option, { - check_same_len(lhs, rhs)?; + check_same_len(lhs, rhs).unwrap(); let mut mut_bitmap = MutableBitmap::with_capacity(lhs.len()); @@ -265,5 +265,5 @@ where // as Null let validity = combine_validities(validity.as_ref(), Some(&bitmap)); - Ok(PrimitiveArray::::from_data(data_type, values, validity)) + PrimitiveArray::::from_data(data_type, values, validity) } diff --git a/src/compute/bitwise.rs b/src/compute/bitwise.rs index e4c0d035d34..6d227f0aa40 100644 --- a/src/compute/bitwise.rs +++ b/src/compute/bitwise.rs @@ -2,41 +2,36 @@ use std::ops::{BitAnd, BitOr, BitXor, Not}; use crate::array::{Array, PrimitiveArray}; -use crate::compute::arithmetics::basic::check_same_type; use crate::compute::arity::{binary, unary}; -use crate::error::Result; use crate::types::NativeType; /// Performs `OR` operation on two arrays. -/// # Error -/// This function errors when the arrays have different lengths or are different types. -pub fn or(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> +/// # Panic +/// This function errors when the arrays have different lengths. +pub fn or(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + BitOr, { - check_same_type(lhs, rhs)?; binary(lhs, rhs, lhs.data_type().clone(), |a, b| a | b) } /// Performs `XOR` operation between two arrays. -/// # Error -/// This function errors when the arrays have different lengths or are different types. -pub fn xor(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> +/// # Panic +/// This function errors when the arrays have different lengths. +pub fn xor(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + BitXor, { - check_same_type(lhs, rhs)?; binary(lhs, rhs, lhs.data_type().clone(), |a, b| a ^ b) } /// Performs `AND` operation on two arrays. -/// # Error -/// This function errors when the arrays have different lengths or are different types. -pub fn and(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> +/// # Panic +/// This function panics when the arrays have different lengths. +pub fn and(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where T: NativeType + BitAnd, { - check_same_type(lhs, rhs)?; binary(lhs, rhs, lhs.data_type().clone(), |a, b| a & b) } From 25bef4335cec6062ca7878d0e30e6f17432a7774 Mon Sep 17 00:00:00 2001 From: "Jorge C. Leitao" Date: Mon, 15 Nov 2021 06:35:03 +0000 Subject: [PATCH 2/3] Simplified arithemtics kernels. --- src/compute/arithmetics/basic/mod.rs | 71 +++- src/compute/arithmetics/decimal/mod.rs | 12 +- src/compute/arithmetics/mod.rs | 538 +++++++++++-------------- src/compute/arithmetics/time.rs | 12 +- 4 files changed, 309 insertions(+), 324 deletions(-) diff --git a/src/compute/arithmetics/basic/mod.rs b/src/compute/arithmetics/basic/mod.rs index 6522338da6c..492a29b37a9 100644 --- a/src/compute/arithmetics/basic/mod.rs +++ b/src/compute/arithmetics/basic/mod.rs @@ -1,4 +1,4 @@ -//! Contains arithemtic functions for [`PrimitiveArray`](crate::array::PrimitiveArray)s. +//! Contains arithemtic functions for [`PrimitiveArray`]s. //! //! Each operation has four variants, like the rest of Rust's ecosystem: //! * usual, that [`panic!`]s on overflow @@ -20,3 +20,72 @@ pub use sub::*; mod common; pub(crate) use common::*; + +use std::ops::Neg; + +use num_traits::{CheckedNeg, WrappingNeg}; + +use crate::{ + array::{Array, PrimitiveArray}, + types::NativeType, +}; + +use super::super::arity::{unary, unary_checked}; + +/// Negates values from array. +/// +/// # Examples +/// ``` +/// use arrow2::compute::arithmetics::negate; +/// use arrow2::array::PrimitiveArray; +/// +/// let a = PrimitiveArray::from([None, Some(6), None, Some(7)]); +/// let result = negate(&a); +/// let expected = PrimitiveArray::from([None, Some(-6), None, Some(-7)]); +/// assert_eq!(result, expected) +/// ``` +pub fn negate(array: &PrimitiveArray) -> PrimitiveArray +where + T: NativeType + Neg, +{ + unary(array, |a| -a, array.data_type().clone()) +} + +/// Checked negates values from array. +/// +/// # Examples +/// ``` +/// use arrow2::compute::arithmetics::checked_negate; +/// use arrow2::array::{Array, PrimitiveArray}; +/// +/// let a = PrimitiveArray::from([None, Some(6), Some(i8::MIN), Some(7)]); +/// let result = checked_negate(&a); +/// let expected = PrimitiveArray::from([None, Some(-6), None, Some(-7)]); +/// assert_eq!(result, expected); +/// assert!(!result.is_valid(2)) +/// ``` +pub fn checked_negate(array: &PrimitiveArray) -> PrimitiveArray +where + T: NativeType + CheckedNeg, +{ + unary_checked(array, |a| a.checked_neg(), array.data_type().clone()) +} + +/// Wrapping negates values from array. +/// +/// # Examples +/// ``` +/// use arrow2::compute::arithmetics::wrapping_negate; +/// use arrow2::array::{Array, PrimitiveArray}; +/// +/// let a = PrimitiveArray::from([None, Some(6), Some(i8::MIN), Some(7)]); +/// let result = wrapping_negate(&a); +/// let expected = PrimitiveArray::from([None, Some(-6), Some(i8::MIN), Some(-7)]); +/// assert_eq!(result, expected); +/// ``` +pub fn wrapping_negate(array: &PrimitiveArray) -> PrimitiveArray +where + T: NativeType + WrappingNeg, +{ + unary(array, |a| a.wrapping_neg(), array.data_type().clone()) +} diff --git a/src/compute/arithmetics/decimal/mod.rs b/src/compute/arithmetics/decimal/mod.rs index c3b69468756..ade2d0cec9c 100644 --- a/src/compute/arithmetics/decimal/mod.rs +++ b/src/compute/arithmetics/decimal/mod.rs @@ -3,10 +3,14 @@ //! precision and scale parameters. These affect the arithmetic operations and //! need to be considered while doing operations with Decimal numbers. -pub mod add; -pub mod div; -pub mod mul; -pub mod sub; +mod add; +pub use add::*; +mod div; +pub use div::*; +mod mul; +pub use mul::*; +mod sub; +pub use sub::*; use crate::datatypes::DataType; use crate::error::{ArrowError, Result}; diff --git a/src/compute/arithmetics/mod.rs b/src/compute/arithmetics/mod.rs index e75c604d20d..9a47889ee91 100644 --- a/src/compute/arithmetics/mod.rs +++ b/src/compute/arithmetics/mod.rs @@ -1,350 +1,262 @@ -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -//! Defines basic arithmetic kernels for [`PrimitiveArray`]s. -//! -//! # Description +//! Defines basic arithmetic kernels for [`PrimitiveArray`](crate::array::PrimitiveArray)s. //! //! The Arithmetics module is composed by basic arithmetics operations that can -//! be performed on PrimitiveArray Arrays. These operations can be the building for -//! any implementation using Arrow. -//! -//! Whenever possible, each of the operations in these modules has variations -//! of the basic operation that offers different guarantees. These options are: -//! -//! * plain: The plain type (add, sub, mul, and div) don't offer any protection -//! when performing the operations. This means that if overflow is found, -//! then the operations will panic. -//! -//! * checked: A checked operation will change the validity Bitmap for the -//! offending operation. For example, if one of the operations overflows, the -//! validity will be changed to None, indicating a Null value. -//! -//! * saturating: If overflowing is presented in one operation, the resulting -//! value for that index will be saturated to the MAX or MIN value possible -//! for that type. For [`Decimal`](crate::datatypes::DataType::Decimal) -//! arrays, the saturated value is calculated considering the precision and -//! scale of the array. +//! be performed on [`PrimitiveArray`](crate::array::PrimitiveArray). //! -//! * overflowing: When an operation overflows, the resulting will be the -//! overflowed value for the operation. The result from the array operation -//! includes a Binary bitmap indicating which values overflowed. -//! -//! * adaptive: For [`Decimal`](crate::datatypes::DataType::Decimal) arrays, -//! the adaptive variation adjusts the precision and scale to avoid -//! saturation or overflowing. -//! -//! # New kernels -//! -//! When adding a new operation to this module, it is strongly suggested to -//! follow the design description presented in the README.md file located in -//! the [`compute`](crate::compute) module and the function descriptions -//! presented in this document. +//! Whenever possible, each operation declares variations +//! of the basic operation that offers different guarantees: +//! * plain: panics on overflowing and underflowing. +//! * checked: turns an overflowing to a null. +//! * saturating: turns the overflowing to the MAX or MIN value respectively. +//! * overflowing: returns an extra [`Bitmap`] denoting whether the operation overflowed. +//! * adaptive: for [`Decimal`](crate::datatypes::DataType::Decimal) only, +//! adjusts the precision and scale to make the resulting value fit. pub mod basic; pub mod decimal; pub mod time; -use std::ops::{Add, Div, Mul, Neg, Rem, Sub}; - -use num_traits::{CheckedNeg, NumCast, WrappingNeg, Zero}; - -use crate::datatypes::{DataType, IntervalUnit, TimeUnit}; -use crate::error::{ArrowError, Result}; -use crate::types::NativeType; -use crate::{array::*, bitmap::Bitmap}; - -use super::arity::{unary, unary_checked}; +use crate::{ + array::Array, + bitmap::Bitmap, + datatypes::{DataType, IntervalUnit, TimeUnit}, + types::NativeType, +}; // Macro to evaluate match branch in arithmetic function. -// The macro is used to downcast both arrays to a primitive_array_type. If there -// is an error then an ArrowError is return with the data_type that cause it. -// It returns the result from the arithmetic_primitive function evaluated with -// the Operator selected macro_rules! primitive { - ($lhs: expr, $rhs: expr, $op: expr, $array_type: ty) => {{ - let res_lhs = $lhs.as_any().downcast_ref().unwrap(); - let res_rhs = $rhs.as_any().downcast_ref().unwrap(); + ($lhs:expr, $rhs:expr, $op:tt, $type:ty) => {{ + let lhs = $lhs.as_any().downcast_ref().unwrap(); + let rhs = $rhs.as_any().downcast_ref().unwrap(); - let res = arithmetic_primitive::<$array_type>(res_lhs, $op, res_rhs); - Ok(Box::new(res) as Box) + let result = basic::$op::<$type>(lhs, rhs); + Box::new(result) as Box }}; } -/// Execute an arithmetic operation with two arrays. It uses the enum Operator -/// to select the type of operation that is going to be performed with the two -/// arrays -pub fn arithmetic(lhs: &dyn Array, op: Operator, rhs: &dyn Array) -> Result> { - use DataType::*; - use Operator::*; - match (lhs.data_type(), op, rhs.data_type()) { - (Int8, _, Int8) => primitive!(lhs, rhs, op, i8), - (Int16, _, Int16) => primitive!(lhs, rhs, op, i16), - (Int32, _, Int32) => primitive!(lhs, rhs, op, i32), - (Int64, _, Int64) | (Duration(_), _, Duration(_)) => { - primitive!(lhs, rhs, op, i64) - } - (UInt8, _, UInt8) => primitive!(lhs, rhs, op, u8), - (UInt16, _, UInt16) => primitive!(lhs, rhs, op, u16), - (UInt32, _, UInt32) => primitive!(lhs, rhs, op, u32), - (UInt64, _, UInt64) => primitive!(lhs, rhs, op, u64), - (Float32, _, Float32) => primitive!(lhs, rhs, op, f32), - (Float64, _, Float64) => primitive!(lhs, rhs, op, f64), - (Decimal(_, _), _, Decimal(_, _)) => { - let lhs = lhs.as_any().downcast_ref().unwrap(); - let rhs = rhs.as_any().downcast_ref().unwrap(); - - let res = match op { - Add => decimal::add::add(lhs, rhs), - Subtract => decimal::sub::sub(lhs, rhs), - Multiply => decimal::mul::mul(lhs, rhs), - Divide => decimal::div::div(lhs, rhs), - Remainder => { - return Err(ArrowError::NotYetImplemented(format!( - "Arithmetics of ({:?}, {:?}, {:?}) is not supported", - lhs, op, rhs - ))) - } - }; - - Ok(Box::new(res) as Box) - } - (Time32(TimeUnit::Second), Add, Duration(_)) - | (Time32(TimeUnit::Millisecond), Add, Duration(_)) - | (Date32, Add, Duration(_)) => { - let lhs = lhs.as_any().downcast_ref().unwrap(); - let rhs = rhs.as_any().downcast_ref().unwrap(); - time::add_duration::(lhs, rhs).map(|x| Box::new(x) as Box) - } - (Time32(TimeUnit::Second), Subtract, Duration(_)) - | (Time32(TimeUnit::Millisecond), Subtract, Duration(_)) - | (Date32, Subtract, Duration(_)) => { - let lhs = lhs.as_any().downcast_ref().unwrap(); - let rhs = rhs.as_any().downcast_ref().unwrap(); - time::subtract_duration::(lhs, rhs).map(|x| Box::new(x) as Box) - } - (Time64(TimeUnit::Microsecond), Add, Duration(_)) - | (Time64(TimeUnit::Nanosecond), Add, Duration(_)) - | (Date64, Add, Duration(_)) - | (Timestamp(_, _), Add, Duration(_)) => { - let lhs = lhs.as_any().downcast_ref().unwrap(); - let rhs = rhs.as_any().downcast_ref().unwrap(); - time::add_duration::(lhs, rhs).map(|x| Box::new(x) as Box) - } - (Timestamp(_, _), Add, Interval(IntervalUnit::MonthDayNano)) => { - let lhs = lhs.as_any().downcast_ref().unwrap(); - let rhs = rhs.as_any().downcast_ref().unwrap(); - time::add_interval(lhs, rhs).map(|x| Box::new(x) as Box) - } - (Time64(TimeUnit::Microsecond), Subtract, Duration(_)) - | (Time64(TimeUnit::Nanosecond), Subtract, Duration(_)) - | (Date64, Subtract, Duration(_)) - | (Timestamp(_, _), Subtract, Duration(_)) => { - let lhs = lhs.as_any().downcast_ref().unwrap(); - let rhs = rhs.as_any().downcast_ref().unwrap(); - time::subtract_duration::(lhs, rhs).map(|x| Box::new(x) as Box) +// Macro to create a `match` statement with dynamic dispatch to functions based on +// the array's logical types +macro_rules! arith { + ($lhs:expr, $rhs:expr, $op:tt $(, decimal = $op_decimal:tt )? $(, duration = $op_duration:tt )? $(, interval = $op_interval:tt )? $(, timestamp = $op_timestamp:tt )?) => {{ + let lhs = $lhs; + let rhs = $rhs; + use DataType::*; + match (lhs.data_type(), rhs.data_type()) { + (Int8, Int8) => primitive!(lhs, rhs, $op, i8), + (Int16, Int16) => primitive!(lhs, rhs, $op, i16), + (Int32, Int32) => primitive!(lhs, rhs, $op, i32), + (Int64, Int64) | (Duration(_), Duration(_)) => { + primitive!(lhs, rhs, $op, i64) + } + (UInt8, UInt8) => primitive!(lhs, rhs, $op, u8), + (UInt16, UInt16) => primitive!(lhs, rhs, $op, u16), + (UInt32, UInt32) => primitive!(lhs, rhs, $op, u32), + (UInt64, UInt64) => primitive!(lhs, rhs, $op, u64), + (Float32, Float32) => primitive!(lhs, rhs, $op, f32), + (Float64, Float64) => primitive!(lhs, rhs, $op, f64), + $ ( + (Decimal(_, _), Decimal(_, _)) => { + let lhs = lhs.as_any().downcast_ref().unwrap(); + let rhs = rhs.as_any().downcast_ref().unwrap(); + Box::new(decimal::$op_decimal(lhs, rhs)) as Box + } + )? + $ ( + (Time32(TimeUnit::Second), Duration(_)) + | (Time32(TimeUnit::Millisecond), Duration(_)) + | (Date32, Duration(_)) => { + let lhs = lhs.as_any().downcast_ref().unwrap(); + let rhs = rhs.as_any().downcast_ref().unwrap(); + Box::new(time::$op_duration::(lhs, rhs)) as Box + } + (Time64(TimeUnit::Microsecond), Duration(_)) + | (Time64(TimeUnit::Nanosecond), Duration(_)) + | (Date64, Duration(_)) + | (Timestamp(_, _), Duration(_)) => { + let lhs = lhs.as_any().downcast_ref().unwrap(); + let rhs = rhs.as_any().downcast_ref().unwrap(); + Box::new(time::$op_duration::(lhs, rhs)) as Box + } + )? + $ ( + (Timestamp(_, _), Interval(IntervalUnit::MonthDayNano)) => { + let lhs = lhs.as_any().downcast_ref().unwrap(); + let rhs = rhs.as_any().downcast_ref().unwrap(); + time::$op_interval(lhs, rhs).map(|x| Box::new(x) as Box).unwrap() + } + )? + $ ( + (Timestamp(_, None), Timestamp(_, None)) => { + let lhs = lhs.as_any().downcast_ref().unwrap(); + let rhs = rhs.as_any().downcast_ref().unwrap(); + time::$op_timestamp(lhs, rhs).map(|x| Box::new(x) as Box).unwrap() + } + )? + _ => todo!( + "Addition of {:?} with {:?} is not supported", + lhs.data_type(), + rhs.data_type() + ), } - (Timestamp(_, None), Subtract, Timestamp(_, None)) => { - let lhs = lhs.as_any().downcast_ref().unwrap(); - let rhs = rhs.as_any().downcast_ref().unwrap(); - time::subtract_timestamps(lhs, rhs).map(|x| Box::new(x) as Box) - } - (lhs, op, rhs) => Err(ArrowError::NotYetImplemented(format!( - "Arithmetics of ({:?}, {:?}, {:?}) is not supported", - lhs, op, rhs - ))), - } + }}; +} + +/// Adds two [`Array`]s. +/// # Panic +/// This function panics iff +/// * the opertion is not supported for the logical types (use [`can_add`] to check) +/// * the arrays have a different length +/// * one of the arrays is a timestamp with timezone and the timezone is not valid. +pub fn add(lhs: &dyn Array, rhs: &dyn Array) -> Box { + arith!( + lhs, + rhs, + add, + duration = add_duration, + interval = add_interval + ) } -/// Checks if an array of type `datatype` can perform basic arithmetic -/// operations. These operations include add, subtract, multiply, divide. -/// -/// # Examples -/// ``` -/// use arrow2::compute::arithmetics::{can_arithmetic, Operator}; -/// use arrow2::datatypes::DataType; -/// -/// let data_type = DataType::Int8; -/// assert_eq!(can_arithmetic(&data_type, Operator::Add, &data_type), true); -/// -/// let data_type = DataType::LargeBinary; -/// assert_eq!(can_arithmetic(&data_type, Operator::Add, &data_type), false) -/// ``` -pub fn can_arithmetic(lhs: &DataType, op: Operator, rhs: &DataType) -> bool { +/// Returns whether two [`DataType`]s can be added by [`add`]. +pub fn can_add(lhs: &DataType, rhs: &DataType) -> bool { use DataType::*; - use Operator::*; - if let (Decimal(_, _), Remainder, Decimal(_, _)) = (lhs, op, rhs) { - return false; - }; + matches!( + (lhs, rhs), + (Int8, Int8) + | (Int16, Int16) + | (Int32, Int32) + | (Int64, Int64) + | (UInt8, UInt8) + | (UInt16, UInt16) + | (UInt32, UInt32) + | (UInt64, UInt64) + | (Float64, Float64) + | (Float32, Float32) + | (Duration(_), Duration(_)) + | (Decimal(_, _), Decimal(_, _)) + | (Date32, Duration(_)) + | (Date64, Duration(_)) + | (Time32(TimeUnit::Millisecond), Duration(_)) + | (Time32(TimeUnit::Second), Duration(_)) + | (Time64(TimeUnit::Microsecond), Duration(_)) + | (Time64(TimeUnit::Nanosecond), Duration(_)) + | (Timestamp(_, _), Duration(_)) + | (Timestamp(_, _), Interval(IntervalUnit::MonthDayNano)) + ) +} + +/// Subtracts two [`Array`]s. +/// # Panic +/// This function panics iff +/// * the opertion is not supported for the logical types (use [`can_sub`] to check) +/// * the arrays have a different length +/// * one of the arrays is a timestamp with timezone and the timezone is not valid. +pub fn sub(lhs: &dyn Array, rhs: &dyn Array) -> Box { + arith!( + lhs, + rhs, + sub, + decimal = sub, + duration = subtract_duration, + timestamp = subtract_timestamps + ) +} +/// Returns whether two [`DataType`]s can be subtracted by [`sub`]. +pub fn can_sub(lhs: &DataType, rhs: &DataType) -> bool { + use DataType::*; matches!( - (lhs, op, rhs), - (Int8, _, Int8) - | (Int16, _, Int16) - | (Int32, _, Int32) - | (Int64, _, Int64) - | (UInt8, _, UInt8) - | (UInt16, _, UInt16) - | (UInt32, _, UInt32) - | (UInt64, _, UInt64) - | (Float64, _, Float64) - | (Float32, _, Float32) - | (Duration(_), _, Duration(_)) - | (Decimal(_, _), _, Decimal(_, _)) - | (Date32, Subtract, Duration(_)) - | (Date32, Add, Duration(_)) - | (Date64, Subtract, Duration(_)) - | (Date64, Add, Duration(_)) - | (Time32(TimeUnit::Millisecond), Subtract, Duration(_)) - | (Time32(TimeUnit::Second), Subtract, Duration(_)) - | (Time32(TimeUnit::Millisecond), Add, Duration(_)) - | (Time32(TimeUnit::Second), Add, Duration(_)) - | (Time64(TimeUnit::Microsecond), Subtract, Duration(_)) - | (Time64(TimeUnit::Nanosecond), Subtract, Duration(_)) - | (Time64(TimeUnit::Microsecond), Add, Duration(_)) - | (Time64(TimeUnit::Nanosecond), Add, Duration(_)) - | (Timestamp(_, _), Subtract, Duration(_)) - | (Timestamp(_, _), Add, Duration(_)) - | (Timestamp(_, _), Add, Interval(IntervalUnit::MonthDayNano)) - | (Timestamp(_, None), Subtract, Timestamp(_, None)) + (lhs, rhs), + (Int8, Int8) + | (Int16, Int16) + | (Int32, Int32) + | (Int64, Int64) + | (UInt8, UInt8) + | (UInt16, UInt16) + | (UInt32, UInt32) + | (UInt64, UInt64) + | (Float64, Float64) + | (Float32, Float32) + | (Duration(_), Duration(_)) + | (Decimal(_, _), Decimal(_, _)) + | (Date32, Duration(_)) + | (Date64, Duration(_)) + | (Time32(TimeUnit::Millisecond), Duration(_)) + | (Time32(TimeUnit::Second), Duration(_)) + | (Time64(TimeUnit::Microsecond), Duration(_)) + | (Time64(TimeUnit::Nanosecond), Duration(_)) + | (Timestamp(_, _), Duration(_)) + | (Timestamp(_, None), Timestamp(_, None)) ) } -/// Arithmetic operator -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum Operator { - /// Add - Add, - /// Subtract - Subtract, - /// Multiply - Multiply, - /// Divide - Divide, - /// Remainder - Remainder, +/// Multiply two [`Array`]s. +/// # Panic +/// This function panics iff +/// * the opertion is not supported for the logical types (use [`can_mul`] to check) +/// * the arrays have a different length +pub fn mul(lhs: &dyn Array, rhs: &dyn Array) -> Box { + arith!(lhs, rhs, mul, decimal = mul) } -/// Perform arithmetic operations on two primitive arrays based on the Operator enum -// -pub fn arithmetic_primitive( - lhs: &PrimitiveArray, - op: Operator, - rhs: &PrimitiveArray, -) -> PrimitiveArray -where - T: NativeType - + Div - + Zero - + Add - + Sub - + Mul - + Rem, -{ - match op { - Operator::Add => basic::add(lhs, rhs), - Operator::Subtract => basic::sub(lhs, rhs), - Operator::Multiply => basic::mul(lhs, rhs), - Operator::Divide => basic::div(lhs, rhs), - Operator::Remainder => basic::rem(lhs, rhs), - } +/// Returns whether two [`DataType`]s can be multiplied by [`mul`]. +pub fn can_mul(lhs: &DataType, rhs: &DataType) -> bool { + use DataType::*; + matches!( + (lhs, rhs), + (Int8, Int8) + | (Int16, Int16) + | (Int32, Int32) + | (Int64, Int64) + | (UInt8, UInt8) + | (UInt16, UInt16) + | (UInt32, UInt32) + | (UInt64, UInt64) + | (Float64, Float64) + | (Float32, Float32) + | (Decimal(_, _), Decimal(_, _)) + ) } -/// Performs primitive operation on an array and and scalar -pub fn arithmetic_primitive_scalar( - lhs: &PrimitiveArray, - op: Operator, - rhs: &T, -) -> Result> -where - T: NativeType - + Div - + Zero - + Add - + Sub - + Mul - + Rem - + NumCast, -{ - match op { - Operator::Add => Ok(basic::add_scalar(lhs, rhs)), - Operator::Subtract => Ok(basic::sub_scalar(lhs, rhs)), - Operator::Multiply => Ok(basic::mul_scalar(lhs, rhs)), - Operator::Divide => Ok(basic::div_scalar(lhs, rhs)), - Operator::Remainder => Ok(basic::rem_scalar(lhs, rhs)), - } +/// Divide of two [`Array`]s. +/// # Panic +/// This function panics iff +/// * the opertion is not supported for the logical types (use [`can_div`] to check) +/// * the arrays have a different length +pub fn div(lhs: &dyn Array, rhs: &dyn Array) -> Box { + arith!(lhs, rhs, div, decimal = div) } -/// Negates values from array. -/// -/// # Examples -/// ``` -/// use arrow2::compute::arithmetics::negate; -/// use arrow2::array::PrimitiveArray; -/// -/// let a = PrimitiveArray::from([None, Some(6), None, Some(7)]); -/// let result = negate(&a); -/// let expected = PrimitiveArray::from([None, Some(-6), None, Some(-7)]); -/// assert_eq!(result, expected) -/// ``` -pub fn negate(array: &PrimitiveArray) -> PrimitiveArray -where - T: NativeType + Neg, -{ - unary(array, |a| -a, array.data_type().clone()) +/// Returns whether two [`DataType`]s can be divided by [`div`]. +pub fn can_div(lhs: &DataType, rhs: &DataType) -> bool { + can_mul(lhs, rhs) } -/// Checked negates values from array. -/// -/// # Examples -/// ``` -/// use arrow2::compute::arithmetics::checked_negate; -/// use arrow2::array::{Array, PrimitiveArray}; -/// -/// let a = PrimitiveArray::from([None, Some(6), Some(i8::MIN), Some(7)]); -/// let result = checked_negate(&a); -/// let expected = PrimitiveArray::from([None, Some(-6), None, Some(-7)]); -/// assert_eq!(result, expected); -/// assert!(!result.is_valid(2)) -/// ``` -pub fn checked_negate(array: &PrimitiveArray) -> PrimitiveArray -where - T: NativeType + CheckedNeg, -{ - unary_checked(array, |a| a.checked_neg(), array.data_type().clone()) +/// Remainder of two [`Array`]s. +/// # Panic +/// This function panics iff +/// * the opertion is not supported for the logical types (use [`can_rem`] to check) +/// * the arrays have a different length +pub fn rem(lhs: &dyn Array, rhs: &dyn Array) -> Box { + arith!(lhs, rhs, rem) } -/// Wrapping negates values from array. -/// -/// # Examples -/// ``` -/// use arrow2::compute::arithmetics::wrapping_negate; -/// use arrow2::array::{Array, PrimitiveArray}; -/// -/// let a = PrimitiveArray::from([None, Some(6), Some(i8::MIN), Some(7)]); -/// let result = wrapping_negate(&a); -/// let expected = PrimitiveArray::from([None, Some(-6), Some(i8::MIN), Some(-7)]); -/// assert_eq!(result, expected); -/// ``` -pub fn wrapping_negate(array: &PrimitiveArray) -> PrimitiveArray -where - T: NativeType + WrappingNeg, -{ - unary(array, |a| a.wrapping_neg(), array.data_type().clone()) +/// Returns whether two [`DataType`]s "can be remainder" by [`rem`]. +pub fn can_rem(lhs: &DataType, rhs: &DataType) -> bool { + use DataType::*; + matches!( + (lhs, rhs), + (Int8, Int8) + | (Int16, Int16) + | (Int32, Int32) + | (Int64, Int64) + | (UInt8, UInt8) + | (UInt16, UInt16) + | (UInt32, UInt32) + | (UInt64, UInt64) + | (Float64, Float64) + | (Float32, Float32) + ) } /// Defines basic addition operation for primitive arrays diff --git a/src/compute/arithmetics/time.rs b/src/compute/arithmetics/time.rs index 1d468853427..d622652823e 100644 --- a/src/compute/arithmetics/time.rs +++ b/src/compute/arithmetics/time.rs @@ -103,18 +103,18 @@ fn create_scale(lhs: &DataType, rhs: &DataType) -> Result { pub fn add_duration( time: &PrimitiveArray, duration: &PrimitiveArray, -) -> Result> +) -> PrimitiveArray where f64: AsPrimitive, T: NativeType + Add, { - let scale = create_scale(time.data_type(), duration.data_type())?; + let scale = create_scale(time.data_type(), duration.data_type()).unwrap(); // Closure for the binary operation. The closure contains the scale // required to add a duration to the timestamp array. let op = move |a: T, b: i64| a + (b as f64 * scale).as_(); - Ok(binary(time, duration, time.data_type().clone(), op)) + binary(time, duration, time.data_type().clone(), op) } /// Subtract a duration to a time array (Timestamp, Time and Date). The timeunit @@ -159,18 +159,18 @@ where pub fn subtract_duration( time: &PrimitiveArray, duration: &PrimitiveArray, -) -> Result> +) -> PrimitiveArray where f64: AsPrimitive, T: NativeType + Sub, { - let scale = create_scale(time.data_type(), duration.data_type())?; + let scale = create_scale(time.data_type(), duration.data_type()).unwrap(); // Closure for the binary operation. The closure contains the scale // required to add a duration to the timestamp array. let op = move |a: T, b: i64| a - (b as f64 * scale).as_(); - Ok(binary(time, duration, time.data_type().clone(), op)) + binary(time, duration, time.data_type().clone(), op) } /// Calculates the difference between two timestamps returning an array of type From 800b1f72d9cf6ac62d25198753ab522caed57b45 Mon Sep 17 00:00:00 2001 From: "Jorge C. Leitao" Date: Mon, 15 Nov 2021 07:27:25 +0000 Subject: [PATCH 3/3] Migrated tests and benches. --- benches/arithmetic_kernels.rs | 2 +- benches/comparison_kernels.rs | 2 +- benches/write_ipc.rs | 2 +- examples/arithmetics.rs | 28 ++++++---------- examples/csv_read_async.rs | 2 -- examples/growable.rs | 2 +- src/compute/arithmetics/basic/add.rs | 16 ++++----- src/compute/arithmetics/basic/mod.rs | 6 ++-- src/compute/arithmetics/basic/rem.rs | 2 +- src/compute/arithmetics/basic/sub.rs | 2 +- src/compute/arithmetics/decimal/add.rs | 12 +++---- src/compute/arithmetics/decimal/div.rs | 10 +++--- src/compute/arithmetics/decimal/mul.rs | 8 ++--- src/compute/arithmetics/decimal/sub.rs | 14 ++++---- src/compute/arithmetics/time.rs | 4 +-- tests/it/compute/aggregate/sum.rs | 2 +- tests/it/compute/arithmetics/basic/add.rs | 37 ++++++++++----------- tests/it/compute/arithmetics/basic/div.rs | 19 +++++------ tests/it/compute/arithmetics/basic/mul.rs | 35 ++++++++++--------- tests/it/compute/arithmetics/basic/rem.rs | 19 +++++------ tests/it/compute/arithmetics/basic/sub.rs | 35 ++++++++++--------- tests/it/compute/arithmetics/decimal/add.rs | 29 ++++++++-------- tests/it/compute/arithmetics/decimal/div.rs | 25 ++++++-------- tests/it/compute/arithmetics/decimal/mul.rs | 27 +++++++-------- tests/it/compute/arithmetics/decimal/sub.rs | 27 +++++++-------- tests/it/compute/arithmetics/mod.rs | 35 ++++++++++--------- tests/it/compute/arithmetics/time.rs | 32 +++++++++--------- tests/it/compute/bitwise.rs | 6 ++-- 28 files changed, 206 insertions(+), 234 deletions(-) diff --git a/benches/arithmetic_kernels.rs b/benches/arithmetic_kernels.rs index ce940c32e24..4d800bd7bec 100644 --- a/benches/arithmetic_kernels.rs +++ b/benches/arithmetic_kernels.rs @@ -22,7 +22,7 @@ fn bench_add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) where T: NativeType + Add + NumCast, { - criterion::black_box(add(lhs, rhs)).unwrap(); + criterion::black_box(add(lhs, rhs)); } fn add_benchmark(c: &mut Criterion) { diff --git a/benches/comparison_kernels.rs b/benches/comparison_kernels.rs index 9842153973b..86e48a89676 100644 --- a/benches/comparison_kernels.rs +++ b/benches/comparison_kernels.rs @@ -2,7 +2,7 @@ use criterion::{criterion_group, criterion_main, Criterion}; use arrow2::scalar::*; use arrow2::util::bench_util::*; -use arrow2::{compute::comparison::*, datatypes::DataType}; +use arrow2::{compute::comparison::eq, datatypes::DataType}; fn add_benchmark(c: &mut Criterion) { (10..=20).step_by(2).for_each(|log2_size| { diff --git a/benches/write_ipc.rs b/benches/write_ipc.rs index ab60d3b856a..96b17299b75 100644 --- a/benches/write_ipc.rs +++ b/benches/write_ipc.rs @@ -16,7 +16,7 @@ fn write(array: &dyn Array) -> Result<()> { let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![clone(array).into()])?; let writer = Cursor::new(vec![]); - let mut writer = FileWriter::try_new(writer, &schema)?; + let mut writer = FileWriter::try_new(writer, &schema, Default::default())?; writer.write(&batch) } diff --git a/examples/arithmetics.rs b/examples/arithmetics.rs index 3123b951521..fe931b07e3e 100644 --- a/examples/arithmetics.rs +++ b/examples/arithmetics.rs @@ -1,48 +1,44 @@ use arrow2::array::{Array, PrimitiveArray}; -use arrow2::compute::arithmetics::*; +use arrow2::compute::arithmetics::basic::*; +use arrow2::compute::arithmetics::{add as dyn_add, can_add}; use arrow2::compute::arity::{binary, unary}; use arrow2::datatypes::DataType; -use arrow2::error::Result; -fn main() -> Result<()> { +fn main() { // say we have two arrays let array0 = PrimitiveArray::::from(&[Some(1), Some(2), Some(3)]); let array1 = PrimitiveArray::::from(&[Some(4), None, Some(6)]); // we can add them as follows: - let added = arithmetic_primitive(&array0, Operator::Add, &array1)?; + let added = add(&array0, &array1); assert_eq!( added, PrimitiveArray::::from(&[Some(5), None, Some(9)]) ); // subtract: - let subtracted = arithmetic_primitive(&array0, Operator::Subtract, &array1)?; + let subtracted = sub(&array0, &array1); assert_eq!( subtracted, PrimitiveArray::::from(&[Some(-3), None, Some(-3)]) ); // add a scalar: - let plus10 = arithmetic_primitive_scalar(&array0, Operator::Add, &10)?; + let plus10 = add_scalar(&array0, &10); assert_eq!( plus10, PrimitiveArray::::from(&[Some(11), Some(12), Some(13)]) ); - // when the array is a trait object, there is a similar API + // a similar API for trait objects: let array0 = &array0 as &dyn Array; let array1 = &array1 as &dyn Array; - // check whether the logical types support addition (they could be any `Array`). - assert!(can_arithmetic( - array0.data_type(), - Operator::Add, - array1.data_type() - )); + // check whether the logical types support addition. + assert!(can_add(array0.data_type(), array1.data_type())); // add them - let added = arithmetic(array0, Operator::Add, array1).unwrap(); + let added = dyn_add(array0, array1); assert_eq!( PrimitiveArray::::from(&[Some(5), None, Some(9)]), added.as_ref(), @@ -54,7 +50,7 @@ fn main() -> Result<()> { let array1 = PrimitiveArray::::from(&[Some(4), None, Some(6)]); let op = |x: i64, y: i64| x.pow(2) + y.pow(2); - let r = binary(&array0, &array1, DataType::Int64, op)?; + let r = binary(&array0, &array1, DataType::Int64, op); assert_eq!( r, PrimitiveArray::::from(&[Some(1 + 16), None, Some(9 + 36)]) @@ -79,6 +75,4 @@ fn main() -> Result<()> { rounded, PrimitiveArray::::from(&[Some(4), None, Some(5)]) ); - - Ok(()) } diff --git a/examples/csv_read_async.rs b/examples/csv_read_async.rs index fa0e9481fc5..c893f59d809 100644 --- a/examples/csv_read_async.rs +++ b/examples/csv_read_async.rs @@ -1,10 +1,8 @@ use std::sync::Arc; -use futures::io::Cursor; use tokio::fs::File; use tokio_util::compat::*; -use arrow2::array::*; use arrow2::error::Result; use arrow2::io::csv::read_async::*; diff --git a/examples/growable.rs b/examples/growable.rs index cc8b7573e49..c79d9cd0cbb 100644 --- a/examples/growable.rs +++ b/examples/growable.rs @@ -1,5 +1,5 @@ use arrow2::array::growable::{Growable, GrowablePrimitive}; -use arrow2::array::{Array, PrimitiveArray}; +use arrow2::array::PrimitiveArray; fn main() { // say we have two sorted arrays diff --git a/src/compute/arithmetics/basic/add.rs b/src/compute/arithmetics/basic/add.rs index 3edf52b7f06..5d4fd7215d5 100644 --- a/src/compute/arithmetics/basic/add.rs +++ b/src/compute/arithmetics/basic/add.rs @@ -8,8 +8,8 @@ use crate::{ bitmap::Bitmap, compute::{ arithmetics::{ - basic::check_same_type, ArrayAdd, ArrayCheckedAdd, ArrayOverflowingAdd, - ArraySaturatingAdd, ArrayWrappingAdd, NativeArithmetics, + ArrayAdd, ArrayCheckedAdd, ArrayOverflowingAdd, ArraySaturatingAdd, ArrayWrappingAdd, + NativeArithmetics, }, arity::{ binary, binary_checked, binary_with_bitmap, unary, unary_checked, unary_with_bitmap, @@ -28,7 +28,7 @@ use crate::{ /// /// let a = PrimitiveArray::from([None, Some(6), None, Some(6)]); /// let b = PrimitiveArray::from([Some(5), None, None, Some(6)]); -/// let result = add(&a, &b).unwrap(); +/// let result = add(&a, &b); /// let expected = PrimitiveArray::from([None, None, None, Some(12)]); /// assert_eq!(result, expected) /// ``` @@ -49,7 +49,7 @@ where /// /// let a = PrimitiveArray::from([Some(-100i8), Some(100i8), Some(100i8)]); /// let b = PrimitiveArray::from([Some(0i8), Some(100i8), Some(0i8)]); -/// let result = wrapping_add(&a, &b).unwrap(); +/// let result = wrapping_add(&a, &b); /// let expected = PrimitiveArray::from([Some(-100i8), Some(-56i8), Some(100i8)]); /// assert_eq!(result, expected); /// ``` @@ -72,7 +72,7 @@ where /// /// let a = PrimitiveArray::from([Some(100i8), Some(100i8), Some(100i8)]); /// let b = PrimitiveArray::from([Some(0i8), Some(100i8), Some(0i8)]); -/// let result = checked_add(&a, &b).unwrap(); +/// let result = checked_add(&a, &b); /// let expected = PrimitiveArray::from([Some(100i8), None, Some(100i8)]); /// assert_eq!(result, expected); /// ``` @@ -80,8 +80,6 @@ pub fn checked_add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Primi where T: NativeType + CheckedAdd, { - check_same_type(lhs, rhs).unwrap(); - let op = move |a: T, b: T| a.checked_add(&b); binary_checked(lhs, rhs, lhs.data_type().clone(), op) @@ -98,7 +96,7 @@ where /// /// let a = PrimitiveArray::from([Some(100i8)]); /// let b = PrimitiveArray::from([Some(100i8)]); -/// let result = saturating_add(&a, &b).unwrap(); +/// let result = saturating_add(&a, &b); /// let expected = PrimitiveArray::from([Some(127)]); /// assert_eq!(result, expected); /// ``` @@ -123,7 +121,7 @@ where /// /// let a = PrimitiveArray::from([Some(1i8), Some(100i8)]); /// let b = PrimitiveArray::from([Some(1i8), Some(100i8)]); -/// let (result, overflow) = overflowing_add(&a, &b).unwrap(); +/// let (result, overflow) = overflowing_add(&a, &b); /// let expected = PrimitiveArray::from([Some(2i8), Some(-56i8)]); /// assert_eq!(result, expected); /// ``` diff --git a/src/compute/arithmetics/basic/mod.rs b/src/compute/arithmetics/basic/mod.rs index 492a29b37a9..d9126e3a7f4 100644 --- a/src/compute/arithmetics/basic/mod.rs +++ b/src/compute/arithmetics/basic/mod.rs @@ -36,7 +36,7 @@ use super::super::arity::{unary, unary_checked}; /// /// # Examples /// ``` -/// use arrow2::compute::arithmetics::negate; +/// use arrow2::compute::arithmetics::basic::negate; /// use arrow2::array::PrimitiveArray; /// /// let a = PrimitiveArray::from([None, Some(6), None, Some(7)]); @@ -55,7 +55,7 @@ where /// /// # Examples /// ``` -/// use arrow2::compute::arithmetics::checked_negate; +/// use arrow2::compute::arithmetics::basic::checked_negate; /// use arrow2::array::{Array, PrimitiveArray}; /// /// let a = PrimitiveArray::from([None, Some(6), Some(i8::MIN), Some(7)]); @@ -75,7 +75,7 @@ where /// /// # Examples /// ``` -/// use arrow2::compute::arithmetics::wrapping_negate; +/// use arrow2::compute::arithmetics::basic::wrapping_negate; /// use arrow2::array::{Array, PrimitiveArray}; /// /// let a = PrimitiveArray::from([None, Some(6), Some(i8::MIN), Some(7)]); diff --git a/src/compute/arithmetics/basic/rem.rs b/src/compute/arithmetics/basic/rem.rs index 8210fe05ca2..30300385028 100644 --- a/src/compute/arithmetics/basic/rem.rs +++ b/src/compute/arithmetics/basic/rem.rs @@ -47,7 +47,7 @@ where /// /// let a = Int8Array::from(&[Some(-100i8), Some(10i8)]); /// let b = Int8Array::from(&[Some(100i8), Some(0i8)]); -/// let result = checked_rem(&a, &b).unwrap(); +/// let result = checked_rem(&a, &b); /// let expected = Int8Array::from(&[Some(-0i8), None]); /// assert_eq!(result, expected); /// ``` diff --git a/src/compute/arithmetics/basic/sub.rs b/src/compute/arithmetics/basic/sub.rs index b5e5ff843a0..d4067be3fc5 100644 --- a/src/compute/arithmetics/basic/sub.rs +++ b/src/compute/arithmetics/basic/sub.rs @@ -72,7 +72,7 @@ where /// /// let a = Int8Array::from(&[Some(100i8), Some(-100i8), Some(100i8)]); /// let b = Int8Array::from(&[Some(1i8), Some(100i8), Some(0i8)]); -/// let result = checked_sub(&a, &b).unwrap(); +/// let result = checked_sub(&a, &b); /// let expected = Int8Array::from(&[Some(99i8), None, Some(100i8)]); /// assert_eq!(result, expected); /// ``` diff --git a/src/compute/arithmetics/decimal/add.rs b/src/compute/arithmetics/decimal/add.rs index 98ccbd07dc6..e6a33ff4490 100644 --- a/src/compute/arithmetics/decimal/add.rs +++ b/src/compute/arithmetics/decimal/add.rs @@ -25,7 +25,7 @@ use super::{adjusted_precision_scale, get_parameters, max_value, number_digits}; /// /// # Examples /// ``` -/// use arrow2::compute::arithmetics::decimal::add::add; +/// use arrow2::compute::arithmetics::decimal::add; /// use arrow2::array::PrimitiveArray; /// use arrow2::datatypes::DataType; /// @@ -64,14 +64,14 @@ pub fn add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveA /// /// # Examples /// ``` -/// use arrow2::compute::arithmetics::decimal::add::saturating_add; +/// use arrow2::compute::arithmetics::decimal::saturating_add; /// use arrow2::array::PrimitiveArray; /// use arrow2::datatypes::DataType; /// /// let a = PrimitiveArray::from([Some(99000i128), Some(11100i128), None, Some(22200i128)]).to(DataType::Decimal(5, 2)); /// let b = PrimitiveArray::from([Some(01000i128), Some(22200i128), None, Some(11100i128)]).to(DataType::Decimal(5, 2)); /// -/// let result = saturating_add(&a, &b).unwrap(); +/// let result = saturating_add(&a, &b); /// let expected = PrimitiveArray::from([Some(99999i128), Some(33300i128), None, Some(33300i128)]).to(DataType::Decimal(5, 2)); /// /// assert_eq!(result, expected); @@ -108,7 +108,7 @@ pub fn saturating_add( /// /// # Examples /// ``` -/// use arrow2::compute::arithmetics::decimal::add::checked_add; +/// use arrow2::compute::arithmetics::decimal::checked_add; /// use arrow2::array::PrimitiveArray; /// use arrow2::datatypes::DataType; /// @@ -172,13 +172,13 @@ impl ArraySaturatingAdd> for PrimitiveArray { /// ``` /// # Examples /// ``` -/// use arrow2::compute::arithmetics::decimal::add::adaptive_add; +/// use arrow2::compute::arithmetics::decimal::adaptive_add; /// use arrow2::array::PrimitiveArray; /// use arrow2::datatypes::DataType; /// /// let a = PrimitiveArray::from([Some(11111_11i128)]).to(DataType::Decimal(7, 2)); /// let b = PrimitiveArray::from([Some(11111_111i128)]).to(DataType::Decimal(8, 3)); -/// let result = adaptive_add(&a, &b); +/// let result = adaptive_add(&a, &b).unwrap(); /// let expected = PrimitiveArray::from([Some(22222_221i128)]).to(DataType::Decimal(8, 3)); /// /// assert_eq!(result, expected); diff --git a/src/compute/arithmetics/decimal/div.rs b/src/compute/arithmetics/decimal/div.rs index 92c02602040..b83e057f632 100644 --- a/src/compute/arithmetics/decimal/div.rs +++ b/src/compute/arithmetics/decimal/div.rs @@ -24,7 +24,7 @@ use super::{adjusted_precision_scale, get_parameters, max_value, number_digits}; /// /// # Examples /// ``` -/// use arrow2::compute::arithmetics::decimal::div::div; +/// use arrow2::compute::arithmetics::decimal::div; /// use arrow2::array::PrimitiveArray; /// use arrow2::datatypes::DataType; /// @@ -77,7 +77,7 @@ pub fn div(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveA /// /// # Examples /// ``` -/// use arrow2::compute::arithmetics::decimal::div::saturating_div; +/// use arrow2::compute::arithmetics::decimal::saturating_div; /// use arrow2::array::PrimitiveArray; /// use arrow2::datatypes::DataType; /// @@ -126,7 +126,7 @@ pub fn saturating_div( /// /// # Examples /// ``` -/// use arrow2::compute::arithmetics::decimal::div::checked_div; +/// use arrow2::compute::arithmetics::decimal::checked_div; /// use arrow2::array::PrimitiveArray; /// use arrow2::datatypes::DataType; /// @@ -188,13 +188,13 @@ impl ArrayCheckedDiv> for PrimitiveArray { /// ``` /// # Examples /// ``` -/// use arrow2::compute::arithmetics::decimal::div::adaptive_div; +/// use arrow2::compute::arithmetics::decimal::adaptive_div; /// use arrow2::array::PrimitiveArray; /// use arrow2::datatypes::DataType; /// /// let a = PrimitiveArray::from([Some(1000_00i128)]).to(DataType::Decimal(7, 2)); /// let b = PrimitiveArray::from([Some(10_0000i128)]).to(DataType::Decimal(6, 4)); -/// let result = adaptive_div(&a, &b); +/// let result = adaptive_div(&a, &b).unwrap(); /// let expected = PrimitiveArray::from([Some(100_0000i128)]).to(DataType::Decimal(9, 4)); /// /// assert_eq!(result, expected); diff --git a/src/compute/arithmetics/decimal/mul.rs b/src/compute/arithmetics/decimal/mul.rs index 8ccf5c7d9dd..b3858aa8e13 100644 --- a/src/compute/arithmetics/decimal/mul.rs +++ b/src/compute/arithmetics/decimal/mul.rs @@ -23,7 +23,7 @@ use super::{adjusted_precision_scale, get_parameters, max_value, number_digits}; /// /// # Examples /// ``` -/// use arrow2::compute::arithmetics::decimal::mul::mul; +/// use arrow2::compute::arithmetics::decimal::mul; /// use arrow2::array::PrimitiveArray; /// use arrow2::datatypes::DataType; /// @@ -78,7 +78,7 @@ pub fn mul(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveA /// /// # Examples /// ``` -/// use arrow2::compute::arithmetics::decimal::mul::saturating_mul; +/// use arrow2::compute::arithmetics::decimal::saturating_mul; /// use arrow2::array::PrimitiveArray; /// use arrow2::datatypes::DataType; /// @@ -128,7 +128,7 @@ pub fn saturating_mul( /// /// # Examples /// ``` -/// use arrow2::compute::arithmetics::decimal::mul::checked_mul; +/// use arrow2::compute::arithmetics::decimal::checked_mul; /// use arrow2::array::PrimitiveArray; /// use arrow2::datatypes::DataType; /// @@ -197,7 +197,7 @@ impl ArraySaturatingMul> for PrimitiveArray { /// ``` /// # Examples /// ``` -/// use arrow2::compute::arithmetics::decimal::mul::adaptive_mul; +/// use arrow2::compute::arithmetics::decimal::adaptive_mul; /// use arrow2::array::PrimitiveArray; /// use arrow2::datatypes::DataType; /// diff --git a/src/compute/arithmetics/decimal/sub.rs b/src/compute/arithmetics/decimal/sub.rs index 973bc11e4c0..d7870401a3f 100644 --- a/src/compute/arithmetics/decimal/sub.rs +++ b/src/compute/arithmetics/decimal/sub.rs @@ -22,14 +22,14 @@ use super::{adjusted_precision_scale, get_parameters, max_value, number_digits}; /// /// # Examples /// ``` -/// use arrow2::compute::arithmetics::decimal::sub::sub; +/// use arrow2::compute::arithmetics::decimal::sub; /// use arrow2::array::PrimitiveArray; /// use arrow2::datatypes::DataType; /// /// let a = PrimitiveArray::from([Some(1i128), Some(1i128), None, Some(2i128)]).to(DataType::Decimal(5, 2)); /// let b = PrimitiveArray::from([Some(1i128), Some(2i128), None, Some(2i128)]).to(DataType::Decimal(5, 2)); /// -/// let result = sub(&a, &b).unwrap(); +/// let result = sub(&a, &b); /// let expected = PrimitiveArray::from([Some(0i128), Some(-1i128), None, Some(0i128)]).to(DataType::Decimal(5, 2)); /// /// assert_eq!(result, expected); @@ -62,14 +62,14 @@ pub fn sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveA /// /// # Examples /// ``` -/// use arrow2::compute::arithmetics::decimal::sub::saturating_sub; +/// use arrow2::compute::arithmetics::decimal::saturating_sub; /// use arrow2::array::PrimitiveArray; /// use arrow2::datatypes::DataType; /// /// let a = PrimitiveArray::from([Some(-99000i128), Some(11100i128), None, Some(22200i128)]).to(DataType::Decimal(5, 2)); /// let b = PrimitiveArray::from([Some(01000i128), Some(22200i128), None, Some(11100i128)]).to(DataType::Decimal(5, 2)); /// -/// let result = saturating_sub(&a, &b).unwrap(); +/// let result = saturating_sub(&a, &b); /// let expected = PrimitiveArray::from([Some(-99999i128), Some(-11100i128), None, Some(11100i128)]).to(DataType::Decimal(5, 2)); /// /// assert_eq!(result, expected); @@ -128,14 +128,14 @@ impl ArraySaturatingSub> for PrimitiveArray { /// /// # Examples /// ``` -/// use arrow2::compute::arithmetics::decimal::sub::checked_sub; +/// use arrow2::compute::arithmetics::decimal::checked_sub; /// use arrow2::array::PrimitiveArray; /// use arrow2::datatypes::DataType; /// /// let a = PrimitiveArray::from([Some(-99000i128), Some(11100i128), None, Some(22200i128)]).to(DataType::Decimal(5, 2)); /// let b = PrimitiveArray::from([Some(01000i128), Some(22200i128), None, Some(11100i128)]).to(DataType::Decimal(5, 2)); /// -/// let result = checked_sub(&a, &b).unwrap(); +/// let result = checked_sub(&a, &b); /// let expected = PrimitiveArray::from([None, Some(-11100i128), None, Some(11100i128)]).to(DataType::Decimal(5, 2)); /// /// assert_eq!(result, expected); @@ -171,7 +171,7 @@ pub fn checked_sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Pr /// ``` /// # Examples /// ``` -/// use arrow2::compute::arithmetics::decimal::sub::adaptive_sub; +/// use arrow2::compute::arithmetics::decimal::adaptive_sub; /// use arrow2::array::PrimitiveArray; /// use arrow2::datatypes::DataType; /// diff --git a/src/compute/arithmetics/time.rs b/src/compute/arithmetics/time.rs index d622652823e..9ef1930e95e 100644 --- a/src/compute/arithmetics/time.rs +++ b/src/compute/arithmetics/time.rs @@ -86,7 +86,7 @@ fn create_scale(lhs: &DataType, rhs: &DataType) -> Result { /// let duration = PrimitiveArray::from([Some(10i64), Some(20i64), None, Some(30i64)]) /// .to(DataType::Duration(TimeUnit::Second)); /// -/// let result = add_duration(×tamp, &duration).unwrap(); +/// let result = add_duration(×tamp, &duration); /// let expected = PrimitiveArray::from([ /// Some(100010i64), /// Some(200020i64), @@ -141,7 +141,7 @@ where /// let duration = PrimitiveArray::from([Some(10i64), Some(20i64), None, Some(30i64)]) /// .to(DataType::Duration(TimeUnit::Second)); /// -/// let result = subtract_duration(×tamp, &duration).unwrap(); +/// let result = subtract_duration(×tamp, &duration); /// let expected = PrimitiveArray::from([ /// Some(99990i64), /// Some(199980i64), diff --git a/tests/it/compute/aggregate/sum.rs b/tests/it/compute/aggregate/sum.rs index 56c93624a0e..c1d8190359b 100644 --- a/tests/it/compute/aggregate/sum.rs +++ b/tests/it/compute/aggregate/sum.rs @@ -46,7 +46,7 @@ fn test_primitive_array_sum_large_64() { .map(|i| if i % 3 == 0 { Some(0) } else { Some(i) }) .collect(); // create an array that actually has non-zero values at the invalid indices - let c = arithmetics::basic::add(&a, &b).unwrap(); + let c = arithmetics::basic::add(&a, &b); assert_eq!( Some((1..=100).filter(|i| i % 3 == 0).sum()), sum_primitive(&c) diff --git a/tests/it/compute/arithmetics/basic/add.rs b/tests/it/compute/arithmetics/basic/add.rs index 32ccb5a0230..c02f4c7d71c 100644 --- a/tests/it/compute/arithmetics/basic/add.rs +++ b/tests/it/compute/arithmetics/basic/add.rs @@ -6,24 +6,23 @@ use arrow2::compute::arithmetics::{ }; #[test] +#[should_panic] fn test_add_mismatched_length() { let a = Int32Array::from_slice(&[5, 6]); let b = Int32Array::from_slice(&[5]); - add(&a, &b) - .err() - .expect("should have failed due to different lengths"); + add(&a, &b); } #[test] fn test_add() { let a = Int32Array::from(&[None, Some(6), None, Some(6)]); let b = Int32Array::from(&[Some(5), None, None, Some(6)]); - let result = add(&a, &b).unwrap(); + let result = add(&a, &b); let expected = Int32Array::from(&[None, None, None, Some(12)]); assert_eq!(result, expected); // Trait testing - let result = a.add(&b).unwrap(); + let result = a.add(&b); assert_eq!(result, expected); } @@ -32,25 +31,25 @@ fn test_add() { fn test_add_panic() { let a = Int8Array::from(&[Some(100i8)]); let b = Int8Array::from(&[Some(100i8)]); - let _ = add(&a, &b); + add(&a, &b); } #[test] fn test_add_checked() { let a = Int32Array::from(&[None, Some(6), None, Some(6)]); let b = Int32Array::from(&[Some(5), None, None, Some(6)]); - let result = checked_add(&a, &b).unwrap(); + let result = checked_add(&a, &b); let expected = Int32Array::from(&[None, None, None, Some(12)]); assert_eq!(result, expected); let a = Int8Array::from(&[Some(100i8), Some(100i8), Some(100i8)]); let b = Int8Array::from(&[Some(0i8), Some(100i8), Some(0i8)]); - let result = checked_add(&a, &b).unwrap(); + let result = checked_add(&a, &b); let expected = Int8Array::from(&[Some(100i8), None, Some(100i8)]); assert_eq!(result, expected); // Trait testing - let result = a.checked_add(&b).unwrap(); + let result = a.checked_add(&b); assert_eq!(result, expected); } @@ -58,18 +57,18 @@ fn test_add_checked() { fn test_add_saturating() { let a = Int32Array::from(&[None, Some(6), None, Some(6)]); let b = Int32Array::from(&[Some(5), None, None, Some(6)]); - let result = saturating_add(&a, &b).unwrap(); + let result = saturating_add(&a, &b); let expected = Int32Array::from(&[None, None, None, Some(12)]); assert_eq!(result, expected); let a = Int8Array::from(&[Some(100i8)]); let b = Int8Array::from(&[Some(100i8)]); - let result = saturating_add(&a, &b).unwrap(); + let result = saturating_add(&a, &b); let expected = Int8Array::from(&[Some(127)]); assert_eq!(result, expected); // Trait testing - let result = a.saturating_add(&b).unwrap(); + let result = a.saturating_add(&b); assert_eq!(result, expected); } @@ -77,20 +76,20 @@ fn test_add_saturating() { fn test_add_overflowing() { let a = Int32Array::from(&[None, Some(6), None, Some(6)]); let b = Int32Array::from(&[Some(5), None, None, Some(6)]); - let (result, overflow) = overflowing_add(&a, &b).unwrap(); + let (result, overflow) = overflowing_add(&a, &b); let expected = Int32Array::from(&[None, None, None, Some(12)]); assert_eq!(result, expected); assert_eq!(overflow, Bitmap::from([false, false, false, false])); let a = Int8Array::from(&[Some(1i8), Some(100i8)]); let b = Int8Array::from(&[Some(1i8), Some(100i8)]); - let (result, overflow) = overflowing_add(&a, &b).unwrap(); + let (result, overflow) = overflowing_add(&a, &b); let expected = Int8Array::from(&[Some(2i8), Some(-56i8)]); assert_eq!(result, expected); assert_eq!(overflow, Bitmap::from([false, true])); // Trait testing - let (result, overflow) = a.overflowing_add(&b).unwrap(); + let (result, overflow) = a.overflowing_add(&b); assert_eq!(result, expected); assert_eq!(overflow, Bitmap::from([false, true])); } @@ -103,7 +102,7 @@ fn test_add_scalar() { assert_eq!(result, expected); // Trait testing - let result = a.add(&1i32).unwrap(); + let result = a.add(&1i32); assert_eq!(result, expected); } @@ -120,7 +119,7 @@ fn test_add_scalar_checked() { assert_eq!(result, expected); // Trait testing - let result = a.checked_add(&100i8).unwrap(); + let result = a.checked_add(&100i8); assert_eq!(result, expected); } @@ -137,7 +136,7 @@ fn test_add_scalar_saturating() { assert_eq!(result, expected); // Trait testing - let result = a.saturating_add(&100i8).unwrap(); + let result = a.saturating_add(&100i8); assert_eq!(result, expected); } @@ -156,7 +155,7 @@ fn test_add_scalar_overflowing() { assert_eq!(overflow, Bitmap::from([false, true])); // Trait testing - let (result, overflow) = a.overflowing_add(&100i8).unwrap(); + let (result, overflow) = a.overflowing_add(&100i8); assert_eq!(result, expected); assert_eq!(overflow, Bitmap::from([false, true])); } diff --git a/tests/it/compute/arithmetics/basic/div.rs b/tests/it/compute/arithmetics/basic/div.rs index 6fd00c7bf4f..8547795842d 100644 --- a/tests/it/compute/arithmetics/basic/div.rs +++ b/tests/it/compute/arithmetics/basic/div.rs @@ -3,24 +3,23 @@ use arrow2::compute::arithmetics::basic::*; use arrow2::compute::arithmetics::{ArrayCheckedDiv, ArrayDiv}; #[test] +#[should_panic] fn test_div_mismatched_length() { let a = Int32Array::from_slice(&[5, 6]); let b = Int32Array::from_slice(&[5]); - div(&a, &b) - .err() - .expect("should have failed due to different lengths"); + div(&a, &b); } #[test] fn test_div() { let a = Int32Array::from(&[Some(5), Some(6)]); let b = Int32Array::from(&[Some(5), Some(6)]); - let result = div(&a, &b).unwrap(); + let result = div(&a, &b); let expected = Int32Array::from(&[Some(1), Some(1)]); assert_eq!(result, expected); // Trait testing - let result = a.div(&b).unwrap(); + let result = a.div(&b); assert_eq!(result, expected); } @@ -36,18 +35,18 @@ fn test_div_panic() { fn test_div_checked() { let a = Int32Array::from(&[Some(5), None, Some(3), Some(6)]); let b = Int32Array::from(&[Some(5), Some(3), None, Some(6)]); - let result = checked_div(&a, &b).unwrap(); + let result = checked_div(&a, &b); let expected = Int32Array::from(&[Some(1), None, None, Some(1)]); assert_eq!(result, expected); let a = Int32Array::from(&[Some(5), None, Some(3), Some(6)]); let b = Int32Array::from(&[Some(5), Some(0), Some(0), Some(6)]); - let result = checked_div(&a, &b).unwrap(); + let result = checked_div(&a, &b); let expected = Int32Array::from(&[Some(1), None, None, Some(1)]); assert_eq!(result, expected); // Trait testing - let result = a.checked_div(&b).unwrap(); + let result = a.checked_div(&b); assert_eq!(result, expected); } @@ -59,7 +58,7 @@ fn test_div_scalar() { assert_eq!(result, expected); // Trait testing - let result = a.div(&1i32).unwrap(); + let result = a.div(&1i32); assert_eq!(result, expected); // check the strength reduced branches @@ -97,6 +96,6 @@ fn test_div_scalar_checked() { assert_eq!(result, expected); // Trait testing - let result = a.checked_div(&0).unwrap(); + let result = a.checked_div(&0); assert_eq!(result, expected); } diff --git a/tests/it/compute/arithmetics/basic/mul.rs b/tests/it/compute/arithmetics/basic/mul.rs index 5c2b8e57a58..d202edecb15 100644 --- a/tests/it/compute/arithmetics/basic/mul.rs +++ b/tests/it/compute/arithmetics/basic/mul.rs @@ -6,24 +6,23 @@ use arrow2::compute::arithmetics::{ }; #[test] +#[should_panic] fn test_mul_mismatched_length() { let a = Int32Array::from_slice(&[5, 6]); let b = Int32Array::from_slice(&[5]); - mul(&a, &b) - .err() - .expect("should have failed due to different lengths"); + mul(&a, &b); } #[test] fn test_mul() { let a = Int32Array::from(&[None, Some(6), None, Some(6)]); let b = Int32Array::from(&[Some(5), None, None, Some(6)]); - let result = mul(&a, &b).unwrap(); + let result = mul(&a, &b); let expected = Int32Array::from(&[None, None, None, Some(36)]); assert_eq!(result, expected); // Trait testing - let result = a.mul(&b).unwrap(); + let result = a.mul(&b); assert_eq!(result, expected); } @@ -39,18 +38,18 @@ fn test_mul_panic() { fn test_mul_checked() { let a = Int32Array::from(&[None, Some(6), None, Some(6)]); let b = Int32Array::from(&[Some(5), None, None, Some(6)]); - let result = checked_mul(&a, &b).unwrap(); + let result = checked_mul(&a, &b); let expected = Int32Array::from(&[None, None, None, Some(36)]); assert_eq!(result, expected); let a = Int8Array::from(&[Some(100i8), Some(100i8), Some(100i8)]); let b = Int8Array::from(&[Some(1i8), Some(100i8), Some(1i8)]); - let result = checked_mul(&a, &b).unwrap(); + let result = checked_mul(&a, &b); let expected = Int8Array::from(&[Some(100i8), None, Some(100i8)]); assert_eq!(result, expected); // Trait testing - let result = a.checked_mul(&b).unwrap(); + let result = a.checked_mul(&b); assert_eq!(result, expected); } @@ -58,18 +57,18 @@ fn test_mul_checked() { fn test_mul_saturating() { let a = Int32Array::from(&[None, Some(6), None, Some(6)]); let b = Int32Array::from(&[Some(5), None, None, Some(6)]); - let result = saturating_mul(&a, &b).unwrap(); + let result = saturating_mul(&a, &b); let expected = Int32Array::from(&[None, None, None, Some(36)]); assert_eq!(result, expected); let a = Int8Array::from(&[Some(-100i8)]); let b = Int8Array::from(&[Some(100i8)]); - let result = saturating_mul(&a, &b).unwrap(); + let result = saturating_mul(&a, &b); let expected = Int8Array::from(&[Some(-128)]); assert_eq!(result, expected); // Trait testing - let result = a.saturating_mul(&b).unwrap(); + let result = a.saturating_mul(&b); assert_eq!(result, expected); } @@ -77,20 +76,20 @@ fn test_mul_saturating() { fn test_mul_overflowing() { let a = Int32Array::from(&[None, Some(6), None, Some(6)]); let b = Int32Array::from(&[Some(5), None, None, Some(6)]); - let (result, overflow) = overflowing_mul(&a, &b).unwrap(); + let (result, overflow) = overflowing_mul(&a, &b); let expected = Int32Array::from(&[None, None, None, Some(36)]); assert_eq!(result, expected); assert_eq!(overflow, Bitmap::from([false, false, false, false])); let a = Int8Array::from(&[Some(1i8), Some(-100i8)]); let b = Int8Array::from(&[Some(1i8), Some(100i8)]); - let (result, overflow) = overflowing_mul(&a, &b).unwrap(); + let (result, overflow) = overflowing_mul(&a, &b); let expected = Int8Array::from(&[Some(1i8), Some(-16i8)]); assert_eq!(result, expected); assert_eq!(overflow, Bitmap::from([false, true])); // Trait testing - let (result, overflow) = a.overflowing_mul(&b).unwrap(); + let (result, overflow) = a.overflowing_mul(&b); assert_eq!(result, expected); assert_eq!(overflow, Bitmap::from([false, true])); } @@ -103,7 +102,7 @@ fn test_mul_scalar() { assert_eq!(result, expected); // Trait testing - let result = a.mul(&1i32).unwrap(); + let result = a.mul(&1i32); assert_eq!(result, expected); } @@ -120,7 +119,7 @@ fn test_mul_scalar_checked() { assert_eq!(result, expected); // Trait testing - let result = a.checked_mul(&100i8).unwrap(); + let result = a.checked_mul(&100i8); assert_eq!(result, expected); } @@ -137,7 +136,7 @@ fn test_mul_scalar_saturating() { assert_eq!(result, expected); // Trait testing - let result = a.saturating_mul(&100i8).unwrap(); + let result = a.saturating_mul(&100i8); assert_eq!(result, expected); } @@ -156,7 +155,7 @@ fn test_mul_scalar_overflowing() { assert_eq!(overflow, Bitmap::from([false, true])); // Trait testing - let (result, overflow) = a.overflowing_mul(&100i8).unwrap(); + let (result, overflow) = a.overflowing_mul(&100i8); assert_eq!(result, expected); assert_eq!(overflow, Bitmap::from([false, true])); } diff --git a/tests/it/compute/arithmetics/basic/rem.rs b/tests/it/compute/arithmetics/basic/rem.rs index e5a487b4f16..52f6db82702 100644 --- a/tests/it/compute/arithmetics/basic/rem.rs +++ b/tests/it/compute/arithmetics/basic/rem.rs @@ -3,24 +3,23 @@ use arrow2::compute::arithmetics::basic::*; use arrow2::compute::arithmetics::{ArrayCheckedRem, ArrayRem}; #[test] +#[should_panic] fn test_rem_mismatched_length() { let a = Int32Array::from_slice(&[5, 6]); let b = Int32Array::from_slice(&[5]); - rem(&a, &b) - .err() - .expect("should have failed due to different lengths"); + rem(&a, &b); } #[test] fn test_rem() { let a = Int32Array::from(&[Some(5), Some(6)]); let b = Int32Array::from(&[Some(4), Some(4)]); - let result = rem(&a, &b).unwrap(); + let result = rem(&a, &b); let expected = Int32Array::from(&[Some(1), Some(2)]); assert_eq!(result, expected); // Trait testing - let result = a.rem(&b).unwrap(); + let result = a.rem(&b); assert_eq!(result, expected); } @@ -36,18 +35,18 @@ fn test_rem_panic() { fn test_rem_checked() { let a = Int32Array::from(&[Some(5), None, Some(3), Some(6)]); let b = Int32Array::from(&[Some(5), Some(3), None, Some(5)]); - let result = checked_rem(&a, &b).unwrap(); + let result = checked_rem(&a, &b); let expected = Int32Array::from(&[Some(0), None, None, Some(1)]); assert_eq!(result, expected); let a = Int32Array::from(&[Some(5), None, Some(3), Some(6)]); let b = Int32Array::from(&[Some(5), Some(0), Some(0), Some(5)]); - let result = checked_rem(&a, &b).unwrap(); + let result = checked_rem(&a, &b); let expected = Int32Array::from(&[Some(0), None, None, Some(1)]); assert_eq!(result, expected); // Trait testing - let result = a.checked_rem(&b).unwrap(); + let result = a.checked_rem(&b); assert_eq!(result, expected); } @@ -59,7 +58,7 @@ fn test_rem_scalar() { assert_eq!(result, expected); // Trait testing - let result = a.rem(&2i32).unwrap(); + let result = a.rem(&2i32); assert_eq!(result, expected); // check the strength reduced branches @@ -97,6 +96,6 @@ fn test_rem_scalar_checked() { assert_eq!(result, expected); // Trait testing - let result = a.checked_rem(&0).unwrap(); + let result = a.checked_rem(&0); assert_eq!(result, expected); } diff --git a/tests/it/compute/arithmetics/basic/sub.rs b/tests/it/compute/arithmetics/basic/sub.rs index 5c638c6e596..c56719f64b4 100644 --- a/tests/it/compute/arithmetics/basic/sub.rs +++ b/tests/it/compute/arithmetics/basic/sub.rs @@ -6,24 +6,23 @@ use arrow2::compute::arithmetics::{ }; #[test] +#[should_panic] fn test_sub_mismatched_length() { let a = Int32Array::from_slice(&[5, 6]); let b = Int32Array::from_slice(&[5]); - sub(&a, &b) - .err() - .expect("should have failed due to different lengths"); + sub(&a, &b); } #[test] fn test_sub() { let a = Int32Array::from(&[None, Some(6), None, Some(6)]); let b = Int32Array::from(&[Some(5), None, None, Some(6)]); - let result = sub(&a, &b).unwrap(); + let result = sub(&a, &b); let expected = Int32Array::from(&[None, None, None, Some(0)]); assert_eq!(result, expected); // Trait testing - let result = a.sub(&b).unwrap(); + let result = a.sub(&b); assert_eq!(result, expected); } @@ -39,18 +38,18 @@ fn test_sub_panic() { fn test_sub_checked() { let a = Int32Array::from(&[None, Some(6), None, Some(6)]); let b = Int32Array::from(&[Some(5), None, None, Some(6)]); - let result = checked_sub(&a, &b).unwrap(); + let result = checked_sub(&a, &b); let expected = Int32Array::from(&[None, None, None, Some(0)]); assert_eq!(result, expected); let a = Int8Array::from(&[Some(100i8), Some(-100i8), Some(100i8)]); let b = Int8Array::from(&[Some(1i8), Some(100i8), Some(0i8)]); - let result = checked_sub(&a, &b).unwrap(); + let result = checked_sub(&a, &b); let expected = Int8Array::from(&[Some(99i8), None, Some(100i8)]); assert_eq!(result, expected); // Trait testing - let result = a.checked_sub(&b).unwrap(); + let result = a.checked_sub(&b); assert_eq!(result, expected); } @@ -58,18 +57,18 @@ fn test_sub_checked() { fn test_sub_saturating() { let a = Int32Array::from(&[None, Some(6), None, Some(6)]); let b = Int32Array::from(&[Some(5), None, None, Some(6)]); - let result = saturating_sub(&a, &b).unwrap(); + let result = saturating_sub(&a, &b); let expected = Int32Array::from(&[None, None, None, Some(0)]); assert_eq!(result, expected); let a = Int8Array::from(&[Some(-100i8)]); let b = Int8Array::from(&[Some(100i8)]); - let result = saturating_sub(&a, &b).unwrap(); + let result = saturating_sub(&a, &b); let expected = Int8Array::from(&[Some(-128)]); assert_eq!(result, expected); // Trait testing - let result = a.saturating_sub(&b).unwrap(); + let result = a.saturating_sub(&b); assert_eq!(result, expected); } @@ -77,20 +76,20 @@ fn test_sub_saturating() { fn test_sub_overflowing() { let a = Int32Array::from(&[None, Some(6), None, Some(6)]); let b = Int32Array::from(&[Some(5), None, None, Some(6)]); - let (result, overflow) = overflowing_sub(&a, &b).unwrap(); + let (result, overflow) = overflowing_sub(&a, &b); let expected = Int32Array::from(&[None, None, None, Some(0)]); assert_eq!(result, expected); assert_eq!(overflow, Bitmap::from([false, false, false, false])); let a = Int8Array::from(&[Some(1i8), Some(-100i8)]); let b = Int8Array::from(&[Some(1i8), Some(100i8)]); - let (result, overflow) = overflowing_sub(&a, &b).unwrap(); + let (result, overflow) = overflowing_sub(&a, &b); let expected = Int8Array::from(&[Some(0i8), Some(56i8)]); assert_eq!(result, expected); assert_eq!(overflow, Bitmap::from([false, true])); // Trait testing - let (result, overflow) = a.overflowing_sub(&b).unwrap(); + let (result, overflow) = a.overflowing_sub(&b); assert_eq!(result, expected); assert_eq!(overflow, Bitmap::from([false, true])); } @@ -103,7 +102,7 @@ fn test_sub_scalar() { assert_eq!(result, expected); // Trait testing - let result = a.sub(&1i32).unwrap(); + let result = a.sub(&1i32); assert_eq!(result, expected); } @@ -120,7 +119,7 @@ fn test_sub_scalar_checked() { assert_eq!(result, expected); // Trait testing - let result = a.checked_sub(&100i8).unwrap(); + let result = a.checked_sub(&100i8); assert_eq!(result, expected); } @@ -137,7 +136,7 @@ fn test_sub_scalar_saturating() { assert_eq!(result, expected); // Trait testing - let result = a.saturating_sub(&100i8).unwrap(); + let result = a.saturating_sub(&100i8); assert_eq!(result, expected); } @@ -156,7 +155,7 @@ fn test_sub_scalar_overflowing() { assert_eq!(overflow, Bitmap::from([false, true])); // Trait testing - let (result, overflow) = a.overflowing_sub(&100i8).unwrap(); + let (result, overflow) = a.overflowing_sub(&100i8); assert_eq!(result, expected); assert_eq!(overflow, Bitmap::from([false, true])); } diff --git a/tests/it/compute/arithmetics/decimal/add.rs b/tests/it/compute/arithmetics/decimal/add.rs index 7f772a5b933..45af77b1519 100644 --- a/tests/it/compute/arithmetics/decimal/add.rs +++ b/tests/it/compute/arithmetics/decimal/add.rs @@ -1,7 +1,7 @@ #![allow(clippy::zero_prefixed_literal, clippy::inconsistent_digit_grouping)] use arrow2::array::*; -use arrow2::compute::arithmetics::decimal::add::*; +use arrow2::compute::arithmetics::decimal::{adaptive_add, add, checked_add, saturating_add}; use arrow2::compute::arithmetics::{ArrayAdd, ArrayCheckedAdd, ArraySaturatingAdd}; use arrow2::datatypes::DataType; @@ -13,26 +13,23 @@ fn test_add_normal() { let b = PrimitiveArray::from([Some(22222i128), Some(22200i128), None, Some(11100i128)]) .to(DataType::Decimal(5, 2)); - let result = add(&a, &b).unwrap(); + let result = add(&a, &b); let expected = PrimitiveArray::from([Some(33333i128), Some(33300i128), None, Some(33300i128)]) .to(DataType::Decimal(5, 2)); assert_eq!(result, expected); // Testing trait - let result = a.add(&b).unwrap(); + let result = a.add(&b); assert_eq!(result, expected); } #[test] +#[should_panic] fn test_add_decimal_wrong_precision() { let a = PrimitiveArray::from([None]).to(DataType::Decimal(5, 2)); let b = PrimitiveArray::from([None]).to(DataType::Decimal(6, 2)); - let result = add(&a, &b); - - if result.is_ok() { - panic!("Should panic for different precision"); - } + add(&a, &b); } #[test] @@ -51,14 +48,14 @@ fn test_add_saturating() { let b = PrimitiveArray::from([Some(22222i128), Some(22200i128), None, Some(11100i128)]) .to(DataType::Decimal(5, 2)); - let result = saturating_add(&a, &b).unwrap(); + let result = saturating_add(&a, &b); let expected = PrimitiveArray::from([Some(33333i128), Some(33300i128), None, Some(33300i128)]) .to(DataType::Decimal(5, 2)); assert_eq!(result, expected); // Testing trait - let result = a.saturating_add(&b).unwrap(); + let result = a.saturating_add(&b); assert_eq!(result, expected); } @@ -79,7 +76,7 @@ fn test_add_saturating_overflow() { ]) .to(DataType::Decimal(5, 2)); - let result = saturating_add(&a, &b).unwrap(); + let result = saturating_add(&a, &b); let expected = PrimitiveArray::from([ Some(99999i128), @@ -92,7 +89,7 @@ fn test_add_saturating_overflow() { assert_eq!(result, expected); // Testing trait - let result = a.saturating_add(&b).unwrap(); + let result = a.saturating_add(&b); assert_eq!(result, expected); } @@ -104,14 +101,14 @@ fn test_add_checked() { let b = PrimitiveArray::from([Some(22222i128), Some(22200i128), None, Some(11100i128)]) .to(DataType::Decimal(5, 2)); - let result = checked_add(&a, &b).unwrap(); + let result = checked_add(&a, &b); let expected = PrimitiveArray::from([Some(33333i128), Some(33300i128), None, Some(33300i128)]) .to(DataType::Decimal(5, 2)); assert_eq!(result, expected); // Testing trait - let result = a.checked_add(&b).unwrap(); + let result = a.checked_add(&b); assert_eq!(result, expected); } @@ -119,12 +116,12 @@ fn test_add_checked() { fn test_add_checked_overflow() { let a = PrimitiveArray::from([Some(1i128), Some(99999i128)]).to(DataType::Decimal(5, 2)); let b = PrimitiveArray::from([Some(1i128), Some(1i128)]).to(DataType::Decimal(5, 2)); - let result = checked_add(&a, &b).unwrap(); + let result = checked_add(&a, &b); let expected = PrimitiveArray::from([Some(2i128), None]).to(DataType::Decimal(5, 2)); assert_eq!(result, expected); // Testing trait - let result = a.checked_add(&b).unwrap(); + let result = a.checked_add(&b); assert_eq!(result, expected); } diff --git a/tests/it/compute/arithmetics/decimal/div.rs b/tests/it/compute/arithmetics/decimal/div.rs index 15b8c6b83a8..55cca8d303d 100644 --- a/tests/it/compute/arithmetics/decimal/div.rs +++ b/tests/it/compute/arithmetics/decimal/div.rs @@ -1,7 +1,7 @@ #![allow(clippy::zero_prefixed_literal, clippy::inconsistent_digit_grouping)] use arrow2::array::*; -use arrow2::compute::arithmetics::decimal::div::*; +use arrow2::compute::arithmetics::decimal::{adaptive_div, checked_div, div, saturating_div}; use arrow2::compute::arithmetics::{ArrayCheckedDiv, ArrayDiv}; use arrow2::datatypes::DataType; @@ -31,7 +31,7 @@ fn test_divide_normal() { ]) .to(DataType::Decimal(7, 3)); - let result = div(&a, &b).unwrap(); + let result = div(&a, &b); let expected = PrimitiveArray::from([ Some(1_800i128), Some(5_000i128), @@ -45,19 +45,16 @@ fn test_divide_normal() { assert_eq!(result, expected); // Testing trait - let result = a.div(&b).unwrap(); + let result = a.div(&b); assert_eq!(result, expected); } #[test] +#[should_panic] fn test_divide_decimal_wrong_precision() { let a = PrimitiveArray::from([None]).to(DataType::Decimal(5, 2)); let b = PrimitiveArray::from([None]).to(DataType::Decimal(6, 2)); - let result = div(&a, &b); - - if result.is_ok() { - panic!("Should panic for different precision"); - } + div(&a, &b); } #[test] @@ -65,7 +62,7 @@ fn test_divide_decimal_wrong_precision() { fn test_divide_panic() { let a = PrimitiveArray::from([Some(99999i128)]).to(DataType::Decimal(5, 2)); let b = PrimitiveArray::from([Some(000_01i128)]).to(DataType::Decimal(5, 2)); - let _ = div(&a, &b); + div(&a, &b); } #[test] @@ -90,7 +87,7 @@ fn test_divide_saturating() { ]) .to(DataType::Decimal(7, 3)); - let result = saturating_div(&a, &b).unwrap(); + let result = saturating_div(&a, &b); let expected = PrimitiveArray::from([ Some(1_800i128), Some(5_000i128), @@ -123,7 +120,7 @@ fn test_divide_saturating_overflow() { ]) .to(DataType::Decimal(5, 2)); - let result = saturating_div(&a, &b).unwrap(); + let result = saturating_div(&a, &b); let expected = PrimitiveArray::from([ Some(-99999i128), @@ -159,7 +156,7 @@ fn test_divide_checked() { ]) .to(DataType::Decimal(7, 3)); - let result = div(&a, &b).unwrap(); + let result = div(&a, &b); let expected = PrimitiveArray::from([ Some(1_800i128), Some(5_000i128), @@ -180,13 +177,13 @@ fn test_divide_checked_overflow() { let b = PrimitiveArray::from([Some(000_00i128), None, Some(2_00i128)]).to(DataType::Decimal(5, 2)); - let result = checked_div(&a, &b).unwrap(); + let result = checked_div(&a, &b); let expected = PrimitiveArray::from([None, None, Some(3_00i128)]).to(DataType::Decimal(5, 2)); assert_eq!(result, expected); // Testing trait - let result = a.checked_div(&b).unwrap(); + let result = a.checked_div(&b); assert_eq!(result, expected); } diff --git a/tests/it/compute/arithmetics/decimal/mul.rs b/tests/it/compute/arithmetics/decimal/mul.rs index 6c26d290059..a4b4a71b257 100644 --- a/tests/it/compute/arithmetics/decimal/mul.rs +++ b/tests/it/compute/arithmetics/decimal/mul.rs @@ -1,7 +1,7 @@ #![allow(clippy::zero_prefixed_literal, clippy::inconsistent_digit_grouping)] use arrow2::array::*; -use arrow2::compute::arithmetics::decimal::mul::*; +use arrow2::compute::arithmetics::decimal::{adaptive_mul, checked_mul, mul, saturating_mul}; use arrow2::compute::arithmetics::{ArrayCheckedMul, ArrayMul, ArraySaturatingMul}; use arrow2::datatypes::DataType; @@ -31,7 +31,7 @@ fn test_multiply_normal() { ]) .to(DataType::Decimal(7, 2)); - let result = mul(&a, &b).unwrap(); + let result = mul(&a, &b); let expected = PrimitiveArray::from([ Some(24690_86i128), Some(20_00i128), @@ -45,19 +45,16 @@ fn test_multiply_normal() { assert_eq!(result, expected); // Testing trait - let result = a.mul(&b).unwrap(); + let result = a.mul(&b); assert_eq!(result, expected); } #[test] +#[should_panic] fn test_multiply_decimal_wrong_precision() { let a = PrimitiveArray::from([None]).to(DataType::Decimal(5, 2)); let b = PrimitiveArray::from([None]).to(DataType::Decimal(6, 2)); - let result = mul(&a, &b); - - if result.is_ok() { - panic!("Should panic for different precision"); - } + mul(&a, &b); } #[test] @@ -90,7 +87,7 @@ fn test_multiply_saturating() { ]) .to(DataType::Decimal(7, 2)); - let result = saturating_mul(&a, &b).unwrap(); + let result = saturating_mul(&a, &b); let expected = PrimitiveArray::from([ Some(24690_86i128), Some(20_00i128), @@ -104,7 +101,7 @@ fn test_multiply_saturating() { assert_eq!(result, expected); // Testing trait - let result = a.saturating_mul(&b).unwrap(); + let result = a.saturating_mul(&b); assert_eq!(result, expected); } @@ -125,7 +122,7 @@ fn test_multiply_saturating_overflow() { ]) .to(DataType::Decimal(5, 2)); - let result = saturating_mul(&a, &b).unwrap(); + let result = saturating_mul(&a, &b); let expected = PrimitiveArray::from([ Some(-99999i128), @@ -138,7 +135,7 @@ fn test_multiply_saturating_overflow() { assert_eq!(result, expected); // Testing trait - let result = a.saturating_mul(&b).unwrap(); + let result = a.saturating_mul(&b); assert_eq!(result, expected); } @@ -164,7 +161,7 @@ fn test_multiply_checked() { ]) .to(DataType::Decimal(7, 2)); - let result = checked_mul(&a, &b).unwrap(); + let result = checked_mul(&a, &b); let expected = PrimitiveArray::from([ Some(24690_86i128), Some(20_00i128), @@ -178,7 +175,7 @@ fn test_multiply_checked() { assert_eq!(result, expected); // Testing trait - let result = a.checked_mul(&b).unwrap(); + let result = a.checked_mul(&b); assert_eq!(result, expected); } @@ -186,7 +183,7 @@ fn test_multiply_checked() { fn test_multiply_checked_overflow() { let a = PrimitiveArray::from([Some(99999i128), Some(1_00i128)]).to(DataType::Decimal(5, 2)); let b = PrimitiveArray::from([Some(10000i128), Some(2_00i128)]).to(DataType::Decimal(5, 2)); - let result = checked_mul(&a, &b).unwrap(); + let result = checked_mul(&a, &b); let expected = PrimitiveArray::from([None, Some(2_00i128)]).to(DataType::Decimal(5, 2)); assert_eq!(result, expected); diff --git a/tests/it/compute/arithmetics/decimal/sub.rs b/tests/it/compute/arithmetics/decimal/sub.rs index 467b1d220c6..343149a5646 100644 --- a/tests/it/compute/arithmetics/decimal/sub.rs +++ b/tests/it/compute/arithmetics/decimal/sub.rs @@ -1,7 +1,7 @@ #![allow(clippy::zero_prefixed_literal, clippy::inconsistent_digit_grouping)] use arrow2::array::*; -use arrow2::compute::arithmetics::decimal::sub::*; +use arrow2::compute::arithmetics::decimal::{adaptive_sub, checked_sub, saturating_sub, sub}; use arrow2::compute::arithmetics::{ArrayCheckedSub, ArraySaturatingSub, ArraySub}; use arrow2::datatypes::DataType; @@ -13,26 +13,23 @@ fn test_subtract_normal() { let b = PrimitiveArray::from([Some(22222i128), Some(11100i128), None, Some(11100i128)]) .to(DataType::Decimal(5, 2)); - let result = sub(&a, &b).unwrap(); + let result = sub(&a, &b); let expected = PrimitiveArray::from([Some(-11111i128), Some(11100i128), None, Some(28900i128)]) .to(DataType::Decimal(5, 2)); assert_eq!(result, expected); // Testing trait - let result = a.sub(&b).unwrap(); + let result = a.sub(&b); assert_eq!(result, expected); } #[test] +#[should_panic] fn test_subtract_decimal_wrong_precision() { let a = PrimitiveArray::from([None]).to(DataType::Decimal(5, 2)); let b = PrimitiveArray::from([None]).to(DataType::Decimal(6, 2)); - let result = sub(&a, &b); - - if result.is_ok() { - panic!("Should panic for different precision"); - } + sub(&a, &b); } #[test] @@ -51,14 +48,14 @@ fn test_subtract_saturating() { let b = PrimitiveArray::from([Some(22222i128), Some(11100i128), None, Some(11100i128)]) .to(DataType::Decimal(5, 2)); - let result = saturating_sub(&a, &b).unwrap(); + let result = saturating_sub(&a, &b); let expected = PrimitiveArray::from([Some(-11111i128), Some(11100i128), None, Some(28900i128)]) .to(DataType::Decimal(5, 2)); assert_eq!(result, expected); // Testing trait - let result = a.saturating_sub(&b).unwrap(); + let result = a.saturating_sub(&b); assert_eq!(result, expected); } @@ -79,7 +76,7 @@ fn test_subtract_saturating_overflow() { ]) .to(DataType::Decimal(5, 2)); - let result = saturating_sub(&a, &b).unwrap(); + let result = saturating_sub(&a, &b); let expected = PrimitiveArray::from([ Some(-99999i128), @@ -92,7 +89,7 @@ fn test_subtract_saturating_overflow() { assert_eq!(result, expected); // Testing trait - let result = a.saturating_sub(&b).unwrap(); + let result = a.saturating_sub(&b); assert_eq!(result, expected); } @@ -104,14 +101,14 @@ fn test_subtract_checked() { let b = PrimitiveArray::from([Some(22222i128), Some(11100i128), None, Some(11100i128)]) .to(DataType::Decimal(5, 2)); - let result = checked_sub(&a, &b).unwrap(); + let result = checked_sub(&a, &b); let expected = PrimitiveArray::from([Some(-11111i128), Some(11100i128), None, Some(28900i128)]) .to(DataType::Decimal(5, 2)); assert_eq!(result, expected); // Testing trait - let result = a.checked_sub(&b).unwrap(); + let result = a.checked_sub(&b); assert_eq!(result, expected); } @@ -119,7 +116,7 @@ fn test_subtract_checked() { fn test_subtract_checked_overflow() { let a = PrimitiveArray::from([Some(4i128), Some(-99999i128)]).to(DataType::Decimal(5, 2)); let b = PrimitiveArray::from([Some(2i128), Some(1i128)]).to(DataType::Decimal(5, 2)); - let result = checked_sub(&a, &b).unwrap(); + let result = checked_sub(&a, &b); let expected = PrimitiveArray::from([Some(2i128), None]).to(DataType::Decimal(5, 2)); assert_eq!(result, expected); } diff --git a/tests/it/compute/arithmetics/mod.rs b/tests/it/compute/arithmetics/mod.rs index 00b90bc0dd1..4b5e6a3e241 100644 --- a/tests/it/compute/arithmetics/mod.rs +++ b/tests/it/compute/arithmetics/mod.rs @@ -3,7 +3,7 @@ mod decimal; mod time; use arrow2::array::new_empty_array; -use arrow2::compute::arithmetics::{arithmetic, can_arithmetic, Operator}; +use arrow2::compute::arithmetics::*; use arrow2::datatypes::DataType::*; use arrow2::datatypes::{IntervalUnit, TimeUnit}; @@ -42,27 +42,26 @@ fn consistency() { Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano), ]; - let operators = vec![ - Operator::Add, - Operator::Divide, - Operator::Subtract, - Operator::Multiply, - Operator::Remainder, - ]; - let cases = datatypes - .clone() - .into_iter() - .zip(operators.into_iter()) - .zip(datatypes.into_iter()); + let cases = datatypes.clone().into_iter().zip(datatypes.into_iter()); - cases.for_each(|((lhs, op), rhs)| { + cases.for_each(|(lhs, rhs)| { let lhs_a = new_empty_array(lhs.clone()); let rhs_a = new_empty_array(rhs.clone()); - if can_arithmetic(&lhs, op, &rhs) { - assert!(arithmetic(lhs_a.as_ref(), op, rhs_a.as_ref()).is_ok()); - } else { - assert!(arithmetic(lhs_a.as_ref(), op, rhs_a.as_ref()).is_err()); + if can_add(&lhs, &rhs) { + add(lhs_a.as_ref(), rhs_a.as_ref()); + } + if can_sub(&lhs, &rhs) { + sub(lhs_a.as_ref(), rhs_a.as_ref()); + } + if can_mul(&lhs, &rhs) { + mul(lhs_a.as_ref(), rhs_a.as_ref()); + } + if can_div(&lhs, &rhs) { + div(lhs_a.as_ref(), rhs_a.as_ref()); + } + if can_rem(&lhs, &rhs) { + rem(lhs_a.as_ref(), rhs_a.as_ref()); } }); } diff --git a/tests/it/compute/arithmetics/time.rs b/tests/it/compute/arithmetics/time.rs index e4462198f3c..0df9416bd5c 100644 --- a/tests/it/compute/arithmetics/time.rs +++ b/tests/it/compute/arithmetics/time.rs @@ -12,7 +12,7 @@ fn test_adding_timestamp() { let duration = PrimitiveArray::from([Some(10i64), Some(20i64), None, Some(30i64)]) .to(DataType::Duration(TimeUnit::Second)); - let result = add_duration(×tamp, &duration).unwrap(); + let result = add_duration(×tamp, &duration); let expected = PrimitiveArray::from([Some(100010i64), Some(200020i64), None, Some(300030i64)]).to( DataType::Timestamp(TimeUnit::Second, Some("America/New_York".to_string())), @@ -36,7 +36,7 @@ fn test_adding_duration_different_scale() { let duration = PrimitiveArray::from([Some(10_000i64), Some(20_000i64), None, Some(30_000i64)]) .to(DataType::Duration(TimeUnit::Millisecond)); - let result = add_duration(×tamp, &duration).unwrap(); + let result = add_duration(×tamp, &duration); assert_eq!(result, expected); // Testing duration in nanoseconds. @@ -51,7 +51,7 @@ fn test_adding_duration_different_scale() { ]) .to(DataType::Duration(TimeUnit::Nanosecond)); - let result = add_duration(×tamp, &duration).unwrap(); + let result = add_duration(×tamp, &duration); assert_eq!(result, expected); } @@ -67,7 +67,7 @@ fn test_adding_subtract_timestamps_scale() { DataType::Timestamp(TimeUnit::Millisecond, Some("America/New_York".to_string())), ); - let result = add_duration(×tamp, &duration).unwrap(); + let result = add_duration(×tamp, &duration); assert_eq!(result, expected); let timestamp = PrimitiveArray::from([Some(10i64), Some(20i64), None, Some(30i64)]).to( @@ -87,7 +87,7 @@ fn test_adding_subtract_timestamps_scale() { Some("America/New_York".to_string()), )); - let result = add_duration(×tamp, &duration).unwrap(); + let result = add_duration(×tamp, &duration); assert_eq!(result, expected); } @@ -101,7 +101,7 @@ fn test_subtract_timestamp() { let duration = PrimitiveArray::from([Some(10i64), Some(20i64), None, Some(30i64)]) .to(DataType::Duration(TimeUnit::Second)); - let result = subtract_duration(×tamp, &duration).unwrap(); + let result = subtract_duration(×tamp, &duration); let expected = PrimitiveArray::from([Some(99990i64), Some(199980i64), None, Some(299970i64)]).to( DataType::Timestamp(TimeUnit::Second, Some("America/New_York".to_string())), @@ -125,7 +125,7 @@ fn test_subtracting_duration_different_scale() { let duration = PrimitiveArray::from([Some(10_000i64), Some(20_000i64), None, Some(30_000i64)]) .to(DataType::Duration(TimeUnit::Millisecond)); - let result = subtract_duration(×tamp, &duration).unwrap(); + let result = subtract_duration(×tamp, &duration); assert_eq!(result, expected); // Testing duration in nanoseconds. @@ -140,7 +140,7 @@ fn test_subtracting_duration_different_scale() { ]) .to(DataType::Duration(TimeUnit::Nanosecond)); - let result = subtract_duration(×tamp, &duration).unwrap(); + let result = subtract_duration(×tamp, &duration); assert_eq!(result, expected); } @@ -157,7 +157,7 @@ fn test_subtracting_subtract_timestamps_scale() { DataType::Timestamp(TimeUnit::Millisecond, Some("America/New_York".to_string())), ); - let result = subtract_duration(×tamp, &duration).unwrap(); + let result = subtract_duration(×tamp, &duration); assert_eq!(result, expected); let timestamp = PrimitiveArray::from([Some(10i64), Some(20i64), None, Some(30i64)]).to( @@ -177,7 +177,7 @@ fn test_subtracting_subtract_timestamps_scale() { Some("America/New_York".to_string()), )); - let result = subtract_duration(×tamp, &duration).unwrap(); + let result = subtract_duration(×tamp, &duration); assert_eq!(result, expected); } @@ -229,7 +229,7 @@ fn test_adding_to_time() { let time_32 = PrimitiveArray::from([Some(100000i32), Some(200000i32), None, Some(300000i32)]) .to(DataType::Time32(TimeUnit::Second)); - let result = add_duration(&time_32, &duration).unwrap(); + let result = add_duration(&time_32, &duration); let expected = PrimitiveArray::from([Some(100010i32), Some(200020i32), None, Some(300030i32)]) .to(DataType::Time32(TimeUnit::Second)); @@ -245,7 +245,7 @@ fn test_subtract_to_time() { let time_32 = PrimitiveArray::from([Some(100000i32), Some(200000i32), None, Some(300000i32)]) .to(DataType::Time32(TimeUnit::Second)); - let result = subtract_duration(&time_32, &duration).unwrap(); + let result = subtract_duration(&time_32, &duration); let expected = PrimitiveArray::from([Some(99990i32), Some(199980i32), None, Some(299970i32)]) .to(DataType::Time32(TimeUnit::Second)); @@ -266,14 +266,14 @@ fn test_date32() { PrimitiveArray::from([Some(100_000i32), Some(100_000i32), None, Some(100_000i32)]) .to(DataType::Date32); - let result = add_duration(&date_32, &duration).unwrap(); + let result = add_duration(&date_32, &duration); let expected = PrimitiveArray::from([Some(100_001i32), Some(100_010i32), None, Some(100_100i32)]) .to(DataType::Date32); assert_eq!(result, expected); - let result = subtract_duration(&date_32, &duration).unwrap(); + let result = subtract_duration(&date_32, &duration); let expected = PrimitiveArray::from([Some(99_999i32), Some(99_990i32), None, Some(99_900i32)]) .to(DataType::Date32); @@ -294,14 +294,14 @@ fn test_date64() { PrimitiveArray::from([Some(100_000i64), Some(100_000i64), None, Some(100_000i64)]) .to(DataType::Date64); - let result = add_duration(&date_64, &duration).unwrap(); + let result = add_duration(&date_64, &duration); let expected = PrimitiveArray::from([Some(100_010i64), Some(100_100i64), None, Some(101_000i64)]) .to(DataType::Date64); assert_eq!(result, expected); - let result = subtract_duration(&date_64, &duration).unwrap(); + let result = subtract_duration(&date_64, &duration); let expected = PrimitiveArray::from([Some(99_990i64), Some(99_900i64), None, Some(99_000i64)]) .to(DataType::Date64); diff --git a/tests/it/compute/bitwise.rs b/tests/it/compute/bitwise.rs index 92b8b47b4ab..3d44e58f219 100644 --- a/tests/it/compute/bitwise.rs +++ b/tests/it/compute/bitwise.rs @@ -5,7 +5,7 @@ use arrow2::compute::bitwise::*; fn test_xor() { let a = Int32Array::from(&[Some(2), Some(4), Some(6), Some(7)]); let b = Int32Array::from(&[None, Some(6), Some(9), Some(7)]); - let result = xor(&a, &b).unwrap(); + let result = xor(&a, &b); let expected = Int32Array::from(&[None, Some(2), Some(15), Some(0)]); assert_eq!(result, expected); @@ -15,7 +15,7 @@ fn test_xor() { fn test_and() { let a = Int32Array::from(&[Some(1), Some(2), Some(15)]); let b = Int32Array::from(&[None, Some(2), Some(6)]); - let result = and(&a, &b).unwrap(); + let result = and(&a, &b); let expected = Int32Array::from(&[None, Some(2), Some(6)]); assert_eq!(result, expected); @@ -25,7 +25,7 @@ fn test_and() { fn test_or() { let a = Int32Array::from(&[Some(1), Some(2), Some(0)]); let b = Int32Array::from(&[None, Some(2), Some(0)]); - let result = or(&a, &b).unwrap(); + let result = or(&a, &b); let expected = Int32Array::from(&[None, Some(2), Some(0)]); assert_eq!(result, expected);