Skip to content

Commit 52043c8

Browse files
committed
Fix the avg accumulator
1 parent 560040a commit 52043c8

1 file changed

Lines changed: 29 additions & 11 deletions

File tree

‎datafusion/functions-aggregate/src/average.rs‎

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -653,10 +653,14 @@ impl Accumulator for AvgAccumulator {
653653
}
654654

655655
fn state(&mut self) -> Result<Vec<ScalarValue>> {
656-
Ok(vec![
657-
ScalarValue::from(self.count),
658-
ScalarValue::Float64(self.sum),
659-
])
656+
// With no non-NULL values both fields are NULL, matching
657+
// `AvgGroupsAccumulator::state`
658+
let (count, sum) = if self.count == 0 {
659+
(None, None)
660+
} else {
661+
(Some(self.count), self.sum)
662+
};
663+
Ok(vec![ScalarValue::UInt64(count), ScalarValue::Float64(sum)])
660664
}
661665

662666
fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {
@@ -833,9 +837,16 @@ where
833837
}
834838

835839
fn state(&mut self) -> Result<Vec<ScalarValue>> {
840+
// With no non-NULL values both fields are NULL, matching
841+
// `AvgGroupsAccumulator::state`
842+
let (count, sum) = if self.count == 0 {
843+
(None, None)
844+
} else {
845+
(Some(self.count), self.sum)
846+
};
836847
Ok(vec![
837-
ScalarValue::from(self.count),
838-
ScalarValue::new_primitive::<S>(self.sum, &self.sum_data_type)?,
848+
ScalarValue::UInt64(count),
849+
ScalarValue::new_primitive::<S>(sum, &self.sum_data_type)?,
839850
])
840851
}
841852

@@ -916,14 +927,21 @@ impl Accumulator for DurationAvgAccumulator {
916927
}
917928

918929
fn state(&mut self) -> Result<Vec<ScalarValue>> {
930+
// With no non-NULL values both fields are NULL, matching
931+
// `AvgGroupsAccumulator::state`
932+
let (count, sum) = if self.count == 0 {
933+
(None, None)
934+
} else {
935+
(Some(self.count), self.sum)
936+
};
919937
let duration_value = match self.time_unit {
920-
TimeUnit::Second => ScalarValue::DurationSecond(self.sum),
921-
TimeUnit::Millisecond => ScalarValue::DurationMillisecond(self.sum),
922-
TimeUnit::Microsecond => ScalarValue::DurationMicrosecond(self.sum),
923-
TimeUnit::Nanosecond => ScalarValue::DurationNanosecond(self.sum),
938+
TimeUnit::Second => ScalarValue::DurationSecond(sum),
939+
TimeUnit::Millisecond => ScalarValue::DurationMillisecond(sum),
940+
TimeUnit::Microsecond => ScalarValue::DurationMicrosecond(sum),
941+
TimeUnit::Nanosecond => ScalarValue::DurationNanosecond(sum),
924942
};
925943

926-
Ok(vec![ScalarValue::from(self.count), duration_value])
944+
Ok(vec![ScalarValue::UInt64(count), duration_value])
927945
}
928946

929947
fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {

0 commit comments

Comments
 (0)