@@ -25,9 +25,6 @@ use std::convert::TryFrom;
25
25
use std:: sync:: Arc ;
26
26
27
27
use crate :: aggregate:: groups_accumulator:: accumulate:: NullState ;
28
- use crate :: aggregate:: row_accumulator:: {
29
- is_row_accumulator_support_dtype, RowAccumulator ,
30
- } ;
31
28
use crate :: aggregate:: sum;
32
29
use crate :: aggregate:: sum:: sum_batch;
33
30
use crate :: aggregate:: utils:: calculate_result_decimal_for_avg;
@@ -46,7 +43,6 @@ use arrow_array::{
46
43
use datafusion_common:: { downcast_value, ScalarValue } ;
47
44
use datafusion_common:: { DataFusionError , Result } ;
48
45
use datafusion_expr:: Accumulator ;
49
- use datafusion_row:: accessor:: RowAccessor ;
50
46
51
47
use super :: groups_accumulator:: EmitTo ;
52
48
use super :: utils:: { adjust_output_array, Decimal128Averager } ;
@@ -139,21 +135,6 @@ impl AggregateExpr for Avg {
139
135
& self . name
140
136
}
141
137
142
- fn row_accumulator_supported ( & self ) -> bool {
143
- is_row_accumulator_support_dtype ( & self . sum_data_type )
144
- }
145
-
146
- fn create_row_accumulator (
147
- & self ,
148
- start_index : usize ,
149
- ) -> Result < Box < dyn RowAccumulator > > {
150
- Ok ( Box :: new ( AvgRowAccumulator :: new (
151
- start_index,
152
- & self . sum_data_type ,
153
- & self . rt_data_type ,
154
- ) ) )
155
- }
156
-
157
138
fn reverse_expr ( & self ) -> Option < Arc < dyn AggregateExpr > > {
158
139
Some ( Arc :: new ( self . clone ( ) ) )
159
140
}
@@ -321,121 +302,6 @@ impl Accumulator for AvgAccumulator {
321
302
}
322
303
}
323
304
324
- #[ derive( Debug ) ]
325
- struct AvgRowAccumulator {
326
- state_index : usize ,
327
- sum_datatype : DataType ,
328
- return_data_type : DataType ,
329
- }
330
-
331
- impl AvgRowAccumulator {
332
- pub fn new (
333
- start_index : usize ,
334
- sum_datatype : & DataType ,
335
- return_data_type : & DataType ,
336
- ) -> Self {
337
- Self {
338
- state_index : start_index,
339
- sum_datatype : sum_datatype. clone ( ) ,
340
- return_data_type : return_data_type. clone ( ) ,
341
- }
342
- }
343
- }
344
-
345
- impl RowAccumulator for AvgRowAccumulator {
346
- fn update_batch (
347
- & mut self ,
348
- values : & [ ArrayRef ] ,
349
- accessor : & mut RowAccessor ,
350
- ) -> Result < ( ) > {
351
- let values = & values[ 0 ] ;
352
- // count
353
- let delta = ( values. len ( ) - values. null_count ( ) ) as u64 ;
354
- accessor. add_u64 ( self . state_index ( ) , delta) ;
355
-
356
- // sum
357
- sum:: add_to_row (
358
- self . state_index ( ) + 1 ,
359
- accessor,
360
- & sum:: sum_batch ( values, & self . sum_datatype ) ?,
361
- )
362
- }
363
-
364
- fn update_scalar_values (
365
- & mut self ,
366
- values : & [ ScalarValue ] ,
367
- accessor : & mut RowAccessor ,
368
- ) -> Result < ( ) > {
369
- let value = & values[ 0 ] ;
370
- sum:: update_avg_to_row ( self . state_index ( ) , accessor, value)
371
- }
372
-
373
- fn update_scalar (
374
- & mut self ,
375
- value : & ScalarValue ,
376
- accessor : & mut RowAccessor ,
377
- ) -> Result < ( ) > {
378
- sum:: update_avg_to_row ( self . state_index ( ) , accessor, value)
379
- }
380
-
381
- fn merge_batch (
382
- & mut self ,
383
- states : & [ ArrayRef ] ,
384
- accessor : & mut RowAccessor ,
385
- ) -> Result < ( ) > {
386
- let counts = downcast_value ! ( states[ 0 ] , UInt64Array ) ;
387
- // count
388
- let delta = compute:: sum ( counts) . unwrap_or ( 0 ) ;
389
- accessor. add_u64 ( self . state_index ( ) , delta) ;
390
-
391
- // sum
392
- let difference = sum:: sum_batch ( & states[ 1 ] , & self . sum_datatype ) ?;
393
- sum:: add_to_row ( self . state_index ( ) + 1 , accessor, & difference)
394
- }
395
-
396
- fn evaluate ( & self , accessor : & RowAccessor ) -> Result < ScalarValue > {
397
- match self . sum_datatype {
398
- DataType :: Decimal128 ( p, s) => {
399
- match accessor. get_u64_opt ( self . state_index ( ) ) {
400
- None => Ok ( ScalarValue :: Decimal128 ( None , p, s) ) ,
401
- Some ( 0 ) => Ok ( ScalarValue :: Decimal128 ( None , p, s) ) ,
402
- Some ( n) => {
403
- // now the sum_type and return type is not the same, need to convert the sum type to return type
404
- accessor. get_i128_opt ( self . state_index ( ) + 1 ) . map_or_else (
405
- || Ok ( ScalarValue :: Decimal128 ( None , p, s) ) ,
406
- |f| {
407
- calculate_result_decimal_for_avg (
408
- f,
409
- n as i128 ,
410
- s,
411
- & self . return_data_type ,
412
- )
413
- } ,
414
- )
415
- }
416
- }
417
- }
418
- DataType :: Float64 => Ok ( match accessor. get_u64_opt ( self . state_index ( ) ) {
419
- None => ScalarValue :: Float64 ( None ) ,
420
- Some ( 0 ) => ScalarValue :: Float64 ( None ) ,
421
- Some ( n) => ScalarValue :: Float64 (
422
- accessor
423
- . get_f64_opt ( self . state_index ( ) + 1 )
424
- . map ( |f| f / n as f64 ) ,
425
- ) ,
426
- } ) ,
427
- _ => Err ( DataFusionError :: Internal (
428
- "Sum should be f64 or decimal128 on average" . to_string ( ) ,
429
- ) ) ,
430
- }
431
- }
432
-
433
- #[ inline( always) ]
434
- fn state_index ( & self ) -> usize {
435
- self . state_index
436
- }
437
- }
438
-
439
305
/// An accumulator to compute the average of `[PrimitiveArray<T>]`.
440
306
/// Stores values as native types, and does overflow checking
441
307
///
0 commit comments