scovich commented on issue #8794:
URL: https://github.com/apache/arrow-rs/issues/8794#issuecomment-3563886963

   One idea that's been percolating:
   
   We already have `Array::as_any()`, which cuts through all the layers of 
wrapping and returns a reference to the  underlying concrete array. What if we 
introduced a similar `Array::as_arc()` method? It wouldn't be perfect, but in 
case the user passed `&Arc<dyn Array>` or even `&Arc<Arc<dyn Array>>` -- which 
_does_ happen! -- we could clone the arc instead of creating a new one. And 
even if we do need to create a new one, at least it's centralized (and can use 
the unsafe/unchecked Array -> ArrayData -> Array path) instead of scattering 
that logic all through the code base.
   
   <details>
   
   The tricky part is figuring out how to handle the recursion nicely -- it 
would be most convenient for the method to return `Option<Arc<dyn Array>>`, so 
that nesting Just Works. But then the caller ends up with an Option they have 
to unwrap.
   
   Maybe we compromise by having both `as_arc` and `as_arc_opt`, where the 
former is the convenience wrapper (provided method) and the latter does the 
real work?
   
   ```rust
   pub trait Array: Debug + Send + Sync {
         ...
       fn as_arc_opt(&self) -> Option<ArrayRef> {
           None // most impl Array know nothing about presence/absence of Arc
       }
       fn as_arc(&self) -> ArrayRef {
           self.as_arc_opt().unwrap_or_else(|| make_array(array.to_data()))
       }
         ...
   }
   
   impl<T: Array> Array for &T {
         ...
       fn as_arc_opt(&self) -> Option<ArrayRef> {
           T::as_arc_opt(self)
       }
         ...
   }
   
   impl Array for Arc<dyn Array> {
         ...
       fn as_arc_opt(&self) -> Option<ArrayRef> {
           Some(self.clone())
       }
         ...
   }
   ```
   
   </details>
   
   relevant: 
[cast_with_options](https://docs.rs/arrow-cast/57.0.0/src/arrow_cast/cast/mod.rs.html#747-756)
 could benefit from such a capability.


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