@@ -196,6 +196,12 @@ pub trait Array:
196196
197197 /// Invoke the batch execution function for the array to produce a canonical vector.
198198 fn bind_kernel ( & self , ctx : & mut BindCtx ) -> VortexResult < KernelRef > ;
199+
200+ /// Reduce the array to a more simple representation, if possible.
201+ fn reduce ( & self ) -> VortexResult < Option < ArrayRef > > ;
202+
203+ /// Attempt to perform a reduction of the parent of this array.
204+ fn reduce_parent ( & self , parent : & ArrayRef , child_idx : usize ) -> VortexResult < Option < ArrayRef > > ;
199205}
200206
201207impl Array for Arc < dyn Array > {
@@ -309,6 +315,14 @@ impl Array for Arc<dyn Array> {
309315 fn bind_kernel ( & self , ctx : & mut BindCtx ) -> VortexResult < KernelRef > {
310316 self . as_ref ( ) . bind_kernel ( ctx)
311317 }
318+
319+ fn reduce ( & self ) -> VortexResult < Option < ArrayRef > > {
320+ self . as_ref ( ) . reduce ( )
321+ }
322+
323+ fn reduce_parent ( & self , parent : & ArrayRef , child_idx : usize ) -> VortexResult < Option < ArrayRef > > {
324+ self . as_ref ( ) . reduce_parent ( parent, child_idx)
325+ }
312326}
313327
314328/// A reference counted pointer to a dynamic [`Array`] trait object.
@@ -658,6 +672,42 @@ impl<V: VTable> Array for ArrayAdapter<V> {
658672 Ok ( kernel)
659673 }
660674 }
675+
676+ fn reduce ( & self ) -> VortexResult < Option < ArrayRef > > {
677+ let Some ( reduced) = V :: reduce ( & self . 0 ) ? else {
678+ return Ok ( None ) ;
679+ } ;
680+ vortex_ensure ! ( reduced. len( ) == self . len( ) , "Reduced array length mismatch" ) ;
681+ vortex_ensure ! (
682+ reduced. dtype( ) == self . dtype( ) ,
683+ "Reduced array dtype mismatch"
684+ ) ;
685+ Ok ( Some ( reduced) )
686+ }
687+
688+ fn reduce_parent ( & self , parent : & ArrayRef , child_idx : usize ) -> VortexResult < Option < ArrayRef > > {
689+ #[ cfg( debug_assertions) ]
690+ vortex_ensure ! (
691+ Arc :: as_ptr( & parent. children( ) [ child_idx] ) == self ,
692+ "Parent array's child at index {} does not match self" ,
693+ child_idx
694+ ) ;
695+
696+ let Some ( reduced) = V :: reduce_parent ( & self . 0 , parent, child_idx) ? else {
697+ return Ok ( None ) ;
698+ } ;
699+
700+ vortex_ensure ! (
701+ reduced. len( ) == parent. len( ) ,
702+ "Reduced array length mismatch"
703+ ) ;
704+ vortex_ensure ! (
705+ reduced. dtype( ) == parent. dtype( ) ,
706+ "Reduced array dtype mismatch"
707+ ) ;
708+
709+ Ok ( Some ( reduced) )
710+ }
661711}
662712
663713impl < V : VTable > ArrayHash for ArrayAdapter < V > {
0 commit comments