@@ -210,7 +210,10 @@ impl Accumulator for SumDecimalAccumulator {
210
210
// are null, in this case we'll return null
211
211
// 2. if `is_empty` is false, but `null_state` is true, it means there's an overflow. In
212
212
// non-ANSI mode Spark returns null.
213
- if self . is_empty || !self . is_not_null {
213
+ if self . is_empty
214
+ || !self . is_not_null
215
+ || !is_valid_decimal_precision ( self . sum , self . precision )
216
+ {
214
217
ScalarValue :: new_primitive :: < Decimal128Type > (
215
218
None ,
216
219
& DataType :: Decimal128 ( self . precision , self . scale ) ,
@@ -375,11 +378,17 @@ impl GroupsAccumulator for SumDecimalGroupsAccumulator {
375
378
// are null, in this case we'll return null
376
379
// 2. if `is_empty` is false, but `null_state` is true, it means there's an overflow. In
377
380
// non-ANSI mode Spark returns null.
381
+ let result = emit_to. take_needed ( & mut self . sum ) ;
382
+ result. iter ( ) . enumerate ( ) . for_each ( |( i, & v) | {
383
+ if !is_valid_decimal_precision ( v, self . precision ) {
384
+ self . is_not_null . set_bit ( i, false ) ;
385
+ }
386
+ } ) ;
387
+
378
388
let nulls = build_bool_state ( & mut self . is_not_null , & emit_to) ;
379
389
let is_empty = build_bool_state ( & mut self . is_empty , & emit_to) ;
380
390
let x = ( !& is_empty) . bitand ( & nulls) ;
381
391
382
- let result = emit_to. take_needed ( & mut self . sum ) ;
383
392
let result = Decimal128Array :: new ( result. into ( ) , Some ( NullBuffer :: new ( x) ) )
384
393
. with_data_type ( self . result_type . clone ( ) ) ;
385
394
0 commit comments