Skip to content

Commit

Permalink
chore: move aggregate_var to slt (#10750)
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinlanhenke authored Jun 2, 2024
1 parent 71a99b8 commit 1db3263
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 109 deletions.
109 changes: 0 additions & 109 deletions datafusion/physical-expr/src/aggregate/variance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,117 +337,8 @@ 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::{array::*, datatypes::*};

#[test]
fn variance_f64_1() -> Result<()> {
let a: ArrayRef = Arc::new(Float64Array::from(vec![1_f64, 2_f64]));
generic_test_op!(
a,
DataType::Float64,
VariancePop,
ScalarValue::from(0.25_f64)
)
}

#[test]
fn variance_f64_2() -> 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, VariancePop, ScalarValue::from(2_f64))
}

#[test]
fn variance_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, Variance, ScalarValue::from(2.5_f64))
}

#[test]
fn variance_f64_4() -> Result<()> {
let a: ArrayRef = Arc::new(Float64Array::from(vec![1.1_f64, 2_f64, 3_f64]));
generic_test_op!(
a,
DataType::Float64,
Variance,
ScalarValue::from(0.9033333333333333_f64)
)
}

#[test]
fn variance_i32() -> Result<()> {
let a: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5]));
generic_test_op!(a, DataType::Int32, VariancePop, ScalarValue::from(2_f64))
}

#[test]
fn variance_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, VariancePop, ScalarValue::from(2.0f64))
}

#[test]
fn variance_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, VariancePop, ScalarValue::from(2_f64))
}

#[test]
fn test_variance_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(Variance::new(
col("a", &schema)?,
"bla".to_string(),
DataType::Float64,
));
let actual = aggregate(&batch, agg).unwrap();
assert_eq!(actual, ScalarValue::Float64(None));

Ok(())
}

#[test]
fn variance_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,
VariancePop,
ScalarValue::from(2.1875_f64)
)
}

#[test]
fn variance_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(Variance::new(
col("a", &schema)?,
"bla".to_string(),
DataType::Float64,
));
let actual = aggregate(&batch, agg).unwrap();
assert_eq!(actual, ScalarValue::Float64(None));

Ok(())
}

#[test]
fn variance_f64_merge_1() -> Result<()> {
let a = Arc::new(Float64Array::from(vec![1_f64, 2_f64, 3_f64]));
Expand Down
118 changes: 118 additions & 0 deletions datafusion/sqllogictest/test_files/aggregate.slt
Original file line number Diff line number Diff line change
Expand Up @@ -2458,7 +2458,125 @@ NULL Float64
statement ok
drop table t;

# aggregate variance f64_1
statement ok
create table t (c1 double) as values (1), (2);

query RT
select var_pop(c1), arrow_typeof(var_pop(c1)) from t;
----
0.25 Float64

statement ok
drop table t;

# aggregate variance f64_2
statement ok
create table t (c1 double) as values (1), (2), (3), (4), (5);

query RT
select var_pop(c1), arrow_typeof(var_pop(c1)) from t;
----
2 Float64

statement ok
drop table t;

# aggregate variance f64_3
statement ok
create table t (c1 double) as values (1), (2), (3), (4), (5);

query RT
select var(c1), arrow_typeof(var(c1)) from t;
----
2.5 Float64

statement ok
drop table t;

# aggregate variance f64_4
statement ok
create table t (c1 double) as values (1.1), (2), (3);

query RT
select var(c1), arrow_typeof(var(c1)) from t;
----
0.903333333333 Float64

statement ok
drop table t;

# aggregate variance i32
statement ok
create table t (c1 int) as values (1), (2), (3), (4), (5);

query RT
select var_pop(c1), arrow_typeof(var_pop(c1)) from t;
----
2 Float64

statement ok
drop table t;

# aggregate variance u32
statement ok
create table t (c1 int unsigned) as values (1), (2), (3), (4), (5);

query RT
select var_pop(c1), arrow_typeof(var_pop(c1)) from t;
----
2 Float64

statement ok
drop table t;

# aggregate variance f32
statement ok
create table t (c1 float) as values (1), (2), (3), (4), (5);

query RT
select var_pop(c1), arrow_typeof(var_pop(c1)) from t;
----
2 Float64

statement ok
drop table t;

# aggregate single input
statement ok
create table t (c1 double) as values (1);

query RT
select var_pop(c1), arrow_typeof(var_pop(c1)) from t;
----
0 Float64

statement ok
drop table t;

# aggregate i32 with nulls
statement ok
create table t (c1 int) as values (1), (null), (3), (4), (5);

query RT
select var_pop(c1), arrow_typeof(var_pop(c1)) from t;
----
2.1875 Float64

statement ok
drop table t;

# aggregate i32 all nulls
statement ok
create table t (c1 int) as values (null), (null);

query RT
select var_pop(c1), arrow_typeof(var_pop(c1)) from t;
----
NULL Float64

statement ok
drop table t;

# simple_mean
query R
Expand Down

0 comments on commit 1db3263

Please sign in to comment.