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

Function try_binary

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

Applies the provided fallible binary operation across `a` and `b`. This will return any error encountered, or collect 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 Like [`try_unary`] the function is only evaluated for non-null indices # Error Return an error if the arrays have different lengths or the o

(
    a: A,
    b: B,
    op: F,
)

Source from the content-addressed store, hash-verified

252/// Return an error if the arrays have different lengths or
253/// the operation is under erroneous
254pub fn try_binary<A: ArrayAccessor, B: ArrayAccessor, F, O>(
255 a: A,
256 b: B,
257 op: F,
258) -> Result<PrimitiveArray<O>, ArrowError>
259where
260 O: ArrowPrimitiveType,
261 F: Fn(A::Item, B::Item) -> Result<O::Native, ArrowError>,
262{
263 if a.len() != b.len() {
264 return Err(ArrowError::ComputeError(
265 "Cannot perform a binary operation on arrays of different length".to_string(),
266 ));
267 }
268 if a.is_empty() {
269 return Ok(PrimitiveArray::from(ArrayData::new_empty(&O::DATA_TYPE)));
270 }
271 let len = a.len();
272
273 if a.null_count() == 0 && b.null_count() == 0 {
274 try_binary_no_nulls(len, a, b, op)
275 } else {
276 let nulls =
277 NullBuffer::union(a.logical_nulls().as_ref(), b.logical_nulls().as_ref()).unwrap();
278
279 let mut buffer = BufferBuilder::<O::Native>::new(len);
280 buffer.append_n_zeroed(len);
281 let slice = buffer.as_slice_mut();
282
283 nulls.try_for_each_valid_idx(|idx| {
284 unsafe {
285 *slice.get_unchecked_mut(idx) = op(a.value_unchecked(idx), b.value_unchecked(idx))?
286 };
287 Ok::<_, ArrowError>(())
288 })?;
289
290 let values = buffer.finish().into();
291 Ok(PrimitiveArray::new(values, Some(nulls)))
292 }
293}
294
295/// Applies the provided fallible binary operation across `a` and `b` by mutating the mutable
296/// [`PrimitiveArray`] `a` with the results.

Callers

nothing calls this directly

Calls 11

try_binary_no_nullsFunction · 0.85
append_n_zeroedMethod · 0.80
lenMethod · 0.45
is_emptyMethod · 0.45
null_countMethod · 0.45
as_refMethod · 0.45
logical_nullsMethod · 0.45
as_slice_mutMethod · 0.45
value_uncheckedMethod · 0.45
finishMethod · 0.45

Tested by

no test coverage detected