@@ -1270,4 +1270,86 @@ mod tests {
12701270 assert_eq ! ( max_result, ScalarValue :: Utf8 ( Some ( "🦀" . to_string( ) ) ) ) ;
12711271 Ok ( ( ) )
12721272 }
1273+
1274+ fn dict_scalar ( key_type : DataType , inner : ScalarValue ) -> ScalarValue {
1275+ ScalarValue :: Dictionary ( Box :: new ( key_type) , Box :: new ( inner) )
1276+ }
1277+
1278+ #[ test]
1279+ fn test_min_max_dictionary_without_coercion ( ) -> Result < ( ) > {
1280+ let values = StringArray :: from ( vec ! [ "b" , "c" , "a" , "d" ] ) ;
1281+ let keys = Int32Array :: from ( vec ! [ Some ( 0 ) , Some ( 1 ) , Some ( 2 ) , Some ( 3 ) ] ) ;
1282+ let dict_array =
1283+ DictionaryArray :: try_new ( keys, Arc :: new ( values) as ArrayRef ) . unwrap ( ) ;
1284+ let dict_array_ref = Arc :: new ( dict_array) as ArrayRef ;
1285+
1286+ // Pass the raw Dictionary type — no get_min_max_result_type() unwrap.
1287+ let dict_type = dict_array_ref. data_type ( ) . clone ( ) ;
1288+
1289+ let mut min_acc = MinAccumulator :: try_new ( & dict_type) ?;
1290+ min_acc. update_batch ( & [ Arc :: clone ( & dict_array_ref) ] ) ?;
1291+ let min_result = min_acc. evaluate ( ) ?;
1292+ assert_eq ! ( min_result, dict_scalar( DataType :: Int32 , ScalarValue :: Utf8 ( Some ( "a" . to_string( ) ) ) ) ) ;
1293+
1294+ let mut max_acc = MaxAccumulator :: try_new ( & dict_type) ?;
1295+ max_acc. update_batch ( & [ Arc :: clone ( & dict_array_ref) ] ) ?;
1296+ let max_result = max_acc. evaluate ( ) ?;
1297+ assert_eq ! ( max_result, dict_scalar( DataType :: Int32 , ScalarValue :: Utf8 ( Some ( "d" . to_string( ) ) ) ) ) ;
1298+ Ok ( ( ) )
1299+ }
1300+
1301+ #[ test]
1302+ fn test_min_max_dictionary_with_nulls ( ) -> Result < ( ) > {
1303+ let values = StringArray :: from ( vec ! [ "b" , "c" , "a" ] ) ;
1304+ let keys = Int32Array :: from ( vec ! [ None , Some ( 0 ) , None , Some ( 1 ) , Some ( 2 ) ] ) ;
1305+ let dict_array =
1306+ DictionaryArray :: try_new ( keys, Arc :: new ( values) as ArrayRef ) . unwrap ( ) ;
1307+ let dict_array_ref = Arc :: new ( dict_array) as ArrayRef ;
1308+
1309+ let dict_type = dict_array_ref. data_type ( ) . clone ( ) ;
1310+
1311+ let mut min_acc = MinAccumulator :: try_new ( & dict_type) ?;
1312+ min_acc. update_batch ( & [ Arc :: clone ( & dict_array_ref) ] ) ?;
1313+ let min_result = min_acc. evaluate ( ) ?;
1314+ assert_eq ! ( min_result, dict_scalar( DataType :: Int32 , ScalarValue :: Utf8 ( Some ( "a" . to_string( ) ) ) ) ) ;
1315+
1316+ let mut max_acc = MaxAccumulator :: try_new ( & dict_type) ?;
1317+ max_acc. update_batch ( & [ Arc :: clone ( & dict_array_ref) ] ) ?;
1318+ let max_result = max_acc. evaluate ( ) ?;
1319+ assert_eq ! ( max_result, dict_scalar( DataType :: Int32 , ScalarValue :: Utf8 ( Some ( "c" . to_string( ) ) ) ) ) ;
1320+ Ok ( ( ) )
1321+ }
1322+
1323+ #[ test]
1324+ fn test_min_max_dictionary_multi_batch ( ) -> Result < ( ) > {
1325+ let dict_type =
1326+ DataType :: Dictionary ( Box :: new ( DataType :: Int32 ) , Box :: new ( DataType :: Utf8 ) ) ;
1327+
1328+ // First batch.
1329+ let values1 = StringArray :: from ( vec ! [ "b" , "c" ] ) ;
1330+ let keys1 = Int32Array :: from ( vec ! [ Some ( 0 ) , Some ( 1 ) ] ) ;
1331+ let batch1 = Arc :: new (
1332+ DictionaryArray :: try_new ( keys1, Arc :: new ( values1) as ArrayRef ) . unwrap ( ) ,
1333+ ) as ArrayRef ;
1334+
1335+ // Second batch with a new min and max.
1336+ let values2 = StringArray :: from ( vec ! [ "a" , "d" ] ) ;
1337+ let keys2 = Int32Array :: from ( vec ! [ Some ( 0 ) , Some ( 1 ) ] ) ;
1338+ let batch2 = Arc :: new (
1339+ DictionaryArray :: try_new ( keys2, Arc :: new ( values2) as ArrayRef ) . unwrap ( ) ,
1340+ ) as ArrayRef ;
1341+
1342+ let mut min_acc = MinAccumulator :: try_new ( & dict_type) ?;
1343+ min_acc. update_batch ( & [ Arc :: clone ( & batch1) ] ) ?;
1344+ min_acc. update_batch ( & [ Arc :: clone ( & batch2) ] ) ?;
1345+ let min_result = min_acc. evaluate ( ) ?;
1346+ assert_eq ! ( min_result, dict_scalar( DataType :: Int32 , ScalarValue :: Utf8 ( Some ( "a" . to_string( ) ) ) ) ) ;
1347+
1348+ let mut max_acc = MaxAccumulator :: try_new ( & dict_type) ?;
1349+ max_acc. update_batch ( & [ Arc :: clone ( & batch1) ] ) ?;
1350+ max_acc. update_batch ( & [ Arc :: clone ( & batch2) ] ) ?;
1351+ let max_result = max_acc. evaluate ( ) ?;
1352+ assert_eq ! ( max_result, dict_scalar( DataType :: Int32 , ScalarValue :: Utf8 ( Some ( "d" . to_string( ) ) ) ) ) ;
1353+ Ok ( ( ) )
1354+ }
12731355}
0 commit comments