@@ -22,7 +22,32 @@ impl Executable for Mask {
2222 /// The array must have a non-nullable boolean dtype. Use [`MaskNullAsFalse`] to execute a
2323 /// nullable boolean array, coercing null elements to `false`.
2424 fn execute ( array : ArrayRef , ctx : & mut ExecutionCtx ) -> VortexResult < Self > {
25- execute_mask ( array, ctx, NullHandling :: Reject )
25+ if !matches ! ( array. dtype( ) , DType :: Bool ( _) ) {
26+ vortex_bail ! ( "Mask array must have boolean dtype, not {}" , array. dtype( ) ) ;
27+ }
28+
29+ if let Some ( constant) = array. as_opt :: < Constant > ( ) {
30+ let mask_value = constant. scalar ( ) . as_bool ( ) . value ( ) . unwrap_or ( false ) ;
31+ return Ok ( Mask :: new ( array. len ( ) , mask_value) ) ;
32+ }
33+
34+ let array_len = array. len ( ) ;
35+ Ok ( match array. execute ( ctx) ? {
36+ Columnar :: Constant ( s) => {
37+ Mask :: new ( array_len, s. scalar ( ) . as_bool ( ) . value ( ) . unwrap_or ( false ) )
38+ }
39+ Columnar :: Canonical ( a) => {
40+ let bool = a. into_array ( ) . execute :: < BoolArray > ( ctx) ?;
41+ if bool. as_ref ( ) . dtype ( ) . is_nullable ( ) {
42+ vortex_bail ! (
43+ "Mask requires a non-nullable boolean array, not {}; \
44+ use MaskNullAsFalse to coerce nulls to false",
45+ bool . as_ref( ) . dtype( )
46+ ) ;
47+ }
48+ Mask :: from ( bool. into_bit_buffer ( ) )
49+ }
50+ } )
2651 }
2752}
2853
@@ -49,58 +74,28 @@ impl From<MaskNullAsFalse> for Mask {
4974
5075impl Executable for MaskNullAsFalse {
5176 fn execute ( array : ArrayRef , ctx : & mut ExecutionCtx ) -> VortexResult < Self > {
52- execute_mask ( array, ctx, NullHandling :: AsFalse ) . map ( Self )
53- }
54- }
55-
56- /// How [`execute_mask`] treats null elements of a nullable boolean array.
57- enum NullHandling {
58- /// Error if the boolean array is nullable.
59- Reject ,
60- /// Treat null elements as `false`.
61- AsFalse ,
62- }
63-
64- fn execute_mask (
65- array : ArrayRef ,
66- ctx : & mut ExecutionCtx ,
67- null_handling : NullHandling ,
68- ) -> VortexResult < Mask > {
69- if !matches ! ( array. dtype( ) , DType :: Bool ( _) ) {
70- vortex_bail ! ( "Mask array must have boolean dtype, not {}" , array. dtype( ) ) ;
71- }
72-
73- if let Some ( constant) = array. as_opt :: < Constant > ( ) {
74- let mask_value = constant. scalar ( ) . as_bool ( ) . value ( ) . unwrap_or ( false ) ;
75- return Ok ( Mask :: new ( array. len ( ) , mask_value) ) ;
76- }
77+ if !matches ! ( array. dtype( ) , DType :: Bool ( _) ) {
78+ vortex_bail ! ( "Mask array must have boolean dtype, not {}" , array. dtype( ) ) ;
79+ }
7780
78- let array_len = array. len ( ) ;
79- Ok ( match array. execute ( ctx) ? {
80- Columnar :: Constant ( s) => {
81- Mask :: new ( array_len, s. scalar ( ) . as_bool ( ) . value ( ) . unwrap_or ( false ) )
81+ if let Some ( constant) = array. as_opt :: < Constant > ( ) {
82+ let mask_value = constant. scalar ( ) . as_bool ( ) . value ( ) . unwrap_or ( false ) ;
83+ return Ok ( Self ( Mask :: new ( array. len ( ) , mask_value) ) ) ;
8284 }
83- Columnar :: Canonical ( a) => {
84- let bool = a. into_array ( ) . execute :: < BoolArray > ( ctx) ?;
85- match null_handling {
86- NullHandling :: Reject => {
87- if bool. as_ref ( ) . dtype ( ) . is_nullable ( ) {
88- vortex_bail ! (
89- "Mask requires a non-nullable boolean array, not {}; \
90- use MaskNullAsFalse to coerce nulls to false",
91- bool . as_ref( ) . dtype( )
92- ) ;
93- }
94- Mask :: from ( bool. into_bit_buffer ( ) )
95- }
96- NullHandling :: AsFalse => {
97- let validity = bool
98- . as_ref ( )
99- . validity ( ) ?
100- . execute_mask ( bool. as_ref ( ) . len ( ) , ctx) ?;
101- validity. bitand ( & Mask :: from ( bool. into_bit_buffer ( ) ) )
102- }
85+
86+ let array_len = array. len ( ) ;
87+ Ok ( Self ( match array. execute ( ctx) ? {
88+ Columnar :: Constant ( s) => {
89+ Mask :: new ( array_len, s. scalar ( ) . as_bool ( ) . value ( ) . unwrap_or ( false ) )
10390 }
104- }
105- } )
91+ Columnar :: Canonical ( a) => {
92+ let bool = a. into_array ( ) . execute :: < BoolArray > ( ctx) ?;
93+ let validity = bool
94+ . as_ref ( )
95+ . validity ( ) ?
96+ . execute_mask ( bool. as_ref ( ) . len ( ) , ctx) ?;
97+ validity. bitand ( & Mask :: from ( bool. into_bit_buffer ( ) ) )
98+ }
99+ } ) )
100+ }
106101}
0 commit comments