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

Fixed error in trait constraint in compute #665

Merged
merged 1 commit into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/compute/arithmetics/basic/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -34,7 +34,7 @@ use crate::{
/// ```
pub fn add<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + Add<Output = T>,
T: NativeArithmetics + Add<Output = T>,
{
binary(lhs, rhs, lhs.data_type().clone(), |a, b| a + b)
}
Expand All @@ -55,7 +55,7 @@ where
/// ```
pub fn wrapping_add<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + WrappingAdd<Output = T>,
T: NativeArithmetics + WrappingAdd<Output = T>,
{
let op = move |a: T, b: T| a.wrapping_add(&b);

Expand All @@ -78,7 +78,7 @@ where
/// ```
pub fn checked_add<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + CheckedAdd<Output = T>,
T: NativeArithmetics + CheckedAdd<Output = T>,
{
let op = move |a: T, b: T| a.checked_add(&b);

Expand All @@ -102,7 +102,7 @@ where
/// ```
pub fn saturating_add<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + SaturatingAdd<Output = T>,
T: NativeArithmetics + SaturatingAdd<Output = T>,
{
let op = move |a: T, b: T| a.saturating_add(&b);

Expand Down Expand Up @@ -130,7 +130,7 @@ pub fn overflowing_add<T>(
rhs: &PrimitiveArray<T>,
) -> (PrimitiveArray<T>, Bitmap)
where
T: NativeType + OverflowingAdd<Output = T>,
T: NativeArithmetics + OverflowingAdd<Output = T>,
{
let op = move |a: T, b: T| a.overflowing_add(&b);

Expand Down Expand Up @@ -201,7 +201,7 @@ where
/// ```
pub fn add_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>
where
T: NativeType + Add<Output = T>,
T: NativeArithmetics + Add<Output = T>,
{
let rhs = *rhs;
unary(lhs, |a| a + rhs, lhs.data_type().clone())
Expand All @@ -222,7 +222,7 @@ where
/// ```
pub fn wrapping_add_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>
where
T: NativeType + WrappingAdd<Output = T>,
T: NativeArithmetics + WrappingAdd<Output = T>,
{
unary(lhs, |a| a.wrapping_add(rhs), lhs.data_type().clone())
}
Expand All @@ -243,7 +243,7 @@ where
/// ```
pub fn checked_add_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>
where
T: NativeType + CheckedAdd<Output = T>,
T: NativeArithmetics + CheckedAdd<Output = T>,
{
let rhs = *rhs;
let op = move |a: T| a.checked_add(&rhs);
Expand All @@ -267,7 +267,7 @@ where
/// ```
pub fn saturating_add_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>
where
T: NativeType + SaturatingAdd<Output = T>,
T: NativeArithmetics + SaturatingAdd<Output = T>,
{
let rhs = *rhs;
let op = move |a: T| a.saturating_add(&rhs);
Expand All @@ -292,7 +292,7 @@ where
/// ```
pub fn overflowing_add_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> (PrimitiveArray<T>, Bitmap)
where
T: NativeType + OverflowingAdd<Output = T>,
T: NativeArithmetics + OverflowingAdd<Output = T>,
{
let rhs = *rhs;
let op = move |a: T| a.overflowing_add(&rhs);
Expand Down
15 changes: 8 additions & 7 deletions src/compute/arithmetics/basic/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -33,7 +34,7 @@ use strength_reduce::{
/// ```
pub fn div<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + Div<Output = T>,
T: NativeArithmetics + Div<Output = T>,
{
if rhs.null_count() == 0 {
binary(lhs, rhs, lhs.data_type().clone(), |a, b| a / b)
Expand Down Expand Up @@ -107,7 +108,7 @@ where
/// ```
pub fn div_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>
where
T: NativeType + Div<Output = T> + NumCast,
T: NativeArithmetics + Div<Output = T> + NumCast,
{
let rhs = *rhs;
match T::DATA_TYPE {
Expand Down Expand Up @@ -190,7 +191,7 @@ where
/// ```
pub fn checked_div_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>
where
T: NativeType + CheckedDiv<Output = T> + Zero,
T: NativeArithmetics + CheckedDiv<Output = T> + Zero,
{
let rhs = *rhs;
let op = move |a: T| a.checked_div(&rhs);
Expand All @@ -201,7 +202,7 @@ where
// Implementation of ArrayDiv trait for PrimitiveArrays with a scalar
impl<T> ArrayDiv<T> for PrimitiveArray<T>
where
T: NativeType + Div<Output = T> + NativeArithmetics + NumCast,
T: NativeArithmetics + Div<Output = T> + NativeArithmetics + NumCast,
{
fn div(&self, rhs: &T) -> Self {
div_scalar(self, rhs)
Expand All @@ -211,7 +212,7 @@ where
// Implementation of ArrayCheckedDiv trait for PrimitiveArrays with a scalar
impl<T> ArrayCheckedDiv<T> for PrimitiveArray<T>
where
T: NativeType + CheckedDiv<Output = T> + Zero + NativeArithmetics,
T: NativeArithmetics + CheckedDiv<Output = T> + Zero + NativeArithmetics,
{
fn checked_div(&self, rhs: &T) -> Self {
checked_div_scalar(self, rhs)
Expand Down
16 changes: 16 additions & 0 deletions src/compute/arithmetics/basic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions src/compute/arithmetics/basic/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -34,7 +34,7 @@ use crate::{
/// ```
pub fn mul<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + Mul<Output = T>,
T: NativeArithmetics + Mul<Output = T>,
{
binary(lhs, rhs, lhs.data_type().clone(), |a, b| a * b)
}
Expand All @@ -55,7 +55,7 @@ where
/// ```
pub fn wrapping_mul<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + WrappingMul<Output = T>,
T: NativeArithmetics + WrappingMul<Output = T>,
{
let op = move |a: T, b: T| a.wrapping_mul(&b);

Expand All @@ -79,7 +79,7 @@ where
/// ```
pub fn checked_mul<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + CheckedMul<Output = T>,
T: NativeArithmetics + CheckedMul<Output = T>,
{
let op = move |a: T, b: T| a.checked_mul(&b);

Expand All @@ -103,7 +103,7 @@ where
/// ```
pub fn saturating_mul<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + SaturatingMul<Output = T>,
T: NativeArithmetics + SaturatingMul<Output = T>,
{
let op = move |a: T, b: T| a.saturating_mul(&b);

Expand Down Expand Up @@ -131,7 +131,7 @@ pub fn overflowing_mul<T>(
rhs: &PrimitiveArray<T>,
) -> (PrimitiveArray<T>, Bitmap)
where
T: NativeType + OverflowingMul<Output = T>,
T: NativeArithmetics + OverflowingMul<Output = T>,
{
let op = move |a: T, b: T| a.overflowing_mul(&b);

Expand Down Expand Up @@ -202,7 +202,7 @@ where
/// ```
pub fn mul_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>
where
T: NativeType + Mul<Output = T>,
T: NativeArithmetics + Mul<Output = T>,
{
let rhs = *rhs;
unary(lhs, |a| a * rhs, lhs.data_type().clone())
Expand All @@ -223,7 +223,7 @@ where
/// ```
pub fn wrapping_mul_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>
where
T: NativeType + WrappingMul<Output = T>,
T: NativeArithmetics + WrappingMul<Output = T>,
{
unary(lhs, |a| a.wrapping_mul(rhs), lhs.data_type().clone())
}
Expand All @@ -244,7 +244,7 @@ where
/// ```
pub fn checked_mul_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>
where
T: NativeType + CheckedMul<Output = T>,
T: NativeArithmetics + CheckedMul<Output = T>,
{
let rhs = *rhs;
let op = move |a: T| a.checked_mul(&rhs);
Expand All @@ -268,7 +268,7 @@ where
/// ```
pub fn saturating_mul_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>
where
T: NativeType + SaturatingMul<Output = T>,
T: NativeArithmetics + SaturatingMul<Output = T>,
{
let rhs = *rhs;
let op = move |a: T| a.saturating_mul(&rhs);
Expand All @@ -293,7 +293,7 @@ where
/// ```
pub fn overflowing_mul_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> (PrimitiveArray<T>, Bitmap)
where
T: NativeType + OverflowingMul<Output = T>,
T: NativeArithmetics + OverflowingMul<Output = T>,
{
let rhs = *rhs;
let op = move |a: T| a.overflowing_mul(&rhs);
Expand All @@ -304,7 +304,7 @@ where
// Implementation of ArrayMul trait for PrimitiveArrays with a scalar
impl<T> ArrayMul<T> for PrimitiveArray<T>
where
T: NativeType + Mul<Output = T> + NativeArithmetics,
T: NativeArithmetics + Mul<Output = T> + NativeArithmetics,
{
fn mul(&self, rhs: &T) -> Self {
mul_scalar(self, rhs)
Expand Down
7 changes: 4 additions & 3 deletions src/compute/arithmetics/basic/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -22,7 +23,7 @@ use crate::{
/// ```
pub fn powf_scalar<T>(array: &PrimitiveArray<T>, exponent: T) -> PrimitiveArray<T>
where
T: NativeType + Pow<T, Output = T>,
T: NativeArithmetics + Pow<T, Output = T>,
{
unary(array, |x| x.pow(exponent), array.data_type().clone())
}
Expand All @@ -43,7 +44,7 @@ where
/// ```
pub fn checked_powf_scalar<T>(array: &PrimitiveArray<T>, exponent: usize) -> PrimitiveArray<T>
where
T: NativeType + Zero + One + CheckedMul,
T: NativeArithmetics + Zero + One + CheckedMul,
{
let op = move |a: T| checked_pow(a, exponent);

Expand Down
13 changes: 7 additions & 6 deletions src/compute/arithmetics/basic/rem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -31,7 +32,7 @@ use strength_reduce::{
/// ```
pub fn rem<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + Rem<Output = T>,
T: NativeArithmetics + Rem<Output = T>,
{
binary(lhs, rhs, lhs.data_type().clone(), |a, b| a % b)
}
Expand All @@ -53,7 +54,7 @@ where
/// ```
pub fn checked_rem<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + CheckedRem<Output = T>,
T: NativeArithmetics + CheckedRem<Output = T>,
{
let op = move |a: T, b: T| a.checked_rem(&b);

Expand Down Expand Up @@ -93,7 +94,7 @@ where
/// ```
pub fn rem_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>
where
T: NativeType + Rem<Output = T> + NumCast,
T: NativeArithmetics + Rem<Output = T> + NumCast,
{
let rhs = *rhs;

Expand Down Expand Up @@ -177,7 +178,7 @@ where
/// ```
pub fn checked_rem_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>
where
T: NativeType + CheckedRem<Output = T>,
T: NativeArithmetics + CheckedRem<Output = T>,
{
let rhs = *rhs;
let op = move |a: T| a.checked_rem(&rhs);
Expand Down
Loading