scovich commented on code in PR #12420:
URL: https://github.com/apache/arrow-rs/pull/12420#discussion_r4673516355
##########
arrow-array/src/array/mod.rs:
##########
@@ -1431,6 +1461,34 @@ mod tests {
assert!(compute_my_thing(arr.as_ref()));
}
+ #[test]
+ fn test_to_arc() {
+ // Array equality ignores offsets, so compare them separately.
+ fn check(array: &dyn Array) {
+ let expected = array.slice(0, array.len());
+ let actual = array.to_arc();
+ assert_eq!(actual.as_ref(), expected.as_ref());
+ assert_eq!(actual.offset(), array.offset());
+ }
+
+ let primitive = Int32Array::from(vec![Some(1), None, Some(3),
Some(4)]);
+ let dict: DictionaryArray<Int32Type> =
+ vec![Some("a"), None, Some("b")].into_iter().collect();
+ let run_ends = Int32Array::from(vec![2, 3, 5]);
+ let run = RunArray::try_new(&run_ends, &primitive.slice(0,
3)).unwrap();
+
+ // Non-zero values and null buffer offsets: the clone must keep both
slices intact.
+ check(&primitive.slice(1, 2));
+ // The typed wrappers' `slice` returns the inner untyped array, so
`to_arc` must as well.
+ check(&dict.downcast_dict::<StringArray>().unwrap());
+ check(&run.downcast::<Int32Array>().unwrap());
+
+ // `ArrayRef` must reuse its `Arc` (not allocate), and `&T` must
forward to `T`.
+ let array_ref: ArrayRef = Arc::new(primitive.clone());
+ assert!(Arc::ptr_eq(&array_ref.to_arc(), &array_ref));
+ assert_eq!(Array::to_arc(&&primitive).as_ref(), &primitive);
+ }
+
Review Comment:
I added this to the doctest (as well as updating the doc comment to make
clear that `to_arc` uses `Arc::clone` when possible).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]