gabotechs commented on code in PR #16519:
URL: https://github.com/apache/datafusion/pull/16519#discussion_r2165946809


##########
datafusion/functions-aggregate/src/array_agg.rs:
##########
@@ -341,12 +341,20 @@ impl Accumulator for ArrayAggAccumulator {
             Some(values) => {
                 // Make sure we don't insert empty lists
                 if !values.is_empty() {
-                    self.values.push(values);
+                    // The ArrayRef might be holding a reference to its 
original input buffer, so
+                    // storing it here directly copied/compacted avoids over 
accounting memory
+                    // not used here.
+                    self.values
+                        .push(make_array(copy_array_data(&values.to_data())));
                 }

Review Comment:
   🤔 I'm not sure if this will solve the issue. Keep in mind that the 
`merge_batch` method argument receives the states of other accumulators, which 
already hold "compacted" data, so I'd expect this compaction here to be 
unnecessary.



##########
datafusion/functions-aggregate/src/array_agg.rs:
##########
@@ -994,6 +1002,34 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn does_not_over_account_memory_for_merge() -> Result<()> {
+        let (mut acc1, mut acc2) = 
ArrayAggAccumulatorBuilder::string().build_two()?;
+
+        let a1 = ListArray::from_iter_primitive::<UInt64Type, _, _>(vec![
+            Some(vec![Some(0), Some(1), Some(2)]),
+            Some(vec![Some(3)]),
+            None,
+            Some(vec![Some(4)]),
+        ]);
+        let a2 = ListArray::from_iter_primitive::<UInt64Type, _, _>(vec![
+            Some(vec![Some(0), Some(1), Some(2)]),
+            Some(vec![Some(3)]),
+            None,
+            Some(vec![Some(4)]),
+        ]);
+
+        acc1.merge_batch(&[Arc::new(a1.slice(0, 1))])?;
+        acc2.merge_batch(&[Arc::new(a2.slice(0, 1))])?;
+
+        acc1 = merge(acc1, acc2)?;

Review Comment:
   The `merge_batch` functions do not receive arbitrary data, they receive the 
results of calling `state()` in other accumulators. A fairer test would be to 
do something like:
   
   ```suggestion
           acc1.update_batch(&[Arc::clone(a1.values())])?;
           acc2.update_batch(&[Arc::clone(a2.values())])?;
   
           acc1 = merge(acc1, acc2)?;
   ```
   
   If you run this, you would notice that the test actually passes without your 
changes.



-- 
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: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to