33
44use std:: sync:: Arc ;
55
6- use vortex_dtype :: DType ;
7- use vortex_error :: { VortexResult , vortex_bail } ;
8- use vortex_vector:: Vector ;
6+ use vortex_error :: { VortexResult , vortex_panic } ;
7+ use vortex_mask :: Mask ;
8+ use vortex_vector:: { Vector , VectorOps , vector_matches_dtype } ;
99
10- use crate :: execution:: { BatchKernelRef , BindCtx } ;
10+ use crate :: execution:: { BatchKernelRef , BindCtx , DummyExecutionCtx , ExecutionCtx } ;
1111use crate :: vtable:: { OperatorVTable , VTable } ;
1212use crate :: { Array , ArrayAdapter , ArrayRef } ;
1313
@@ -16,13 +16,13 @@ use crate::{Array, ArrayAdapter, ArrayRef};
1616/// Note: the public functions such as "execute" should move onto the main `Array` trait when
1717/// operators is stabilized. The other functions should remain on a `pub(crate)` trait.
1818pub trait ArrayOperator : ' static + Send + Sync {
19- /// Execute the array producing a canonical vector .
20- fn execute ( & self ) -> VortexResult < Vector > {
21- self . execute_with_selection ( None )
22- }
23-
24- /// Execute the array with a selection mask, producing a canonical vector .
25- fn execute_with_selection ( & self , selection : Option < & ArrayRef > ) -> VortexResult < Vector > ;
19+ /// Execute the array's batch kernel with the given selection mask .
20+ ///
21+ /// # Panics
22+ ///
23+ /// If the mask length does not match the array length.
24+ /// If the array's implementation returns an invalid vector (wrong length, wrong type, etc) .
25+ fn execute_batch ( & self , selection : & Mask , ctx : & mut dyn ExecutionCtx ) -> VortexResult < Vector > ;
2626
2727 /// Optimize the array by running the optimization rules.
2828 fn reduce_children ( & self ) -> VortexResult < Option < ArrayRef > > ;
@@ -39,8 +39,8 @@ pub trait ArrayOperator: 'static + Send + Sync {
3939}
4040
4141impl ArrayOperator for Arc < dyn Array > {
42- fn execute_with_selection ( & self , selection : Option < & ArrayRef > ) -> VortexResult < Vector > {
43- self . as_ref ( ) . execute_with_selection ( selection)
42+ fn execute_batch ( & self , selection : & Mask , ctx : & mut dyn ExecutionCtx ) -> VortexResult < Vector > {
43+ self . as_ref ( ) . execute_batch ( selection, ctx )
4444 }
4545
4646 fn reduce_children ( & self ) -> VortexResult < Option < ArrayRef > > {
@@ -61,23 +61,31 @@ impl ArrayOperator for Arc<dyn Array> {
6161}
6262
6363impl < V : VTable > ArrayOperator for ArrayAdapter < V > {
64- fn execute_with_selection ( & self , selection : Option < & ArrayRef > ) -> VortexResult < Vector > {
65- if let Some ( selection) = selection. as_ref ( ) {
66- if !matches ! ( selection. dtype( ) , DType :: Bool ( _) ) {
67- vortex_bail ! (
68- "Selection array must be of boolean type, got {}" ,
69- selection. dtype( )
70- ) ;
71- }
72- if selection. len ( ) != self . len ( ) {
73- vortex_bail ! (
74- "Selection array length {} does not match array length {}" ,
75- selection. len( ) ,
76- self . len( )
64+ fn execute_batch ( & self , selection : & Mask , ctx : & mut dyn ExecutionCtx ) -> VortexResult < Vector > {
65+ let vector =
66+ <V :: OperatorVTable as OperatorVTable < V > >:: execute_batch ( & self . 0 , selection, ctx) ?;
67+
68+ // Such a cheap check that we run it always. More expensive DType checks live in
69+ // debug_assertions.
70+ assert_eq ! (
71+ vector. len( ) ,
72+ selection. true_count( ) ,
73+ "Batch execution returned vector of incorrect length"
74+ ) ;
75+
76+ #[ cfg( debug_assertions) ]
77+ {
78+ // Checks for correct type and nullability.
79+ if !vector_matches_dtype ( & vector, self . dtype ( ) ) {
80+ vortex_panic ! (
81+ "Returned vector {:?} does not match expected dtype {}" ,
82+ vector,
83+ self . dtype( )
7784 ) ;
7885 }
7986 }
80- self . bind ( selection, & mut ( ) ) ?. execute ( )
87+
88+ Ok ( vector)
8189 }
8290
8391 fn reduce_children ( & self ) -> VortexResult < Option < ArrayRef > > {
@@ -107,3 +115,14 @@ impl BindCtx for () {
107115 array. bind ( selection, self )
108116 }
109117}
118+
119+ impl dyn Array + ' _ {
120+ pub fn execute ( & self ) -> VortexResult < Vector > {
121+ self . execute_batch ( & Mask :: new_true ( self . len ( ) ) , & mut DummyExecutionCtx )
122+ }
123+
124+ pub fn execute_with_selection ( & self , mask : & Mask ) -> VortexResult < Vector > {
125+ assert_eq ! ( self . len( ) , mask. len( ) ) ;
126+ self . execute_batch ( mask, & mut DummyExecutionCtx )
127+ }
128+ }
0 commit comments