Skip to content

Commit

Permalink
Move ArrowNativeTypeOp to arrow-array (#2594)
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold committed Nov 4, 2022
1 parent b4365eb commit 2a3e2f2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
41 changes: 20 additions & 21 deletions arrow/src/datatypes/native.rs → arrow-array/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -44,27 +43,27 @@ pub trait ArrowNativeTypeOp: ArrowNativeType {
/// The multiplicative identity
const ONE: Self;

fn add_checked(self, rhs: Self) -> Result<Self>;
fn add_checked(self, rhs: Self) -> Result<Self, ArrowError>;

fn add_wrapping(self, rhs: Self) -> Self;

fn sub_checked(self, rhs: Self) -> Result<Self>;
fn sub_checked(self, rhs: Self) -> Result<Self, ArrowError>;

fn sub_wrapping(self, rhs: Self) -> Self;

fn mul_checked(self, rhs: Self) -> Result<Self>;
fn mul_checked(self, rhs: Self) -> Result<Self, ArrowError>;

fn mul_wrapping(self, rhs: Self) -> Self;

fn div_checked(self, rhs: Self) -> Result<Self>;
fn div_checked(self, rhs: Self) -> Result<Self, ArrowError>;

fn div_wrapping(self, rhs: Self) -> Self;

fn mod_checked(self, rhs: Self) -> Result<Self>;
fn mod_checked(self, rhs: Self) -> Result<Self, ArrowError>;

fn mod_wrapping(self, rhs: Self) -> Self;

fn neg_checked(self) -> Result<Self>;
fn neg_checked(self) -> Result<Self, ArrowError>;

fn neg_wrapping(self) -> Self;

Expand Down Expand Up @@ -92,7 +91,7 @@ macro_rules! native_type_op {
const ZERO: Self = $zero;
const ONE: Self = $one;

fn add_checked(self, rhs: Self) -> Result<Self> {
fn add_checked(self, rhs: Self) -> Result<Self, ArrowError> {
self.checked_add(rhs).ok_or_else(|| {
ArrowError::ComputeError(format!(
"Overflow happened on: {:?} + {:?}",
Expand All @@ -105,7 +104,7 @@ macro_rules! native_type_op {
self.wrapping_add(rhs)
}

fn sub_checked(self, rhs: Self) -> Result<Self> {
fn sub_checked(self, rhs: Self) -> Result<Self, ArrowError> {
self.checked_sub(rhs).ok_or_else(|| {
ArrowError::ComputeError(format!(
"Overflow happened on: {:?} - {:?}",
Expand All @@ -118,7 +117,7 @@ macro_rules! native_type_op {
self.wrapping_sub(rhs)
}

fn mul_checked(self, rhs: Self) -> Result<Self> {
fn mul_checked(self, rhs: Self) -> Result<Self, ArrowError> {
self.checked_mul(rhs).ok_or_else(|| {
ArrowError::ComputeError(format!(
"Overflow happened on: {:?} * {:?}",
Expand All @@ -131,7 +130,7 @@ macro_rules! native_type_op {
self.wrapping_mul(rhs)
}

fn div_checked(self, rhs: Self) -> Result<Self> {
fn div_checked(self, rhs: Self) -> Result<Self, ArrowError> {
if rhs.is_zero() {
Err(ArrowError::DivideByZero)
} else {
Expand All @@ -148,7 +147,7 @@ macro_rules! native_type_op {
self.wrapping_div(rhs)
}

fn mod_checked(self, rhs: Self) -> Result<Self> {
fn mod_checked(self, rhs: Self) -> Result<Self, ArrowError> {
if rhs.is_zero() {
Err(ArrowError::DivideByZero)
} else {
Expand All @@ -165,7 +164,7 @@ macro_rules! native_type_op {
self.wrapping_rem(rhs)
}

fn neg_checked(self) -> Result<Self> {
fn neg_checked(self) -> Result<Self, ArrowError> {
self.checked_neg().ok_or_else(|| {
ArrowError::ComputeError(format!("Overflow happened on: {:?}", self))
})
Expand Down Expand Up @@ -223,31 +222,31 @@ macro_rules! native_type_float_op {
const ZERO: Self = $zero;
const ONE: Self = $one;

fn add_checked(self, rhs: Self) -> Result<Self> {
fn add_checked(self, rhs: Self) -> Result<Self, ArrowError> {
Ok(self + rhs)
}

fn add_wrapping(self, rhs: Self) -> Self {
self + rhs
}

fn sub_checked(self, rhs: Self) -> Result<Self> {
fn sub_checked(self, rhs: Self) -> Result<Self, ArrowError> {
Ok(self - rhs)
}

fn sub_wrapping(self, rhs: Self) -> Self {
self - rhs
}

fn mul_checked(self, rhs: Self) -> Result<Self> {
fn mul_checked(self, rhs: Self) -> Result<Self, ArrowError> {
Ok(self * rhs)
}

fn mul_wrapping(self, rhs: Self) -> Self {
self * rhs
}

fn div_checked(self, rhs: Self) -> Result<Self> {
fn div_checked(self, rhs: Self) -> Result<Self, ArrowError> {
if rhs.is_zero() {
Err(ArrowError::DivideByZero)
} else {
Expand All @@ -259,7 +258,7 @@ macro_rules! native_type_float_op {
self / rhs
}

fn mod_checked(self, rhs: Self) -> Result<Self> {
fn mod_checked(self, rhs: Self) -> Result<Self, ArrowError> {
if rhs.is_zero() {
Err(ArrowError::DivideByZero)
} else {
Expand All @@ -271,7 +270,7 @@ macro_rules! native_type_float_op {
self % rhs
}

fn neg_checked(self) -> Result<Self> {
fn neg_checked(self) -> Result<Self, ArrowError> {
Ok(-self)
}

Expand Down
3 changes: 3 additions & 0 deletions arrow-array/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions arrow/src/datatypes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2a3e2f2

Please sign in to comment.