alamb commented on code in PR #17255:
URL: https://github.com/apache/datafusion/pull/17255#discussion_r2294598360
##########
datafusion/sqllogictest/test_files/aggregate.slt:
##########
@@ -7387,3 +7389,58 @@ FROM (VALUES ('a'), ('d'), ('c'), ('a')) t(a_varchar);
query error Error during planning: ORDER BY and WITHIN GROUP clauses cannot be
used together in the same aggregate function
SELECT array_agg(a_varchar order by a_varchar) WITHIN GROUP (ORDER BY
a_varchar)
FROM (VALUES ('a'), ('d'), ('c'), ('a')) t(a_varchar);
+
+# distinct average
+statement ok
+create table distinct_avg (a int, b int) as values
+ (null, null),
+ (1, 1),
+ (2, 2),
+ (3, 3),
+ (4, 4),
Review Comment:
Could you update this test so:
1. The input isn't in order
2. Add a test for floating point values
3. Test for an input that includes at least one null value
4. the values in `b` are different than the values in `b`
##########
datafusion/functions-aggregate/src/average.rs:
##########
@@ -114,79 +115,95 @@ impl AggregateUDFImpl for Avg {
}
fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn
Accumulator>> {
- if acc_args.is_distinct {
- return exec_err!("avg(DISTINCT) aggregations are not available");
- }
+ let data_type = acc_args.exprs[0].data_type(acc_args.schema)?;
use DataType::*;
- let data_type = acc_args.exprs[0].data_type(acc_args.schema)?;
// instantiate specialized accumulator based for the type
- match (&data_type, acc_args.return_field.data_type()) {
- (Float64, Float64) => Ok(Box::<AvgAccumulator>::default()),
- (
- Decimal128(sum_precision, sum_scale),
- Decimal128(target_precision, target_scale),
- ) => Ok(Box::new(DecimalAvgAccumulator::<Decimal128Type> {
- sum: None,
- count: 0,
- sum_scale: *sum_scale,
- sum_precision: *sum_precision,
- target_precision: *target_precision,
- target_scale: *target_scale,
- })),
-
- (
- Decimal256(sum_precision, sum_scale),
- Decimal256(target_precision, target_scale),
- ) => Ok(Box::new(DecimalAvgAccumulator::<Decimal256Type> {
- sum: None,
- count: 0,
- sum_scale: *sum_scale,
- sum_precision: *sum_precision,
- target_precision: *target_precision,
- target_scale: *target_scale,
- })),
-
- (Duration(time_unit), Duration(result_unit)) => {
- Ok(Box::new(DurationAvgAccumulator {
+ if acc_args.is_distinct {
+ match &data_type {
+ // Numeric types are converted to Float64 via
`coerce_avg_type` during logical plan creation
+ Float64 =>
Ok(Box::new(Float64DistinctAvgAccumulator::default())),
+ _ => exec_err!("AVG(DISTINCT) for {} not supported",
data_type),
+ }
+ } else {
+ match (&data_type, acc_args.return_field.data_type()) {
+ (Float64, Float64) => Ok(Box::<AvgAccumulator>::default()),
+ (
+ Decimal128(sum_precision, sum_scale),
+ Decimal128(target_precision, target_scale),
+ ) => Ok(Box::new(DecimalAvgAccumulator::<Decimal128Type> {
sum: None,
count: 0,
- time_unit: *time_unit,
- result_unit: *result_unit,
- }))
- }
+ sum_scale: *sum_scale,
+ sum_precision: *sum_precision,
+ target_precision: *target_precision,
+ target_scale: *target_scale,
+ })),
+
+ (
+ Decimal256(sum_precision, sum_scale),
+ Decimal256(target_precision, target_scale),
+ ) => Ok(Box::new(DecimalAvgAccumulator::<Decimal256Type> {
+ sum: None,
+ count: 0,
+ sum_scale: *sum_scale,
+ sum_precision: *sum_precision,
+ target_precision: *target_precision,
+ target_scale: *target_scale,
+ })),
+
+ (Duration(time_unit), Duration(result_unit)) => {
+ Ok(Box::new(DurationAvgAccumulator {
+ sum: None,
+ count: 0,
+ time_unit: *time_unit,
+ result_unit: *result_unit,
+ }))
+ }
- _ => exec_err!(
- "AvgAccumulator for ({} --> {})",
- &data_type,
- acc_args.return_field.data_type()
- ),
+ _ => exec_err!(
+ "AvgAccumulator for ({} --> {})",
+ &data_type,
+ acc_args.return_field.data_type()
+ ),
+ }
}
}
fn state_fields(&self, args: StateFieldsArgs) -> Result<Vec<FieldRef>> {
- Ok(vec![
- Field::new(
- format_state_name(args.name, "count"),
- DataType::UInt64,
- true,
- ),
- Field::new(
- format_state_name(args.name, "sum"),
- args.input_fields[0].data_type().clone(),
- true,
- ),
- ]
- .into_iter()
- .map(Arc::new)
- .collect())
+ if args.is_distinct {
+ // Copied from
datafusion_functions_aggregate::sum::Sum::state_fields
+ // since the accumulator uses DistinctSumAccumulator internally.
+ Ok(vec![Field::new_list(
+ format_state_name(args.name, "sum distinct"),
Review Comment:
```suggestion
format_state_name(args.name, "avg distinct"),
```
##########
datafusion/functions-aggregate/src/average.rs:
##########
@@ -114,72 +115,88 @@ impl AggregateUDFImpl for Avg {
}
fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn
Accumulator>> {
- if acc_args.is_distinct {
- return exec_err!("avg(DISTINCT) aggregations are not available");
- }
+ let data_type = acc_args.exprs[0].data_type(acc_args.schema)?;
use DataType::*;
- let data_type = acc_args.exprs[0].data_type(acc_args.schema)?;
// instantiate specialized accumulator based for the type
- match (&data_type, acc_args.return_field.data_type()) {
- (Float64, Float64) => Ok(Box::<AvgAccumulator>::default()),
- (
- Decimal128(sum_precision, sum_scale),
- Decimal128(target_precision, target_scale),
- ) => Ok(Box::new(DecimalAvgAccumulator::<Decimal128Type> {
- sum: None,
- count: 0,
- sum_scale: *sum_scale,
- sum_precision: *sum_precision,
- target_precision: *target_precision,
- target_scale: *target_scale,
- })),
-
- (
- Decimal256(sum_precision, sum_scale),
- Decimal256(target_precision, target_scale),
- ) => Ok(Box::new(DecimalAvgAccumulator::<Decimal256Type> {
- sum: None,
- count: 0,
- sum_scale: *sum_scale,
- sum_precision: *sum_precision,
- target_precision: *target_precision,
- target_scale: *target_scale,
- })),
-
- (Duration(time_unit), Duration(result_unit)) => {
- Ok(Box::new(DurationAvgAccumulator {
+ if acc_args.is_distinct {
+ match &data_type {
+ // Numeric types are converted to Float64 via
`coerce_avg_type` during logical plan creation
+ Float64 =>
Ok(Box::new(Float64DistinctAvgAccumulator::default())),
+ _ => exec_err!("AVG(DISTINCT) for {} not supported",
data_type),
+ }
+ } else {
+ match (&data_type, acc_args.return_field.data_type()) {
+ (Float64, Float64) => Ok(Box::<AvgAccumulator>::default()),
+ (
+ Decimal128(sum_precision, sum_scale),
+ Decimal128(target_precision, target_scale),
+ ) => Ok(Box::new(DecimalAvgAccumulator::<Decimal128Type> {
sum: None,
count: 0,
- time_unit: *time_unit,
- result_unit: *result_unit,
- }))
- }
+ sum_scale: *sum_scale,
+ sum_precision: *sum_precision,
+ target_precision: *target_precision,
+ target_scale: *target_scale,
+ })),
+
+ (
+ Decimal256(sum_precision, sum_scale),
+ Decimal256(target_precision, target_scale),
+ ) => Ok(Box::new(DecimalAvgAccumulator::<Decimal256Type> {
+ sum: None,
+ count: 0,
+ sum_scale: *sum_scale,
+ sum_precision: *sum_precision,
+ target_precision: *target_precision,
+ target_scale: *target_scale,
+ })),
+
+ (Duration(time_unit), Duration(result_unit)) => {
+ Ok(Box::new(DurationAvgAccumulator {
+ sum: None,
+ count: 0,
+ time_unit: *time_unit,
+ result_unit: *result_unit,
+ }))
+ }
- _ => exec_err!(
- "AvgAccumulator for ({} --> {})",
- &data_type,
- acc_args.return_field.data_type()
- ),
+ _ => exec_err!(
+ "AvgAccumulator for ({} --> {})",
+ &data_type,
+ acc_args.return_field.data_type()
+ ),
+ }
}
}
fn state_fields(&self, args: StateFieldsArgs) -> Result<Vec<FieldRef>> {
- Ok(vec![
- Field::new(
- format_state_name(args.name, "count"),
- DataType::UInt64,
- true,
- ),
- Field::new(
- format_state_name(args.name, "sum"),
- args.input_fields[0].data_type().clone(),
- true,
- ),
- ]
- .into_iter()
- .map(Arc::new)
- .collect())
+ if args.is_distinct {
+ // Copied from
datafusion_functions_aggregate::sum::Sum::state_fields
+ // since the accumulator uses DistinctSumAccumulator internally.
+ Ok(vec![Field::new_list(
+ format_state_name(args.name, "sum distinct"),
+ Field::new_list_field(args.return_type().clone(), true),
+ false,
+ )
+ .into()])
+ } else {
+ Ok(vec![
+ Field::new(
+ format_state_name(args.name, "count"),
+ DataType::UInt64,
+ true,
+ ),
+ Field::new(
+ format_state_name(args.name, "sum"),
+ args.input_fields[0].data_type().clone(),
+ true,
+ ),
+ ]
+ .into_iter()
+ .map(Arc::new)
+ .collect())
+ }
Review Comment:
I don't really understand this question -- the PR's code looks good to me
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]