yashdotrv commented on code in PR #27129:
URL: https://github.com/apache/datafusion/pull/27129#discussion_r4701835570
##########
datafusion/physical-plan/src/aggregates/clustered_final_stream.rs:
##########
@@ -768,4 +789,149 @@ mod tests {
let limit = held_memory(&plan).await?;
assert_resources_exhausted_under_limit(&plan, limit).await
}
+
+ /// The table stays small: only the incoming batch can exceed the limit.
+ #[tokio::test]
+ async fn clustered_final_refuses_large_state_batch() -> Result<()> {
+ let schema = Arc::new(Schema::new(vec![
+ Field::new("k", DataType::Utf8, 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();
+ // 64 rows of 16 KB string keys (1 MB), like the few rows of a
`median` state.
+ let key_str = "a".repeat(16 * 1024);
+ let keys: ArrayRef = Arc::new(StringArray::from_iter_values(
+ (0..64).map(|_| key_str.as_str()),
+ ));
+ let values: ArrayRef = Arc::new(Int64Array::from(vec![1_i64; 64]));
+ let batch = RecordBatch::try_new(Arc::clone(&state), vec![keys,
values])?;
+ let ordering =
+ LexOrdering::new([PhysicalSortExpr::new_default(col("k",
&state)?)]).unwrap();
+ let input = TestMemoryExec::try_new(&[vec![batch]],
Arc::clone(&state), None)?
+ .try_with_sort_information(vec![ordering])?;
+ let plan = AggregateExec::try_new(
+ AggregateMode::Final,
+ group_by.as_final(),
+ vec![aggregate],
+ vec![None],
+ Arc::new(TestMemoryExec::update_cache(&Arc::new(input))),
+ state,
+ )?;
+ assert_eq!(plan.group_clustering_mode(), &GroupClusteringMode::Full);
+ assert_resources_exhausted_under_limit(&plan, 512 * 1024).await?;
+ assert_output_unchanged_under_limit(&plan, 4 * 1024 * 1024).await
+ }
+
+ /// Spilling the table makes room for a state batch that did not fit next
to it.
+ #[tokio::test]
+ async fn clustered_final_spills_to_admit_large_state_batch() -> Result<()>
{
+ let schema = Arc::new(Schema::new(vec![
+ Field::new("k1", DataType::Int64, false),
+ Field::new("k2", DataType::Utf8, false),
+ Field::new("v", DataType::Int64, false),
+ ]));
+ let group_by = PhysicalGroupBy::new_single(vec![
+ (col("k1", &schema)?, "k1".into()),
+ (col("k2", &schema)?, "k2".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();
+ let regular_batch = {
+ let k1: ArrayRef = Arc::new(Int64Array::from(vec![0_i64; 4000]));
+ let k2: ArrayRef = Arc::new(StringArray::from_iter_values(
+ (0..4000).map(|i| format!("k_{i:04}")),
+ ));
+ let v: ArrayRef = Arc::new(Int64Array::from(vec![1_i64; 4000]));
+ RecordBatch::try_new(Arc::clone(&state), vec![k1, k2, v])?
+ };
+ // 64 rows of 32 KB string keys (2 MB), like the few rows of a
`median` state.
+ let large_batch = {
+ let large_str = "x".repeat(32 * 1024);
+ let k1: ArrayRef = Arc::new(Int64Array::from(vec![0_i64; 64]));
+ let k2: ArrayRef = Arc::new(StringArray::from_iter_values(
+ (0..64).map(|i| format!("large_{i:02}_{large_str}")),
+ ));
+ let v: ArrayRef = Arc::new(Int64Array::from(vec![1_i64; 64]));
+ RecordBatch::try_new(Arc::clone(&state), vec![k1, k2, v])?
+ };
+ let ordering =
+ LexOrdering::new([PhysicalSortExpr::new_default(col("k1",
&state)?)])
+ .unwrap();
+ let input = TestMemoryExec::try_new(
+ &[vec![regular_batch, large_batch]],
+ Arc::clone(&state),
+ None,
+ )?
+ .try_with_sort_information(vec![ordering])?;
+ let plan = Arc::new(AggregateExec::try_new(
+ AggregateMode::Final,
+ group_by.as_final(),
+ vec![aggregate],
+ vec![None],
+ Arc::new(TestMemoryExec::update_cache(&Arc::new(input))),
+ state,
+ )?);
+ assert_eq!(
+ plan.group_clustering_mode(),
+ &GroupClusteringMode::Partial(vec![0])
+ );
+
+ // Fits the large batch, but not next to the table of 4000 groups.
+ let runtime = RuntimeEnvBuilder::new()
+ .with_memory_pool(Arc::new(GreedyMemoryPool::new(2600 * 1024)))
Review Comment:
done!
--
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]