@@ -12,6 +12,7 @@ use crate::arrays::ScalarFn;
1212use crate :: arrays:: scalar_fn:: ExactScalarFn ;
1313use crate :: arrays:: scalar_fn:: ScalarFnArrayExt ;
1414use crate :: arrays:: scalar_fn:: ScalarFnArrayView ;
15+ use crate :: builtins:: ArrayBuiltins ;
1516use crate :: kernel:: ExecuteParentKernel ;
1617use crate :: optimizer:: rules:: ArrayParentReduceRule ;
1718use crate :: scalar_fn:: fns:: zip:: Zip as ZipExpr ;
@@ -22,8 +23,8 @@ use crate::scalar_fn::fns::zip::Zip as ZipExpr;
2223/// and structure without needing to read or execute on the underlying buffers.
2324/// Implementations should return `None` if the operation requires buffer access.
2425///
25- /// Dispatch is on child 0 (if_true) . The `if_false` and `mask` are extracted from
26- /// the parent `ScalarFnArray` .
26+ /// Dispatch supports either value branch . The adaptor normalizes the inputs so
27+ /// `array` is selected where `mask` is true, and `if_false` everywhere else .
2728pub trait ZipReduce : VTable {
2829 fn zip (
2930 array : ArrayView < ' _ , Self > ,
@@ -37,8 +38,8 @@ pub trait ZipReduce: VTable {
3738/// Unlike [`ZipReduce`], this trait is for zip implementations that may need
3839/// to read and execute on the underlying buffers to produce the result.
3940///
40- /// Dispatch is on child 0 (if_true) . The `if_false` and `mask` are extracted from
41- /// the parent `ScalarFnArray` .
41+ /// Dispatch supports either value branch . The adaptor normalizes the inputs so
42+ /// `array` is selected where `mask` is true, and `if_false` everywhere else .
4243pub trait ZipKernel : VTable {
4344 fn zip (
4445 array : ArrayView < ' _ , Self > ,
@@ -64,15 +65,10 @@ where
6465 parent : ScalarFnArrayView < ' _ , ZipExpr > ,
6566 child_idx : usize ,
6667 ) -> VortexResult < Option < ArrayRef > > {
67- if child_idx != 0 {
68+ let Some ( ( if_false , mask ) ) = normalized_zip_inputs ( parent , child_idx ) ? else {
6869 return Ok ( None ) ;
69- }
70- let scalar_fn_array = parent
71- . as_opt :: < ScalarFn > ( )
72- . vortex_expect ( "ExactScalarFn matcher confirmed ScalarFnArray" ) ;
73- let if_false = scalar_fn_array. get_child ( 1 ) ;
74- let mask_array = scalar_fn_array. get_child ( 2 ) ;
75- <V as ZipReduce >:: zip ( array, if_false, mask_array)
70+ } ;
71+ <V as ZipReduce >:: zip ( array, & if_false, & mask)
7672 }
7773}
7874
@@ -93,14 +89,97 @@ where
9389 child_idx : usize ,
9490 ctx : & mut ExecutionCtx ,
9591 ) -> VortexResult < Option < ArrayRef > > {
96- if child_idx != 0 {
92+ let Some ( ( if_false , mask ) ) = normalized_zip_inputs ( parent , child_idx ) ? else {
9793 return Ok ( None ) ;
94+ } ;
95+ <V as ZipKernel >:: zip ( array, & if_false, & mask, ctx)
96+ }
97+ }
98+
99+ fn normalized_zip_inputs (
100+ parent : ScalarFnArrayView < ' _ , ZipExpr > ,
101+ child_idx : usize ,
102+ ) -> VortexResult < Option < ( ArrayRef , ArrayRef ) > > {
103+ let scalar_fn_array = parent
104+ . as_opt :: < ScalarFn > ( )
105+ . vortex_expect ( "ExactScalarFn matcher confirmed ScalarFnArray" ) ;
106+ let mask = scalar_fn_array. get_child ( 2 ) ;
107+ match child_idx {
108+ 0 => Ok ( Some ( ( scalar_fn_array. get_child ( 1 ) . clone ( ) , mask. clone ( ) ) ) ) ,
109+ 1 => {
110+ // Null mask rows select the original if_false branch, so they must
111+ // become true when that branch is passed to the kernel as if_true.
112+ let mask = mask. fill_null ( false ) ?. not ( ) ?;
113+ Ok ( Some ( ( scalar_fn_array. get_child ( 0 ) . clone ( ) , mask) ) )
114+ }
115+ _ => Ok ( None ) ,
116+ }
117+ }
118+
119+ #[ cfg( test) ]
120+ mod tests {
121+ use rstest:: rstest;
122+ use vortex_session:: VortexSession ;
123+
124+ use super :: * ;
125+ use crate :: IntoArray ;
126+ use crate :: VortexSessionExecute ;
127+ use crate :: arrays:: Bool ;
128+ use crate :: arrays:: BoolArray ;
129+ use crate :: assert_arrays_eq;
130+
131+ // A metadata-only reduction exposes the inputs passed by the adaptor.
132+ impl ZipReduce for Bool {
133+ fn zip (
134+ array : ArrayView < ' _ , Self > ,
135+ if_false : & ArrayRef ,
136+ mask : & ArrayRef ,
137+ ) -> VortexResult < Option < ArrayRef > > {
138+ mask. zip ( array. array ( ) . clone ( ) , if_false. clone ( ) ) . map ( Some )
139+ }
140+ }
141+
142+ #[ rstest]
143+ #[ case:: if_true( 0 ) ]
144+ #[ case:: if_false( 1 ) ]
145+ #[ case:: mask( 2 ) ]
146+ fn adaptors_preserve_branch_and_null_mask_semantics (
147+ #[ case] child_idx : usize ,
148+ ) -> VortexResult < ( ) > {
149+ let mut ctx = VortexSession :: empty ( ) . create_execution_ctx ( ) ;
150+ let children = [
151+ BoolArray :: from_iter ( [ Some ( true ) , Some ( false ) , None , Some ( true ) ] ) . into_array ( ) ,
152+ BoolArray :: from_iter ( [ Some ( false ) , Some ( true ) , Some ( true ) , None ] ) . into_array ( ) ,
153+ BoolArray :: from_iter ( [ Some ( true ) , Some ( false ) , None , None ] ) . into_array ( ) ,
154+ ] ;
155+ let parent = ZipExpr :: try_new (
156+ children[ 0 ] . clone ( ) ,
157+ children[ 1 ] . clone ( ) ,
158+ children[ 2 ] . clone ( ) ,
159+ ) ?
160+ . into_array ( ) ;
161+ let child = children[ child_idx] . as_opt :: < Bool > ( ) . unwrap ( ) ;
162+ let reduced = ZipReduceAdaptor ( Bool ) . reduce_parent (
163+ child,
164+ parent. as_opt :: < ExactScalarFn < ZipExpr > > ( ) . unwrap ( ) ,
165+ child_idx,
166+ ) ?;
167+ let executed = ZipExecuteAdaptor ( Bool ) . execute_parent (
168+ child,
169+ parent. as_opt :: < ExactScalarFn < ZipExpr > > ( ) . unwrap ( ) ,
170+ child_idx,
171+ & mut ctx,
172+ ) ?;
173+
174+ for result in [ reduced, executed] {
175+ if child_idx == 2 {
176+ assert ! ( result. is_none( ) , "the mask is not a value branch" ) ;
177+ } else {
178+ let result = result. expect ( "both value branches dispatch to their kernel" ) ;
179+ let expected = BoolArray :: from_iter ( [ Some ( true ) , Some ( true ) , Some ( true ) , None ] ) ;
180+ assert_arrays_eq ! ( result, expected, & mut ctx) ;
181+ }
98182 }
99- let scalar_fn_array = parent
100- . as_opt :: < ScalarFn > ( )
101- . vortex_expect ( "ExactScalarFn matcher confirmed ScalarFnArray" ) ;
102- let if_false = scalar_fn_array. get_child ( 1 ) ;
103- let mask_array = scalar_fn_array. get_child ( 2 ) ;
104- <V as ZipKernel >:: zip ( array, if_false, mask_array, ctx)
183+ Ok ( ( ) )
105184 }
106185}
0 commit comments