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

Introduce return type for aggregate sum #8141

Merged
merged 4 commits into from
Nov 14, 2023
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
26 changes: 15 additions & 11 deletions datafusion/physical-expr/src/aggregate/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ use datafusion_expr::Accumulator;
#[derive(Debug, Clone)]
pub struct Sum {
name: String,
// The DataType for the input expression
data_type: DataType,
// The DataType for the final sum
return_type: DataType,
expr: Arc<dyn PhysicalExpr>,
nullable: bool,
}
Expand All @@ -53,11 +56,12 @@ impl Sum {
name: impl Into<String>,
data_type: DataType,
) -> Self {
let data_type = sum_return_type(&data_type).unwrap();
let return_type = sum_return_type(&data_type).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self {
name: name.into(),
expr,
data_type,
return_type,
expr,
nullable: true,
}
}
Expand All @@ -70,13 +74,13 @@ impl Sum {
/// `s` is a `Sum`, `helper` is a macro accepting (ArrowPrimitiveType, DataType)
macro_rules! downcast_sum {
($s:ident, $helper:ident) => {
match $s.data_type {
DataType::UInt64 => $helper!(UInt64Type, $s.data_type),
DataType::Int64 => $helper!(Int64Type, $s.data_type),
DataType::Float64 => $helper!(Float64Type, $s.data_type),
DataType::Decimal128(_, _) => $helper!(Decimal128Type, $s.data_type),
DataType::Decimal256(_, _) => $helper!(Decimal256Type, $s.data_type),
_ => not_impl_err!("Sum not supported for {}: {}", $s.name, $s.data_type),
match $s.return_type {
DataType::UInt64 => $helper!(UInt64Type, $s.return_type),
DataType::Int64 => $helper!(Int64Type, $s.return_type),
DataType::Float64 => $helper!(Float64Type, $s.return_type),
DataType::Decimal128(_, _) => $helper!(Decimal128Type, $s.return_type),
DataType::Decimal256(_, _) => $helper!(Decimal256Type, $s.return_type),
_ => not_impl_err!("Sum not supported for {}: {}", $s.name, $s.return_type),
}
};
}
Expand All @@ -91,7 +95,7 @@ impl AggregateExpr for Sum {
fn field(&self) -> Result<Field> {
Ok(Field::new(
&self.name,
self.data_type.clone(),
self.return_type.clone(),
self.nullable,
))
}
Expand All @@ -108,7 +112,7 @@ impl AggregateExpr for Sum {
fn state_fields(&self) -> Result<Vec<Field>> {
Ok(vec![Field::new(
format_state_name(&self.name, "sum"),
self.data_type.clone(),
self.return_type.clone(),
self.nullable,
)])
}
Expand Down
11 changes: 7 additions & 4 deletions datafusion/physical-expr/src/aggregate/sum_distinct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ use datafusion_expr::Accumulator;
pub struct DistinctSum {
/// Column name
name: String,
/// The DataType for the final sum
// The DataType for the input expression
data_type: DataType,
// The DataType for the final sum
return_type: DataType,
/// The input arguments, only contains 1 item for sum
exprs: Vec<Arc<dyn PhysicalExpr>>,
}
Expand All @@ -53,10 +55,11 @@ impl DistinctSum {
name: String,
data_type: DataType,
) -> Self {
let data_type = sum_return_type(&data_type).unwrap();
let return_type = sum_return_type(&data_type).unwrap();
Self {
name,
data_type,
return_type,
exprs,
}
}
Expand All @@ -68,14 +71,14 @@ impl AggregateExpr for DistinctSum {
}

fn field(&self) -> Result<Field> {
Ok(Field::new(&self.name, self.data_type.clone(), true))
Ok(Field::new(&self.name, self.return_type.clone(), true))
}

fn state_fields(&self) -> Result<Vec<Field>> {
// State field is a List which stores items to rebuild hash set.
Ok(vec![Field::new_list(
format_state_name(&self.name, "sum distinct"),
Field::new("item", self.data_type.clone(), true),
Field::new("item", self.return_type.clone(), true),
false,
)])
}
Expand Down