Compute stddev or variance. `population` = true for population variant. `finalize` transforms the variance into the final result.
(
docs: &[&[u8]],
field: &str,
expr: Option<&crate::expr::SqlExpr>,
finalize: fn(f64, usize) -> f64,
population: bool,
)
| 369 | /// Compute stddev or variance. `population` = true for population variant. |
| 370 | /// `finalize` transforms the variance into the final result. |
| 371 | fn stat_aggregate( |
| 372 | docs: &[&[u8]], |
| 373 | field: &str, |
| 374 | expr: Option<&crate::expr::SqlExpr>, |
| 375 | finalize: fn(f64, usize) -> f64, |
| 376 | population: bool, |
| 377 | ) -> Value { |
| 378 | let values: Vec<f64> = docs |
| 379 | .iter() |
| 380 | .filter_map(|d| extract_f64_val(d, field, expr)) |
| 381 | .collect(); |
| 382 | if values.len() < 2 { |
| 383 | return Value::Null; |
| 384 | } |
| 385 | let mean = values.iter().sum::<f64>() / values.len() as f64; |
| 386 | let divisor = if population { |
| 387 | values.len() as f64 |
| 388 | } else { |
| 389 | (values.len() - 1) as f64 |
| 390 | }; |
| 391 | let variance = values.iter().map(|v| (v - mean).powi(2)).sum::<f64>() / divisor; |
| 392 | Value::Float(finalize(variance, values.len())) |
| 393 | } |
| 394 | |
| 395 | fn extract_value_bytes( |
| 396 | doc: &[u8], |
no test coverage detected