@@ -8,6 +8,7 @@ use std::fmt::Debug;
88use std:: fmt:: Formatter ;
99use std:: hash:: Hash ;
1010use std:: hash:: Hasher ;
11+ use std:: ops:: Deref ;
1112use std:: ops:: Range ;
1213use std:: sync:: Arc ;
1314
@@ -18,6 +19,7 @@ use vortex_dtype::Nullability;
1819use vortex_error:: VortexExpect ;
1920use vortex_error:: VortexResult ;
2021use vortex_error:: vortex_ensure;
22+ use vortex_error:: vortex_err;
2123use vortex_error:: vortex_panic;
2224use vortex_mask:: Mask ;
2325use vortex_scalar:: Scalar ;
@@ -73,6 +75,9 @@ pub trait Array:
7375 /// Returns the array as a reference to a generic [`Any`] trait object.
7476 fn as_any ( & self ) -> & dyn Any ;
7577
78+ /// Returns the array as an `Arc<dyn Any + Send + Sync>`.
79+ fn as_any_arc ( self : Arc < Self > ) -> Arc < dyn Any + Send + Sync > ;
80+
7681 /// Returns the array as an [`ArrayRef`].
7782 fn to_array ( & self ) -> ArrayRef ;
7883
@@ -211,6 +216,10 @@ impl Array for Arc<dyn Array> {
211216 self . as_ref ( ) . as_any ( )
212217 }
213218
219+ fn as_any_arc ( self : Arc < Self > ) -> Arc < dyn Any + Send + Sync > {
220+ self
221+ }
222+
214223 #[ inline]
215224 fn to_array ( & self ) -> ArrayRef {
216225 self . clone ( )
@@ -350,6 +359,24 @@ impl dyn Array + '_ {
350359 . map ( |array_adapter| & array_adapter. 0 )
351360 }
352361
362+ /// Returns the array downcast to the given `A` as an owned object.
363+ pub fn try_into < V : VTable > ( self : Arc < Self > ) -> Result < V :: Array , Arc < Self > > {
364+ match self . is :: < V > ( ) {
365+ true => {
366+ let arc = self
367+ . as_any_arc ( )
368+ . downcast :: < ArrayAdapter < V > > ( )
369+ . map_err ( |_| vortex_err ! ( "failed to downcast" ) )
370+ . vortex_expect ( "Failed to downcast" ) ;
371+ Ok ( match Arc :: try_unwrap ( arc) {
372+ Ok ( array) => array. 0 ,
373+ Err ( arc) => arc. deref ( ) . 0 . clone ( ) ,
374+ } )
375+ }
376+ false => Err ( self ) ,
377+ }
378+ }
379+
353380 /// Is self an array with encoding from vtable `V`.
354381 pub fn is < V : VTable > ( & self ) -> bool {
355382 self . as_opt :: < V > ( ) . is_some ( )
@@ -443,6 +470,10 @@ impl<V: VTable> Array for ArrayAdapter<V> {
443470 self
444471 }
445472
473+ fn as_any_arc ( self : Arc < Self > ) -> Arc < dyn Any + Send + Sync > {
474+ self
475+ }
476+
446477 fn to_array ( & self ) -> ArrayRef {
447478 Arc :: new ( ArrayAdapter :: < V > ( self . 0 . clone ( ) ) )
448479 }
@@ -691,13 +722,6 @@ impl<V: VTable> Array for ArrayAdapter<V> {
691722 }
692723
693724 fn reduce_parent ( & self , parent : & ArrayRef , child_idx : usize ) -> VortexResult < Option < ArrayRef > > {
694- #[ cfg( debug_assertions) ]
695- vortex_ensure ! (
696- Arc :: as_ptr( & parent. children( ) [ child_idx] ) == self ,
697- "Parent array's child at index {} does not match self" ,
698- child_idx
699- ) ;
700-
701725 let Some ( reduced) = V :: reduce_parent ( & self . 0 , parent, child_idx) ? else {
702726 return Ok ( None ) ;
703727 } ;
0 commit comments