@@ -210,7 +210,10 @@ impl Accumulator for SumDecimalAccumulator {
210210 // are null, in this case we'll return null
211211 // 2. if `is_empty` is false, but `null_state` is true, it means there's an overflow. In
212212 // 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+ {
214217 ScalarValue :: new_primitive :: < Decimal128Type > (
215218 None ,
216219 & DataType :: Decimal128 ( self . precision , self . scale ) ,
@@ -375,11 +378,17 @@ impl GroupsAccumulator for SumDecimalGroupsAccumulator {
375378 // are null, in this case we'll return null
376379 // 2. if `is_empty` is false, but `null_state` is true, it means there's an overflow. In
377380 // 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+
378388 let nulls = build_bool_state ( & mut self . is_not_null , & emit_to) ;
379389 let is_empty = build_bool_state ( & mut self . is_empty , & emit_to) ;
380390 let x = ( !& is_empty) . bitand ( & nulls) ;
381391
382- let result = emit_to. take_needed ( & mut self . sum ) ;
383392 let result = Decimal128Array :: new ( result. into ( ) , Some ( NullBuffer :: new ( x) ) )
384393 . with_data_type ( self . result_type . clone ( ) ) ;
385394
0 commit comments