-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Make SUM
and AVG
Aggregate Type Coercion Explicit
#7369
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ use arrow::datatypes::{ | |
DataType, TimeUnit, DECIMAL128_MAX_PRECISION, DECIMAL128_MAX_SCALE, | ||
DECIMAL256_MAX_PRECISION, DECIMAL256_MAX_SCALE, | ||
}; | ||
|
||
use datafusion_common::{internal_err, plan_err, DataFusionError, Result}; | ||
use std::ops::Deref; | ||
|
||
|
@@ -89,6 +90,7 @@ pub fn coerce_types( | |
input_types: &[DataType], | ||
signature: &Signature, | ||
) -> Result<Vec<DataType>> { | ||
use DataType::*; | ||
// Validate input_types matches (at least one of) the func signature. | ||
check_arg_count(agg_fun, input_types, &signature.type_signature)?; | ||
|
||
|
@@ -105,26 +107,44 @@ pub fn coerce_types( | |
AggregateFunction::Sum => { | ||
// Refer to https://www.postgresql.org/docs/8.2/functions-aggregate.html doc | ||
// smallint, int, bigint, real, double precision, decimal, or interval. | ||
if !is_sum_support_arg_type(&input_types[0]) { | ||
return plan_err!( | ||
"The function {:?} does not support inputs of type {:?}.", | ||
agg_fun, | ||
input_types[0] | ||
); | ||
} | ||
Ok(input_types.to_vec()) | ||
let v = match &input_types[0] { | ||
Decimal128(p, s) => Decimal128(*p, *s), | ||
Decimal256(p, s) => Decimal256(*p, *s), | ||
d if d.is_signed_integer() => Int64, | ||
d if d.is_unsigned_integer() => UInt64, | ||
d if d.is_floating() => Float64, | ||
Dictionary(_, v) => { | ||
return coerce_types(agg_fun, &[v.as_ref().clone()], signature) | ||
} | ||
_ => { | ||
return plan_err!( | ||
"The function {:?} does not support inputs of type {:?}.", | ||
agg_fun, | ||
input_types[0] | ||
) | ||
} | ||
}; | ||
Ok(vec![v]) | ||
} | ||
AggregateFunction::Avg => { | ||
// Refer to https://www.postgresql.org/docs/8.2/functions-aggregate.html doc | ||
// smallint, int, bigint, real, double precision, decimal, or interval | ||
if !is_avg_support_arg_type(&input_types[0]) { | ||
return plan_err!( | ||
"The function {:?} does not support inputs of type {:?}.", | ||
agg_fun, | ||
input_types[0] | ||
); | ||
} | ||
Ok(input_types.to_vec()) | ||
let v = match &input_types[0] { | ||
Decimal128(p, s) => Decimal128(*p, *s), | ||
Decimal256(p, s) => Decimal256(*p, *s), | ||
d if d.is_numeric() => Float64, | ||
Dictionary(_, v) => { | ||
return coerce_types(agg_fun, &[v.as_ref().clone()], signature) | ||
} | ||
_ => { | ||
return plan_err!( | ||
"The function {:?} does not support inputs of type {:?}.", | ||
agg_fun, | ||
input_types[0] | ||
) | ||
} | ||
}; | ||
Ok(vec![v]) | ||
} | ||
AggregateFunction::BitAnd | ||
| AggregateFunction::BitOr | ||
|
@@ -160,7 +180,7 @@ pub fn coerce_types( | |
input_types[0] | ||
); | ||
} | ||
Ok(input_types.to_vec()) | ||
Ok(vec![Float64, Float64]) | ||
} | ||
AggregateFunction::Covariance | AggregateFunction::CovariancePop => { | ||
if !is_covariance_support_arg_type(&input_types[0]) { | ||
|
@@ -170,7 +190,7 @@ pub fn coerce_types( | |
input_types[0] | ||
); | ||
} | ||
Ok(input_types.to_vec()) | ||
Ok(vec![Float64, Float64]) | ||
} | ||
AggregateFunction::Stddev | AggregateFunction::StddevPop => { | ||
if !is_stddev_support_arg_type(&input_types[0]) { | ||
|
@@ -180,7 +200,7 @@ pub fn coerce_types( | |
input_types[0] | ||
); | ||
} | ||
Ok(input_types.to_vec()) | ||
Ok(vec![Float64]) | ||
} | ||
AggregateFunction::Correlation => { | ||
if !is_correlation_support_arg_type(&input_types[0]) { | ||
|
@@ -190,7 +210,7 @@ pub fn coerce_types( | |
input_types[0] | ||
); | ||
} | ||
Ok(input_types.to_vec()) | ||
Ok(vec![Float64, Float64]) | ||
} | ||
AggregateFunction::RegrSlope | ||
| AggregateFunction::RegrIntercept | ||
|
@@ -211,7 +231,7 @@ pub fn coerce_types( | |
input_types[0] | ||
); | ||
} | ||
Ok(input_types.to_vec()) | ||
Ok(vec![Float64, Float64]) | ||
} | ||
AggregateFunction::ApproxPercentileCont => { | ||
if !is_approx_percentile_cont_supported_arg_type(&input_types[0]) { | ||
|
@@ -357,11 +377,9 @@ fn get_min_max_result_type(input_types: &[DataType]) -> Result<Vec<DataType>> { | |
/// function return type of a sum | ||
pub fn sum_return_type(arg_type: &DataType) -> Result<DataType> { | ||
match arg_type { | ||
arg_type if SIGNED_INTEGERS.contains(arg_type) => Ok(DataType::Int64), | ||
arg_type if UNSIGNED_INTEGERS.contains(arg_type) => Ok(DataType::UInt64), | ||
// In the https://www.postgresql.org/docs/current/functions-aggregate.html doc, | ||
// the result type of floating-point is FLOAT64 with the double precision. | ||
DataType::Float64 | DataType::Float32 => Ok(DataType::Float64), | ||
DataType::Int64 => Ok(DataType::Int64), | ||
DataType::UInt64 => Ok(DataType::UInt64), | ||
DataType::Float64 => Ok(DataType::Float64), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looked like we lost support for Float32 -- but then I see the test below for it, so 👍 (presumably what happens is the arguments are coerced first and |
||
DataType::Decimal128(precision, scale) => { | ||
// in the spark, the result type is DECIMAL(min(38,precision+10), s) | ||
// ref: https://github.com/apache/spark/blob/fcf636d9eb8d645c24be3db2d599aba2d7e2955a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/Sum.scala#L66 | ||
|
@@ -374,9 +392,6 @@ pub fn sum_return_type(arg_type: &DataType) -> Result<DataType> { | |
let new_precision = DECIMAL256_MAX_PRECISION.min(*precision + 10); | ||
Ok(DataType::Decimal256(new_precision, *scale)) | ||
} | ||
DataType::Dictionary(_, dict_value_type) => { | ||
sum_return_type(dict_value_type.as_ref()) | ||
} | ||
other => plan_err!("SUM does not support type \"{other:?}\""), | ||
} | ||
} | ||
|
@@ -601,21 +616,29 @@ mod tests { | |
assert_eq!(*input_type, result.unwrap()); | ||
} | ||
} | ||
// test sum, avg | ||
let funs = vec![AggregateFunction::Sum, AggregateFunction::Avg]; | ||
let input_types = vec![ | ||
vec![DataType::Int32], | ||
vec![DataType::Float32], | ||
vec![DataType::Decimal128(20, 3)], | ||
vec![DataType::Decimal256(20, 3)], | ||
]; | ||
for fun in funs { | ||
for input_type in &input_types { | ||
let signature = fun.signature(); | ||
let result = coerce_types(&fun, input_type, &signature); | ||
assert_eq!(*input_type, result.unwrap()); | ||
} | ||
} | ||
// test sum | ||
let fun = AggregateFunction::Sum; | ||
let signature = fun.signature(); | ||
let r = coerce_types(&fun, &[DataType::Int32], &signature).unwrap(); | ||
assert_eq!(r[0], DataType::Int64); | ||
let r = coerce_types(&fun, &[DataType::Float32], &signature).unwrap(); | ||
assert_eq!(r[0], DataType::Float64); | ||
let r = coerce_types(&fun, &[DataType::Decimal128(20, 3)], &signature).unwrap(); | ||
assert_eq!(r[0], DataType::Decimal128(20, 3)); | ||
let r = coerce_types(&fun, &[DataType::Decimal256(20, 3)], &signature).unwrap(); | ||
assert_eq!(r[0], DataType::Decimal256(20, 3)); | ||
|
||
// test avg | ||
let fun = AggregateFunction::Avg; | ||
let signature = fun.signature(); | ||
let r = coerce_types(&fun, &[DataType::Int32], &signature).unwrap(); | ||
assert_eq!(r[0], DataType::Float64); | ||
let r = coerce_types(&fun, &[DataType::Float32], &signature).unwrap(); | ||
assert_eq!(r[0], DataType::Float64); | ||
let r = coerce_types(&fun, &[DataType::Decimal128(20, 3)], &signature).unwrap(); | ||
assert_eq!(r[0], DataType::Decimal128(20, 3)); | ||
let r = coerce_types(&fun, &[DataType::Decimal256(20, 3)], &signature).unwrap(); | ||
assert_eq!(r[0], DataType::Decimal256(20, 3)); | ||
|
||
// ApproxPercentileCont input types | ||
let input_types = vec![ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,7 @@ fn subquery_filter_with_cast() -> Result<()> { | |
\n Inner Join: Filter: CAST(test.col_int32 AS Float64) > __scalar_sq_1.AVG(test.col_int32)\ | ||
\n TableScan: test projection=[col_int32]\ | ||
\n SubqueryAlias: __scalar_sq_1\ | ||
\n Aggregate: groupBy=[[]], aggr=[[AVG(test.col_int32)]]\ | ||
\n Aggregate: groupBy=[[]], aggr=[[AVG(CAST(test.col_int32 AS Float64))]]\ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is actually nice to see the explicit cast I think |
||
\n Projection: test.col_int32\ | ||
\n Filter: test.col_utf8 >= Utf8(\"2002-05-08\") AND test.col_utf8 <= Utf8(\"2002-05-13\")\ | ||
\n TableScan: test projection=[col_int32, col_utf8]"; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer need this custom logic, it is handled automatically by the optimizer