@@ -290,6 +290,59 @@ impl Patches {
290290 } )
291291 }
292292
293+ /// Reconstruct patches from serialized metadata and their child arrays.
294+ ///
295+ /// Preserves the number of patches sliced off within the first chunk. Metadata without this
296+ /// field defaults to zero for compatibility with older arrays.
297+ pub fn from_metadata (
298+ array_len : usize ,
299+ metadata : & PatchesMetadata ,
300+ indices : ArrayRef ,
301+ values : ArrayRef ,
302+ chunk_offsets : Option < ArrayRef > ,
303+ ctx : & mut ExecutionCtx ,
304+ ) -> VortexResult < Self > {
305+ let mut patches = Self :: new (
306+ array_len,
307+ metadata. offset ( ) ?,
308+ indices,
309+ values,
310+ chunk_offsets,
311+ ctx,
312+ ) ?;
313+ if let Some ( chunk_offsets) = & patches. chunk_offsets {
314+ let offset_within_chunk = usize:: try_from ( metadata. offset_within_chunk ( ) ) ?;
315+ let sliced_rows = patches. offset % PATCH_CHUNK_SIZE ;
316+ vortex_ensure ! (
317+ offset_within_chunk <= sliced_rows,
318+ "Patch offset within chunk exceeds the sliced row count"
319+ ) ;
320+
321+ // A wrong offset can assign first-chunk patches to a later chunk during decoding.
322+ // Check the boundary against the indices, not just the original chunk's patch count.
323+ if chunk_offsets. len ( ) > 1 && chunk_offsets. is_host ( ) && patches. indices . is_host ( ) {
324+ let first_chunk_end = patches
325+ . offset
326+ . checked_add ( PATCH_CHUNK_SIZE - sliced_rows)
327+ . ok_or_else ( || vortex_err ! ( "Patch chunk boundary overflows usize" ) ) ?;
328+ let retained =
329+ search_index_binary_search ( & patches. indices , first_chunk_end, ctx) ?. to_index ( ) ;
330+ let expected_offset = patches
331+ . chunk_offset_at ( 1 , ctx) ?
332+ . checked_sub ( patches. chunk_offset_at ( 0 , ctx) ?)
333+ . and_then ( |count| count. checked_sub ( retained) )
334+ . ok_or_else ( || vortex_err ! ( "Invalid patch chunk offsets" ) ) ?;
335+ vortex_ensure_eq ! (
336+ offset_within_chunk,
337+ expected_offset,
338+ "Patch offset within chunk disagrees with the patch indices"
339+ ) ;
340+ }
341+ patches. offset_within_chunk = Some ( offset_within_chunk) ;
342+ }
343+ Ok ( patches)
344+ }
345+
293346 /// Construct new patches without validating any of the arguments
294347 ///
295348 /// # Safety
@@ -1354,6 +1407,7 @@ fn take_indices_with_search_fn<
13541407
13551408#[ cfg( test) ]
13561409mod test {
1410+ use std:: ops:: Range ;
13571411 use std:: sync:: LazyLock ;
13581412
13591413 use rstest:: rstest;
@@ -1369,13 +1423,103 @@ mod test {
13691423 use crate :: IntoArray ;
13701424 use crate :: VortexSessionExecute ;
13711425 use crate :: assert_arrays_eq;
1426+ use crate :: dtype:: PType ;
13721427 use crate :: patches:: Patches ;
1428+ use crate :: patches:: PatchesMetadata ;
13731429 use crate :: patches:: PrimitiveArray ;
13741430 use crate :: search_sorted:: SearchResult ;
13751431 use crate :: validity:: Validity ;
13761432
13771433 static SESSION : LazyLock < VortexSession > = LazyLock :: new ( VortexSession :: empty) ;
13781434
1435+ #[ rstest]
1436+ #[ case:: unchunked( false ) ]
1437+ #[ case:: chunked( true ) ]
1438+ fn test_from_metadata_without_offset_within_chunk ( #[ case] chunked : bool ) -> VortexResult < ( ) > {
1439+ let mut ctx = SESSION . create_execution_ctx ( ) ;
1440+ let metadata = PatchesMetadata :: new (
1441+ 3 ,
1442+ 0 ,
1443+ PType :: U32 ,
1444+ chunked. then_some ( 3 ) ,
1445+ chunked. then_some ( PType :: U32 ) ,
1446+ None ,
1447+ ) ;
1448+ let patches = Patches :: from_metadata (
1449+ 3072 ,
1450+ & metadata,
1451+ buffer ! [ 7u32 , 1029 , 2051 ] . into_array ( ) ,
1452+ buffer ! [ 11i32 , 22 , 33 ] . into_array ( ) ,
1453+ chunked. then ( || buffer ! [ 0u32 , 1 , 2 ] . into_array ( ) ) ,
1454+ & mut ctx,
1455+ ) ?;
1456+
1457+ assert_eq ! ( patches. offset_within_chunk( ) , chunked. then_some( 0 ) ) ;
1458+ assert_eq ! (
1459+ patches. search_index( 1029 , & mut ctx) ?,
1460+ SearchResult :: Found ( 1 )
1461+ ) ;
1462+ Ok ( ( ) )
1463+ }
1464+
1465+ #[ rstest]
1466+ fn test_from_metadata_rejects_inconsistent_chunk_offset (
1467+ #[ values( 0 , 2 , 3 , 501 , usize :: MAX ) ] offset_within_chunk : usize ,
1468+ ) {
1469+ let mut ctx = SESSION . create_execution_ctx ( ) ;
1470+ // The original first chunk had patches at 100 and 500; slicing at 500 removed one.
1471+ let metadata = PatchesMetadata :: new (
1472+ 2 ,
1473+ 500 ,
1474+ PType :: U32 ,
1475+ Some ( 2 ) ,
1476+ Some ( PType :: U32 ) ,
1477+ Some ( offset_within_chunk) ,
1478+ ) ;
1479+ let error = Patches :: from_metadata (
1480+ 1000 ,
1481+ & metadata,
1482+ buffer ! [ 500u32 , 1200 ] . into_array ( ) ,
1483+ buffer ! [ 11i32 , 22 ] . into_array ( ) ,
1484+ Some ( buffer ! [ 0u32 , 2 ] . into_array ( ) ) ,
1485+ & mut ctx,
1486+ )
1487+ . expect_err ( "inconsistent patch offset must be rejected" ) ;
1488+ assert ! ( error. to_string( ) . contains( "Patch offset within chunk" ) ) ;
1489+ }
1490+
1491+ #[ rstest]
1492+ #[ case:: single_chunk( 900 ..901 ) ]
1493+ #[ case:: later_single_chunk( 1500 ..1501 ) ]
1494+ #[ case:: multiple_chunks( 900 ..2050 ) ]
1495+ fn test_from_metadata_short_slice ( #[ case] range : Range < usize > ) -> VortexResult < ( ) > {
1496+ let mut ctx = SESSION . create_execution_ctx ( ) ;
1497+ let indices = PrimitiveArray :: from_iter ( 0u32 ..3072 ) . into_array ( ) ;
1498+ let patches = Patches :: new (
1499+ 3072 ,
1500+ 0 ,
1501+ indices. clone ( ) ,
1502+ indices,
1503+ Some ( buffer ! [ 0u32 , 1024 , 2048 ] . into_array ( ) ) ,
1504+ & mut ctx,
1505+ ) ?;
1506+ let sliced = patches. slice ( range, & mut ctx) ?. expect ( "slice has patches" ) ;
1507+ let metadata = sliced. to_metadata ( sliced. array_len ( ) , sliced. dtype ( ) ) ?;
1508+ let read = Patches :: from_metadata (
1509+ sliced. array_len ( ) ,
1510+ & metadata,
1511+ sliced. indices ( ) . clone ( ) ,
1512+ sliced. values ( ) . clone ( ) ,
1513+ sliced. chunk_offsets ( ) . clone ( ) ,
1514+ & mut ctx,
1515+ ) ?;
1516+
1517+ assert_eq ! ( read. offset_within_chunk( ) , sliced. offset_within_chunk( ) ) ;
1518+ assert_eq ! ( read. search_index( 0 , & mut ctx) ?, SearchResult :: Found ( 0 ) ) ;
1519+ assert_arrays_eq ! ( read. values( ) , sliced. values( ) , & mut ctx) ;
1520+ Ok ( ( ) )
1521+ }
1522+
13791523 #[ test]
13801524 fn test_filter ( ) {
13811525 let mut ctx = SESSION . create_execution_ctx ( ) ;
0 commit comments