SteNicholas commented on code in PR #1480:
URL: https://github.com/apache/paimon-rust/pull/1480#discussion_r4667291346


##########
crates/paimon/src/table/sort_merge.rs:
##########
@@ -2013,9 +2020,49 @@ fn sort_merge_stream(
     .boxed())
 }
 
-/// Concatenates per-key changelog batches into one changelog batch.
-fn changelog_batch(schema: &SchemaRef, batches: &[RecordBatch]) -> 
crate::Result<RecordBatch> {
-    arrow_select::concat::concat_batches(schema, batches).map_err(|error| 
Error::DataInvalid {
+/// Copies the pending per-key changelog rows into one chunk that owns its
+/// buffers. A row sliced from a source batch keeps that whole batch alive and
+/// is charged in full by the format writer.
+fn detach_changelog(
+    chunks: &mut Vec<RecordBatch>,
+    pending: &mut Vec<RecordBatch>,
+) -> crate::Result<()> {
+    if pending.is_empty() {
+        return Ok(());
+    }
+    let batches = pending.iter().collect::<Vec<_>>();
+    let indices = pending
+        .iter()
+        .enumerate()
+        .flat_map(|(batch, rows)| (0..rows.num_rows()).map(move |row| (batch, 
row)))
+        .collect::<Vec<_>>();
+    let chunk =
+        arrow_select::interleave::interleave_record_batch(&batches, 
&indices).map_err(|error| {

Review Comment:
   **Avoid allocating an index pair for every nested element when detaching 
changelog rows.** Arrow 59.3.2 expands each selected `MAP` row into a 
`Vec<(usize, usize)>` with one entry per map item. This adds 16 bytes per item 
on 64-bit builds before the child buffers are copied. The same expansion 
applies to lists with non-primitive children. The scratch allocation happens 
before the batch reaches the writer’s memory reservation.
   
   In an Arrow-level reproduction of these two materialization paths, a 
singleton `MAP<INT, INT>` with 500,000 entries produced the same ~4 MB output, 
but peak additional allocation grew from **5.0 MB with `take` + concat to 12.0 
MB with interleave**. Could we use a range-based copy for nested columns, or 
retain the previous `take` path for them, and cover a large map in the 
regression tests?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to