Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stddev and variance #1525

Merged
merged 22 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ballista/rust/core/proto/ballista.proto
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ enum AggregateFunction {
COUNT = 4;
APPROX_DISTINCT = 5;
ARRAY_AGG = 6;
VARIANCE=7;
STDDEV=8;
}

message AggregateExprNode {
Expand Down
4 changes: 4 additions & 0 deletions ballista/rust/core/src/serde/logical_plan/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,8 @@ impl TryInto<protobuf::LogicalExprNode> for &Expr {
AggregateFunction::Sum => protobuf::AggregateFunction::Sum,
AggregateFunction::Avg => protobuf::AggregateFunction::Avg,
AggregateFunction::Count => protobuf::AggregateFunction::Count,
AggregateFunction::Variance => protobuf::AggregateFunction::Variance,
AggregateFunction::Stddev => protobuf::AggregateFunction::Stddev,
};

let arg = &args[0];
Expand Down Expand Up @@ -1364,6 +1366,8 @@ impl From<&AggregateFunction> for protobuf::AggregateFunction {
AggregateFunction::Count => Self::Count,
AggregateFunction::ApproxDistinct => Self::ApproxDistinct,
AggregateFunction::ArrayAgg => Self::ArrayAgg,
AggregateFunction::Variance => Self::Variance,
AggregateFunction::Stddev => Self::Stddev,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions ballista/rust/core/src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ impl From<protobuf::AggregateFunction> for AggregateFunction {
AggregateFunction::ApproxDistinct
}
protobuf::AggregateFunction::ArrayAgg => AggregateFunction::ArrayAgg,
protobuf::AggregateFunction::Variance => AggregateFunction::Variance,
protobuf::AggregateFunction::Stddev => AggregateFunction::Stddev,
}
}
}
Expand Down
167 changes: 164 additions & 3 deletions datafusion/src/physical_plan/aggregates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ use crate::physical_plan::coercion_rule::aggregate_rule::{coerce_exprs, coerce_t
use crate::physical_plan::distinct_expressions;
use crate::physical_plan::expressions;
use arrow::datatypes::{DataType, Field, Schema, TimeUnit};
use expressions::{avg_return_type, sum_return_type};
use expressions::{
avg_return_type, stddev_return_type, sum_return_type, variance_return_type,
};
use std::{fmt, str::FromStr, sync::Arc};

/// the implementation of an aggregate function
Expand Down Expand Up @@ -64,6 +66,10 @@ pub enum AggregateFunction {
ApproxDistinct,
/// array_agg
ArrayAgg,
/// Variance (Population)
Variance,
/// Standard Deviation (Population)
Stddev,
}

impl fmt::Display for AggregateFunction {
Expand All @@ -84,6 +90,8 @@ impl FromStr for AggregateFunction {
"sum" => AggregateFunction::Sum,
"approx_distinct" => AggregateFunction::ApproxDistinct,
"array_agg" => AggregateFunction::ArrayAgg,
"variance" => AggregateFunction::Variance,
"stddev" => AggregateFunction::Stddev,
_ => {
return Err(DataFusionError::Plan(format!(
"There is no built-in function named {}",
Expand Down Expand Up @@ -116,6 +124,8 @@ pub fn return_type(
Ok(coerced_data_types[0].clone())
}
AggregateFunction::Sum => sum_return_type(&coerced_data_types[0]),
AggregateFunction::Variance => variance_return_type(&coerced_data_types[0]),
AggregateFunction::Stddev => stddev_return_type(&coerced_data_types[0]),
AggregateFunction::Avg => avg_return_type(&coerced_data_types[0]),
AggregateFunction::ArrayAgg => Ok(DataType::List(Box::new(Field::new(
"item",
Expand Down Expand Up @@ -212,6 +222,26 @@ pub fn create_aggregate_expr(
"AVG(DISTINCT) aggregations are not available".to_string(),
));
}
(AggregateFunction::Variance, false) => Arc::new(expressions::Variance::new(
coerced_phy_exprs[0].clone(),
name,
return_type,
)),
(AggregateFunction::Variance, true) => {
return Err(DataFusionError::NotImplemented(
"VARIANCE(DISTINCT) aggregations are not available".to_string(),
));
}
(AggregateFunction::Stddev, false) => Arc::new(expressions::Stddev::new(
coerced_phy_exprs[0].clone(),
name,
return_type,
)),
(AggregateFunction::Stddev, true) => {
return Err(DataFusionError::NotImplemented(
"VARIANCE(DISTINCT) aggregations are not available".to_string(),
realno marked this conversation as resolved.
Show resolved Hide resolved
));
}
})
}

Expand Down Expand Up @@ -256,7 +286,10 @@ pub fn signature(fun: &AggregateFunction) -> Signature {
.collect::<Vec<_>>();
Signature::uniform(1, valid, Volatility::Immutable)
}
AggregateFunction::Avg | AggregateFunction::Sum => {
AggregateFunction::Avg
| AggregateFunction::Sum
| AggregateFunction::Variance
| AggregateFunction::Stddev => {
Signature::uniform(1, NUMERICS.to_vec(), Volatility::Immutable)
}
}
Expand All @@ -267,7 +300,7 @@ mod tests {
use super::*;
use crate::error::Result;
use crate::physical_plan::expressions::{
ApproxDistinct, ArrayAgg, Avg, Count, Max, Min, Sum,
ApproxDistinct, ArrayAgg, Avg, Count, Max, Min, Stddev, Sum, Variance,
};

#[test]
Expand Down Expand Up @@ -450,6 +483,82 @@ mod tests {
Ok(())
}

#[test]
fn test_variance_expr() -> Result<()> {
let funcs = vec![AggregateFunction::Variance];
let data_types = vec![
DataType::UInt32,
DataType::UInt64,
DataType::Int32,
DataType::Int64,
DataType::Float32,
DataType::Float64,
];
for fun in funcs {
for data_type in &data_types {
let input_schema =
Schema::new(vec![Field::new("c1", data_type.clone(), true)]);
let input_phy_exprs: Vec<Arc<dyn PhysicalExpr>> = vec![Arc::new(
expressions::Column::new_with_schema("c1", &input_schema).unwrap(),
)];
let result_agg_phy_exprs = create_aggregate_expr(
&fun,
false,
&input_phy_exprs[0..1],
&input_schema,
"c1",
)?;
if fun == AggregateFunction::Variance {
assert!(result_agg_phy_exprs.as_any().is::<Variance>());
assert_eq!("c1", result_agg_phy_exprs.name());
assert_eq!(
Field::new("c1", DataType::Float64, true),
result_agg_phy_exprs.field().unwrap()
)
}
}
}
Ok(())
}

#[test]
fn test_stddev_expr() -> Result<()> {
let funcs = vec![AggregateFunction::Stddev];
let data_types = vec![
DataType::UInt32,
DataType::UInt64,
DataType::Int32,
DataType::Int64,
DataType::Float32,
DataType::Float64,
];
for fun in funcs {
for data_type in &data_types {
let input_schema =
Schema::new(vec![Field::new("c1", data_type.clone(), true)]);
let input_phy_exprs: Vec<Arc<dyn PhysicalExpr>> = vec![Arc::new(
expressions::Column::new_with_schema("c1", &input_schema).unwrap(),
)];
let result_agg_phy_exprs = create_aggregate_expr(
&fun,
false,
&input_phy_exprs[0..1],
&input_schema,
"c1",
)?;
if fun == AggregateFunction::Variance {
assert!(result_agg_phy_exprs.as_any().is::<Stddev>());
assert_eq!("c1", result_agg_phy_exprs.name());
assert_eq!(
Field::new("c1", DataType::Float64, true),
result_agg_phy_exprs.field().unwrap()
)
}
}
}
Ok(())
}

#[test]
fn test_min_max() -> Result<()> {
let observed = return_type(&AggregateFunction::Min, &[DataType::Utf8])?;
Expand Down Expand Up @@ -544,4 +653,56 @@ mod tests {
let observed = return_type(&AggregateFunction::Avg, &[DataType::Utf8]);
assert!(observed.is_err());
}

#[test]
fn test_variance_return_type() -> Result<()> {
let observed = return_type(&AggregateFunction::Variance, &[DataType::Float32])?;
assert_eq!(DataType::Float64, observed);

let observed = return_type(&AggregateFunction::Variance, &[DataType::Float64])?;
assert_eq!(DataType::Float64, observed);

let observed = return_type(&AggregateFunction::Variance, &[DataType::Int32])?;
assert_eq!(DataType::Float64, observed);

let observed = return_type(&AggregateFunction::Variance, &[DataType::UInt32])?;
assert_eq!(DataType::Float64, observed);

let observed = return_type(&AggregateFunction::Variance, &[DataType::Int64])?;
assert_eq!(DataType::Float64, observed);

Ok(())
}

#[test]
fn test_variance_no_utf8() {
let observed = return_type(&AggregateFunction::Variance, &[DataType::Utf8]);
assert!(observed.is_err());
}

#[test]
fn test_stddev_return_type() -> Result<()> {
let observed = return_type(&AggregateFunction::Stddev, &[DataType::Float32])?;
assert_eq!(DataType::Float64, observed);

let observed = return_type(&AggregateFunction::Stddev, &[DataType::Float64])?;
assert_eq!(DataType::Float64, observed);

let observed = return_type(&AggregateFunction::Stddev, &[DataType::Int32])?;
assert_eq!(DataType::Float64, observed);

let observed = return_type(&AggregateFunction::Stddev, &[DataType::UInt32])?;
assert_eq!(DataType::Float64, observed);

let observed = return_type(&AggregateFunction::Stddev, &[DataType::Int64])?;
assert_eq!(DataType::Float64, observed);

Ok(())
}

#[test]
fn test_stddev_no_utf8() {
let observed = return_type(&AggregateFunction::Stddev, &[DataType::Utf8]);
assert!(observed.is_err());
}
}
21 changes: 20 additions & 1 deletion datafusion/src/physical_plan/coercion_rule/aggregate_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use crate::arrow::datatypes::Schema;
use crate::error::{DataFusionError, Result};
use crate::physical_plan::aggregates::AggregateFunction;
use crate::physical_plan::expressions::{
is_avg_support_arg_type, is_sum_support_arg_type, try_cast,
is_avg_support_arg_type, is_stddev_support_arg_type, is_sum_support_arg_type,
is_variance_support_arg_type, try_cast,
};
use crate::physical_plan::functions::{Signature, TypeSignature};
use crate::physical_plan::PhysicalExpr;
Expand Down Expand Up @@ -86,6 +87,24 @@ pub(crate) fn coerce_types(
}
Ok(input_types.to_vec())
}
AggregateFunction::Variance => {
if !is_variance_support_arg_type(&input_types[0]) {
return Err(DataFusionError::Plan(format!(
"The function {:?} does not support inputs of type {:?}.",
agg_fun, input_types[0]
)));
}
Ok(input_types.to_vec())
}
AggregateFunction::Stddev => {
if !is_stddev_support_arg_type(&input_types[0]) {
return Err(DataFusionError::Plan(format!(
"The function {:?} does not support inputs of type {:?}.",
agg_fun, input_types[0]
)));
}
Ok(input_types.to_vec())
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions datafusion/src/physical_plan/expressions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ mod nth_value;
mod nullif;
mod rank;
mod row_number;
mod stddev;
mod sum;
mod try_cast;
mod variance;

/// Module with some convenient methods used in expression building
pub mod helpers {
Expand Down Expand Up @@ -84,9 +86,13 @@ pub use nth_value::NthValue;
pub use nullif::{nullif_func, SUPPORTED_NULLIF_TYPES};
pub use rank::{dense_rank, percent_rank, rank};
pub use row_number::RowNumber;
pub(crate) use stddev::is_stddev_support_arg_type;
pub use stddev::{stddev_return_type, Stddev};
realno marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) use sum::is_sum_support_arg_type;
pub use sum::{sum_return_type, Sum};
pub use try_cast::{try_cast, TryCastExpr};
pub(crate) use variance::is_variance_support_arg_type;
pub use variance::{variance_return_type, Variance};
realno marked this conversation as resolved.
Show resolved Hide resolved

/// returns the name of the state
pub fn format_state_name(name: &str, state_name: &str) -> String {
Expand Down
Loading