66//!
77//! [`OnPairArray`]: crate::OnPairArray
88
9+ use std:: mem:: MaybeUninit ;
910use std:: sync:: Arc ;
1011
1112use num_traits:: AsPrimitive ;
13+ use onpair:: CompactDictionaryView ;
1214use vortex_array:: ArrayRef ;
1315use vortex_array:: ArrayView ;
1416use vortex_array:: ExecutionCtx ;
@@ -23,8 +25,8 @@ use vortex_buffer::Buffer;
2325use vortex_buffer:: ByteBuffer ;
2426use vortex_buffer:: ByteBufferMut ;
2527use vortex_error:: VortexResult ;
28+ use vortex_error:: vortex_bail;
2629use vortex_error:: vortex_ensure;
27- use vortex_error:: vortex_err;
2830
2931use crate :: OnPair ;
3032use crate :: OnPairArraySlotsExt ;
@@ -44,64 +46,103 @@ pub(super) fn canonicalize_onpair(
4446 } )
4547}
4648
49+ pub ( crate ) struct OnPairDecodePlan < ' a > {
50+ codes : Buffer < u16 > ,
51+ dict : CompactDictionaryView < ' a > ,
52+ /// Per-row uncompressed lengths, zero for null rows.
53+ pub ( crate ) lengths : PrimitiveArray ,
54+ /// Total decoded size, i.e. the sum of `lengths`.
55+ pub ( crate ) total_size : usize ,
56+ }
57+
58+ impl < ' a > OnPairDecodePlan < ' a > {
59+ pub ( crate ) fn new ( array : ArrayView < ' a , OnPair > , ctx : & mut ExecutionCtx ) -> VortexResult < Self > {
60+ let lengths = array
61+ . uncompressed_lengths ( )
62+ . clone ( )
63+ . execute :: < PrimitiveArray > ( ctx) ?;
64+
65+ let total_size: usize = match_each_integer_ptype ! ( lengths. ptype( ) , |P | {
66+ lengths
67+ . as_slice:: <P >( )
68+ . iter( )
69+ . map( |& l| AsPrimitive :: <usize >:: as_( l) )
70+ . sum( )
71+ } ) ;
72+
73+ // `codes_offsets` holds the per-row code boundaries and may itself be a
74+ // sliced or filtered view of the original. Its first and last entries
75+ // bound the contiguous run of `codes` belonging to the rows present in
76+ // this array: `slice` keeps the full `codes` child and only narrows
77+ // `codes_offsets` (so `code_start > 0` and/or `code_end < codes.len()`),
78+ // while `filter` rebuilds both children so the window is the whole stream.
79+ // OnPair has no `TakeExecute`, so a reordering take is served from the
80+ // canonical `VarBinView` and never reaches this path. We only need those
81+ // two boundaries, so point-look them up rather than decoding every offset.
82+ let codes_offsets = array. codes_offsets ( ) ;
83+ let code_start = code_boundary_at ( codes_offsets, 0 , ctx) ?;
84+ let code_end = code_boundary_at ( codes_offsets, array. len ( ) , ctx) ?;
85+ vortex_ensure ! (
86+ code_start <= code_end,
87+ "OnPair codes_offsets must be nondecreasing"
88+ ) ;
89+ vortex_ensure ! (
90+ code_end <= array. codes( ) . len( ) ,
91+ "OnPair codes_offsets end {} exceeds codes len {}" ,
92+ code_end,
93+ array. codes( ) . len( )
94+ ) ;
95+
96+ // Slice the `codes` child to that window *before* unpacking it, so a sliced
97+ // array materialises only its own codes rather than the whole column's. The
98+ // contiguous decoder walks `codes` in order and never reads the per-row
99+ // boundaries, so an empty boundary slice is sound.
100+ let codes = collect_widened :: < u16 > ( & array. codes ( ) . slice ( code_start..code_end) ?, ctx) ?;
101+ let dict = dict_view ( array, ctx) ?;
102+
103+ Ok ( Self {
104+ codes,
105+ dict,
106+ lengths,
107+ total_size,
108+ } )
109+ }
110+
111+ /// Bulk-decodes the whole code stream into `out`, which must hold at least `total_size` bytes.
112+ ///
113+ /// Do not reach for `#[inline(always)]` here. `try_decode_into` is monomorphized for
114+ /// `CompactDictionaryView` whichever way this is annotated, and it is far too large to inline
115+ /// either way — it stays an out-of-line call in both. All `always` buys is dropping this
116+ /// wrapper's own frame, one `bl`/`ret` per decoded chunk against a whole-column decode, which
117+ /// `benches/decode.rs::canonicalize_to_varbinview` cannot tell apart from noise.
118+ #[ inline]
119+ pub ( crate ) fn decode_into ( & self , out : & mut [ MaybeUninit < u8 > ] ) -> VortexResult < usize > {
120+ let written = match onpair:: try_decode_into ( self . codes . as_slice ( ) , self . dict , out) {
121+ Ok ( written) => written,
122+ Err ( _) => {
123+ vortex_bail ! ( "OnPair codes decode to more bytes than uncompressed_lengths records" )
124+ }
125+ } ;
126+
127+ vortex_ensure ! (
128+ written == self . total_size,
129+ "OnPair codes decoded to {written} bytes but uncompressed_lengths records {}" ,
130+ self . total_size
131+ ) ;
132+ Ok ( written)
133+ }
134+ }
135+
47136pub ( crate ) fn onpair_decode_bytes (
48137 array : ArrayView < ' _ , OnPair > ,
49138 ctx : & mut ExecutionCtx ,
50139) -> VortexResult < ( ByteBufferMut , PrimitiveArray ) > {
51- let lengths = array
52- . uncompressed_lengths ( )
53- . clone ( )
54- . execute :: < PrimitiveArray > ( ctx) ?;
55-
56- let total_size: usize = match_each_integer_ptype ! ( lengths. ptype( ) , |P | {
57- lengths
58- . as_slice:: <P >( )
59- . iter( )
60- . map( |& l| AsPrimitive :: <usize >:: as_( l) )
61- . sum( )
62- } ) ;
63-
64- // `codes_offsets` holds the per-row code boundaries and may itself be a
65- // sliced or filtered view of the original. Its first and last entries
66- // bound the contiguous run of `codes` belonging to the rows present in
67- // this array: `slice` keeps the full `codes` child and only narrows
68- // `codes_offsets` (so `code_start > 0` and/or `code_end < codes.len()`),
69- // while `filter` rebuilds both children so the window is the whole stream.
70- // OnPair has no `TakeExecute`, so a reordering take is served from the
71- // canonical `VarBinView` and never reaches this path. We only need those
72- // two boundaries, so point-look them up rather than decoding every offset.
73- let codes_offsets = array. codes_offsets ( ) ;
74- let code_start = code_boundary_at ( codes_offsets, 0 , ctx) ?;
75- let code_end = code_boundary_at ( codes_offsets, array. len ( ) , ctx) ?;
76- vortex_ensure ! (
77- code_start <= code_end,
78- "OnPair codes_offsets must be nondecreasing"
79- ) ;
80- vortex_ensure ! (
81- code_end <= array. codes( ) . len( ) ,
82- "OnPair codes_offsets end {} exceeds codes len {}" ,
83- code_end,
84- array. codes( ) . len( )
85- ) ;
86-
87- // Slice the `codes` child to that window *before* unpacking it, so a sliced
88- // array materialises only its own codes rather than the whole column's. The
89- // contiguous decoder walks `codes` in order and never reads the per-row
90- // boundaries, so an empty boundary slice is sound.
91- let codes = collect_widened :: < u16 > ( & array. codes ( ) . slice ( code_start..code_end) ?, ctx) ?;
92- let dict = dict_view ( array, ctx) ?;
93- let mut out_bytes = ByteBufferMut :: with_capacity ( total_size) ;
94- let written = onpair:: try_decode_into ( codes. as_slice ( ) , dict, out_bytes. spare_capacity_mut ( ) )
95- . map_err ( |_| {
96- vortex_err ! ( "OnPair codes decode to more bytes than uncompressed_lengths records" )
97- } ) ?;
98- vortex_ensure ! (
99- written == total_size,
100- "OnPair codes decoded to {written} bytes but uncompressed_lengths records {total_size}"
101- ) ;
102- // SAFETY: `try_decode_into` initialised exactly `written` bytes.
140+ let plan = OnPairDecodePlan :: new ( array, ctx) ?;
141+ let mut out_bytes = ByteBufferMut :: with_capacity ( plan. total_size ) ;
142+ let written = plan. decode_into ( out_bytes. spare_capacity_mut ( ) ) ?;
143+ // SAFETY: `decode_into` initialised exactly `written` bytes.
103144 unsafe { out_bytes. set_len ( written) } ;
104- Ok ( ( out_bytes, lengths) )
145+ Ok ( ( out_bytes, plan . lengths ) )
105146}
106147
107148pub ( crate ) fn onpair_decode_views (
0 commit comments