18
18
//! Defines physical expressions that can evaluated at runtime during query execution
19
19
20
20
use std:: any:: Any ;
21
- use std:: convert:: TryFrom ;
22
21
use std:: sync:: Arc ;
23
22
24
23
use crate :: { AggregateExpr , PhysicalExpr } ;
@@ -161,7 +160,7 @@ impl AggregateExpr for BoolAnd {
161
160
}
162
161
163
162
fn create_accumulator ( & self ) -> Result < Box < dyn Accumulator > > {
164
- Ok ( Box :: new ( BoolAndAccumulator :: try_new ( & self . data_type ) ? ) )
163
+ Ok ( Box :: < BoolAndAccumulator > :: default ( ) )
165
164
}
166
165
167
166
fn state_fields ( & self ) -> Result < Vec < Field > > {
@@ -199,7 +198,7 @@ impl AggregateExpr for BoolAnd {
199
198
}
200
199
201
200
fn create_sliding_accumulator ( & self ) -> Result < Box < dyn Accumulator > > {
202
- Ok ( Box :: new ( BoolAndAccumulator :: try_new ( & self . data_type ) ? ) )
201
+ Ok ( Box :: < BoolAndAccumulator > :: default ( ) )
203
202
}
204
203
}
205
204
@@ -217,25 +216,20 @@ impl PartialEq<dyn Any> for BoolAnd {
217
216
}
218
217
}
219
218
220
- #[ derive( Debug ) ]
219
+ #[ derive( Debug , Default ) ]
221
220
struct 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 > ,
232
222
}
233
223
234
224
impl Accumulator for BoolAndAccumulator {
235
225
fn update_batch ( & mut self , values : & [ ArrayRef ] ) -> Result < ( ) > {
236
226
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
+ } ;
239
233
Ok ( ( ) )
240
234
}
241
235
@@ -244,16 +238,15 @@ impl Accumulator for BoolAndAccumulator {
244
238
}
245
239
246
240
fn state ( & self ) -> Result < Vec < ScalarValue > > {
247
- Ok ( vec ! [ self . bool_and . clone ( ) ] )
241
+ Ok ( vec ! [ ScalarValue :: Boolean ( self . acc ) ] )
248
242
}
249
243
250
244
fn evaluate ( & self ) -> Result < ScalarValue > {
251
- Ok ( self . bool_and . clone ( ) )
245
+ Ok ( ScalarValue :: Boolean ( self . acc ) )
252
246
}
253
247
254
248
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 )
257
250
}
258
251
}
259
252
@@ -355,7 +348,7 @@ impl AggregateExpr for BoolOr {
355
348
}
356
349
357
350
fn create_accumulator ( & self ) -> Result < Box < dyn Accumulator > > {
358
- Ok ( Box :: new ( BoolOrAccumulator :: try_new ( & self . data_type ) ? ) )
351
+ Ok ( Box :: < BoolOrAccumulator > :: default ( ) )
359
352
}
360
353
361
354
fn state_fields ( & self ) -> Result < Vec < Field > > {
@@ -393,7 +386,7 @@ impl AggregateExpr for BoolOr {
393
386
}
394
387
395
388
fn create_sliding_accumulator ( & self ) -> Result < Box < dyn Accumulator > > {
396
- Ok ( Box :: new ( BoolOrAccumulator :: try_new ( & self . data_type ) ? ) )
389
+ Ok ( Box :: < BoolOrAccumulator > :: default ( ) )
397
390
}
398
391
}
399
392
@@ -411,29 +404,24 @@ impl PartialEq<dyn Any> for BoolOr {
411
404
}
412
405
}
413
406
414
- #[ derive( Debug ) ]
407
+ #[ derive( Debug , Default ) ]
415
408
struct 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 > ,
426
410
}
427
411
428
412
impl Accumulator for BoolOrAccumulator {
429
413
fn state ( & self ) -> Result < Vec < ScalarValue > > {
430
- Ok ( vec ! [ self . bool_or . clone ( ) ] )
414
+ Ok ( vec ! [ ScalarValue :: Boolean ( self . acc ) ] )
431
415
}
432
416
433
417
fn update_batch ( & mut self , values : & [ ArrayRef ] ) -> Result < ( ) > {
434
418
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
+ } ;
437
425
Ok ( ( ) )
438
426
}
439
427
@@ -442,12 +430,11 @@ impl Accumulator for BoolOrAccumulator {
442
430
}
443
431
444
432
fn evaluate ( & self ) -> Result < ScalarValue > {
445
- Ok ( self . bool_or . clone ( ) )
433
+ Ok ( ScalarValue :: Boolean ( self . acc ) )
446
434
}
447
435
448
436
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 )
451
438
}
452
439
}
453
440
0 commit comments