@@ -14,11 +14,13 @@ use vortex_mask::AllOr;
1414
1515use crate :: ArrayRef ;
1616use crate :: Canonical ;
17+ use crate :: Columnar ;
1718use crate :: ExecutionCtx ;
1819use crate :: aggregate_fn:: AggregateFnId ;
1920use crate :: aggregate_fn:: AggregateFnVTable ;
2021use crate :: aggregate_fn:: EmptyOptions ;
2122use crate :: arrays:: BoolArray ;
23+ use crate :: arrays:: ConstantArray ;
2224use crate :: arrays:: DecimalArray ;
2325use crate :: arrays:: PrimitiveArray ;
2426use crate :: dtype:: DType ;
@@ -149,7 +151,7 @@ impl AggregateFnVTable for Sum {
149151 fn accumulate (
150152 & self ,
151153 partial : & mut Self :: Partial ,
152- batch : & Canonical ,
154+ batch : & Columnar ,
153155 _ctx : & mut ExecutionCtx ,
154156 ) -> VortexResult < ( ) > {
155157 let mut inner = match partial. current . take ( ) {
@@ -158,10 +160,13 @@ impl AggregateFnVTable for Sum {
158160 } ;
159161
160162 let result = match batch {
161- Canonical :: Primitive ( p) => accumulate_primitive ( & mut inner, p) ,
162- Canonical :: Bool ( b) => accumulate_bool ( & mut inner, b) ,
163- Canonical :: Decimal ( d) => accumulate_decimal ( & mut inner, d) ,
164- _ => vortex_bail ! ( "Unsupported canonical type for sum: {}" , batch. dtype( ) ) ,
163+ Columnar :: Canonical ( c) => match c {
164+ Canonical :: Primitive ( p) => accumulate_primitive ( & mut inner, p) ,
165+ Canonical :: Bool ( b) => accumulate_bool ( & mut inner, b) ,
166+ Canonical :: Decimal ( d) => accumulate_decimal ( & mut inner, d) ,
167+ _ => vortex_bail ! ( "Unsupported canonical type for sum: {}" , batch. dtype( ) ) ,
168+ } ,
169+ Columnar :: Constant ( c) => accumulate_constant ( & mut inner, c) ,
165170 } ;
166171
167172 match result {
@@ -349,6 +354,85 @@ fn accumulate_bool(inner: &mut SumState, b: &BoolArray) -> VortexResult<bool> {
349354 Ok ( checked_add_u64 ( acc, true_count) )
350355}
351356
357+ /// Accumulate a constant array into the sum state.
358+ /// Computes `scalar * len` and adds to the accumulator.
359+ /// Returns Ok(true) if saturated (overflow), Ok(false) if not.
360+ fn accumulate_constant ( inner : & mut SumState , c : & ConstantArray ) -> VortexResult < bool > {
361+ let scalar = c. scalar ( ) ;
362+ if scalar. is_null ( ) || c. is_empty ( ) {
363+ return Ok ( false ) ;
364+ }
365+ let len = c. len ( ) ;
366+
367+ match scalar. dtype ( ) {
368+ DType :: Bool ( _) => {
369+ let SumState :: Unsigned ( acc) = inner else {
370+ vortex_panic ! ( "expected unsigned sum state for bool input" ) ;
371+ } ;
372+ let val = scalar
373+ . as_bool ( )
374+ . value ( )
375+ . ok_or_else ( || vortex_err ! ( "Expected non-null bool scalar for sum" ) ) ?;
376+ if val {
377+ Ok ( checked_add_u64 ( acc, len as u64 ) )
378+ } else {
379+ Ok ( false )
380+ }
381+ }
382+ DType :: Primitive ( ..) => {
383+ let pvalue = scalar
384+ . as_primitive ( )
385+ . pvalue ( )
386+ . ok_or_else ( || vortex_err ! ( "Expected non-null primitive scalar for sum" ) ) ?;
387+ match inner {
388+ SumState :: Unsigned ( acc) => {
389+ let val = pvalue. cast :: < u64 > ( ) ?;
390+ match val. checked_mul ( len as u64 ) {
391+ Some ( product) => Ok ( checked_add_u64 ( acc, product) ) ,
392+ None => Ok ( true ) ,
393+ }
394+ }
395+ SumState :: Signed ( acc) => {
396+ let val = pvalue. cast :: < i64 > ( ) ?;
397+ match i64:: try_from ( len) . ok ( ) . and_then ( |l| val. checked_mul ( l) ) {
398+ Some ( product) => Ok ( checked_add_i64 ( acc, product) ) ,
399+ None => Ok ( true ) ,
400+ }
401+ }
402+ SumState :: Float ( acc) => {
403+ let val = pvalue. cast :: < f64 > ( ) ?;
404+ * acc += val * len as f64 ;
405+ Ok ( false )
406+ }
407+ SumState :: Decimal ( _) => {
408+ vortex_panic ! ( "decimal sum state with primitive input" )
409+ }
410+ }
411+ }
412+ DType :: Decimal ( ..) => {
413+ let SumState :: Decimal ( acc) = inner else {
414+ vortex_panic ! ( "expected decimal sum state for decimal input" ) ;
415+ } ;
416+ let val = scalar
417+ . as_decimal ( )
418+ . decimal_value ( )
419+ . ok_or_else ( || vortex_err ! ( "Expected non-null decimal scalar for sum" ) ) ?;
420+ let len_decimal = DecimalValue :: from ( len as i128 ) ;
421+ match val. checked_mul ( & len_decimal) {
422+ Some ( product) => match acc. checked_add ( & product) {
423+ Some ( r) => {
424+ * acc = r;
425+ Ok ( false )
426+ }
427+ None => Ok ( true ) ,
428+ } ,
429+ None => Ok ( true ) ,
430+ }
431+ }
432+ _ => vortex_bail ! ( "Unsupported constant type for sum: {}" , scalar. dtype( ) ) ,
433+ }
434+ }
435+
352436/// Accumulate a decimal array into the sum state.
353437/// Returns Ok(true) if saturated (overflow), Ok(false) if not.
354438fn accumulate_decimal ( inner : & mut SumState , d : & DecimalArray ) -> VortexResult < bool > {
0 commit comments