mbutrovich commented on code in PR #12420:
URL: https://github.com/apache/arrow-rs/pull/12420#discussion_r4672201767
##########
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:
> Can we also add a test that takes `&dyn Array` and passes in an `&Arc<&dyn
Array>` -- and confirm it returns the same Arc?
`Arc<&dyn Array>` doesn't implement `Array`, so I read this as `&ArrayRef`
passed as `&dyn Array`. The existing `array_ref.to_arc()` call resolves to
`<ArrayRef as Array>::to_arc` at compile time, so it doesn't go through a `&dyn
Array`. Something like this would cover it:
```rust
fn to_arc_dyn(array: &dyn Array) -> ArrayRef {
array.to_arc()
}
assert!(Arc::ptr_eq(&to_arc_dyn(&array_ref), &array_ref));
```
--
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]