| 467 | } |
| 468 | |
| 469 | pub fn variance( |
| 470 | &mut self, |
| 471 | emit_to: datafusion_expr::EmitTo, |
| 472 | ) -> (Vec<f64>, NullBuffer) { |
| 473 | let mut counts = emit_to.take_needed(&mut self.counts); |
| 474 | // means are only needed for updating m2s and are not needed for the final result. |
| 475 | // But we still need to take them to ensure the internal state is consistent. |
| 476 | let _ = emit_to.take_needed(&mut self.means); |
| 477 | let m2s = emit_to.take_needed(&mut self.m2s); |
| 478 | |
| 479 | if let StatsType::Sample = self.stats_type { |
| 480 | counts.iter_mut().for_each(|count| { |
| 481 | *count = count.saturating_sub(1); |
| 482 | }); |
| 483 | } |
| 484 | let nulls = NullBuffer::from_iter(counts.iter().map(|&count| count != 0)); |
| 485 | let variance = m2s |
| 486 | .iter() |
| 487 | .zip(counts) |
| 488 | .map(|(m2, count)| m2 / count as f64) |
| 489 | .collect(); |
| 490 | (variance, nulls) |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | impl GroupsAccumulator for VarianceGroupsAccumulator { |