jayzhan211 commented on code in PR #27129:
URL: https://github.com/apache/datafusion/pull/27129#discussion_r4570479143


##########
datafusion/physical-plan/src/aggregates/mod.rs:
##########
@@ -217,6 +217,24 @@ mod skip_partial;
 mod spill;
 mod topk;
 
+/// Batches passing between operators are bounded by `batch_size` rows and are
+/// not charged to the memory pool. A partial-state batch is charged only when
+/// its rows carry more than this many bytes on average, which means a row 
holds
+/// many input values (`median`, `array_agg`), so its size follows the input.
+const MAX_IN_FLIGHT_STATE_BYTES_PER_ROW: usize = 1024;

Review Comment:
   Removing the per-row rule (charging every batch over 64 KiB again) fails no 
test, so the fix for the many-`sum` regression from the first round isn't 
pinned. The test below covers it: a full 8192-row batch of two `Int64` states 
(128 KB) in a 96 KB pool passes now and fails without the rule.
   
   <details>
   <summary>Test for `aggregate_stream.rs` (also needs `AsArray` in the 
`arrow::array` import)</summary>
   
   ```rs
   /// A full batch of fixed-width states is bounded by `batch_size`, so it is
   /// not charged even when it is larger than the pool.
   #[tokio::test]
   async fn final_aggregate_does_not_charge_bounded_state_batch() -> Result<()> 
{
       let schema = Arc::new(Schema::new(vec![Field::new("v", DataType::Int64, 
false)]));
       let aggregates = vec![
           sum_aggregate(&schema, "v", "a")?,
           sum_aggregate(&schema, "v", "b")?,
       ];
       let empty = TestMemoryExec::try_new_exec(&[vec![]], Arc::clone(&schema), 
None)?;
       let state = AggregateExec::try_new(
           AggregateMode::Partial,
           PhysicalGroupBy::default(),
           aggregates.clone(),
           vec![None, None],
           empty,
           Arc::clone(&schema),
       )?
       .schema();
       // 8192 rows of two `Int64` states: 128 KB, 16 bytes per row.
       let values = || Arc::new(Int64Array::from(vec![1_i64; 8192])) as 
ArrayRef;
       let batch = RecordBatch::try_new(Arc::clone(&state), vec![values(), 
values()])?;
       let input = TestMemoryExec::try_new_exec(&[vec![batch]], 
Arc::clone(&state), None)?;
       let plan = Arc::new(AggregateExec::try_new(
           AggregateMode::Final,
           PhysicalGroupBy::default(),
           aggregates,
           vec![None, None],
           input,
           state,
       )?);
       let runtime = RuntimeEnvBuilder::new()
           .with_memory_pool(Arc::new(GreedyMemoryPool::new(96 * 1024)))
           .build_arc()?;
       let context = Arc::new(TaskContext::default().with_runtime(runtime));
       let output = collect(plan as Arc<dyn ExecutionPlan>, context).await?;
       let result = output[0]
           .column(0)
           .as_primitive::<arrow::datatypes::Int64Type>();
       assert_eq!(result.value(0), 8192);
       Ok(())
   }
   ```
   
   </details>
   



##########
datafusion/physical-plan/src/aggregates/mod.rs:
##########
@@ -217,6 +217,24 @@ mod skip_partial;
 mod spill;
 mod topk;
 
+/// Batches passing between operators are bounded by `batch_size` rows and are
+/// not charged to the memory pool. A partial-state batch is charged only when
+/// its rows carry more than this many bytes on average, which means a row 
holds
+/// many input values (`median`, `array_agg`), so its size follows the input.
+const MAX_IN_FLIGHT_STATE_BYTES_PER_ROW: usize = 1024;
+
+/// Below this size a batch is never charged, whatever its row count.
+const MIN_CHARGED_STATE_BATCH_BYTES: usize = 64 * 1024;
+
+fn incoming_state_batch_reservation_bytes(batch: &RecordBatch) -> usize {
+    let bytes = 
datafusion_common::utils::memory::get_record_batch_memory_size(batch);
+    let in_flight_bytes = batch
+        .num_rows()
+        .saturating_mul(MAX_IN_FLIGHT_STATE_BYTES_PER_ROW)
+        .max(MIN_CHARGED_STATE_BATCH_BYTES);
+    if bytes > in_flight_bytes { bytes } else { 0 }
+}

Review Comment:
   This charges the whole buffer behind a zero-copy slice, so a grouped query 
