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

Method try_unary

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

Applies a unary fallible function to all valid values in a primitive array, producing a new array of potentially different type. 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

967 ///
968 /// Note: LLVM is currently unable to effectively vectorize fallible operations
969 pub fn try_unary<F, O, E>(&self, op: F) -> Result<PrimitiveArray<O>, E>
970 where
971 O: ArrowPrimitiveType,
972 F: Fn(T::Native) -> Result<O::Native, E>,
973 {
974 let len = self.len();
975
976 let nulls = self.nulls().cloned();
977 let mut buffer = BufferBuilder::<O::Native>::new(len);
978 buffer.append_n_zeroed(len);
979 let slice = buffer.as_slice_mut();
980
981 let f = |idx| {
982 unsafe { *slice.get_unchecked_mut(idx) = op(self.value_unchecked(idx))? };
983 Ok::<_, E>(())
984 };
985
986 match &nulls {
987 Some(nulls) => nulls.try_for_each_valid_idx(f)?,
988 None => (0..len).try_for_each(f)?,
989 }
990
991 let values = buffer.finish().into();
992 Ok(PrimitiveArray::new(values, nulls))
993 }
994
995 /// Applies a unary fallible function to all valid values in a mutable
996 /// primitive array.

Callers 7

apply_decimal_castFunction · 0.80
timestamp_to_date32Function · 0.80
try_numeric_castFunction · 0.80
try_unaryFunction · 0.80
try_new_from_builderMethod · 0.80
try_new_from_builderMethod · 0.80
try_new_from_builderMethod · 0.80

Calls 7

append_n_zeroedMethod · 0.80
lenMethod · 0.45
nullsMethod · 0.45
as_slice_mutMethod · 0.45
value_uncheckedMethod · 0.45
finishMethod · 0.45

Tested by

no test coverage detected