Skip to content

Apache DataFusion SQL Query Engine

fix: unwrap nested encodings inside Dictionary arguments to MIN/MAX (#26371)

David Grevene3fe60c8b332
diff --git a/datafusion/functions-aggregate/src/min_max.rs b/datafusion/functions-aggregate/src/min_max.rs
index 03d80dd75b..03368ebc77 100644
--- a/datafusion/functions-aggregate/src/min_max.rs
+++ b/datafusion/functions-aggregate/src/min_max.rs
@@ -58,7 +58,7 @@ fn get_min_max_result_type(input_types: &[DataType]) -> Result<Vec<DataType>> {
match &input_types[0] {
DataType::Dictionary(_, dict_value_type) => {
// TODO add checker, if the value type is complex data type
- Ok(vec![dict_value_type.deref().clone()])
+ get_min_max_result_type(&[dict_value_type.deref().clone()])
}
DataType::RunEndEncoded(_, values) => {
get_min_max_result_type(&[values.data_type().clone()])
@@ -1508,6 +1508,19 @@ mod tests {
DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8));
let result = get_min_max_result_type(&[data_type])?;
assert_eq!(result, vec![DataType::Utf8]);
+
+ let dict = |values: DataType| {
+ DataType::Dictionary(Box::new(DataType::Int32), Box::new(values))
+ };
+ let ree = DataType::RunEndEncoded(
+ Arc::new(Field::new("run_ends", DataType::Int32, false)),
+ Arc::new(Field::new("values", DataType::UInt32, true)),
+ );
+ let result = get_min_max_result_type(&[dict(ree)])?;
+ assert_eq!(result, vec![DataType::UInt32]);
+
+ let result = get_min_max_result_type(&[dict(dict(DataType::UInt32))])?;
+ assert_eq!(result, vec![DataType::UInt32]);
Ok(())
}
diff --git a/datafusion/sqllogictest/test_files/dictionary.slt b/datafusion/sqllogictest/test_files/dictionary.slt
index f831ab73d9..daa27dba86 100644
--- a/datafusion/sqllogictest/test_files/dictionary.slt
+++ b/datafusion/sqllogictest/test_files/dictionary.slt
@@ -651,3 +651,26 @@ GROUP BY region;
NULL 2
east 4
west 4
+
+# MIN/MAX unwrap a dictionary whose values are themselves encoded
+statement ok
+CREATE TABLE dict_encoded_values AS
+SELECT
+ arrow_cast(arrow_cast(arrow_cast(column1, 'UInt32'), 'Dictionary(Int32, UInt32)'),
+ 'Dictionary(Int32, Dictionary(Int32, UInt32))') AS dict_of_dict,
+ arrow_cast(arrow_cast(arrow_cast(column1, 'UInt32'), 'Dictionary(Int32, UInt32)'),
+ 'Dictionary(Int32, RunEndEncoded("run_ends": non-null Int32, "values": UInt32))') AS dict_of_ree
+FROM (VALUES (1), (1), (3)) AS t(column1);
+
+query IIT
+SELECT min(dict_of_dict), max(dict_of_dict), arrow_typeof(max(dict_of_dict)) FROM dict_encoded_values;
+----
+1 3 UInt32
+
+query IIT
+SELECT min(dict_of_ree), max(dict_of_ree), arrow_typeof(max(dict_of_ree)) FROM dict_encoded_values;
+----
+1 3 UInt32
+
+statement ok
+DROP TABLE dict_encoded_values;