without a hash repartition now fails: with 500k groups and 
`repartition_aggregations = false`, main passes 4/4 at 60 MB and 80 MB, and 
this branch fails 4/4. The partial aggregate emits 8192-row slices of one 
output batch that it still reserves, and each slice is charged the whole 8 MB 
again. Charging only the bytes the rows reference fixes it (`SpilledRowSizes` 
already measures them, including `List` children) and still refuses the issue's 
`median` and `array_agg` queries:
   
   ```suggestion
   fn incoming_state_batch_reservation_bytes(batch: &RecordBatch) -> usize {
       let in_flight_bytes = batch
           .num_rows()
           .saturating_mul(MAX_IN_FLIGHT_STATE_BYTES_PER_ROW)
           .max(MIN_CHARGED_STATE_BATCH_BYTES);
       let buffer_bytes =
           
datafusion_common::utils::memory::get_record_batch_memory_size(batch);
       if buffer_bytes <= in_flight_bytes {
           return 0;
       }
       // A zero-copy slice of a larger output shares buffers that its producer
       // still reserves, so charge only the bytes its rows reference.
       let sizes = crate::spill::spilled_row_sizes::SpilledRowSizes::new(batch);
       let bytes = (0..batch.num_rows())
           .map(|row| sizes.row(row))
           .fold(0usize, usize::saturating_add);
       if bytes > in_flight_bytes { bytes } else { 0 }
   }
   ```
   
   The five new tests fake a `median` state with a 64-row slice of a 1 MB 
array, which this now exempts. They need rows that reference the bytes 
themselves, such as 64 rows of 16 KB `Utf8` keys or a one-row `List`.
   
   <details>
   <summary>Repro query, and a test that fails on this branch and passes with 
the fix</summary>
   
   ```sh
   datafusion-cli -m 60m \
     -c "SET datafusion.execution.target_partitions = 4;" \
     -c "SET datafusion.optimizer.repartition_aggregations = false;" \
     -c "SET datafusion.execution.skip_partial_aggregation_probe_rows_threshold 
= 1000000000;" \
     -c "SELECT count(*), sum(s1) FROM (SELECT v % 500000 AS k, sum(v) s1, 
sum(v + 1) s2, sum(v + 2) s3 FROM generate_series(1, 16000000) t(v) GROUP BY 
k);"
   ```
   
   In `hash_stream.rs` tests:
   
   ```rs
   /// Slices of a larger partial output share buffers that the partial
   /// aggregate still reserves, so only their rows are charged.
   #[tokio::test]
   async fn final_hash_does_not_charge_buffers_shared_by_slices() -> Result<()> 
{
       let schema = Arc::new(Schema::new(vec![
           Field::new("k", DataType::Int64, false),
           Field::new("v", DataType::Int64, false),
       ]));
       let group_by = PhysicalGroupBy::new_single(vec![(col("k", &schema)?, 
"k".into())]);
       let aggregate = Arc::new(
           AggregateExprBuilder::new(sum_udaf(), vec![col("v", &schema)?])
               .schema(Arc::clone(&schema))
               .alias("sum")
               .build()?,
       );
       let empty = TestMemoryExec::try_new_exec(&[vec![]], Arc::clone(&schema), 
None)?;
       let state = AggregateExec::try_new(
           AggregateMode::Partial,
           group_by.clone(),
           vec![Arc::clone(&aggregate)],
           vec![None],
           empty,
           Arc::clone(&schema),
       )?
       .schema();
       // A partial output of 1M groups (16 MB) emitted as 8192-row slices.
       let rows = 1 << 20;
       let output = RecordBatch::try_new(
           Arc::clone(&state),
           vec![
               Arc::new(Int64Array::from((0..rows).collect::<Vec<_>>())) as 
ArrayRef,
               Arc::new(Int64Array::from(vec![1_i64; rows as usize])),
           ],
       )?;
       let slices = vec![output.slice(0, 8192), output.slice(8192, 8192)];
       let input = TestMemoryExec::try_new_exec(&[slices], Arc::clone(&state), 
None)?;
       let plan = Arc::new(AggregateExec::try_new(
           AggregateMode::Final,
           group_by.as_final(),
           vec![aggregate],
           vec![None],
           input,
           Arc::clone(&state),
       )?);
       let runtime = RuntimeEnvBuilder::new()
           .with_memory_pool(Arc::new(GreedyMemoryPool::new(4 * 1024 * 1024)))
           .build_arc()?;
       let context = Arc::new(
           TaskContext::default()
               .with_session_config(SessionConfig::new().set_bool(
                   "datafusion.execution.enable_migration_aggregate",
                   true,
               ))
               .with_runtime(runtime),
       );
       let output = collect(plan.execute(0, context)?).await?;
       let rows: usize = output.iter().map(|b| b.num_rows()).sum();
       assert_eq!(rows, 16384);
       Ok(())
   }
   ```
   
   </details>
   



##########
datafusion/physical-plan/src/aggregates/aggregate_stream.rs:
##########
@@ -386,6 +387,22 @@ impl AggregateStream {
             loop {
                 let result = match this.input.next().await {
                     Some(Ok(batch)) => {
+                        // Partial states can own the entire input. Reserve 
the batch
+                        // before merging it, then transfer that reservation 
to the
+                        // accumulator's state below.
+                        let previous_size = this.reservation.size();
+                        let batch_memory =
+                            if this.mode.input_mode() == 
AggregateInputMode::Partial {

Review Comment:
   Optional: removing the `input_mode() == AggregateInputMode::Partial` check 
fails no test, so a `Single` ungrouped aggregate could start charging raw input 
without a test noticing. A test that runs `Single` `count` over 64 rows of 16 
KB `Utf8` values in a 512 KB pool would pin it.
   



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to