diff --git a/src/compute/arithmetics/basic/add.rs b/src/compute/arithmetics/basic/add.rs index 5d4fd7215d5..c89f2cd8cd0 100644 --- a/src/compute/arithmetics/basic/add.rs +++ b/src/compute/arithmetics/basic/add.rs @@ -9,15 +9,15 @@ use crate::{ compute::{ arithmetics::{ ArrayAdd, ArrayCheckedAdd, ArrayOverflowingAdd, ArraySaturatingAdd, ArrayWrappingAdd, - NativeArithmetics, }, arity::{ binary, binary_checked, binary_with_bitmap, unary, unary_checked, unary_with_bitmap, }, }, - types::NativeType, }; +use super::NativeArithmetics; + /// Adds two primitive arrays with the same type. /// Panics if the sum of one pair of values overflows. /// @@ -34,7 +34,7 @@ use crate::{ /// ``` pub fn add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where - T: NativeType + Add, + T: NativeArithmetics + Add, { binary(lhs, rhs, lhs.data_type().clone(), |a, b| a + b) } @@ -55,7 +55,7 @@ where /// ``` pub fn wrapping_add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where - T: NativeType + WrappingAdd, + T: NativeArithmetics + WrappingAdd, { let op = move |a: T, b: T| a.wrapping_add(&b); @@ -78,7 +78,7 @@ where /// ``` pub fn checked_add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where - T: NativeType + CheckedAdd, + T: NativeArithmetics + CheckedAdd, { let op = move |a: T, b: T| a.checked_add(&b); @@ -102,7 +102,7 @@ where /// ``` pub fn saturating_add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where - T: NativeType + SaturatingAdd, + T: NativeArithmetics + SaturatingAdd, { let op = move |a: T, b: T| a.saturating_add(&b); @@ -130,7 +130,7 @@ pub fn overflowing_add( rhs: &PrimitiveArray, ) -> (PrimitiveArray, Bitmap) where - T: NativeType + OverflowingAdd, + T: NativeArithmetics + OverflowingAdd, { let op = move |a: T, b: T| a.overflowing_add(&b); @@ -201,7 +201,7 @@ where /// ``` pub fn add_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + Add, + T: NativeArithmetics + Add, { let rhs = *rhs; unary(lhs, |a| a + rhs, lhs.data_type().clone()) @@ -222,7 +222,7 @@ where /// ``` pub fn wrapping_add_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + WrappingAdd, + T: NativeArithmetics + WrappingAdd, { unary(lhs, |a| a.wrapping_add(rhs), lhs.data_type().clone()) } @@ -243,7 +243,7 @@ where /// ``` pub fn checked_add_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + CheckedAdd, + T: NativeArithmetics + CheckedAdd, { let rhs = *rhs; let op = move |a: T| a.checked_add(&rhs); @@ -267,7 +267,7 @@ where /// ``` pub fn saturating_add_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + SaturatingAdd, + T: NativeArithmetics + SaturatingAdd, { let rhs = *rhs; let op = move |a: T| a.saturating_add(&rhs); @@ -292,7 +292,7 @@ where /// ``` pub fn overflowing_add_scalar(lhs: &PrimitiveArray, rhs: &T) -> (PrimitiveArray, Bitmap) where - T: NativeType + OverflowingAdd, + T: NativeArithmetics + OverflowingAdd, { let rhs = *rhs; let op = move |a: T| a.overflowing_add(&rhs); diff --git a/src/compute/arithmetics/basic/div.rs b/src/compute/arithmetics/basic/div.rs index 2491cc5aaaf..2592ca4878a 100644 --- a/src/compute/arithmetics/basic/div.rs +++ b/src/compute/arithmetics/basic/div.rs @@ -7,16 +7,17 @@ use crate::datatypes::DataType; use crate::{ array::{Array, PrimitiveArray}, compute::{ - arithmetics::{ArrayCheckedDiv, ArrayDiv, NativeArithmetics}, + arithmetics::{ArrayCheckedDiv, ArrayDiv}, arity::{binary, binary_checked, unary, unary_checked}, utils::check_same_len, }, - types::NativeType, }; use strength_reduce::{ StrengthReducedU16, StrengthReducedU32, StrengthReducedU64, StrengthReducedU8, }; +use super::NativeArithmetics; + /// Divides two primitive arrays with the same type. /// Panics if the divisor is zero of one pair of values overflows. /// @@ -33,7 +34,7 @@ use strength_reduce::{ /// ``` pub fn div(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where - T: NativeType + Div, + T: NativeArithmetics + Div, { if rhs.null_count() == 0 { binary(lhs, rhs, lhs.data_type().clone(), |a, b| a / b) @@ -107,7 +108,7 @@ where /// ``` pub fn div_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + Div + NumCast, + T: NativeArithmetics + Div + NumCast, { let rhs = *rhs; match T::DATA_TYPE { @@ -190,7 +191,7 @@ where /// ``` pub fn checked_div_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + CheckedDiv + Zero, + T: NativeArithmetics + CheckedDiv + Zero, { let rhs = *rhs; let op = move |a: T| a.checked_div(&rhs); @@ -201,7 +202,7 @@ where // Implementation of ArrayDiv trait for PrimitiveArrays with a scalar impl ArrayDiv for PrimitiveArray where - T: NativeType + Div + NativeArithmetics + NumCast, + T: NativeArithmetics + Div + NativeArithmetics + NumCast, { fn div(&self, rhs: &T) -> Self { div_scalar(self, rhs) @@ -211,7 +212,7 @@ where // Implementation of ArrayCheckedDiv trait for PrimitiveArrays with a scalar impl ArrayCheckedDiv for PrimitiveArray where - T: NativeType + CheckedDiv + Zero + NativeArithmetics, + T: NativeArithmetics + CheckedDiv + Zero + NativeArithmetics, { fn checked_div(&self, rhs: &T) -> Self { checked_div_scalar(self, rhs) diff --git a/src/compute/arithmetics/basic/mod.rs b/src/compute/arithmetics/basic/mod.rs index 53456f03ab0..32f52cea6d4 100644 --- a/src/compute/arithmetics/basic/mod.rs +++ b/src/compute/arithmetics/basic/mod.rs @@ -29,6 +29,22 @@ use crate::{ use super::super::arity::{unary, unary_checked}; +/// Trait describing a [`NativeType`] whose semantics of arithmetic in Arrow equals +/// the semantics in Rust. +/// A counter example is `i128`, that in arrow represents a decimal while in rust represents +/// a signed integer. +pub trait NativeArithmetics: NativeType {} +impl NativeArithmetics for u8 {} +impl NativeArithmetics for u16 {} +impl NativeArithmetics for u32 {} +impl NativeArithmetics for u64 {} +impl NativeArithmetics for i8 {} +impl NativeArithmetics for i16 {} +impl NativeArithmetics for i32 {} +impl NativeArithmetics for i64 {} +impl NativeArithmetics for f32 {} +impl NativeArithmetics for f64 {} + /// Negates values from array. /// /// # Examples diff --git a/src/compute/arithmetics/basic/mul.rs b/src/compute/arithmetics/basic/mul.rs index ff2b4fb1dc0..6b3ae4b4d08 100644 --- a/src/compute/arithmetics/basic/mul.rs +++ b/src/compute/arithmetics/basic/mul.rs @@ -9,15 +9,15 @@ use crate::{ compute::{ arithmetics::{ ArrayCheckedMul, ArrayMul, ArrayOverflowingMul, ArraySaturatingMul, ArrayWrappingMul, - NativeArithmetics, }, arity::{ binary, binary_checked, binary_with_bitmap, unary, unary_checked, unary_with_bitmap, }, }, - types::NativeType, }; +use super::NativeArithmetics; + /// Multiplies two primitive arrays with the same type. /// Panics if the multiplication of one pair of values overflows. /// @@ -34,7 +34,7 @@ use crate::{ /// ``` pub fn mul(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where - T: NativeType + Mul, + T: NativeArithmetics + Mul, { binary(lhs, rhs, lhs.data_type().clone(), |a, b| a * b) } @@ -55,7 +55,7 @@ where /// ``` pub fn wrapping_mul(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where - T: NativeType + WrappingMul, + T: NativeArithmetics + WrappingMul, { let op = move |a: T, b: T| a.wrapping_mul(&b); @@ -79,7 +79,7 @@ where /// ``` pub fn checked_mul(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where - T: NativeType + CheckedMul, + T: NativeArithmetics + CheckedMul, { let op = move |a: T, b: T| a.checked_mul(&b); @@ -103,7 +103,7 @@ where /// ``` pub fn saturating_mul(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where - T: NativeType + SaturatingMul, + T: NativeArithmetics + SaturatingMul, { let op = move |a: T, b: T| a.saturating_mul(&b); @@ -131,7 +131,7 @@ pub fn overflowing_mul( rhs: &PrimitiveArray, ) -> (PrimitiveArray, Bitmap) where - T: NativeType + OverflowingMul, + T: NativeArithmetics + OverflowingMul, { let op = move |a: T, b: T| a.overflowing_mul(&b); @@ -202,7 +202,7 @@ where /// ``` pub fn mul_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + Mul, + T: NativeArithmetics + Mul, { let rhs = *rhs; unary(lhs, |a| a * rhs, lhs.data_type().clone()) @@ -223,7 +223,7 @@ where /// ``` pub fn wrapping_mul_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + WrappingMul, + T: NativeArithmetics + WrappingMul, { unary(lhs, |a| a.wrapping_mul(rhs), lhs.data_type().clone()) } @@ -244,7 +244,7 @@ where /// ``` pub fn checked_mul_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + CheckedMul, + T: NativeArithmetics + CheckedMul, { let rhs = *rhs; let op = move |a: T| a.checked_mul(&rhs); @@ -268,7 +268,7 @@ where /// ``` pub fn saturating_mul_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + SaturatingMul, + T: NativeArithmetics + SaturatingMul, { let rhs = *rhs; let op = move |a: T| a.saturating_mul(&rhs); @@ -293,7 +293,7 @@ where /// ``` pub fn overflowing_mul_scalar(lhs: &PrimitiveArray, rhs: &T) -> (PrimitiveArray, Bitmap) where - T: NativeType + OverflowingMul, + T: NativeArithmetics + OverflowingMul, { let rhs = *rhs; let op = move |a: T| a.overflowing_mul(&rhs); @@ -304,7 +304,7 @@ where // Implementation of ArrayMul trait for PrimitiveArrays with a scalar impl ArrayMul for PrimitiveArray where - T: NativeType + Mul + NativeArithmetics, + T: NativeArithmetics + Mul + NativeArithmetics, { fn mul(&self, rhs: &T) -> Self { mul_scalar(self, rhs) diff --git a/src/compute/arithmetics/basic/pow.rs b/src/compute/arithmetics/basic/pow.rs index 0782619f880..e2e28deb9ba 100644 --- a/src/compute/arithmetics/basic/pow.rs +++ b/src/compute/arithmetics/basic/pow.rs @@ -4,9 +4,10 @@ use num_traits::{checked_pow, CheckedMul, One, Pow, Zero}; use crate::{ array::{Array, PrimitiveArray}, compute::arity::{unary, unary_checked}, - types::NativeType, }; +use super::NativeArithmetics; + /// Raises an array of primitives to the power of exponent. Panics if one of /// the values values overflows. /// @@ -22,7 +23,7 @@ use crate::{ /// ``` pub fn powf_scalar(array: &PrimitiveArray, exponent: T) -> PrimitiveArray where - T: NativeType + Pow, + T: NativeArithmetics + Pow, { unary(array, |x| x.pow(exponent), array.data_type().clone()) } @@ -43,7 +44,7 @@ where /// ``` pub fn checked_powf_scalar(array: &PrimitiveArray, exponent: usize) -> PrimitiveArray where - T: NativeType + Zero + One + CheckedMul, + T: NativeArithmetics + Zero + One + CheckedMul, { let op = move |a: T| checked_pow(a, exponent); diff --git a/src/compute/arithmetics/basic/rem.rs b/src/compute/arithmetics/basic/rem.rs index 30300385028..baa554a806c 100644 --- a/src/compute/arithmetics/basic/rem.rs +++ b/src/compute/arithmetics/basic/rem.rs @@ -6,15 +6,16 @@ use crate::datatypes::DataType; use crate::{ array::{Array, PrimitiveArray}, compute::{ - arithmetics::{ArrayCheckedRem, ArrayRem, NativeArithmetics}, + arithmetics::{ArrayCheckedRem, ArrayRem}, arity::{binary, binary_checked, unary, unary_checked}, }, - types::NativeType, }; use strength_reduce::{ StrengthReducedU16, StrengthReducedU32, StrengthReducedU64, StrengthReducedU8, }; +use super::NativeArithmetics; + /// Remainder of two primitive arrays with the same type. /// Panics if the divisor is zero of one pair of values overflows. /// @@ -31,7 +32,7 @@ use strength_reduce::{ /// ``` pub fn rem(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where - T: NativeType + Rem, + T: NativeArithmetics + Rem, { binary(lhs, rhs, lhs.data_type().clone(), |a, b| a % b) } @@ -53,7 +54,7 @@ where /// ``` pub fn checked_rem(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where - T: NativeType + CheckedRem, + T: NativeArithmetics + CheckedRem, { let op = move |a: T, b: T| a.checked_rem(&b); @@ -93,7 +94,7 @@ where /// ``` pub fn rem_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + Rem + NumCast, + T: NativeArithmetics + Rem + NumCast, { let rhs = *rhs; @@ -177,7 +178,7 @@ where /// ``` pub fn checked_rem_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + CheckedRem, + T: NativeArithmetics + CheckedRem, { let rhs = *rhs; let op = move |a: T| a.checked_rem(&rhs); diff --git a/src/compute/arithmetics/basic/sub.rs b/src/compute/arithmetics/basic/sub.rs index d4067be3fc5..4358db32990 100644 --- a/src/compute/arithmetics/basic/sub.rs +++ b/src/compute/arithmetics/basic/sub.rs @@ -9,15 +9,15 @@ use crate::{ compute::{ arithmetics::{ ArrayCheckedSub, ArrayOverflowingSub, ArraySaturatingSub, ArraySub, ArrayWrappingSub, - NativeArithmetics, }, arity::{ binary, binary_checked, binary_with_bitmap, unary, unary_checked, unary_with_bitmap, }, }, - types::NativeType, }; +use super::NativeArithmetics; + /// Subtracts two primitive arrays with the same type. /// Panics if the subtraction of one pair of values overflows. /// @@ -34,7 +34,7 @@ use crate::{ /// ``` pub fn sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where - T: NativeType + Sub, + T: NativeArithmetics + Sub, { binary(lhs, rhs, lhs.data_type().clone(), |a, b| a - b) } @@ -55,7 +55,7 @@ where /// ``` pub fn wrapping_sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where - T: NativeType + WrappingSub, + T: NativeArithmetics + WrappingSub, { let op = move |a: T, b: T| a.wrapping_sub(&b); @@ -78,7 +78,7 @@ where /// ``` pub fn checked_sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where - T: NativeType + CheckedSub, + T: NativeArithmetics + CheckedSub, { let op = move |a: T, b: T| a.checked_sub(&b); @@ -102,7 +102,7 @@ where /// ``` pub fn saturating_sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> PrimitiveArray where - T: NativeType + SaturatingSub, + T: NativeArithmetics + SaturatingSub, { let op = move |a: T, b: T| a.saturating_sub(&b); @@ -130,7 +130,7 @@ pub fn overflowing_sub( rhs: &PrimitiveArray, ) -> (PrimitiveArray, Bitmap) where - T: NativeType + OverflowingSub, + T: NativeArithmetics + OverflowingSub, { let op = move |a: T, b: T| a.overflowing_sub(&b); @@ -201,7 +201,7 @@ where /// ``` pub fn sub_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + Sub, + T: NativeArithmetics + Sub, { let rhs = *rhs; unary(lhs, |a| a - rhs, lhs.data_type().clone()) @@ -222,7 +222,7 @@ where /// ``` pub fn wrapping_sub_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + WrappingSub, + T: NativeArithmetics + WrappingSub, { unary(lhs, |a| a.wrapping_sub(rhs), lhs.data_type().clone()) } @@ -243,7 +243,7 @@ where /// ``` pub fn checked_sub_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + CheckedSub, + T: NativeArithmetics + CheckedSub, { let rhs = *rhs; let op = move |a: T| a.checked_sub(&rhs); @@ -267,7 +267,7 @@ where /// ``` pub fn saturating_sub_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + SaturatingSub, + T: NativeArithmetics + SaturatingSub, { let rhs = *rhs; let op = move |a: T| a.saturating_sub(&rhs); @@ -292,7 +292,7 @@ where /// ``` pub fn overflowing_sub_scalar(lhs: &PrimitiveArray, rhs: &T) -> (PrimitiveArray, Bitmap) where - T: NativeType + OverflowingSub, + T: NativeArithmetics + OverflowingSub, { let rhs = *rhs; let op = move |a: T| a.overflowing_sub(&rhs); diff --git a/src/compute/arithmetics/mod.rs b/src/compute/arithmetics/mod.rs index 02948ef5823..c7b5f7af39f 100644 --- a/src/compute/arithmetics/mod.rs +++ b/src/compute/arithmetics/mod.rs @@ -21,7 +21,6 @@ use crate::{ bitmap::Bitmap, datatypes::{DataType, IntervalUnit, TimeUnit}, scalar::Scalar, - types::NativeType, }; // Macro to evaluate match branch in arithmetic function. @@ -421,19 +420,3 @@ pub trait ArrayCheckedRem: Sized { /// checked remainder fn checked_rem(&self, rhs: &Rhs) -> Self; } - -/// Trait describing a [`NativeType`] whose semantics of arithmetic in Arrow equals -/// the semantics in Rust. -/// A counter example is `i128`, that in arrow represents a decimal while in rust represents -/// a signed integer. -pub trait NativeArithmetics: NativeType {} -impl NativeArithmetics for u8 {} -impl NativeArithmetics for u16 {} -impl NativeArithmetics for u32 {} -impl NativeArithmetics for u64 {} -impl NativeArithmetics for i8 {} -impl NativeArithmetics for i16 {} -impl NativeArithmetics for i32 {} -impl NativeArithmetics for i64 {} -impl NativeArithmetics for f32 {} -impl NativeArithmetics for f64 {}