@@ -24,9 +24,10 @@ use crate::IntoArray;
2424use crate :: Precision ;
2525use crate :: arrays:: LEGACY_SESSION ;
2626use crate :: arrays:: filter:: array:: FilterArray ;
27+ use crate :: arrays:: filter:: kernel:: FilterKernel ;
2728use crate :: kernel:: BindCtx ;
2829use crate :: kernel:: KernelRef ;
29- use crate :: kernel:: kernel ;
30+ use crate :: kernel:: PushDownResult ;
3031use crate :: serde:: ArrayChildren ;
3132use crate :: stats:: StatsSetRef ;
3233use crate :: vectors:: VectorIntoArray ;
@@ -82,22 +83,46 @@ impl VTable for FilterVTable {
8283 & self ,
8384 dtype : & DType ,
8485 len : usize ,
85- metadata : & Self :: Metadata ,
86+ selection_mask : & Mask ,
8687 _buffers : & [ BufferHandle ] ,
8788 children : & dyn ArrayChildren ,
8889 ) -> VortexResult < Self :: Array > {
89- let child = children. get ( 0 , dtype, len) ?;
90+ assert_eq ! ( len, selection_mask. true_count( ) ) ;
91+ let child = children. get ( 0 , dtype, selection_mask. len ( ) ) ?;
9092 Ok ( FilterArray {
9193 child,
92- mask : metadata . clone ( ) ,
94+ mask : selection_mask . clone ( ) ,
9395 stats : Default :: default ( ) ,
9496 } )
9597 }
9698
9799 fn bind_kernel ( array : & Self :: Array , ctx : & mut BindCtx ) -> VortexResult < KernelRef > {
98- let child = array. child . bind_kernel ( ctx) ?;
100+ let mut child = array. child . bind_kernel ( ctx) ?;
99101 let mask = array. mask . clone ( ) ;
100- Ok ( kernel ( move || Ok ( Filter :: filter ( & child. execute ( ) ?, & mask) ) ) )
102+
103+ // NOTE(ngates): for now we keep the same behavior as develop where we push-down any
104+ // query with <20% true values.
105+ let pushdown = array. mask . density ( ) < 0.2 ;
106+
107+ if pushdown {
108+ // Try to push down the filter to the child if it's cheaper.
109+ child = match child. push_down_filter ( & mask) ? {
110+ PushDownResult :: Pushed ( new_k) => {
111+ tracing:: debug!( "Filter push down kernel:\n {:?}" , new_k) ;
112+ return Ok ( new_k) ;
113+ }
114+ PushDownResult :: NotPushed ( child) => {
115+ tracing:: warn!(
116+ "Filter pushdown was cheaper but not supported by child array {}" ,
117+ array. child. display_tree( )
118+ ) ;
119+ child
120+ }
121+ } ;
122+ }
123+
124+ // Otherwise, wrap up the child in a filter kernel.
125+ Ok ( Box :: new ( FilterKernel :: new ( child, mask) ) )
101126 }
102127}
103128
0 commit comments