1818//! Defines physical expressions that can evaluated at runtime during query execution
1919
2020use std:: any:: Any ;
21- use std:: convert:: TryFrom ;
2221use std:: sync:: Arc ;
2322
2423use crate :: { AggregateExpr , PhysicalExpr } ;
@@ -161,7 +160,7 @@ impl AggregateExpr for BoolAnd {
161160 }
162161
163162 fn create_accumulator ( & self ) -> Result < Box < dyn Accumulator > > {
164- Ok ( Box :: new ( BoolAndAccumulator :: try_new ( & self . data_type ) ? ) )
163+ Ok ( Box :: < BoolAndAccumulator > :: default ( ) )
165164 }
166165
167166 fn state_fields ( & self ) -> Result < Vec < Field > > {
@@ -199,7 +198,7 @@ impl AggregateExpr for BoolAnd {
199198 }
200199
201200 fn create_sliding_accumulator ( & self ) -> Result < Box < dyn Accumulator > > {
202- Ok ( Box :: new ( BoolAndAccumulator :: try_new ( & self . data_type ) ? ) )
201+ Ok ( Box :: < BoolAndAccumulator > :: default ( ) )
203202 }
204203}
205204
@@ -217,25 +216,20 @@ impl PartialEq<dyn Any> for BoolAnd {
217216 }
218217}
219218
220- #[ derive( Debug ) ]
219+ #[ derive( Debug , Default ) ]
221220struct BoolAndAccumulator {
222- bool_and : ScalarValue ,
223- }
224-
225- impl BoolAndAccumulator {
226- /// new bool_and accumulator
227- pub fn try_new ( data_type : & DataType ) -> Result < Self > {
228- Ok ( Self {
229- bool_and : ScalarValue :: try_from ( data_type) ?,
230- } )
231- }
221+ acc : Option < bool > ,
232222}
233223
234224impl Accumulator for BoolAndAccumulator {
235225 fn update_batch ( & mut self , values : & [ ArrayRef ] ) -> Result < ( ) > {
236226 let values = & values[ 0 ] ;
237- let delta = & bool_and_batch ( values) ?;
238- self . bool_and = self . bool_and . and ( delta) ?;
227+ self . acc = match ( self . acc , bool_and_batch ( values) ?) {
228+ ( None , ScalarValue :: Boolean ( v) ) => v,
229+ ( Some ( v) , ScalarValue :: Boolean ( None ) ) => Some ( v) ,
230+ ( Some ( a) , ScalarValue :: Boolean ( Some ( b) ) ) => Some ( a && b) ,
231+ _ => unreachable ! ( ) ,
232+ } ;
239233 Ok ( ( ) )
240234 }
241235
@@ -244,16 +238,15 @@ impl Accumulator for BoolAndAccumulator {
244238 }
245239
246240 fn state ( & self ) -> Result < Vec < ScalarValue > > {
247- Ok ( vec ! [ self . bool_and . clone ( ) ] )
241+ Ok ( vec ! [ ScalarValue :: Boolean ( self . acc ) ] )
248242 }
249243
250244 fn evaluate ( & self ) -> Result < ScalarValue > {
251- Ok ( self . bool_and . clone ( ) )
245+ Ok ( ScalarValue :: Boolean ( self . acc ) )
252246 }
253247
254248 fn size ( & self ) -> usize {
255- std:: mem:: size_of_val ( self ) - std:: mem:: size_of_val ( & self . bool_and )
256- + self . bool_and . size ( )
249+ std:: mem:: size_of_val ( self )
257250 }
258251}
259252
@@ -355,7 +348,7 @@ impl AggregateExpr for BoolOr {
355348 }
356349
357350 fn create_accumulator ( & self ) -> Result < Box < dyn Accumulator > > {
358- Ok ( Box :: new ( BoolOrAccumulator :: try_new ( & self . data_type ) ? ) )
351+ Ok ( Box :: < BoolOrAccumulator > :: default ( ) )
359352 }
360353
361354 fn state_fields ( & self ) -> Result < Vec < Field > > {
@@ -393,7 +386,7 @@ impl AggregateExpr for BoolOr {
393386 }
394387
395388 fn create_sliding_accumulator ( & self ) -> Result < Box < dyn Accumulator > > {
396- Ok ( Box :: new ( BoolOrAccumulator :: try_new ( & self . data_type ) ? ) )
389+ Ok ( Box :: < BoolOrAccumulator > :: default ( ) )
397390 }
398391}
399392
@@ -411,29 +404,24 @@ impl PartialEq<dyn Any> for BoolOr {
411404 }
412405}
413406
414- #[ derive( Debug ) ]
407+ #[ derive( Debug , Default ) ]
415408struct BoolOrAccumulator {
416- bool_or : ScalarValue ,
417- }
418-
419- impl BoolOrAccumulator {
420- /// new bool_or accumulator
421- pub fn try_new ( data_type : & DataType ) -> Result < Self > {
422- Ok ( Self {
423- bool_or : ScalarValue :: try_from ( data_type) ?,
424- } )
425- }
409+ acc : Option < bool > ,
426410}
427411
428412impl Accumulator for BoolOrAccumulator {
429413 fn state ( & self ) -> Result < Vec < ScalarValue > > {
430- Ok ( vec ! [ self . bool_or . clone ( ) ] )
414+ Ok ( vec ! [ ScalarValue :: Boolean ( self . acc ) ] )
431415 }
432416
433417 fn update_batch ( & mut self , values : & [ ArrayRef ] ) -> Result < ( ) > {
434418 let values = & values[ 0 ] ;
435- let delta = bool_or_batch ( values) ?;
436- self . bool_or = self . bool_or . or ( & delta) ?;
419+ self . acc = match ( self . acc , bool_or_batch ( values) ?) {
420+ ( None , ScalarValue :: Boolean ( v) ) => v,
421+ ( Some ( v) , ScalarValue :: Boolean ( None ) ) => Some ( v) ,
422+ ( Some ( a) , ScalarValue :: Boolean ( Some ( b) ) ) => Some ( a || b) ,
423+ _ => unreachable ! ( ) ,
424+ } ;
437425 Ok ( ( ) )
438426 }
439427
@@ -442,12 +430,11 @@ impl Accumulator for BoolOrAccumulator {
442430 }
443431
444432 fn evaluate ( & self ) -> Result < ScalarValue > {
445- Ok ( self . bool_or . clone ( ) )
433+ Ok ( ScalarValue :: Boolean ( self . acc ) )
446434 }
447435
448436 fn size ( & self ) -> usize {
449- std:: mem:: size_of_val ( self ) - std:: mem:: size_of_val ( & self . bool_or )
450- + self . bool_or . size ( )
437+ std:: mem:: size_of_val ( self )
451438 }
452439}
453440
0 commit comments