Skip to content

Commit

Permalink
Remove _mut arithmetic kernels
Browse files Browse the repository at this point in the history
  • Loading branch information
viirya committed Nov 28, 2022
1 parent 1b3be81 commit f32042c
Showing 1 changed file with 5 additions and 46 deletions.
51 changes: 5 additions & 46 deletions arrow/src/compute/kernels/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,47 +915,6 @@ where
Ok(unary(array, |value| value.add_wrapping(scalar)))
}

/// Mutate an array by adding every value in an array by a scalar. If any value in the array
/// is null then the result is also null.
///
/// This only mutates the array if it is not shared buffers with other arrays. For shared
/// array, it returns an `Err` which wraps input array.
///
/// This doesn't detect overflow. Once overflowing, the result will wrap around.
/// For an overflow-checking variant, use `add_scalar_checked_mut` instead.
pub fn add_scalar_mut<T>(
array: PrimitiveArray<T>,
scalar: T::Native,
) -> std::result::Result<PrimitiveArray<T>, PrimitiveArray<T>>
where
T: ArrowNumericType,
T::Native: ArrowNativeTypeOp,
{
unary_mut(array, |value| value.add_wrapping(scalar))
}

/// Mutate an array by adding every value in an array by a scalar. If any value in the array
/// is null then the result is also null.
///
/// This only mutates the array if it is not shared buffers with other arrays. For shared
/// array, it returns an `Err` which wraps input array.
///
/// This detects overflow and returns an `Err` within an `Ok` which wraps an `Error` of
/// actual error. For an non-overflow-checking variant, use `add_scalar_mut` instead.
pub fn add_scalar_checked_mut<T>(
array: PrimitiveArray<T>,
scalar: T::Native,
) -> std::result::Result<
std::result::Result<PrimitiveArray<T>, ArrowError>,
PrimitiveArray<T>,
>
where
T: ArrowNumericType,
T::Native: ArrowNativeTypeOp,
{
try_unary_mut(array, |value| value.add_checked(scalar))
}

/// Add every value in an array by a scalar. If any value in the array is null then the
/// result is also null.
///
Expand Down Expand Up @@ -3142,24 +3101,24 @@ mod tests {
}

#[test]
fn test_primitive_array_add_scalar_mut() {
fn test_primitive_add_scalar_by_unary_mut() {
let a = Int32Array::from(vec![15, 14, 9, 8, 1]);
let b = 3;
let c = add_scalar_mut(a, b).unwrap();
let c = unary_mut(a, |value| value.add_wrapping(b)).unwrap();
let expected = Int32Array::from(vec![18, 17, 12, 11, 4]);
assert_eq!(c, expected);
}

#[test]
fn test_primitive_add_scalar_mut_wrapping_overflow() {
fn test_primitive_add_scalar_overflow_by_try_unary_mut() {
let a = Int32Array::from(vec![i32::MAX, i32::MIN]);

let wrapped = add_scalar_mut(a, 1).unwrap();
let wrapped = unary_mut(a, |value| value.add_wrapping(1)).unwrap();
let expected = Int32Array::from(vec![-2147483648, -2147483647]);
assert_eq!(expected, wrapped);

let a = Int32Array::from(vec![i32::MAX, i32::MIN]);
let overflow = add_scalar_checked_mut(a, 1);
let overflow = try_unary_mut(a, |value| value.add_checked(1));
let _ = overflow.unwrap().expect_err("overflow should be detected");
}
}

0 comments on commit f32042c

Please sign in to comment.