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

feat: Support Null input type in Sum and Avg functions #162

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions datafusion/expr/src/aggregate_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ pub fn sum_return_type(arg_type: &DataType) -> Result<DataType> {
let new_precision = DECIMAL_MAX_PRECISION.min(*precision + 10);
Ok(DataType::Decimal(new_precision, *scale))
}
DataType::Null => Ok(DataType::Null),
other => Err(DataFusionError::Plan(format!(
"SUM does not support type \"{:?}\"",
other
Expand Down Expand Up @@ -526,6 +527,7 @@ pub fn avg_return_type(arg_type: &DataType) -> Result<DataType> {
| DataType::UInt64
| DataType::Float32
| DataType::Float64 => Ok(DataType::Float64),
DataType::Null => Ok(DataType::Null),
other => Err(DataFusionError::Plan(format!(
"AVG does not support {:?}",
other
Expand Down Expand Up @@ -616,6 +618,7 @@ pub fn is_sum_support_arg_type(arg_type: &DataType) -> bool {
| DataType::Float32
| DataType::Float64
| DataType::Decimal(_, _)
| DataType::Null
)
}

Expand All @@ -633,6 +636,7 @@ pub fn is_avg_support_arg_type(arg_type: &DataType) -> bool {
| DataType::Float32
| DataType::Float64
| DataType::Decimal(_, _)
| DataType::Null
)
}

Expand Down Expand Up @@ -778,6 +782,7 @@ mod tests {
vec![DataType::Int32],
vec![DataType::Float32],
vec![DataType::Decimal(20, 3)],
vec![DataType::Null],
];
for fun in funs {
for input_type in &input_types {
Expand Down
9 changes: 8 additions & 1 deletion datafusion/physical-expr/src/expressions/average.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Avg {
// the result of avg just support FLOAT64 and Decimal data type.
assert!(matches!(
data_type,
DataType::Float64 | DataType::Decimal(_, _)
DataType::Float64 | DataType::Decimal(_, _) | DataType::Null
));
Self {
name: name.into(),
Expand Down Expand Up @@ -160,6 +160,7 @@ impl Accumulator for AvgAccumulator {
),
})
}
ScalarValue::Null => Ok(ScalarValue::Null),
_ => Err(DataFusionError::Internal(
"Sum should be f64 on average".to_string(),
)),
Expand Down Expand Up @@ -311,6 +312,12 @@ mod tests {
)
}

#[test]
fn avg_null_type() -> Result<()> {
let a: ArrayRef = Arc::new(NullArray::new(5));
generic_test_op!(a, DataType::Null, Avg, ScalarValue::Null, DataType::Null)
}

fn aggregate(
batch: &RecordBatch,
agg: Arc<dyn AggregateExpr>,
Expand Down
10 changes: 9 additions & 1 deletion datafusion/physical-expr/src/expressions/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ pub(super) fn sum_batch(values: &ArrayRef) -> Result<ScalarValue> {
DataType::UInt32 => typed_sum_delta_batch!(values, UInt32Array, UInt32),
DataType::UInt16 => typed_sum_delta_batch!(values, UInt16Array, UInt16),
DataType::UInt8 => typed_sum_delta_batch!(values, UInt8Array, UInt8),
DataType::Null => ScalarValue::Null,
e => {
return Err(DataFusionError::Internal(format!(
"Sum is not expected to receive the type {:?}",
Expand Down Expand Up @@ -297,6 +298,7 @@ pub(super) fn sum(lhs: &ScalarValue, rhs: &ScalarValue) -> Result<ScalarValue> {
(ScalarValue::Int64(lhs), ScalarValue::Int8(rhs)) => {
typed_sum!(lhs, rhs, Int64, i64)
}
(ScalarValue::Null, ScalarValue::Null) => ScalarValue::Null,
e => {
return Err(DataFusionError::Internal(format!(
"Sum is not expected to receive a scalar {:?}",
Expand Down Expand Up @@ -334,8 +336,8 @@ mod tests {
use super::*;
use crate::expressions::col;
use crate::generic_test_op;
use arrow::datatypes::*;
use arrow::record_batch::RecordBatch;
use arrow::{array::*, datatypes::*};
use datafusion_common::Result;

#[test]
Expand Down Expand Up @@ -532,6 +534,12 @@ mod tests {
)
}

#[test]
fn sum_null_type() -> Result<()> {
let a: ArrayRef = Arc::new(NullArray::new(5));
generic_test_op!(a, DataType::Null, Sum, ScalarValue::Null, DataType::Null)
}

fn aggregate(
batch: &RecordBatch,
agg: Arc<dyn AggregateExpr>,
Expand Down
Loading