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

feat: support extended pow arithmetic #4167

Closed
wants to merge 3 commits into from
Closed
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
93 changes: 92 additions & 1 deletion arrow-arith/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,19 @@ pub fn negate_checked<T: ArrowNumericType>(
try_unary(array, |value| value.neg_checked())
}

/// Perform `left ^ right` operation on two float arrays (`Float*Array`, except `Float16Array`). If either left or right value is null
/// then the result is also null.
pub fn powf<T: ArrowNumericType>(
left: &PrimitiveArray<T>,
right: &PrimitiveArray<T>,
) -> Result<PrimitiveArray<T>, ArrowError>
where
T: ArrowFloatNumericType,
T::Native: Pow<T::Native, Output = T::Native>,
{
math_op(left, right, |a, b| a.pow(b))
}

/// Raise array with floating point values to the power of a scalar.
pub fn powf_scalar<T>(
array: &PrimitiveArray<T>,
Expand All @@ -1354,6 +1367,31 @@ where
Ok(unary(array, |x| x.pow(raise)))
}

/// Perform `left ^ right` operation on integer array (`Int*Array`, `UInt*Array`) and `UInt32Array`. If either left or right value is null
/// then the result is also null.
pub fn powi<T: ArrowNumericType>(
left: &PrimitiveArray<T>,
right: &UInt32Array,
) -> Result<PrimitiveArray<T>, ArrowError>
where
T: ArrowIntegerNumericType,
T::Native: Pow<u32, Output = T::Native>,
{
math_op(left, right, |a, b| a.pow(b))
}

/// Raise array with integer values to the power of a scalar (`u32`).
pub fn powi_scalar<T>(
array: &PrimitiveArray<T>,
raise: u32,
) -> Result<PrimitiveArray<T>, ArrowError>
where
T: ArrowIntegerNumericType,
T::Native: Pow<u32, Output = T::Native>,
{
Ok(unary(array, |x| x.pow(raise)))
}

/// Perform `left * right` operation on two arrays. If either left or right value is null
/// then the result is also null.
///
Expand Down Expand Up @@ -3218,7 +3256,60 @@ mod tests {
}

#[test]
fn test_primitive_array_raise_power_scalar() {
fn test_primitive_array_raise_integer_power_array() {
// Int64Array
let a = Int64Array::from(vec![1, 2, 3]);
let b = UInt32Array::from(vec![1, 2, 3]);
let actual = powi(&a, &b).unwrap();
let expected = Int64Array::from(vec![1, 4, 27]);
assert_eq!(expected, actual);
let a = Int64Array::from(vec![Some(1), None, Some(3)]);
let b = UInt32Array::from(vec![None, Some(2), Some(3)]);
let actual = powi(&a, &b).unwrap();
let expected = Int64Array::from(vec![None, None, Some(27)]);
assert_eq!(expected, actual);

// UInt64Array
let a = UInt64Array::from(vec![1, 2, 3]);
let b = UInt32Array::from(vec![1, 2, 3]);
let actual = powi(&a, &b).unwrap();
let expected = UInt64Array::from(vec![1, 4, 27]);
assert_eq!(expected, actual);
let a = UInt64Array::from(vec![Some(1), None, Some(3)]);
let b = UInt32Array::from(vec![None, Some(2), Some(3)]);
let actual = powi(&a, &b).unwrap();
let expected = UInt64Array::from(vec![None, None, Some(27)]);
assert_eq!(expected, actual);
}

#[test]
fn test_primitive_array_raise_float_power_array() {
let a = Float64Array::from(vec![1.0, 2.0, 3.0]);
let b = Float64Array::from(vec![1.0, 2.0, 3.0]);
let actual = powf(&a, &b).unwrap();
let expected = Float64Array::from(vec![1.0, 4.0, 27.0]);
assert_eq!(expected, actual);
let a = Float64Array::from(vec![Some(1.0), None, Some(3.0)]);
let b = Float64Array::from(vec![None, Some(2.0), Some(3.0)]);
let actual = powf(&a, &b).unwrap();
let expected = Float64Array::from(vec![None, None, Some(27.0)]);
assert_eq!(expected, actual);
}

#[test]
fn test_primitive_array_raise_integer_power_scalar() {
let a = Int64Array::from(vec![1, 2, 3]);
let actual = powi_scalar(&a, 2_u32).unwrap();
let expected = Int64Array::from(vec![1, 4, 9]);
assert_eq!(expected, actual);
let a = Int64Array::from(vec![Some(1), None, Some(3)]);
let actual = powi_scalar(&a, 2_u32).unwrap();
let expected = Int64Array::from(vec![Some(1), None, Some(9)]);
assert_eq!(expected, actual);
}

#[test]
fn test_primitive_array_raise_float_power_scalar() {
let a = Float64Array::from(vec![1.0, 2.0, 3.0]);
let actual = powf_scalar(&a, 2.0).unwrap();
let expected = Float64Array::from(vec![1.0, 4.0, 9.0]);
Expand Down
27 changes: 27 additions & 0 deletions arrow-array/src/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,33 @@ impl ArrowNumericType for Decimal256Type {
}
}

/// A subtype of primitive type that represents numeric integer values
#[cfg(feature = "simd")]
pub trait ArrowIntegerNumericType: ArrowNumericType {}

/// A subtype of primitive type that represents numeric integer values
#[cfg(not(feature = "simd"))]
pub trait ArrowIntegerNumericType: ArrowNumericType {}

macro_rules! make_integer_numeric_type {
($impl_ty:ty, $simd_ty:ident) => {
#[cfg(feature = "simd")]
impl ArrowIntegerNumericType for $impl_ty {}

#[cfg(not(feature = "simd"))]
impl ArrowIntegerNumericType for $impl_ty {}
};
}

make_integer_numeric_type!(Int8Type, i8x64);
make_integer_numeric_type!(Int16Type, i16x32);
make_integer_numeric_type!(Int32Type, i32x16);
make_integer_numeric_type!(Int64Type, i64x8);
make_integer_numeric_type!(UInt8Type, u8x64);
make_integer_numeric_type!(UInt16Type, u16x32);
make_integer_numeric_type!(UInt32Type, u32x16);
make_integer_numeric_type!(UInt64Type, u64x8);

/// A subtype of primitive type that represents numeric float values
#[cfg(feature = "simd")]
pub trait ArrowFloatNumericType: ArrowNumericType {
Expand Down
3 changes: 2 additions & 1 deletion arrow/src/datatypes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

pub use arrow_array::types::*;
pub use arrow_array::{
ArrowFloatNumericType, ArrowNativeTypeOp, ArrowNumericType, ArrowPrimitiveType,
ArrowFloatNumericType, ArrowIntegerNumericType, ArrowNativeTypeOp, ArrowNumericType,
ArrowPrimitiveType,
};
pub use arrow_buffer::{i256, ArrowNativeType, ToByteSlice};
pub use arrow_data::decimal::*;
Expand Down