@@ -621,6 +621,7 @@ mod tests {
621621 use vortex_array:: ArrayContext ;
622622 use vortex_array:: VortexSessionExecute ;
623623 use vortex_array:: arrays:: BoolArray ;
624+ use vortex_array:: arrays:: ChunkedArray ;
624625 use vortex_array:: arrays:: ListArray ;
625626 use vortex_array:: arrays:: PrimitiveArray ;
626627 use vortex_array:: assert_arrays_eq;
@@ -1217,4 +1218,103 @@ mod tests {
12171218 assert_arrays_eq ! ( result, expected, & mut exec_ctx) ;
12181219 Ok ( ( ) )
12191220 }
1221+
1222+ /// Lists `[0, 1, 2]`, `[]`, `[3, 4, 5]`, `[6, 7]`, `[8, 9]`. When nullable, the third is null
1223+ /// and keeps its elements.
1224+ fn create_ten_element_list_array ( nullable : bool ) -> ArrayRef {
1225+ let mut ctx = SESSION . create_execution_ctx ( ) ;
1226+ let validity = if nullable {
1227+ Validity :: Array ( BoolArray :: from_iter ( [ true , true , false , true , true ] ) . into_array ( ) )
1228+ } else {
1229+ Validity :: NonNullable
1230+ } ;
1231+ ListArray :: try_new (
1232+ PrimitiveArray :: from_iter ( 0 ..10i32 ) . into_array ( ) ,
1233+ buffer ! [ 0u32 , 3 , 3 , 6 , 8 , 10 ] . into_array ( ) ,
1234+ validity,
1235+ & mut ctx,
1236+ )
1237+ . expect ( "array is valid" )
1238+ . into_array ( )
1239+ }
1240+
1241+ /// Slices of one list share its whole `elements`, so every chunk but the first starts at a
1242+ /// nonzero offset and every chunk but the last leaves trailing elements unreferenced.
1243+ fn sliced_chunks ( array : & ArrayRef , cuts : & [ usize ] ) -> VortexResult < Vec < ArrayRef > > {
1244+ let mut bounds = vec ! [ 0 ] ;
1245+ bounds. extend_from_slice ( cuts) ;
1246+ bounds. push ( array. len ( ) ) ;
1247+ bounds
1248+ . windows ( 2 )
1249+ . map ( |range| array. slice ( range[ 0 ] ..range[ 1 ] ) )
1250+ . collect ( )
1251+ }
1252+
1253+ fn chunked_flat ( ) -> Arc < dyn LayoutStrategy > {
1254+ Arc :: new ( ChunkedLayoutStrategy :: new ( FlatLayoutStrategy :: default ( ) ) )
1255+ }
1256+
1257+ /// A list strategy that keeps every chunk, so a multi-chunk stream reaches the list writer.
1258+ fn chunked_list_strategy ( ) -> ListLayoutStrategy {
1259+ ListLayoutStrategy :: default ( )
1260+ . with_elements ( chunked_flat ( ) )
1261+ . with_offsets ( chunked_flat ( ) )
1262+ . with_validity ( chunked_flat ( ) )
1263+ }
1264+
1265+ async fn assert_round_trips < S : LayoutStrategy > (
1266+ strategy : & S ,
1267+ chunks : Vec < ArrayRef > ,
1268+ expected : ArrayRef ,
1269+ ) -> VortexResult < ( ) > {
1270+ let chunked = ChunkedArray :: try_new ( chunks, expected. dtype ( ) . clone ( ) ) ?. into_array ( ) ;
1271+ let ( segments, layout, session) = write_layout ( strategy, chunked) . await ?;
1272+ let reader =
1273+ layout. new_reader ( "" . into ( ) , segments, & session, & LayoutReaderContext :: new ( ) ) ?;
1274+
1275+ let len = expected. len ( ) ;
1276+ let expr = root ( ) . bind ( reader. dtype ( ) ) ?;
1277+ let result = reader
1278+ . projection_evaluation ( & ( 0 ..len as u64 ) , & expr, MaskFuture :: new_true ( len) ) ?
1279+ . await ?;
1280+ let mut exec_ctx = session. create_execution_ctx ( ) ;
1281+ assert_arrays_eq ! ( result, expected, & mut exec_ctx) ;
1282+ Ok ( ( ) )
1283+ }
1284+
1285+ #[ rstest]
1286+ #[ case:: one_cut( & [ 2 ] , false ) ]
1287+ #[ case:: two_cuts( & [ 1 , 3 ] , false ) ]
1288+ #[ case:: null_and_empty_at_cuts( & [ 1 , 3 ] , true ) ]
1289+ #[ tokio:: test]
1290+ async fn sliced_list_chunks_round_trip (
1291+ #[ case] cuts : & [ usize ] ,
1292+ #[ case] nullable : bool ,
1293+ ) -> VortexResult < ( ) > {
1294+ let list = create_ten_element_list_array ( nullable) ;
1295+ let chunks = sliced_chunks ( & list, cuts) ?;
1296+ assert_round_trips ( & chunked_list_strategy ( ) , chunks, list) . await
1297+ }
1298+
1299+ #[ tokio:: test]
1300+ async fn sliced_nested_list_chunks_round_trip ( ) -> VortexResult < ( ) > {
1301+ let mut ctx = SESSION . create_execution_ctx ( ) ;
1302+ let inner = ListArray :: try_new (
1303+ PrimitiveArray :: from_iter ( 0 ..12i32 ) . into_array ( ) ,
1304+ buffer ! [ 0u32 , 2 , 2 , 5 , 7 , 9 , 12 ] . into_array ( ) ,
1305+ Validity :: NonNullable ,
1306+ & mut ctx,
1307+ ) ?
1308+ . into_array ( ) ;
1309+ let outer = ListArray :: try_new (
1310+ inner,
1311+ buffer ! [ 0u32 , 2 , 3 , 5 , 6 ] . into_array ( ) ,
1312+ Validity :: Array ( BoolArray :: from_iter ( [ true , false , true , true ] ) . into_array ( ) ) ,
1313+ & mut ctx,
1314+ ) ?
1315+ . into_array ( ) ;
1316+
1317+ let strategy = chunked_list_strategy ( ) . with_elements ( Arc :: new ( chunked_list_strategy ( ) ) ) ;
1318+ assert_round_trips ( & strategy, sliced_chunks ( & outer, & [ 1 , 2 ] ) ?, outer) . await
1319+ }
12201320}
0 commit comments