From 19c0b7a30eaa0941c44b83d38e92f8054c0bf763 Mon Sep 17 00:00:00 2001 From: Liang-Chi Hsieh Date: Thu, 6 Oct 2022 23:49:50 -0700 Subject: [PATCH] Expose ArrowNativeTypeOp --- arrow/src/compute/kernels/aggregate.rs | 3 +- arrow/src/compute/kernels/arithmetic.rs | 2 +- arrow/src/compute/kernels/comparison.rs | 14 +- arrow/src/datatypes/native.rs | 164 ++++++++++++------------ 4 files changed, 91 insertions(+), 92 deletions(-) diff --git a/arrow/src/compute/kernels/aggregate.rs b/arrow/src/compute/kernels/aggregate.rs index 083defdde7d..e6c927c2486 100644 --- a/arrow/src/compute/kernels/aggregate.rs +++ b/arrow/src/compute/kernels/aggregate.rs @@ -27,8 +27,7 @@ use crate::array::{ as_primitive_array, Array, ArrayAccessor, ArrayIter, BooleanArray, GenericBinaryArray, GenericStringArray, OffsetSizeTrait, PrimitiveArray, }; -use crate::datatypes::native_op::ArrowNativeTypeOp; -use crate::datatypes::{ArrowNativeType, ArrowNumericType, DataType}; +use crate::datatypes::{ArrowNativeType, ArrowNativeTypeOp, ArrowNumericType, DataType}; use crate::error::Result; use crate::util::bit_iterator::BitIndexIterator; diff --git a/arrow/src/compute/kernels/arithmetic.rs b/arrow/src/compute/kernels/arithmetic.rs index 1e6e55248b7..fe222c3d15d 100644 --- a/arrow/src/compute/kernels/arithmetic.rs +++ b/arrow/src/compute/kernels/arithmetic.rs @@ -34,7 +34,7 @@ use crate::compute::{ binary, binary_opt, try_binary, try_unary, try_unary_dyn, unary_dyn, }; use crate::datatypes::{ - native_op::ArrowNativeTypeOp, ArrowNumericType, DataType, Date32Type, Date64Type, + ArrowNativeTypeOp, ArrowNumericType, DataType, Date32Type, Date64Type, IntervalDayTimeType, IntervalMonthDayNanoType, IntervalUnit, IntervalYearMonthType, }; #[cfg(feature = "dyn_arith_dict")] diff --git a/arrow/src/compute/kernels/comparison.rs b/arrow/src/compute/kernels/comparison.rs index d1d1e470e51..143050ea97f 100644 --- a/arrow/src/compute/kernels/comparison.rs +++ b/arrow/src/compute/kernels/comparison.rs @@ -27,13 +27,13 @@ use crate::array::*; use crate::buffer::{buffer_unary_not, Buffer, MutableBuffer}; use crate::compute::util::combine_option_bitmap; use crate::datatypes::{ - native_op::ArrowNativeTypeOp, ArrowNativeType, ArrowNumericType, DataType, - Date32Type, Date64Type, Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, - Int8Type, IntervalDayTimeType, IntervalMonthDayNanoType, IntervalUnit, - IntervalYearMonthType, Time32MillisecondType, Time32SecondType, - Time64MicrosecondType, Time64NanosecondType, TimeUnit, TimestampMicrosecondType, - TimestampMillisecondType, TimestampNanosecondType, TimestampSecondType, UInt16Type, - UInt32Type, UInt64Type, UInt8Type, + ArrowNativeType, ArrowNativeTypeOp, ArrowNumericType, DataType, Date32Type, + Date64Type, Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, + IntervalDayTimeType, IntervalMonthDayNanoType, IntervalUnit, IntervalYearMonthType, + Time32MillisecondType, Time32SecondType, Time64MicrosecondType, Time64NanosecondType, + TimeUnit, TimestampMicrosecondType, TimestampMillisecondType, + TimestampNanosecondType, TimestampSecondType, UInt16Type, UInt32Type, UInt64Type, + UInt8Type, }; #[allow(unused_imports)] use crate::downcast_dictionary_array; diff --git a/arrow/src/datatypes/native.rs b/arrow/src/datatypes/native.rs index 374d0b950ea..2ff9574c78a 100644 --- a/arrow/src/datatypes/native.rs +++ b/arrow/src/datatypes/native.rs @@ -16,114 +16,113 @@ // under the License. use crate::error::{ArrowError, Result}; +pub use arrow_array::ArrowPrimitiveType; pub use arrow_buffer::{ArrowNativeType, ToByteSlice}; use half::f16; use num::Zero; +use std::ops::{Add, Div, Mul, Rem, Sub}; -pub use arrow_array::ArrowPrimitiveType; +mod private { + pub trait Sealed {} +} -pub(crate) mod native_op { - use super::ArrowNativeType; - use crate::error::{ArrowError, Result}; - use num::Zero; - use std::ops::{Add, Div, Mul, Rem, Sub}; - - /// Trait for ArrowNativeType to provide overflow-checking and non-overflow-checking - /// variants for arithmetic operations. For floating point types, this provides some - /// default implementations. Integer types that need to deal with overflow can implement - /// this trait. - /// - /// The APIs with `_wrapping` suffix are the variant of non-overflow-checking. If overflow - /// occurred, they will supposedly wrap around the boundary of the type. - /// - /// The APIs with `_checked` suffix are the variant of overflow-checking which return `None` - /// if overflow occurred. - pub trait ArrowNativeTypeOp: - ArrowNativeType - + Add - + Sub - + Mul - + Div - + Rem - + Zero - { - fn add_checked(self, rhs: Self) -> Result { - Ok(self + rhs) - } +/// Trait for ArrowNativeType to provide overflow-checking and non-overflow-checking +/// variants for arithmetic operations. For floating point types, this provides some +/// default implementations. Integer types that need to deal with overflow can implement +/// this trait. +/// +/// The APIs with `_wrapping` suffix are the variant of non-overflow-checking. If overflow +/// occurred, they will supposedly wrap around the boundary of the type. +/// +/// The APIs with `_checked` suffix are the variant of overflow-checking which return `None` +/// if overflow occurred. +pub trait ArrowNativeTypeOp: + ArrowNativeType + + Add + + Sub + + Mul + + Div + + Rem + + Zero + + private::Sealed +{ + fn add_checked(self, rhs: Self) -> Result { + Ok(self + rhs) + } - fn add_wrapping(self, rhs: Self) -> Self { - self + rhs - } + fn add_wrapping(self, rhs: Self) -> Self { + self + rhs + } - fn sub_checked(self, rhs: Self) -> Result { - Ok(self - rhs) - } + fn sub_checked(self, rhs: Self) -> Result { + Ok(self - rhs) + } - fn sub_wrapping(self, rhs: Self) -> Self { - self - rhs - } + fn sub_wrapping(self, rhs: Self) -> Self { + self - rhs + } - fn mul_checked(self, rhs: Self) -> Result { - Ok(self * rhs) - } + fn mul_checked(self, rhs: Self) -> Result { + Ok(self * rhs) + } - fn mul_wrapping(self, rhs: Self) -> Self { - self * rhs - } + fn mul_wrapping(self, rhs: Self) -> Self { + self * rhs + } - fn div_checked(self, rhs: Self) -> Result { - if rhs.is_zero() { - Err(ArrowError::DivideByZero) - } else { - Ok(self / rhs) - } + fn div_checked(self, rhs: Self) -> Result { + if rhs.is_zero() { + Err(ArrowError::DivideByZero) + } else { + Ok(self / rhs) } + } - fn div_wrapping(self, rhs: Self) -> Self { - self / rhs - } + fn div_wrapping(self, rhs: Self) -> Self { + self / rhs + } - fn mod_checked(self, rhs: Self) -> Result { - if rhs.is_zero() { - Err(ArrowError::DivideByZero) - } else { - Ok(self % rhs) - } + fn mod_checked(self, rhs: Self) -> Result { + if rhs.is_zero() { + Err(ArrowError::DivideByZero) + } else { + Ok(self % rhs) } + } - fn mod_wrapping(self, rhs: Self) -> Self { - self % rhs - } + fn mod_wrapping(self, rhs: Self) -> Self { + self % rhs + } - fn is_eq(self, rhs: Self) -> bool { - self == rhs - } + fn is_eq(self, rhs: Self) -> bool { + self == rhs + } - fn is_ne(self, rhs: Self) -> bool { - self != rhs - } + fn is_ne(self, rhs: Self) -> bool { + self != rhs + } - fn is_lt(self, rhs: Self) -> bool { - self < rhs - } + fn is_lt(self, rhs: Self) -> bool { + self < rhs + } - fn is_le(self, rhs: Self) -> bool { - self <= rhs - } + fn is_le(self, rhs: Self) -> bool { + self <= rhs + } - fn is_gt(self, rhs: Self) -> bool { - self > rhs - } + fn is_gt(self, rhs: Self) -> bool { + self > rhs + } - fn is_ge(self, rhs: Self) -> bool { - self >= rhs - } + fn is_ge(self, rhs: Self) -> bool { + self >= rhs } } macro_rules! native_type_op { ($t:tt) => { - impl native_op::ArrowNativeTypeOp for $t { + impl private::Sealed for $t {} + impl ArrowNativeTypeOp for $t { fn add_checked(self, rhs: Self) -> Result { self.checked_add(rhs).ok_or_else(|| { ArrowError::ComputeError(format!( @@ -212,7 +211,8 @@ native_type_op!(u64); macro_rules! native_type_float_op { ($t:tt) => { - impl native_op::ArrowNativeTypeOp for $t { + impl private::Sealed for $t {} + impl ArrowNativeTypeOp for $t { fn is_eq(self, rhs: Self) -> bool { self.total_cmp(&rhs).is_eq() }