Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move ArrowNativeTypeOp to arrow-array (#2594) #3018

Merged
merged 3 commits into from
Nov 4, 2022
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
2 changes: 1 addition & 1 deletion arrow-array/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ arrow-data = { version = "26.0.0", path = "../arrow-data" }
chrono = { version = "0.4", default-features = false, features = ["clock"] }
chrono-tz = { version = "0.8", optional = true }
num = { version = "0.4", default-features = false, features = ["std"] }
half = { version = "2.1", default-features = false }
half = { version = "2.1", default-features = false, features = ["num-traits"] }
hashbrown = { version = "0.12", default-features = false }

[dev-dependencies]
Expand Down
47 changes: 23 additions & 24 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;
use num::complex::ComplexFloat;

Expand Down Expand Up @@ -45,31 +44,31 @@ 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;

fn pow_checked(self, exp: u32) -> Result<Self>;
fn pow_checked(self, exp: u32) -> Result<Self, ArrowError>;

fn pow_wrapping(self, exp: u32) -> Self;

Expand Down Expand Up @@ -97,7 +96,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 @@ -110,7 +109,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 @@ -123,7 +122,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 @@ -136,7 +135,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 @@ -153,7 +152,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 @@ -170,13 +169,13 @@ 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))
})
}

fn pow_checked(self, exp: u32) -> Result<Self> {
fn pow_checked(self, exp: u32) -> Result<Self, ArrowError> {
self.checked_pow(exp).ok_or_else(|| {
ArrowError::ComputeError(format!("Overflow happened on: {:?}", self))
})
Expand Down Expand Up @@ -238,31 +237,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 @@ -274,7 +273,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 @@ -286,15 +285,15 @@ macro_rules! native_type_float_op {
self % rhs
}

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

fn neg_wrapping(self) -> Self {
-self
}

fn pow_checked(self, exp: u32) -> Result<Self> {
fn pow_checked(self, exp: u32) -> Result<Self, ArrowError> {
Ok(self.powi(exp as i32))
}

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