From 2a3e2f26d2e738bbb78030c68459eb4b7006ef3f Mon Sep 17 00:00:00 2001 From: Raphael Taylor-Davies Date: Sat, 5 Nov 2022 09:56:04 +1300 Subject: [PATCH] Move ArrowNativeTypeOp to arrow-array (#2594) --- .../src/arithmetic.rs | 41 +++++++++---------- arrow-array/src/lib.rs | 3 ++ arrow/src/datatypes/mod.rs | 4 +- 3 files changed, 25 insertions(+), 23 deletions(-) rename arrow/src/datatypes/native.rs => arrow-array/src/arithmetic.rs (86%) diff --git a/arrow/src/datatypes/native.rs b/arrow-array/src/arithmetic.rs similarity index 86% rename from arrow/src/datatypes/native.rs rename to arrow-array/src/arithmetic.rs index bbdec14b44a..48d577c1782 100644 --- a/arrow/src/datatypes/native.rs +++ b/arrow-array/src/arithmetic.rs @@ -15,9 +15,8 @@ // specific language governing permissions and limitations // under the License. -use crate::error::{ArrowError, Result}; -pub use arrow_array::ArrowPrimitiveType; -pub use arrow_buffer::{i256, ArrowNativeType, ToByteSlice}; +use arrow_buffer::{i256, ArrowNativeType}; +use arrow_schema::ArrowError; use half::f16; /// Trait for [`ArrowNativeType`] that adds checked and unchecked arithmetic operations, @@ -44,27 +43,27 @@ pub trait ArrowNativeTypeOp: ArrowNativeType { /// The multiplicative identity const ONE: Self; - fn add_checked(self, rhs: Self) -> Result; + fn add_checked(self, rhs: Self) -> Result; fn add_wrapping(self, rhs: Self) -> Self; - fn sub_checked(self, rhs: Self) -> Result; + fn sub_checked(self, rhs: Self) -> Result; fn sub_wrapping(self, rhs: Self) -> Self; - fn mul_checked(self, rhs: Self) -> Result; + fn mul_checked(self, rhs: Self) -> Result; fn mul_wrapping(self, rhs: Self) -> Self; - fn div_checked(self, rhs: Self) -> Result; + fn div_checked(self, rhs: Self) -> Result; fn div_wrapping(self, rhs: Self) -> Self; - fn mod_checked(self, rhs: Self) -> Result; + fn mod_checked(self, rhs: Self) -> Result; fn mod_wrapping(self, rhs: Self) -> Self; - fn neg_checked(self) -> Result; + fn neg_checked(self) -> Result; fn neg_wrapping(self) -> Self; @@ -92,7 +91,7 @@ macro_rules! native_type_op { const ZERO: Self = $zero; const ONE: Self = $one; - fn add_checked(self, rhs: Self) -> Result { + fn add_checked(self, rhs: Self) -> Result { self.checked_add(rhs).ok_or_else(|| { ArrowError::ComputeError(format!( "Overflow happened on: {:?} + {:?}", @@ -105,7 +104,7 @@ macro_rules! native_type_op { self.wrapping_add(rhs) } - fn sub_checked(self, rhs: Self) -> Result { + fn sub_checked(self, rhs: Self) -> Result { self.checked_sub(rhs).ok_or_else(|| { ArrowError::ComputeError(format!( "Overflow happened on: {:?} - {:?}", @@ -118,7 +117,7 @@ macro_rules! native_type_op { self.wrapping_sub(rhs) } - fn mul_checked(self, rhs: Self) -> Result { + fn mul_checked(self, rhs: Self) -> Result { self.checked_mul(rhs).ok_or_else(|| { ArrowError::ComputeError(format!( "Overflow happened on: {:?} * {:?}", @@ -131,7 +130,7 @@ macro_rules! native_type_op { self.wrapping_mul(rhs) } - fn div_checked(self, rhs: Self) -> Result { + fn div_checked(self, rhs: Self) -> Result { if rhs.is_zero() { Err(ArrowError::DivideByZero) } else { @@ -148,7 +147,7 @@ macro_rules! native_type_op { self.wrapping_div(rhs) } - fn mod_checked(self, rhs: Self) -> Result { + fn mod_checked(self, rhs: Self) -> Result { if rhs.is_zero() { Err(ArrowError::DivideByZero) } else { @@ -165,7 +164,7 @@ macro_rules! native_type_op { self.wrapping_rem(rhs) } - fn neg_checked(self) -> Result { + fn neg_checked(self) -> Result { self.checked_neg().ok_or_else(|| { ArrowError::ComputeError(format!("Overflow happened on: {:?}", self)) }) @@ -223,7 +222,7 @@ macro_rules! native_type_float_op { const ZERO: Self = $zero; const ONE: Self = $one; - fn add_checked(self, rhs: Self) -> Result { + fn add_checked(self, rhs: Self) -> Result { Ok(self + rhs) } @@ -231,7 +230,7 @@ macro_rules! native_type_float_op { self + rhs } - fn sub_checked(self, rhs: Self) -> Result { + fn sub_checked(self, rhs: Self) -> Result { Ok(self - rhs) } @@ -239,7 +238,7 @@ macro_rules! native_type_float_op { self - rhs } - fn mul_checked(self, rhs: Self) -> Result { + fn mul_checked(self, rhs: Self) -> Result { Ok(self * rhs) } @@ -247,7 +246,7 @@ macro_rules! native_type_float_op { self * rhs } - fn div_checked(self, rhs: Self) -> Result { + fn div_checked(self, rhs: Self) -> Result { if rhs.is_zero() { Err(ArrowError::DivideByZero) } else { @@ -259,7 +258,7 @@ macro_rules! native_type_float_op { self / rhs } - fn mod_checked(self, rhs: Self) -> Result { + fn mod_checked(self, rhs: Self) -> Result { if rhs.is_zero() { Err(ArrowError::DivideByZero) } else { @@ -271,7 +270,7 @@ macro_rules! native_type_float_op { self % rhs } - fn neg_checked(self) -> Result { + fn neg_checked(self) -> Result { Ok(-self) } diff --git a/arrow-array/src/lib.rs b/arrow-array/src/lib.rs index e616099ccc8..5c86978dc94 100644 --- a/arrow-array/src/lib.rs +++ b/arrow-array/src/lib.rs @@ -164,6 +164,9 @@ pub use array::*; mod record_batch; pub use record_batch::{RecordBatch, RecordBatchOptions}; +mod arithmetic; +pub use arithmetic::ArrowNativeTypeOp; + pub mod builder; pub mod cast; mod delta; diff --git a/arrow/src/datatypes/mod.rs b/arrow/src/datatypes/mod.rs index 01462aeca96..5d625a051fd 100644 --- a/arrow/src/datatypes/mod.rs +++ b/arrow/src/datatypes/mod.rs @@ -22,12 +22,12 @@ //! * [`Field`](crate::datatypes::Field) to describe one field within a schema. //! * [`DataType`](crate::datatypes::DataType) to describe the type of a field. -mod native; -pub use native::*; mod numeric; pub use numeric::*; pub use arrow_array::types::*; +pub use arrow_array::{ArrowNativeTypeOp, ArrowPrimitiveType}; +pub use arrow_buffer::{i256, ArrowNativeType, ToByteSlice}; pub use arrow_data::decimal::*; pub use arrow_schema::{ DataType, Field, IntervalUnit, Schema, SchemaRef, TimeUnit, UnionMode,