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

Method unary_opt

arrow-array/src/array/primitive_array.rs:1044–1082  ·  view source on GitHub ↗

Applies a unary and nullable function to all valid values in a primitive array Applies `op` to only rows that are valid, which is often significantly slower than [`Self::unary`], which should be preferred if `op` is fallible. Note: LLVM is currently unable to effectively vectorize fallible operations

(&self, op: F)

Source from the content-addressed store, hash-verified

1042 ///
1043 /// Note: LLVM is currently unable to effectively vectorize fallible operations
1044 pub fn unary_opt<F, O>(&self, op: F) -> PrimitiveArray<O>
1045 where
1046 O: ArrowPrimitiveType,
1047 F: Fn(T::Native) -> Option<O::Native>,
1048 {
1049 let len = self.len();
1050 let (nulls, null_count, offset) = match self.nulls() {
1051 Some(n) => (Some(n.validity()), n.null_count(), n.offset()),
1052 None => (None, 0, 0),
1053 };
1054
1055 let mut null_builder = BooleanBufferBuilder::new(len);
1056 match nulls {
1057 Some(b) => null_builder.append_packed_range(offset..offset + len, b),
1058 None => null_builder.append_n(len, true),
1059 }
1060
1061 let mut buffer = BufferBuilder::<O::Native>::new(len);
1062 buffer.append_n_zeroed(len);
1063 let slice = buffer.as_slice_mut();
1064
1065 let mut out_null_count = null_count;
1066
1067 let _ = try_for_each_valid_idx(len, offset, null_count, nulls, |idx| {
1068 match op(unsafe { self.value_unchecked(idx) }) {
1069 Some(v) => unsafe { *slice.get_unchecked_mut(idx) = v },
1070 None => {
1071 out_null_count += 1;
1072 null_builder.set_bit(idx, false);
1073 }
1074 }
1075 Ok::<_, ()>(())
1076 });
1077
1078 let nulls = null_builder.finish();
1079 let values = buffer.finish().into();
1080 let nulls = unsafe { NullBuffer::new_unchecked(nulls, out_null_count) };
1081 PrimitiveArray::new(values, Some(nulls))
1082 }
1083
1084 /// Applies a unary infallible function to each value in an array, producing a
1085 /// new primitive array.

Callers 2

apply_decimal_castFunction · 0.80
date_partMethod · 0.80

Calls 13

try_for_each_valid_idxFunction · 0.85
validityMethod · 0.80
append_packed_rangeMethod · 0.80
append_n_zeroedMethod · 0.80
lenMethod · 0.45
nullsMethod · 0.45
null_countMethod · 0.45
offsetMethod · 0.45
append_nMethod · 0.45
as_slice_mutMethod · 0.45
value_uncheckedMethod · 0.45
set_bitMethod · 0.45

Tested by

no test coverage detected