@@ -38,12 +38,21 @@ pub(super) fn accumulate_decimal(inner: &mut SumState, d: &DecimalArray) -> Vort
3838 let values_type = DecimalType :: smallest_decimal_value_type ( dtype) ;
3939 match_each_decimal_value_type ! ( d. values_type( ) , |I | {
4040 match_each_decimal_value_type!( values_type, |O | {
41- let initial_val : O = value
41+ let initial : O = value
4242 . cast( )
4343 . vortex_expect( "cannot fail to cast initial value" ) ;
4444
45- let res = sum_to_scalar( d. buffer:: <I >( ) , validity, initial_val, * dtype) ;
46- match res {
45+ let sum = match validity {
46+ Some ( v) => sum_decimal_with_validity( d. buffer:: <I >( ) , v, initial) ,
47+ None => sum_decimal( d. buffer:: <I >( ) , initial) ,
48+ } ;
49+
50+ let decimal_sum = sum
51+ . map( |v| DecimalValue :: from( v) )
52+ // We have to make sure that the decimal value fits the precision of the decimal dtype.
53+ . filter( |v| v. fits_in_precision( * dtype) ) ;
54+
55+ match decimal_sum {
4756 Some ( v) => * value = v,
4857 None => return Ok ( true ) ,
4958 }
@@ -52,32 +61,6 @@ pub(super) fn accumulate_decimal(inner: &mut SumState, d: &DecimalArray) -> Vort
5261 } )
5362}
5463
55- /// Compute the checked sum and convert the result to a [`Scalar`].
56- ///
57- /// Returns a null scalar if the sum overflows the underlying integer type or if the result
58- /// exceeds the declared decimal precision.
59- fn sum_to_scalar < T , O > (
60- values : Buffer < T > ,
61- validity : Option < & BitBuffer > ,
62- initial : O ,
63- return_decimal_dtype : DecimalDType ,
64- ) -> Option < DecimalValue >
65- where
66- T : AsPrimitive < O > ,
67- O : CheckedAdd + NumOps + Into < DecimalValue > + Copy + ' static ,
68- bool : AsPrimitive < O > ,
69- {
70- let raw_sum = match validity {
71- Some ( v) => sum_decimal_with_validity ( values, v, initial) ,
72- None => sum_decimal ( values, initial) ,
73- } ;
74-
75- raw_sum
76- . map ( Into :: < DecimalValue > :: into)
77- // We have to make sure that the decimal value fits the precision of the decimal dtype.
78- . filter ( |v| v. fits_in_precision ( return_decimal_dtype) )
79- }
80-
8164fn sum_decimal < T : AsPrimitive < I > , I : Copy + CheckedAdd + ' static > (
8265 values : Buffer < T > ,
8366 initial : I ,
0 commit comments