Apache DataFusion SQL Query Engine
Sum and Avg should be able to take inputs of type Dictionary (#6197)
Liang-Chi Hsieh6e996e7eab5f
diff --git a/datafusion/core/tests/sqllogictests/test_files/aggregate.slt b/datafusion/core/tests/sqllogictests/test_files/aggregate.sltindex 9998e766f7..a9b6552931 100644--- a/datafusion/core/tests/sqllogictests/test_files/aggregate.slt+++ b/datafusion/core/tests/sqllogictests/test_files/aggregate.slt@@ -1643,3 +1643,74 @@ NULL 1 10.1 10.1 10.1 10.1 0 NULL# Restore the default dialectstatement okset datafusion.sql_parser.dialect = 'Generic';++# Prepare the table with dictionary values for testing+statement ok+CREATE TABLE value(x bigint) AS VALUES (1), (2), (3), (1), (3), (4), (5), (2);++statement ok+CREATE TABLE value_dict AS SELECT arrow_cast(x, 'Dictionary(Int64, Int32)') AS x_dict FROM value;++query ?+select x_dict from value_dict;+----+1+2+3+1+3+4+5+2++query I+select sum(x_dict) from value_dict;+----+21++query R+select avg(x_dict) from value_dict;+----+2.625++query I+select min(x_dict) from value_dict;+----+1++query I+select max(x_dict) from value_dict;+----+5++query I+select sum(x_dict) from value_dict where x_dict > 3;+----+9++query R+select avg(x_dict) from value_dict where x_dict > 3;+----+4.5++query I+select min(x_dict) from value_dict where x_dict > 3;+----+4++query I+select max(x_dict) from value_dict where x_dict > 3;+----+5++query error DataFusion error: External error: Arrow error: Invalid argument error: RowConverter column schema mismatch, expected Int64 got Dictionary\(Int64, Int64\)+select sum(x_dict) from value_dict group by x_dict % 2;++query error DataFusion error: External error: Arrow error: Invalid argument error: RowConverter column schema mismatch, expected Int64 got Dictionary\(Int64, Int64\)+select avg(x_dict) from value_dict group by x_dict % 2;++query error DataFusion error: External error: Arrow error: Invalid argument error: RowConverter column schema mismatch, expected Int64 got Dictionary\(Int64, Int64\)+select min(x_dict) from value_dict group by x_dict % 2;++query error DataFusion error: External error: Arrow error: Invalid argument error: RowConverter column schema mismatch, expected Int64 got Dictionary\(Int64, Int64\)+select max(x_dict) from value_dict group by x_dict % 2;diff --git a/datafusion/expr/src/type_coercion/aggregates.rs b/datafusion/expr/src/type_coercion/aggregates.rsindex cc57c72b02..26498ed5b6 100644--- a/datafusion/expr/src/type_coercion/aggregates.rs+++ b/datafusion/expr/src/type_coercion/aggregates.rs@@ -313,6 +313,9 @@ pub fn sum_return_type(arg_type: &DataType) -> Result<DataType> {let new_precision = DECIMAL128_MAX_PRECISION.min(*precision + 10);Ok(DataType::Decimal128(new_precision, *scale))}+ DataType::Dictionary(_, dict_value_type) => {+ sum_return_type(dict_value_type.as_ref())+ }other => Err(DataFusionError::Plan(format!("SUM does not support type \"{other:?}\""))),@@ -374,6 +377,9 @@ pub fn avg_return_type(arg_type: &DataType) -> Result<DataType> {Ok(DataType::Decimal128(new_precision, new_scale))}arg_type if NUMERICS.contains(arg_type) => Ok(DataType::Float64),+ DataType::Dictionary(_, dict_value_type) => {+ avg_return_type(dict_value_type.as_ref())+ }other => Err(DataFusionError::Plan(format!("AVG does not support {other:?}"))),@@ -389,6 +395,9 @@ pub fn avg_sum_type(arg_type: &DataType) -> Result<DataType> {Ok(DataType::Decimal128(new_precision, *scale))}arg_type if NUMERICS.contains(arg_type) => Ok(DataType::Float64),+ DataType::Dictionary(_, dict_value_type) => {+ avg_sum_type(dict_value_type.as_ref())+ }other => Err(DataFusionError::Plan(format!("AVG does not support {other:?}"))),@@ -396,19 +405,29 @@ pub fn avg_sum_type(arg_type: &DataType) -> Result<DataType> {}pub fn is_sum_support_arg_type(arg_type: &DataType) -> bool {- matches!(- arg_type,- arg_type if NUMERICS.contains(arg_type)- || matches!(arg_type, DataType::Decimal128(_, _))- )+ match arg_type {+ DataType::Dictionary(_, dict_value_type) => {+ is_sum_support_arg_type(dict_value_type.as_ref())+ }+ _ => matches!(+ arg_type,+ arg_type if NUMERICS.contains(arg_type)+ || matches!(arg_type, DataType::Decimal128(_, _))+ ),+ }}pub fn is_avg_support_arg_type(arg_type: &DataType) -> bool {- matches!(- arg_type,- arg_type if NUMERICS.contains(arg_type)- || matches!(arg_type, DataType::Decimal128(_, _))- )+ match arg_type {+ DataType::Dictionary(_, dict_value_type) => {+ is_avg_support_arg_type(dict_value_type.as_ref())+ }+ _ => matches!(+ arg_type,+ arg_type if NUMERICS.contains(arg_type)+ || matches!(arg_type, DataType::Decimal128(_, _))+ ),+ }}pub fn is_variance_support_arg_type(arg_type: &DataType) -> bool {