@@ -1267,94 +1267,79 @@ mod tests {
12671267 ScalarValue :: Dictionary ( Box :: new ( key_type) , Box :: new ( inner) )
12681268 }
12691269
1270- #[ test]
1271- fn test_min_max_dictionary_without_coercion ( ) -> Result < ( ) > {
1272- let values = StringArray :: from ( vec ! [ "b" , "c" , "a" , "d" ] ) ;
1273- let keys = Int32Array :: from ( vec ! [ Some ( 0 ) , Some ( 1 ) , Some ( 2 ) , Some ( 3 ) ] ) ;
1274- let dict_array =
1275- DictionaryArray :: try_new ( keys, Arc :: new ( values) as ArrayRef ) . unwrap ( ) ;
1276- let dict_array_ref = Arc :: new ( dict_array) as ArrayRef ;
1277- let dict_type = dict_array_ref. data_type ( ) . clone ( ) ;
1270+ fn utf8_dict_scalar ( key_type : DataType , value : & str ) -> ScalarValue {
1271+ dict_scalar ( key_type, ScalarValue :: Utf8 ( Some ( value. to_string ( ) ) ) )
1272+ }
1273+
1274+ fn string_dictionary_batch (
1275+ values : Vec < & str > ,
1276+ keys : Vec < Option < i32 > > ,
1277+ ) -> ArrayRef {
1278+ let values = Arc :: new ( StringArray :: from ( values) ) as ArrayRef ;
1279+ Arc :: new ( DictionaryArray :: try_new ( Int32Array :: from ( keys) , values) . unwrap ( ) )
1280+ as ArrayRef
1281+ }
1282+
1283+ fn assert_dictionary_min_max (
1284+ dict_type : & DataType ,
1285+ batches : & [ ArrayRef ] ,
1286+ expected_min : & str ,
1287+ expected_max : & str ,
1288+ ) -> Result < ( ) > {
1289+ let key_type = match dict_type {
1290+ DataType :: Dictionary ( key_type, _) => key_type. as_ref ( ) . clone ( ) ,
1291+ other => panic ! ( "expected dictionary type, got {other:?}" ) ,
1292+ } ;
12781293
1279- let mut min_acc = MinAccumulator :: try_new ( & dict_type) ?;
1280- min_acc. update_batch ( & [ Arc :: clone ( & dict_array_ref) ] ) ?;
1281- let min_result = min_acc. evaluate ( ) ?;
1294+ let mut min_acc = MinAccumulator :: try_new ( dict_type) ?;
1295+ for batch in batches {
1296+ min_acc. update_batch ( & [ Arc :: clone ( batch) ] ) ?;
1297+ }
12821298 assert_eq ! (
1283- min_result ,
1284- dict_scalar ( DataType :: Int32 , ScalarValue :: Utf8 ( Some ( "a" . to_string ( ) ) ) )
1299+ min_acc . evaluate ( ) ? ,
1300+ utf8_dict_scalar ( key_type . clone ( ) , expected_min )
12851301 ) ;
12861302
1287- let mut max_acc = MaxAccumulator :: try_new ( & dict_type) ?;
1288- max_acc. update_batch ( & [ Arc :: clone ( & dict_array_ref) ] ) ?;
1289- let max_result = max_acc. evaluate ( ) ?;
1290- assert_eq ! (
1291- max_result,
1292- dict_scalar( DataType :: Int32 , ScalarValue :: Utf8 ( Some ( "d" . to_string( ) ) ) )
1293- ) ;
1303+ let mut max_acc = MaxAccumulator :: try_new ( dict_type) ?;
1304+ for batch in batches {
1305+ max_acc. update_batch ( & [ Arc :: clone ( batch) ] ) ?;
1306+ }
1307+ assert_eq ! ( max_acc. evaluate( ) ?, utf8_dict_scalar( key_type, expected_max) ) ;
1308+
12941309 Ok ( ( ) )
12951310 }
12961311
12971312 #[ test]
1298- fn test_min_max_dictionary_with_nulls ( ) -> Result < ( ) > {
1299- let values = StringArray :: from ( vec ! [ "b" , "c" , "a" ] ) ;
1300- let keys = Int32Array :: from ( vec ! [ None , Some ( 0 ) , None , Some ( 1 ) , Some ( 2 ) ] ) ;
1301- let dict_array =
1302- DictionaryArray :: try_new ( keys, Arc :: new ( values) as ArrayRef ) . unwrap ( ) ;
1303- let dict_array_ref = Arc :: new ( dict_array) as ArrayRef ;
1313+ fn test_min_max_dictionary_without_coercion ( ) -> Result < ( ) > {
1314+ let dict_array_ref = string_dictionary_batch (
1315+ vec ! [ "b" , "c" , "a" , "d" ] ,
1316+ vec ! [ Some ( 0 ) , Some ( 1 ) , Some ( 2 ) , Some ( 3 ) ] ,
1317+ ) ;
13041318 let dict_type = dict_array_ref. data_type ( ) . clone ( ) ;
13051319
1306- let mut min_acc = MinAccumulator :: try_new ( & dict_type) ?;
1307- min_acc. update_batch ( & [ Arc :: clone ( & dict_array_ref) ] ) ?;
1308- let min_result = min_acc. evaluate ( ) ?;
1309- assert_eq ! (
1310- min_result,
1311- dict_scalar( DataType :: Int32 , ScalarValue :: Utf8 ( Some ( "a" . to_string( ) ) ) )
1312- ) ;
1320+ assert_dictionary_min_max ( & dict_type, & [ dict_array_ref] , "a" , "d" )
1321+ }
13131322
1314- let mut max_acc = MaxAccumulator :: try_new ( & dict_type) ?;
1315- max_acc. update_batch ( & [ Arc :: clone ( & dict_array_ref) ] ) ?;
1316- let max_result = max_acc. evaluate ( ) ?;
1317- assert_eq ! (
1318- max_result,
1319- dict_scalar( DataType :: Int32 , ScalarValue :: Utf8 ( Some ( "c" . to_string( ) ) ) )
1323+ #[ test]
1324+ fn test_min_max_dictionary_with_nulls ( ) -> Result < ( ) > {
1325+ let dict_array_ref = string_dictionary_batch (
1326+ vec ! [ "b" , "c" , "a" ] ,
1327+ vec ! [ None , Some ( 0 ) , None , Some ( 1 ) , Some ( 2 ) ] ,
13201328 ) ;
1321- Ok ( ( ) )
1329+ let dict_type = dict_array_ref. data_type ( ) . clone ( ) ;
1330+
1331+ assert_dictionary_min_max ( & dict_type, & [ dict_array_ref] , "a" , "c" )
13221332 }
13231333
13241334 #[ test]
13251335 fn test_min_max_dictionary_multi_batch ( ) -> Result < ( ) > {
13261336 let dict_type =
13271337 DataType :: Dictionary ( Box :: new ( DataType :: Int32 ) , Box :: new ( DataType :: Utf8 ) ) ;
1338+ let batch1 =
1339+ string_dictionary_batch ( vec ! [ "b" , "c" ] , vec ! [ Some ( 0 ) , Some ( 1 ) ] ) ;
1340+ let batch2 =
1341+ string_dictionary_batch ( vec ! [ "a" , "d" ] , vec ! [ Some ( 0 ) , Some ( 1 ) ] ) ;
13281342
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- let values2 = StringArray :: from ( vec ! [ "a" , "d" ] ) ;
1336- let keys2 = Int32Array :: from ( vec ! [ Some ( 0 ) , Some ( 1 ) ] ) ;
1337- let batch2 = Arc :: new (
1338- DictionaryArray :: try_new ( keys2, Arc :: new ( values2) as ArrayRef ) . unwrap ( ) ,
1339- ) as ArrayRef ;
1340-
1341- let mut min_acc = MinAccumulator :: try_new ( & dict_type) ?;
1342- min_acc. update_batch ( & [ Arc :: clone ( & batch1) ] ) ?;
1343- min_acc. update_batch ( & [ Arc :: clone ( & batch2) ] ) ?;
1344- let min_result = min_acc. evaluate ( ) ?;
1345- assert_eq ! (
1346- min_result,
1347- dict_scalar( DataType :: Int32 , ScalarValue :: Utf8 ( Some ( "a" . to_string( ) ) ) )
1348- ) ;
1349-
1350- let mut max_acc = MaxAccumulator :: try_new ( & dict_type) ?;
1351- max_acc. update_batch ( & [ Arc :: clone ( & batch1) ] ) ?;
1352- max_acc. update_batch ( & [ Arc :: clone ( & batch2) ] ) ?;
1353- let max_result = max_acc. evaluate ( ) ?;
1354- assert_eq ! (
1355- max_result,
1356- dict_scalar( DataType :: Int32 , ScalarValue :: Utf8 ( Some ( "d" . to_string( ) ) ) )
1357- ) ;
1358- Ok ( ( ) )
1343+ assert_dictionary_min_max ( & dict_type, & [ batch1, batch2] , "a" , "d" )
13591344 }
13601345}
0 commit comments