MCPcopy Create free account
hub / github.com/apache/arrow-rs / try_binary_mut

Function try_binary_mut

arrow-arith/src/arity.rs:305–352  ·  view source on GitHub ↗

Applies the provided fallible binary operation across `a` and `b` by mutating the mutable [`PrimitiveArray`] `a` with the results. Returns any error encountered, or collects the results into a [`PrimitiveArray`] as return value. 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 indice

(
    a: PrimitiveArray<T>,
    b: &PrimitiveArray<T>,
    op: F,
)

Source from the content-addressed store, hash-verified

303///
304/// See [`binary_mut`] for errors and buffer reuse information.
305pub fn try_binary_mut<T, F>(
306 a: PrimitiveArray<T>,
307 b: &PrimitiveArray<T>,
308 op: F,
309) -> Result<Result<PrimitiveArray<T>, ArrowError>, PrimitiveArray<T>>
310where
311 T: ArrowPrimitiveType,
312 F: Fn(T::Native, T::Native) -> Result<T::Native, ArrowError>,
313{
314 if a.len() != b.len() {
315 return Ok(Err(ArrowError::ComputeError(
316 "Cannot perform binary operation on arrays of different length".to_string(),
317 )));
318 }
319 let len = a.len();
320
321 if a.is_empty() {
322 return Ok(Ok(PrimitiveArray::from(ArrayData::new_empty(
323 &T::DATA_TYPE,
324 ))));
325 }
326
327 if a.null_count() == 0 && b.null_count() == 0 {
328 try_binary_no_nulls_mut(len, a, b, op)
329 } else {
330 let nulls =
331 create_union_null_buffer(a.logical_nulls().as_ref(), b.logical_nulls().as_ref())
332 .unwrap();
333
334 let mut builder = a.into_builder()?;
335
336 let slice = builder.values_slice_mut();
337
338 let r = nulls.try_for_each_valid_idx(|idx| {
339 unsafe {
340 *slice.get_unchecked_mut(idx) =
341 op(*slice.get_unchecked(idx), b.value_unchecked(idx))?
342 };
343 Ok::<_, ArrowError>(())
344 });
345 if let Err(err) = r {
346 return Ok(Err(err));
347 }
348 let array_builder = builder.finish().into_data().into_builder();
349 let array_data = unsafe { array_builder.nulls(Some(nulls)).build_unchecked() };
350 Ok(Ok(PrimitiveArray::<T>::from(array_data)))
351 }
352}
353
354/// Computes the union of the nulls in two optional [`NullBuffer`] which
355/// is not shared with the input buffers.

Callers 2

test_try_binary_mutFunction · 0.85

Calls 15

try_binary_no_nulls_mutFunction · 0.85
create_union_null_bufferFunction · 0.85
lenMethod · 0.45
is_emptyMethod · 0.45
null_countMethod · 0.45
as_refMethod · 0.45
logical_nullsMethod · 0.45
into_builderMethod · 0.45
values_slice_mutMethod · 0.45
value_uncheckedMethod · 0.45
into_dataMethod · 0.45

Tested by 2

test_try_binary_mutFunction · 0.68