mbutrovich commented on code in PR #12420:
URL: https://github.com/apache/arrow-rs/pull/12420#discussion_r4672235257
##########
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.
+ ///
Review Comment:
Should the doc say that an `ArrayRef` returns a new reference to the same
allocation? That's the main reason to call this instead of `slice`
(https://github.com/apache/arrow-rs/pull/12420#issuecomment-7111430497), and
the test already checks it with `Arc::ptr_eq`. Once it ships, callers will rely
on it, so it belongs in the contract.
The doc should also say which call gets it. Passing `array_ref.as_ref()` to
a `&dyn Array` parameter dispatches to the concrete array's `to_arc`, which
allocates a new `Arc`. Passing `&array_ref` returns the same allocation. This
is the distinction that was easy to miss in
https://github.com/apache/arrow-rs/pull/12318#issuecomment-7052199941.
```suggestion
/// Returns an [`ArrayRef`] with the same contents as `self`, sharing
its buffers.
///
/// If `self` is an [`ArrayRef`], this returns a new reference to the
same allocation. To get
/// this through a `&dyn Array` parameter, pass `&array_ref` rather than
`array_ref.as_ref()`.
/// The latter dispatches to the inner array, which allocates a new
[`Arc`].
///
```
##########
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());
Review Comment:
The `offset()` assertion in `check` compares 0 with 0 for all three inputs.
[`PrimitiveArray::offset`](https://github.com/apache/arrow-rs/blob/08a6e4fbda9260fbf52c42cce418bce1fe445db8/arrow-array/src/array/primitive_array.rs#L1267-L1269)
always returns 0.
[`DictionaryArray::offset`](https://github.com/apache/arrow-rs/blob/08a6e4fbda9260fbf52c42cce418bce1fe445db8/arrow-array/src/array/dictionary_array.rs#L731-L733)
returns the offset of its keys, which are a `PrimitiveArray`.
[`RunArray::offset`](https://github.com/apache/arrow-rs/blob/08a6e4fbda9260fbf52c42cce418bce1fe445db8/arrow-array/src/array/run_array.rs#L578-L580)
is nonzero only after a slice, and `run` isn't sliced. Could we slice the run
array so the assertion compares a nonzero offset (1 here)?
```suggestion
check(&run.slice(1, 3).downcast::<Int32Array>().unwrap());
```
--
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]