alamb commented on code in PR #12420:
URL: https://github.com/apache/arrow-rs/pull/12420#discussion_r4670781794
##########
arrow-array/src/array/mod.rs:
##########
@@ -149,6 +149,28 @@ pub unsafe trait Array: std::fmt::Debug + Send + Sync {
/// Panics if `offset + length > self.len()`
fn slice(&self, offset: usize, length: usize) -> ArrayRef;
+ /// Returns an [`ArrayRef`] with the same contents as `self`, sharing its
buffers.
+ ///
+ /// The default implementation is `self.slice(0, self.len())`. The
arrow-rs array types
+ /// implement this as a shallow clone, which avoids the null-count
recomputation and child
+ /// re-slicing of [`Self::slice`].
+ ///
+ /// # Example:
+ ///
+ /// ```
+ /// use arrow_array::{Array, ArrayRef, Int32Array};
Review Comment:
recommend prefixing with `#` to get the use out of the example
##########
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?
--
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]