-
Notifications
You must be signed in to change notification settings - Fork 841
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
Add binary_mut and try_binary_mut #3144
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1c6504c
Add add_mut
viirya 526abd5
Add try_binary_mut
viirya c1e3f4d
Add test
viirya 7191dba
Change result type
viirya 0b672be
Remove _mut kernels
viirya 583fd9c
Merge remote-tracking branch 'upstream/master' into add_mut
viirya fccee61
Fix clippy
viirya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,6 +205,72 @@ where | |
Ok(unsafe { build_primitive_array(len, buffer, null_count, null_buffer) }) | ||
} | ||
|
||
/// Given two arrays of length `len`, calls `op(a[i], b[i])` for `i` in `0..len`, mutating | ||
/// the mutable [`PrimitiveArray`] `a`. If any index is null in either `a` or `b`, the | ||
/// corresponding index in the result will also be null. | ||
/// | ||
/// Mutable primitive array means that the buffer is not shared with other arrays. | ||
/// As a result, this mutates the buffer directly without allocating new buffer. | ||
/// | ||
/// Like [`unary`] the provided function is evaluated for every index, ignoring validity. This | ||
/// is beneficial when the cost of the operation is low compared to the cost of branching, and | ||
/// especially when the operation can be vectorised, however, requires `op` to be infallible | ||
/// for all possible values of its inputs | ||
/// | ||
/// # Error | ||
/// | ||
/// This function gives error if the arrays have different lengths. | ||
/// This function gives error of original [`PrimitiveArray`] `a` if it is not a mutable | ||
/// primitive array. | ||
pub fn binary_mut<T, F>( | ||
a: PrimitiveArray<T>, | ||
b: &PrimitiveArray<T>, | ||
op: F, | ||
) -> std::result::Result< | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned on #3134 I think this Result should be the other way round |
||
PrimitiveArray<T>, | ||
std::result::Result<PrimitiveArray<T>, ArrowError>, | ||
> | ||
where | ||
T: ArrowPrimitiveType, | ||
F: Fn(T::Native, T::Native) -> T::Native, | ||
{ | ||
if a.len() != b.len() { | ||
return Err(Err(ArrowError::ComputeError( | ||
"Cannot perform binary operation on arrays of different length".to_string(), | ||
))); | ||
} | ||
let len = a.len(); | ||
|
||
if a.is_empty() { | ||
return Ok(PrimitiveArray::from(ArrayData::new_empty(&T::DATA_TYPE))); | ||
} | ||
|
||
let null_buffer = combine_option_bitmap(&[a.data(), b.data()], len).unwrap(); | ||
let null_count = null_buffer | ||
.as_ref() | ||
.map(|x| len - x.count_set_bits_offset(0, len)) | ||
.unwrap_or_default(); | ||
|
||
let mut builder = a.into_builder().map_err(Ok)?; | ||
|
||
builder | ||
.values_slice_mut() | ||
.iter_mut() | ||
.zip(b.values()) | ||
.for_each(|(l, r)| *l = op(*l, *r)); | ||
|
||
let array_builder = builder | ||
.finish() | ||
.data() | ||
.clone() | ||
.into_builder() | ||
.null_bit_buffer(null_buffer) | ||
.null_count(null_count); | ||
|
||
let array_data = unsafe { array_builder.build_unchecked() }; | ||
Ok(PrimitiveArray::<T>::from(array_data)) | ||
} | ||
|
||
/// Applies the provided fallible binary operation across `a` and `b`, returning any error, | ||
/// and collecting the results into a [`PrimitiveArray`]. If any index is null in either `a` | ||
/// or `b`, the corresponding index in the result will also be null | ||
|
@@ -262,6 +328,79 @@ where | |
} | ||
} | ||
|
||
/// Applies the provided fallible binary operation across `a` and `b` by mutating the mutable | ||
/// [`PrimitiveArray`] `a` with the results, returning any error. If any index is null in | ||
/// either `a` or `b`, the corresponding index in the result will also be null | ||
/// | ||
/// Like [`try_unary`] the function is only evaluated for non-null indices | ||
/// | ||
/// Mutable primitive array means that the buffer is not shared with other arrays. | ||
/// As a result, this mutates the buffer directly without allocating new buffer. | ||
/// | ||
/// # Error | ||
/// | ||
/// Return an error if the arrays have different lengths or | ||
/// the operation is under erroneous. | ||
/// This function gives error of original [`PrimitiveArray`] `a` if it is not a mutable | ||
/// primitive array. | ||
pub fn try_binary_mut<T, F>( | ||
a: PrimitiveArray<T>, | ||
b: &PrimitiveArray<T>, | ||
op: F, | ||
) -> std::result::Result< | ||
PrimitiveArray<T>, | ||
std::result::Result<PrimitiveArray<T>, ArrowError>, | ||
> | ||
where | ||
T: ArrowPrimitiveType, | ||
F: Fn(T::Native, T::Native) -> Result<T::Native>, | ||
{ | ||
if a.len() != b.len() { | ||
return Err(Err(ArrowError::ComputeError( | ||
"Cannot perform binary operation on arrays of different length".to_string(), | ||
))); | ||
} | ||
let len = a.len(); | ||
|
||
if a.is_empty() { | ||
return Ok(PrimitiveArray::from(ArrayData::new_empty(&T::DATA_TYPE))); | ||
} | ||
viirya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if a.null_count() == 0 && b.null_count() == 0 { | ||
try_binary_no_nulls_mut(len, a, b, op) | ||
} else { | ||
let null_buffer = combine_option_bitmap(&[a.data(), b.data()], len).unwrap(); | ||
let null_count = null_buffer | ||
.as_ref() | ||
.map(|x| len - x.count_set_bits_offset(0, len)) | ||
.unwrap_or_default(); | ||
|
||
let mut builder = a.into_builder().map_err(Ok)?; | ||
|
||
let slice = builder.values_slice_mut(); | ||
|
||
try_for_each_valid_idx(len, 0, null_count, null_buffer.as_deref(), |idx| { | ||
unsafe { | ||
*slice.get_unchecked_mut(idx) = | ||
op(*slice.get_unchecked(idx), b.value_unchecked(idx))? | ||
}; | ||
Ok::<_, ArrowError>(()) | ||
}) | ||
.map_err(Err)?; | ||
|
||
let array_builder = builder | ||
.finish() | ||
.data() | ||
.clone() | ||
.into_builder() | ||
.null_bit_buffer(null_buffer) | ||
.null_count(null_count); | ||
|
||
let array_data = unsafe { array_builder.build_unchecked() }; | ||
Ok(PrimitiveArray::<T>::from(array_data)) | ||
} | ||
} | ||
|
||
/// This intentional inline(never) attribute helps LLVM optimize the loop. | ||
#[inline(never)] | ||
fn try_binary_no_nulls<A: ArrayAccessor, B: ArrayAccessor, F, O>( | ||
|
@@ -283,6 +422,33 @@ where | |
Ok(unsafe { build_primitive_array(len, buffer.into(), 0, None) }) | ||
} | ||
|
||
/// This intentional inline(never) attribute helps LLVM optimize the loop. | ||
#[inline(never)] | ||
fn try_binary_no_nulls_mut<T, F>( | ||
len: usize, | ||
a: PrimitiveArray<T>, | ||
b: &PrimitiveArray<T>, | ||
op: F, | ||
) -> std::result::Result< | ||
PrimitiveArray<T>, | ||
std::result::Result<PrimitiveArray<T>, ArrowError>, | ||
> | ||
where | ||
T: ArrowPrimitiveType, | ||
F: Fn(T::Native, T::Native) -> Result<T::Native>, | ||
{ | ||
let mut builder = a.into_builder().map_err(Ok)?; | ||
let slice = builder.values_slice_mut(); | ||
|
||
for idx in 0..len { | ||
unsafe { | ||
*slice.get_unchecked_mut(idx) = | ||
op(*slice.get_unchecked(idx), b.value_unchecked(idx)).map_err(Err)?; | ||
}; | ||
} | ||
Ok(builder.finish()) | ||
} | ||
|
||
#[inline(never)] | ||
fn try_binary_opt_no_nulls<A: ArrayAccessor, B: ArrayAccessor, F, O>( | ||
len: usize, | ||
|
@@ -358,6 +524,7 @@ mod tests { | |
use super::*; | ||
use crate::array::{as_primitive_array, Float64Array, PrimitiveDictionaryBuilder}; | ||
use crate::datatypes::{Float64Type, Int32Type, Int8Type}; | ||
use arrow_array::Int32Array; | ||
|
||
#[test] | ||
fn test_unary_f64_slice() { | ||
|
@@ -417,4 +584,37 @@ mod tests { | |
&expected | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_binary_mut() { | ||
let a = Int32Array::from(vec![15, 14, 9, 8, 1]); | ||
let b = Int32Array::from(vec![Some(1), None, Some(3), None, Some(5)]); | ||
let c = binary_mut(a, &b, |l, r| l + r).unwrap(); | ||
|
||
let expected = Int32Array::from(vec![Some(16), None, Some(12), None, Some(6)]); | ||
assert_eq!(c, expected); | ||
} | ||
|
||
#[test] | ||
fn test_try_binary_mut() { | ||
viirya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let a = Int32Array::from(vec![15, 14, 9, 8, 1]); | ||
let b = Int32Array::from(vec![Some(1), None, Some(3), None, Some(5)]); | ||
let c = try_binary_mut(a, &b, |l, r| Ok(l + r)).unwrap(); | ||
|
||
let expected = Int32Array::from(vec![Some(16), None, Some(12), None, Some(6)]); | ||
assert_eq!(c, expected); | ||
|
||
let a = Int32Array::from(vec![15, 14, 9, 8, 1]); | ||
let b = Int32Array::from(vec![Some(1), None, Some(3), None, Some(5)]); | ||
let _ = try_binary_mut(a, &b, |l, r| { | ||
if l == 1 { | ||
Err(ArrowError::InvalidArgumentError( | ||
"got error".parse().unwrap(), | ||
)) | ||
} else { | ||
Ok(l + r) | ||
} | ||
}) | ||
.expect_err("should got error"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly, I will leave this only and remove
_mut
kernels.