@@ -62,6 +62,7 @@ use datafusion::{
6262use datafusion_comet_common:: decode_string_arrays;
6363use datafusion_comet_proto:: spark_expression:: agg_expr:: ExprStruct as AggExprStruct ;
6464use datafusion_comet_proto:: spark_operator:: { AggregateMode , Operator , ShuffleScan } ;
65+ use datafusion_comet_shuffle:: decoded_batch_coalescer:: DecodedBatchCoalescer ;
6566use datafusion_comet_spark_expr:: url_funcs:: { CometParseUrl , CometTryParseUrl } ;
6667use datafusion_spark:: function:: array:: array_contains:: SparkArrayContains ;
6768use datafusion_spark:: function:: array:: repeat:: SparkArrayRepeat ;
@@ -2165,6 +2166,137 @@ pub unsafe extern "system" fn Java_org_apache_comet_Native_decodeShuffleBlock(
21652166 } )
21662167}
21672168
2169+ /// Decode a local frame into owned arrays. The direct buffer can be reused after this call.
2170+ fn append_shuffle_batch (
2171+ coalescer : & mut DecodedBatchCoalescer ,
2172+ block : & [ u8 ] ,
2173+ num_columns : usize ,
2174+ ) -> CometResult < ( ) > {
2175+ let batch = read_ipc_compressed ( block) ?;
2176+ if batch. num_columns ( ) != num_columns {
2177+ return Err ( CometError :: Internal (
2178+ "Shuffle frame column count mismatch" . into ( ) ,
2179+ ) ) ;
2180+ }
2181+ coalescer. push ( batch) ?;
2182+ Ok ( ( ) )
2183+ }
2184+
2185+ /// The reader owns this handle and serializes native calls with cleanup.
2186+ /// # Safety
2187+ /// A nonzero handle must refer to a live coalescer exclusively borrowed for this call.
2188+ unsafe fn shuffle_batch_coalescer < ' a > ( handle : jlong ) -> CometResult < & ' a mut DecodedBatchCoalescer > {
2189+ unsafe { ( handle as * mut DecodedBatchCoalescer ) . as_mut ( ) }
2190+ . ok_or_else ( || CometError :: Internal ( "Closed shuffle batch coalescer" . into ( ) ) )
2191+ }
2192+
2193+ fn shuffle_batch_ready_columns ( coalescer : & DecodedBatchCoalescer ) -> jint {
2194+ coalescer
2195+ . peek_ready ( )
2196+ . map_or ( -1 , |batch| batch. num_columns ( ) as jint )
2197+ }
2198+
2199+ #[ no_mangle]
2200+ pub extern "system" fn Java_org_apache_comet_Native_createShuffleBatchCoalescer (
2201+ e : EnvUnowned ,
2202+ _class : JClass ,
2203+ max_rows : jint ,
2204+ ) -> jlong {
2205+ try_unwrap_or_throw ( & e, |_| {
2206+ let coalescer = DecodedBatchCoalescer :: try_new ( max_rows. max ( 0 ) as usize ) ?;
2207+ Ok ( Box :: into_raw ( Box :: new ( coalescer) ) as jlong )
2208+ } )
2209+ }
2210+
2211+ #[ no_mangle]
2212+ /// Decode and retain one local frame, returning ready columns or -1 for more input.
2213+ /// # Safety
2214+ /// The handle must be live and exclusively owned by this reader. On error, close the reader.
2215+ pub unsafe extern "system" fn Java_org_apache_comet_Native_appendShuffleBatch (
2216+ e : EnvUnowned ,
2217+ _class : JClass ,
2218+ handle : jlong ,
2219+ byte_buffer : JByteBuffer ,
2220+ length : jint ,
2221+ num_columns : jint ,
2222+ tracing_enabled : jboolean ,
2223+ ) -> jint {
2224+ try_unwrap_or_throw ( & e, |env| {
2225+ with_trace ( "appendShuffleBatch" , tracing_enabled != JNI_FALSE , || {
2226+ if num_columns < 0 {
2227+ return Err ( CometError :: Internal ( "Invalid shuffle frame bounds" . into ( ) ) ) ;
2228+ }
2229+ let block = unsafe { shuffle_frame_bytes ( env, & byte_buffer, length) ? } ;
2230+ let coalescer = unsafe { shuffle_batch_coalescer ( handle) ? } ;
2231+ append_shuffle_batch ( coalescer, block, num_columns as usize ) ?;
2232+ Ok ( shuffle_batch_ready_columns ( coalescer) )
2233+ } )
2234+ } )
2235+ }
2236+
2237+ #[ no_mangle]
2238+ /// Report ready output, flushing the pending group at EOF.
2239+ /// # Safety
2240+ /// The handle must be live and exclusively owned by this reader.
2241+ pub unsafe extern "system" fn Java_org_apache_comet_Native_peekShuffleBatch (
2242+ e : EnvUnowned ,
2243+ _class : JClass ,
2244+ handle : jlong ,
2245+ end_of_input : jboolean ,
2246+ tracing_enabled : jboolean ,
2247+ ) -> jint {
2248+ try_unwrap_or_throw ( & e, |_| {
2249+ let coalescer = unsafe { shuffle_batch_coalescer ( handle) ? } ;
2250+ if end_of_input != JNI_FALSE {
2251+ with_trace (
2252+ "coalesceShuffleBatches" ,
2253+ tracing_enabled != JNI_FALSE ,
2254+ || coalescer. finish ( ) ,
2255+ ) ?;
2256+ }
2257+ Ok ( shuffle_batch_ready_columns ( coalescer) )
2258+ } )
2259+ }
2260+
2261+ #[ no_mangle]
2262+ /// Transfer one ready output using the ordinary export path, including offset repair.
2263+ /// # Safety
2264+ /// The handle and Arrow FFI output addresses must be live and exclusively owned by this reader.
2265+ /// Both address arrays must have the column count reported by `peekShuffleBatch`.
2266+ pub unsafe extern "system" fn Java_org_apache_comet_Native_takeShuffleBatch (
2267+ e : EnvUnowned ,
2268+ _class : JClass ,
2269+ handle : jlong ,
2270+ array_addrs : JLongArray ,
2271+ schema_addrs : JLongArray ,
2272+ tracing_enabled : jboolean ,
2273+ ) -> jlong {
2274+ try_unwrap_or_throw ( & e, |env| {
2275+ with_trace ( "exportShuffleBatch" , tracing_enabled != JNI_FALSE , || {
2276+ let batch = unsafe { shuffle_batch_coalescer ( handle) ? }
2277+ . take_ready ( )
2278+ . ok_or_else ( || CometError :: Internal ( "No decoded shuffle batch is ready" . into ( ) ) ) ?;
2279+ prepare_output ( env, array_addrs, schema_addrs, batch, false )
2280+ } )
2281+ } )
2282+ }
2283+
2284+ #[ no_mangle]
2285+ /// # Safety
2286+ /// A nonzero handle must be live, must not be in use, and must be released exactly once.
2287+ pub unsafe extern "system" fn Java_org_apache_comet_Native_closeShuffleBatchCoalescer (
2288+ e : EnvUnowned ,
2289+ _class : JClass ,
2290+ handle : jlong ,
2291+ ) {
2292+ try_unwrap_or_throw ( & e, |_| {
2293+ if handle != 0 {
2294+ drop ( unsafe { Box :: from_raw ( handle as * mut DecodedBatchCoalescer ) } ) ;
2295+ }
2296+ Ok ( ( ) )
2297+ } )
2298+ }
2299+
21682300#[ no_mangle]
21692301/// Parse the expected schema once for a remote shuffle iterator.
21702302///
@@ -2251,6 +2383,22 @@ pub unsafe extern "system" fn Java_org_apache_comet_Native_decodeShuffleBlockWit
22512383 } )
22522384}
22532385
2386+ /// Borrows a codec header and IPC payload from a direct buffer.
2387+ /// # Safety
2388+ /// The buffer's backing memory must remain valid and unmodified for the borrow.
2389+ unsafe fn shuffle_frame_bytes < ' a > (
2390+ env : & mut Env ,
2391+ byte_buffer : & ' a JByteBuffer < ' _ > ,
2392+ length : jint ,
2393+ ) -> CometResult < & ' a [ u8 ] > {
2394+ let capacity = env. get_direct_buffer_capacity ( byte_buffer) ?;
2395+ if length < 4 || length as usize > capacity {
2396+ return Err ( CometError :: Internal ( "Invalid shuffle frame bounds" . into ( ) ) ) ;
2397+ }
2398+ let pointer = env. get_direct_buffer_address ( byte_buffer) ?;
2399+ Ok ( unsafe { std:: slice:: from_raw_parts ( pointer, length as usize ) } )
2400+ }
2401+
22542402/// Converts the JNI arguments of the `decodeShuffleBlock` entry points for
22552403/// [`decode_shuffle_block`].
22562404fn decode_shuffle_block_jni (
@@ -2261,9 +2409,7 @@ fn decode_shuffle_block_jni(
22612409 schema_addrs : JLongArray ,
22622410 expected_types : Option < & [ ArrowDataType ] > ,
22632411) -> CometResult < jlong > {
2264- let raw_pointer = env. get_direct_buffer_address ( & byte_buffer) ?;
2265- let length = length as usize ;
2266- let block: & [ u8 ] = unsafe { std:: slice:: from_raw_parts ( raw_pointer, length) } ;
2412+ let block = unsafe { shuffle_frame_bytes ( env, & byte_buffer, length) ? } ;
22672413 let array_addrs = unsafe { array_addrs. get_elements ( env, ReleaseMode :: NoCopyBack ) ? } ;
22682414 let schema_addrs = unsafe { schema_addrs. get_elements ( env, ReleaseMode :: NoCopyBack ) ? } ;
22692415 unsafe { decode_shuffle_block ( block, & array_addrs, & schema_addrs, expected_types) }
@@ -3398,6 +3544,36 @@ mod tests {
33983544 Ok ( ( rows, arrow:: array:: make_array ( data) ) )
33993545 }
34003546
3547+ #[ test]
3548+ fn append_shuffle_batches_owns_decoded_input_and_preserves_values ( ) {
3549+ let input = int_batch ( ) ;
3550+ let mut block = shuffle_block ( & input) ;
3551+ let mut coalescer = DecodedBatchCoalescer :: try_new ( 16 ) . unwrap ( ) ;
3552+ append_shuffle_batch ( & mut coalescer, & block, 1 ) . unwrap ( ) ;
3553+ assert_eq ! ( shuffle_batch_ready_columns( & coalescer) , -1 ) ;
3554+ // The JVM reuses its direct input buffer before the pending group is exported.
3555+ block. fill ( 0 ) ;
3556+ append_shuffle_batch ( & mut coalescer, & shuffle_block ( & input) , 1 ) . unwrap ( ) ;
3557+ coalescer. finish ( ) . unwrap ( ) ;
3558+ assert_eq ! ( shuffle_batch_ready_columns( & coalescer) , 1 ) ;
3559+ let output = coalescer. take_ready ( ) . unwrap ( ) ;
3560+ assert_eq ! ( output. num_rows( ) , 6 ) ;
3561+ assert_eq ! ( output. slice( 0 , 3 ) , input) ;
3562+ assert_eq ! ( output. slice( 3 , 3 ) , input) ;
3563+ }
3564+
3565+ #[ test]
3566+ fn append_shuffle_batch_rejects_frame_count_mismatch ( ) {
3567+ let mut coalescer = DecodedBatchCoalescer :: try_new ( 16 ) . unwrap ( ) ;
3568+ let error =
3569+ append_shuffle_batch ( & mut coalescer, & shuffle_block ( & int_batch ( ) ) , 2 ) . unwrap_err ( ) ;
3570+ assert ! ( error
3571+ . to_string( )
3572+ . contains( "Shuffle frame column count mismatch" ) ) ;
3573+ coalescer. finish ( ) . unwrap ( ) ;
3574+ assert ! ( coalescer. take_ready( ) . is_none( ) ) ;
3575+ }
3576+
34013577 #[ test]
34023578 fn decode_shuffle_block_exports_columns ( ) {
34033579 let batch = int_batch ( ) ;
0 commit comments