@@ -30,7 +30,7 @@ use arrow::{
3030} ;
3131use datafusion_common:: hash_utils:: RandomState ;
3232use datafusion_common:: {
33- HashMap , Result , ScalarValue , downcast_value, internal_err, not_impl_err,
33+ HashMap , Result , ScalarValue , downcast_value, exec_err , internal_err, not_impl_err,
3434 stats:: Precision , utils:: expr:: COUNT_STAR_EXPANSION ,
3535} ;
3636use datafusion_expr:: {
@@ -174,11 +174,6 @@ impl Count {
174174}
175175fn get_count_accumulator ( data_type : & DataType ) -> Box < dyn Accumulator > {
176176 match data_type {
177- // Optimized bitmap/bool array accumulators for small integer types
178- DataType :: UInt8 => Box :: new ( BoolArray256DistinctCountAccumulator :: new ( ) ) ,
179- DataType :: Int8 => Box :: new ( BoolArray256DistinctCountAccumulatorI8 :: new ( ) ) ,
180- DataType :: UInt16 => Box :: new ( Bitmap65536DistinctCountAccumulator :: new ( ) ) ,
181- DataType :: Int16 => Box :: new ( Bitmap65536DistinctCountAccumulatorI16 :: new ( ) ) ,
182177 // HashSet-based accumulator for larger integer types
183178 DataType :: Int32 => Box :: new ( PrimitiveDistinctCountAccumulator :: < Int32Type > :: new (
184179 data_type,
@@ -192,6 +187,10 @@ fn get_count_accumulator(data_type: &DataType) -> Box<dyn Accumulator> {
192187 DataType :: UInt64 => Box :: new (
193188 PrimitiveDistinctCountAccumulator :: < UInt64Type > :: new ( data_type) ,
194189 ) ,
190+ // Small int types - cold path
191+ DataType :: UInt8 | DataType :: Int8 | DataType :: UInt16 | DataType :: Int16 => {
192+ get_small_int_accumulator ( data_type) . unwrap ( )
193+ }
195194 DataType :: Decimal128 ( _, _) => Box :: new ( PrimitiveDistinctCountAccumulator :: <
196195 Decimal128Type ,
197196 > :: new ( data_type) ) ,
@@ -267,6 +266,18 @@ fn get_count_accumulator(data_type: &DataType) -> Box<dyn Accumulator> {
267266 }
268267}
269268
269+ /// Uses optimized bitmap accumulators but separated to keep hot path small
270+ #[ cold]
271+ fn get_small_int_accumulator ( data_type : & DataType ) -> Result < Box < dyn Accumulator > > {
272+ match data_type {
273+ DataType :: UInt8 => Ok ( Box :: new ( BoolArray256DistinctCountAccumulator :: new ( ) ) ) ,
274+ DataType :: Int8 => Ok ( Box :: new ( BoolArray256DistinctCountAccumulatorI8 :: new ( ) ) ) ,
275+ DataType :: UInt16 => Ok ( Box :: new ( Bitmap65536DistinctCountAccumulator :: new ( ) ) ) ,
276+ DataType :: Int16 => Ok ( Box :: new ( Bitmap65536DistinctCountAccumulatorI16 :: new ( ) ) ) ,
277+ _ => exec_err ! ( "unsupported accumulator for datatype: {}" , data_type) ,
278+ }
279+ }
280+
270281impl AggregateUDFImpl for Count {
271282 fn name ( & self ) -> & str {
272283 "count"
0 commit comments