Skip to content

Apache DataFusion SQL Query Engine

fix: support RunEndEncoded arguments in MIN/MAX (#26248)

David Greven9dec77584ea3
diff --git a/datafusion/functions-aggregate/src/min_max.rs b/datafusion/functions-aggregate/src/min_max.rs
index 0cf2837f5e..e12adad906 100644
--- a/datafusion/functions-aggregate/src/min_max.rs
+++ b/datafusion/functions-aggregate/src/min_max.rs
@@ -59,6 +59,9 @@ fn get_min_max_result_type(input_types: &[DataType]) -> Result<Vec<DataType>> {
// TODO add checker, if the value type is complex data type
Ok(vec![dict_value_type.deref().clone()])
}
+ DataType::RunEndEncoded(_, values) => {
+ get_min_max_result_type(&[values.data_type().clone()])
+ }
// TODO add checker for datatype which min and max supported
// For example, the `Struct` and `Map` type are not supported in the MIN and MAX function
_ => Ok(input_types.to_vec()),
@@ -1507,6 +1510,26 @@ mod tests {
Ok(())
}
+ #[test]
+ fn test_get_min_max_return_type_coerce_run_end_encoded() -> Result<()> {
+ let run_ends = Arc::new(Field::new("run_ends", DataType::Int32, false));
+ let ree = |values: DataType| {
+ DataType::RunEndEncoded(
+ Arc::clone(&run_ends),
+ Arc::new(Field::new("values", values, true)),
+ )
+ };
+
+ let result = get_min_max_result_type(&[ree(DataType::UInt32)])?;
+ assert_eq!(result, vec![DataType::UInt32]);
+
+ let dict =
+ DataType::Dictionary(Box::new(DataType::UInt32), Box::new(DataType::Utf8));
+ let result = get_min_max_result_type(&[ree(dict)])?;
+ assert_eq!(result, vec![DataType::Utf8]);
+ Ok(())
+ }
+
#[test]
fn test_min_max_dictionary() -> Result<()> {
let values = StringArray::from(vec!["b", "c", "a", "🦀", "d"]);
diff --git a/datafusion/sqllogictest/test_files/run_end_encoded.slt b/datafusion/sqllogictest/test_files/run_end_encoded.slt
index cbac587b17..f94e7635a3 100644
--- a/datafusion/sqllogictest/test_files/run_end_encoded.slt
+++ b/datafusion/sqllogictest/test_files/run_end_encoded.slt
@@ -147,3 +147,56 @@ SELECT
) AS windowed_sum
FROM sensor_readings
ORDER BY temperature;
+
+# MIN / MAX over REE columns go through the same planning path as SQL users hit,
+# so the coerced argument type comes from get_min_max_result_type.
+statement ok
+CREATE TABLE ree_readings AS
+SELECT
+ sensor_id,
+ arrow_cast(arrow_cast(temperature, 'UInt32'), 'RunEndEncoded("run_ends": non-null Int32, "values": UInt32)') AS temp_u32
+FROM sensor_readings;
+
+# Ungrouped MIN / MAX over a REE integer column
+query II
+SELECT MIN(temp_u32), MAX(temp_u32) FROM ree_readings;
+----
+20 24
+
+# MIN / MAX return the REE column's value type, as they do for Dictionary
+query TT
+SELECT arrow_typeof(MIN(temp_u32)), arrow_typeof(MAX(temp_u32)) FROM ree_readings;
+----
+UInt32 UInt32
+
+# Grouped MIN / MAX over a REE integer column
+query ?II rowsort
+SELECT sensor_id, MIN(temp_u32), MAX(temp_u32)
+FROM ree_readings
+GROUP BY sensor_id;
+----
+sensor_A 22 24
+sensor_B 20 20
+
+# MAX as a window function over a REE column
+query ?I rowsort
+SELECT sensor_id, MAX(temp_u32) OVER (PARTITION BY sensor_id)
+FROM ree_readings;
+----
+sensor_A 24
+sensor_A 24
+sensor_A 24
+sensor_B 20
+
+# MIN / MAX over a REE column whose values are Dictionary-encoded
+query TT
+SELECT MAX(arrow_cast(arrow_cast(sensor_id, 'Dictionary(UInt32, Utf8)'),
+ 'RunEndEncoded("run_ends": non-null Int32, "values": Dictionary(UInt32, Utf8))')),
+ arrow_typeof(MAX(arrow_cast(arrow_cast(sensor_id, 'Dictionary(UInt32, Utf8)'),
+ 'RunEndEncoded("run_ends": non-null Int32, "values": Dictionary(UInt32, Utf8))')))
+FROM sensor_readings;
+----
+sensor_B Utf8
+
+statement ok
+DROP TABLE ree_readings;