yashdotrv commented on code in PR #27129:
URL: https://github.com/apache/datafusion/pull/27129#discussion_r4701829676
##########
datafusion/physical-plan/src/aggregates/hash_stream.rs:
##########
@@ -1843,4 +1866,232 @@ mod tests {
);
Ok(())
}
+
+ #[tokio::test]
+ async fn final_hash_reserves_incoming_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 partial = AggregateExec::try_new(
+ AggregateMode::Partial,
+ group_by.clone(),
+ vec![Arc::clone(&aggregate)],
+ vec![None],
+ empty,
+ Arc::clone(&schema),
+ )?;
+ let state_schema = partial.schema();
+ // 64 rows that reference 1 MB of string keys, 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_schema), vec![keys,
values])?;
+ let input = TestMemoryExec::try_new_exec(
+ &[vec![batch]],
+ Arc::clone(&state_schema),
+ None,
+ )?;
+ let final_aggregate = Arc::new(AggregateExec::try_new(
+ AggregateMode::Final,
+ group_by.as_final(),
+ vec![aggregate],
+ vec![None],
+ input,
+ state_schema,
+ )?);
+ assert_eq!(
+ final_aggregate.group_clustering_mode(),
+ &GroupClusteringMode::None
+ );
+
+ let config = SessionConfig::new()
+ .set_bool("datafusion.execution.enable_migration_aggregate", true);
+ let runtime = RuntimeEnvBuilder::new()
+ .with_memory_pool(Arc::new(GreedyMemoryPool::new(64 * 1024)))
+ .build_arc()?;
+ let context = Arc::new(
+ TaskContext::default()
+ .with_session_config(config.clone())
+ .with_runtime(runtime),
+ );
+ let err = collect(final_aggregate.execute(0, context)?)
+ .await
+ .unwrap_err();
+ assert!(matches!(
+ err.find_root(),
+ DataFusionError::ResourcesExhausted(_)
+ ));
+
+ let runtime = RuntimeEnvBuilder::new()
+ .with_memory_pool(Arc::new(GreedyMemoryPool::new(3 * 1024 * 1024)))
+ .build_arc()?;
+ let context = Arc::new(
+ TaskContext::default()
+ .with_session_config(config)
+ .with_runtime(runtime),
+ );
+ let output = collect(final_aggregate.execute(0, context)?).await?;
+ let result = output[0]
+ .column(1)
+ .as_any()
+ .downcast_ref::<Int64Array>()
+ .unwrap();
+ assert_eq!(result.value(0), 64);
+ Ok(())
+ }
+
+ /// Spilling the table makes room for a state batch that did not fit next
to it.
+ #[tokio::test]
+ async fn final_hash_spills_to_admit_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();
+ let regular_batch = {
+ let keys: ArrayRef = Arc::new(StringArray::from_iter_values(
+ (0..4000).map(|i| format!("k_{i:04}")),
+ ));
+ let values: ArrayRef = Arc::new(Int64Array::from(vec![1_i64;
4000]));
+ RecordBatch::try_new(Arc::clone(&state), vec![keys, values])?
+ };
+ // 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 keys: ArrayRef = Arc::new(StringArray::from_iter_values(
+ (0..64).map(|i| format!("large_{i:02}_{large_str}")),
+ ));
+ let values: ArrayRef = Arc::new(Int64Array::from(vec![1_i64; 64]));
+ RecordBatch::try_new(Arc::clone(&state), vec![keys, values])?
+ };
+ let input = TestMemoryExec::try_new_exec(
+ &[vec![regular_batch, large_batch]],
+ 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),
+ )?);
+ // 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]