11use itertools:: Itertools ;
2- use vortex_error:: VortexResult ;
2+ use vortex_error:: { vortex_err , VortexResult } ;
33use vortex_scalar:: Scalar ;
44
55use crate :: array:: struct_:: StructArray ;
66use crate :: compute:: unary:: { scalar_at, scalar_at_unchecked, ScalarAtFn } ;
7- use crate :: compute:: { slice, take, ArrayCompute , SliceFn , TakeFn } ;
7+ use crate :: compute:: { filter, slice, take, ArrayCompute , FilterFn , SliceFn , TakeFn } ;
8+ use crate :: stats:: ArrayStatistics ;
89use crate :: variants:: StructArrayTrait ;
910use crate :: { Array , ArrayDType , IntoArray } ;
1011
@@ -20,6 +21,10 @@ impl ArrayCompute for StructArray {
2021 fn take ( & self ) -> Option < & dyn TakeFn > {
2122 Some ( self )
2223 }
24+
25+ fn filter ( & self ) -> Option < & dyn FilterFn > {
26+ Some ( self )
27+ }
2328}
2429
2530impl ScalarAtFn for StructArray {
@@ -71,3 +76,25 @@ impl SliceFn for StructArray {
7176 . map ( |a| a. into_array ( ) )
7277 }
7378}
79+
80+ impl FilterFn for StructArray {
81+ fn filter ( & self , predicate : & Array ) -> VortexResult < Array > {
82+ let fields = self
83+ . children ( )
84+ . map ( |field| filter ( & field, predicate) )
85+ . try_collect ( ) ?;
86+
87+ let predicate_true_count = predicate
88+ . statistics ( )
89+ . compute_true_count ( )
90+ . ok_or_else ( || vortex_err ! ( "Predicate should always be a boolean array" ) ) ?;
91+
92+ Self :: try_new (
93+ self . names ( ) . clone ( ) ,
94+ fields,
95+ predicate_true_count,
96+ self . validity ( ) . filter ( predicate) ?,
97+ )
98+ . map ( |a| a. into_array ( ) )
99+ }
100+ }
0 commit comments