Skip to content

Commit 87a9af8

Browse files
author
B Vadlamani
committed
move_bitmap_accumulator_cold_path
1 parent 554f60c commit 87a9af8

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

  • datafusion
    • functions-aggregate-common/src/aggregate/count_distinct
    • functions-aggregate/src

datafusion/functions-aggregate-common/src/aggregate/count_distinct/native.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ where
7878
])
7979
}
8080

81+
#[inline(never)]
8182
fn update_batch(&mut self, values: &[ArrayRef]) -> datafusion_common::Result<()> {
8283
if values.is_empty() {
8384
return Ok(());
@@ -149,6 +150,7 @@ impl<T: ArrowPrimitiveType + Debug> Accumulator for FloatDistinctCountAccumulato
149150
self.values.state()
150151
}
151152

153+
#[inline(never)]
152154
fn update_batch(&mut self, values: &[ArrayRef]) -> datafusion_common::Result<()> {
153155
self.values.update_batch(values)
154156
}

datafusion/functions-aggregate/src/count.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use arrow::{
3030
};
3131
use datafusion_common::hash_utils::RandomState;
3232
use 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
};
3636
use datafusion_expr::{
@@ -174,11 +174,6 @@ impl Count {
174174
}
175175
fn 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+
270281
impl AggregateUDFImpl for Count {
271282
fn name(&self) -> &str {
272283
"count"

0 commit comments

Comments
 (0)