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


##########
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:
   Thanks @SteNicholas and @JingsongLi. In e6cb08fb, columns that `interleave` 
expands per element, MAP and lists of non-primitive items, are copied slice by 
slice with `MutableArrayData` instead. Other columns still go through 
`interleave`. In your fixture (127 published keys, one new key with a 
500,000-entry `MAP<INT, INT>`) the 8,000,000-byte allocation is gone, and the 
compaction allocates 169.5 MB in total, against 177.2 MB on the previous commit 
and 174.1 MB on main. The cost is the per-slice copy: when all 65,536 keys 
change and each holds a 16-entry map, compaction takes 749–774 ms, against 
684–694 ms on the previous commit and 1168–1199 ms on main. 
`lookup_changelog_copies_a_large_map_row` runs the large map through compaction 
and an incremental read, and 
`test_detached_nested_changelog_rows_own_their_entries` checks that the copy no 
longer shares the source entries.
   



-- 
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