MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / stat_aggregate

Function stat_aggregate

nodedb-query/src/msgpack_scan/aggregate.rs:371–393  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

369/// Compute stddev or variance. `population` = true for population variant.
370/// `finalize` transforms the variance into the final result.
371fn 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
395fn extract_value_bytes(
396 doc: &[u8],

Callers 1

compute_aggregate_binaryFunction · 0.85

Calls 5

extract_f64_valFunction · 0.85
finalizeFunction · 0.85
collectMethod · 0.80
iterMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected