Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
viirya committed Sep 21, 2022
1 parent d1ee07c commit 4637a48
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
24 changes: 24 additions & 0 deletions arrow/src/compute/kernels/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2830,4 +2830,28 @@ mod tests {
let overflow = divide_dyn_checked(&a, &b);
overflow.expect_err("overflow should be detected");
}

#[test]
#[cfg(feature = "dyn_arith_dict")]
fn test_div_dyn_opt_overflow_division_by_zero() {
let a = Int32Array::from(vec![i32::MIN]);
let b = Int32Array::from(vec![0]);

let division_by_zero = divide_dyn_opt(&a, &b);
let expected = Arc::new(Int32Array::from(vec![None])) as ArrayRef;
assert_eq!(&expected, &division_by_zero.unwrap());

let mut builder =
PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::with_capacity(1, 1);
builder.append(i32::MIN).unwrap();
let a = builder.finish();

let mut builder =
PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::with_capacity(1, 1);
builder.append(0).unwrap();
let b = builder.finish();

let division_by_zero = divide_dyn_opt(&a, &b);
assert_eq!(&expected, &division_by_zero.unwrap());
}
}
24 changes: 24 additions & 0 deletions arrow/src/compute/kernels/arity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,26 @@ where
Ok(unsafe { build_primitive_array(len, buffer.into(), 0, None) })
}

#[inline(never)]
fn try_binary_opt_no_nulls<A: ArrayAccessor, B: ArrayAccessor, F, O>(
len: usize,
a: A,
b: B,
op: F,
) -> Result<PrimitiveArray<O>>
where
O: ArrowPrimitiveType,
F: Fn(A::Item, B::Item) -> Option<O::Native>,
{
let mut buffer = Vec::with_capacity(10);
for idx in 0..len {
unsafe {
buffer.push(op(a.value_unchecked(idx), b.value_unchecked(idx)));
};
}
Ok(buffer.iter().collect())
}

/// Applies the provided binary operation across `a` and `b`, collecting the optional results
/// into a [`PrimitiveArray`]. If any index is null in either `a` or `b`, the corresponding
/// index in the result will also be null. The binary operation could return `None` which
Expand Down Expand Up @@ -386,6 +406,10 @@ where
return Ok(PrimitiveArray::from(ArrayData::new_empty(&O::DATA_TYPE)));
}

if a.null_count() == 0 && b.null_count() == 0 {
return Ok(try_binary_opt_no_nulls(a.len(), a, b, op)?);
}

let iter_a = ArrayIter::new(a);
let iter_b = ArrayIter::new(b);

Expand Down

0 comments on commit 4637a48

Please sign in to comment.