mbutrovich commented on code in PR #12455:
URL: https://github.com/apache/arrow-rs/pull/12455#discussion_r4802277686


##########
arrow-array/src/array/mod.rs:
##########
@@ -467,6 +485,10 @@ unsafe impl Array for ArrayRef {
         self.as_ref().as_any()
     }
 
+    fn into_any_arc(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
+        Arc::unwrap_or_clone(self).into_any_arc()

Review Comment:
   No test reaches this impl. If I replace the body with `self`, the way the 
concrete impls read, `cast::tests` and the `downcast_arc` doctest still pass. 
With that change, `downcast_arc` returns `None` for an `ArrayRef` that wraps an 
`ArrayRef`. Could we add that case to `test_downcast_arc_of_references`?
   
   ```rust
   // An `ArrayRef` that wraps an `ArrayRef` shares the inner `Arc`
   let nested: ArrayRef = Arc::new(Arc::clone(&array));
   let shared: ArrayRef = downcast_arc::<StringViewArray>(&nested).unwrap();
   assert!(Arc::ptr_eq(&shared, &array));
   ```



##########
arrow-array/src/array/mod.rs:
##########
@@ -67,6 +67,13 @@ use crate::iterator::ArrayIter;
 
 /// An array in the [Arrow Columnar 
Format](https://arrow.apache.org/docs/format/Columnar.html)
 ///
+/// Conceptually, an array is a concrete arrow-rs type that maps one-to-one to 
a [`DataType`], such
+/// as [`Int32Array`] for [`DataType::Int32`]. For convenience, other types 
also implement this

Review Comment:
   Several data types map to the same concrete type, so "one-to-one" isn't 
accurate. A `Decimal128Array` covers every precision and scale 
([`with_precision_and_scale`](https://github.com/apache/arrow-rs/blob/e9017fe078eb27225e84b0ae1d0d781a0f5052e9/arrow-array/src/array/primitive_array.rs#L1706)),
 and a `TimestampMicrosecondArray` covers every timezone 
([`with_timezone`](https://github.com/apache/arrow-rs/blob/e9017fe078eb27225e84b0ae1d0d781a0f5052e9/arrow-array/src/array/primitive_array.rs#L1661)).
   
   ```suggestion
   /// Conceptually, an array is a concrete arrow-rs type that a [`DataType`] 
maps to, such as
   /// [`Int32Array`] for [`DataType::Int32`]. For convenience, other types 
also implement this
   ```



##########
arrow-array/src/cast.rs:
##########
@@ -846,6 +847,35 @@ where
     T::from(array.to_data())
 }
 
+/// Downcasts a `dyn Array` to an [`Arc`] of its concrete type, or returns 
`None` if its concrete
+/// type is not `T`.
+///
+/// If `array` is or wraps an `Arc` of the concrete array, bump its reference 
count as if by
+/// [`Arc::clone`]. Otherwise, shallow clone the array into a new `Arc` as if 
by [`Array::to_arc`].
+///
+/// # Example
+///
+/// ```
+/// # use std::sync::Arc;
+/// # use arrow_array::{Array, ArrayRef, Int32Array, StringArray};
+/// # use arrow_array::cast::downcast_arc;
+/// // An `Arc` of the concrete array is shared, not cloned
+/// let array: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3]));
+/// let shared: ArrayRef = downcast_arc::<Int32Array>(&array).unwrap();
+/// assert!(Arc::ptr_eq(&shared, &array));
+///
+/// // A plain reference has no `Arc` to share, so the array is shallow cloned 
into a new one
+/// let array = Int32Array::from(vec![1, 2, 3]);
+/// let cloned = downcast_arc::<Int32Array>(&array).unwrap();
+/// assert_eq!(*cloned, array);
+///
+/// // The wrong type returns `None`
+/// assert!(downcast_arc::<StringArray>(&array).is_none());
+/// ```
+pub fn downcast_arc<T: Array + 'static>(array: &dyn Array) -> Option<Arc<T>> {
+    array.to_arc().into_any_arc().downcast::<T>().ok()
+}

Review Comment:
   `downcast_arc` builds the `Arc` before it checks the type, so a mismatch 
still pays for `to_arc`. For a borrowed concrete array that is a new allocation 
plus a shallow clone. A `StructArray` also clones its `Vec<ArrayRef>` of 
children 
([`to_arc`](https://github.com/apache/arrow-rs/blob/e9017fe078eb27225e84b0ae1d0d781a0f5052e9/arrow-array/src/array/struct_array.rs#L483-L485),
 
[`fields`](https://github.com/apache/arrow-rs/blob/e9017fe078eb27225e84b0ae1d0d781a0f5052e9/arrow-array/src/array/struct_array.rs#L63-L69)).
 For an `ArrayRef` it is an atomic increment and decrement. A caller that tries 
several types in turn takes the mismatch path for all but one of them. Could we 
check with `as_any` first? The match path gains one `as_any` call.
   
   ```suggestion
   pub fn downcast_arc<T: Array + 'static>(array: &dyn Array) -> Option<Arc<T>> 
{
       if !array.as_any().is::<T>() {
           return None;
       }
       array.to_arc().into_any_arc().downcast::<T>().ok()
   }
   ```



-- 
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]

Reply via email to