use std::any::Any;
use std::sync::Arc;
use crate::aggregate::stats::StatsType;
use crate::aggregate::utils::down_cast_any_ref;
use crate::aggregate::variance::VarianceAccumulator;
use crate::expressions::format_state_name;
use crate::{AggregateExpr, PhysicalExpr};
use arrow::{array::ArrayRef, datatypes::DataType, datatypes::Field};
use datafusion_common::ScalarValue;
use datafusion_common::{internal_err, DataFusionError, Result};
use datafusion_expr::Accumulator;
#[derive(Debug)]
pub struct Stddev {
name: String,
expr: Arc<dyn PhysicalExpr>,
}
#[derive(Debug)]
pub struct StddevPop {
name: String,
expr: Arc<dyn PhysicalExpr>,
}
impl Stddev {
pub fn new(
expr: Arc<dyn PhysicalExpr>,
name: impl Into<String>,
data_type: DataType,
) -> Self {
assert!(matches!(data_type, DataType::Float64));
Self {
name: name.into(),
expr,
}
}
}
impl AggregateExpr for Stddev {
fn as_any(&self) -> &dyn Any {
self
}
fn field(&self) -> Result<Field> {
Ok(Field::new(&self.name, DataType::Float64, true))
}
fn create_accumulator(&self) -> Result<Box<dyn Accumulator>> {
Ok(Box::new(StddevAccumulator::try_new(StatsType::Sample)?))
}
fn create_sliding_accumulator(&self) -> Result<Box<dyn Accumulator>> {
Ok(Box::new(StddevAccumulator::try_new(StatsType::Sample)?))
}
fn state_fields(&self) -> Result<Vec<Field>> {
Ok(vec![
Field::new(
format_state_name(&self.name, "count"),
DataType::UInt64,
true,
),
Field::new(
format_state_name(&self.name, "mean"),
DataType::Float64,
true,
),
Field::new(format_state_name(&self.name, "m2"), DataType::Float64, true),
])
}
fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>> {
vec![self.expr.clone()]
}
fn name(&self) -> &str {
&self.name
}
}
impl PartialEq<dyn Any> for Stddev {
fn eq(&self, other: &dyn Any) -> bool {
down_cast_any_ref(other)
.downcast_ref::<Self>()
.map(|x| self.name == x.name && self.expr.eq(&x.expr))
.unwrap_or(false)
}
}
impl StddevPop {
pub fn new(
expr: Arc<dyn PhysicalExpr>,
name: impl Into<String>,
data_type: DataType,
) -> Self {
assert!(matches!(data_type, DataType::Float64));
Self {
name: name.into(),
expr,
}
}
}
impl AggregateExpr for StddevPop {
fn as_any(&self) -> &dyn Any {
self
}
fn field(&self) -> Result<Field> {
Ok(Field::new(&self.name, DataType::Float64, true))
}
fn create_accumulator(&self) -> Result<Box<dyn Accumulator>> {
Ok(Box::new(StddevAccumulator::try_new(StatsType::Population)?))
}
fn create_sliding_accumulator(&self) -> Result<Box<dyn Accumulator>> {
Ok(Box::new(StddevAccumulator::try_new(StatsType::Population)?))
}
fn state_fields(&self) -> Result<Vec<Field>> {
Ok(vec![
Field::new(
format_state_name(&self.name, "count"),
DataType::UInt64,
true,
),
Field::new(
format_state_name(&self.name, "mean"),
DataType::Float64,
true,
),
Field::new(format_state_name(&self.name, "m2"), DataType::Float64, true),
])
}
fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>> {
vec![self.expr.clone()]
}
fn name(&self) -> &str {
&self.name
}
}
impl PartialEq<dyn Any> for StddevPop {
fn eq(&self, other: &dyn Any) -> bool {
down_cast_any_ref(other)
.downcast_ref::<Self>()
.map(|x| self.name == x.name && self.expr.eq(&x.expr))
.unwrap_or(false)
}
}
#[derive(Debug)]
pub struct StddevAccumulator {
variance: VarianceAccumulator,
}
impl StddevAccumulator {
pub fn try_new(s_type: StatsType) -> Result<Self> {
Ok(Self {
variance: VarianceAccumulator::try_new(s_type)?,
})
}
pub fn get_m2(&self) -> f64 {
self.variance.get_m2()
}
}
impl Accumulator for StddevAccumulator {
fn state(&self) -> Result<Vec<ScalarValue>> {
Ok(vec![
ScalarValue::from(self.variance.get_count()),
ScalarValue::from(self.variance.get_mean()),
ScalarValue::from(self.variance.get_m2()),
])
}
fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
self.variance.update_batch(values)
}
fn retract_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
self.variance.retract_batch(values)
}
fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {
self.variance.merge_batch(states)
}
fn evaluate(&self) -> Result<ScalarValue> {
let variance = self.variance.evaluate()?;
match variance {
ScalarValue::Float64(e) => {
if e.is_none() {
Ok(ScalarValue::Float64(None))
} else {
Ok(ScalarValue::Float64(e.map(|f| f.sqrt())))
}
}
_ => internal_err!("Variance should be f64"),
}
}
fn size(&self) -> usize {
std::mem::align_of_val(self) - std::mem::align_of_val(&self.variance)
+ self.variance.size()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::aggregate::utils::get_accum_scalar_values_as_arrays;
use crate::expressions::col;
use crate::expressions::tests::aggregate;
use crate::generic_test_op;
use arrow::record_batch::RecordBatch;
use arrow::{array::*, datatypes::*};
use datafusion_common::Result;
#[test]
fn stddev_f64_1() -> Result<()> {
let a: ArrayRef = Arc::new(Float64Array::from(vec![1_f64, 2_f64]));
generic_test_op!(a, DataType::Float64, StddevPop, ScalarValue::from(0.5_f64))
}
#[test]
fn stddev_f64_2() -> Result<()> {
let a: ArrayRef = Arc::new(Float64Array::from(vec![1.1_f64, 2_f64, 3_f64]));
generic_test_op!(
a,
DataType::Float64,
StddevPop,
ScalarValue::from(0.7760297817881877_f64)
)
}
#[test]
fn stddev_f64_3() -> Result<()> {
let a: ArrayRef =
Arc::new(Float64Array::from(vec![1_f64, 2_f64, 3_f64, 4_f64, 5_f64]));
generic_test_op!(
a,
DataType::Float64,
StddevPop,
ScalarValue::from(std::f64::consts::SQRT_2)
)
}
#[test]
fn stddev_f64_4() -> Result<()> {
let a: ArrayRef = Arc::new(Float64Array::from(vec![1.1_f64, 2_f64, 3_f64]));
generic_test_op!(
a,
DataType::Float64,
Stddev,
ScalarValue::from(0.9504384952922168_f64)
)
}
#[test]
fn stddev_i32() -> Result<()> {
let a: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5]));
generic_test_op!(
a,
DataType::Int32,
StddevPop,
ScalarValue::from(std::f64::consts::SQRT_2)
)
}
#[test]
fn stddev_u32() -> Result<()> {
let a: ArrayRef =
Arc::new(UInt32Array::from(vec![1_u32, 2_u32, 3_u32, 4_u32, 5_u32]));
generic_test_op!(
a,
DataType::UInt32,
StddevPop,
ScalarValue::from(std::f64::consts::SQRT_2)
)
}
#[test]
fn stddev_f32() -> Result<()> {
let a: ArrayRef =
Arc::new(Float32Array::from(vec![1_f32, 2_f32, 3_f32, 4_f32, 5_f32]));
generic_test_op!(
a,
DataType::Float32,
StddevPop,
ScalarValue::from(std::f64::consts::SQRT_2)
)
}
#[test]
fn test_stddev_1_input() -> Result<()> {
let a: ArrayRef = Arc::new(Float64Array::from(vec![1_f64]));
let schema = Schema::new(vec![Field::new("a", DataType::Float64, false)]);
let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![a])?;
let agg = Arc::new(Stddev::new(
col("a", &schema)?,
"bla".to_string(),
DataType::Float64,
));
let actual = aggregate(&batch, agg).unwrap();
assert_eq!(actual, ScalarValue::Float64(None));
Ok(())
}
#[test]
fn stddev_i32_with_nulls() -> Result<()> {
let a: ArrayRef = Arc::new(Int32Array::from(vec![
Some(1),
None,
Some(3),
Some(4),
Some(5),
]));
generic_test_op!(
a,
DataType::Int32,
StddevPop,
ScalarValue::from(1.479019945774904_f64)
)
}
#[test]
fn stddev_i32_all_nulls() -> Result<()> {
let a: ArrayRef = Arc::new(Int32Array::from(vec![None, None]));
let schema = Schema::new(vec![Field::new("a", DataType::Int32, true)]);
let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![a])?;
let agg = Arc::new(Stddev::new(
col("a", &schema)?,
"bla".to_string(),
DataType::Float64,
));
let actual = aggregate(&batch, agg).unwrap();
assert_eq!(actual, ScalarValue::Float64(None));
Ok(())
}
#[test]
fn stddev_f64_merge_1() -> Result<()> {
let a = Arc::new(Float64Array::from(vec![1_f64, 2_f64, 3_f64]));
let b = Arc::new(Float64Array::from(vec![4_f64, 5_f64]));
let schema = Schema::new(vec![Field::new("a", DataType::Float64, false)]);
let batch1 = RecordBatch::try_new(Arc::new(schema.clone()), vec![a])?;
let batch2 = RecordBatch::try_new(Arc::new(schema.clone()), vec![b])?;
let agg1 = Arc::new(StddevPop::new(
col("a", &schema)?,
"bla".to_string(),
DataType::Float64,
));
let agg2 = Arc::new(StddevPop::new(
col("a", &schema)?,
"bla".to_string(),
DataType::Float64,
));
let actual = merge(&batch1, &batch2, agg1, agg2)?;
assert!(actual == ScalarValue::from(std::f64::consts::SQRT_2));
Ok(())
}
#[test]
fn stddev_f64_merge_2() -> Result<()> {
let a = Arc::new(Float64Array::from(vec![1_f64, 2_f64, 3_f64, 4_f64, 5_f64]));
let b = Arc::new(Float64Array::from(vec![None]));
let schema = Schema::new(vec![Field::new("a", DataType::Float64, true)]);
let batch1 = RecordBatch::try_new(Arc::new(schema.clone()), vec![a])?;
let batch2 = RecordBatch::try_new(Arc::new(schema.clone()), vec![b])?;
let agg1 = Arc::new(StddevPop::new(
col("a", &schema)?,
"bla".to_string(),
DataType::Float64,
));
let agg2 = Arc::new(StddevPop::new(
col("a", &schema)?,
"bla".to_string(),
DataType::Float64,
));
let actual = merge(&batch1, &batch2, agg1, agg2)?;
assert!(actual == ScalarValue::from(std::f64::consts::SQRT_2));
Ok(())
}
fn merge(
batch1: &RecordBatch,
batch2: &RecordBatch,
agg1: Arc<dyn AggregateExpr>,
agg2: Arc<dyn AggregateExpr>,
) -> Result<ScalarValue> {
let mut accum1 = agg1.create_accumulator()?;
let mut accum2 = agg2.create_accumulator()?;
let expr1 = agg1.expressions();
let expr2 = agg2.expressions();
let values1 = expr1
.iter()
.map(|e| e.evaluate(batch1))
.map(|r| r.map(|v| v.into_array(batch1.num_rows())))
.collect::<Result<Vec<_>>>()?;
let values2 = expr2
.iter()
.map(|e| e.evaluate(batch2))
.map(|r| r.map(|v| v.into_array(batch2.num_rows())))
.collect::<Result<Vec<_>>>()?;
accum1.update_batch(&values1)?;
accum2.update_batch(&values2)?;
let state2 = get_accum_scalar_values_as_arrays(accum2.as_ref())?;
accum1.merge_batch(&state2)?;
accum1.evaluate()
}
}