use std::sync::Arc;
use arrow::array::{ArrayRef, Int64Array};
use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
use arrow::record_batch::RecordBatch;
use datafusion_common::test_util::batches_to_string;
use datafusion_common::{DataFusionError, Result};
use datafusion_execution::TaskContext;
use datafusion_execution::config::SessionConfig;
use datafusion_execution::disk_manager::{DiskManagerBuilder, DiskManagerMode};
use datafusion_execution::memory_pool::{
GreedyMemoryPool, MemoryPool, UnboundedMemoryPool,
};
use datafusion_execution::runtime_env::RuntimeEnvBuilder;
use datafusion_expr::AggregateUDF;
use datafusion_functions_aggregate::sum::sum_udaf;
use datafusion_physical_expr::PhysicalSortExpr;
use datafusion_physical_expr::aggregate::AggregateExprBuilder;
use datafusion_physical_expr::expressions::col;
use datafusion_physical_expr_common::sort_expr::LexOrdering;
use futures::StreamExt;
use super::{AggregateExec, AggregateMode, PhysicalGroupBy};
use crate::ExecutionPlan;
use crate::test::TestMemoryExec;
pub(super) fn full_batches() -> Vec<Vec<Vec<i64>>> {
vec![vec![(0..=256).collect()], vec![(256..=512).collect()]]
}
pub(super) fn distinct_key_batches() -> Vec<Vec<Vec<i64>>> {
vec![
vec![(0..8193).collect()],
vec![(8192..16385).collect()],
vec![(16384..24576).collect()],
]
}
pub(super) fn single_key_batches() -> Vec<Vec<Vec<i64>>> {
vec![vec![vec![0; 512]]; 2]
}
pub(super) fn sum_over_sorted_input(
mode: AggregateMode,
keys: &[&str],
batches: Vec<Vec<Vec<i64>>>,
) -> Result<AggregateExec> {
aggregate_over_sorted_input(mode, keys, batches, sum_udaf())
}
pub(super) fn aggregate_over_sorted_input(
mode: AggregateMode,
keys: &[&str],
batches: Vec<Vec<Vec<i64>>>,
udaf: Arc<AggregateUDF>,
) -> Result<AggregateExec> {
aggregate_expr_over_sorted_input(mode, keys, batches, udaf, |builder, _| Ok(builder))
}
pub(super) fn aggregate_expr_over_sorted_input(
mode: AggregateMode,
keys: &[&str],
batches: Vec<Vec<Vec<i64>>>,
udaf: Arc<AggregateUDF>,
configure: impl FnOnce(AggregateExprBuilder, &SchemaRef) -> Result<AggregateExprBuilder>,
) -> Result<AggregateExec> {
let fields = keys
.iter()
.chain(&["v"])
.map(|name| Field::new(*name, DataType::Int64, false))
.collect::<Vec<_>>();
let schema = Arc::new(Schema::new(fields));
let group_by = PhysicalGroupBy::new_single(
keys.iter()
.map(|key| Ok((col(key, &schema)?, key.to_string())))
.collect::<Result<_>>()?,
);
let aggregate_exprs = vec![Arc::new(
configure(
AggregateExprBuilder::new(Arc::clone(&udaf), vec![col("v", &schema)?]),
&schema,
)?
.schema(Arc::clone(&schema))
.alias(udaf.name())
.build()?,
)];
let (input_schema, group_by) = if mode == AggregateMode::Final {
let empty = TestMemoryExec::try_new_exec(&[vec![]], Arc::clone(&schema), None)?;
let partial = AggregateExec::try_new(
AggregateMode::Partial,
group_by.clone(),
aggregate_exprs.clone(),
vec![None],
empty,
Arc::clone(&schema),
)?;
(partial.schema(), group_by.as_final())
} else {
(Arc::clone(&schema), group_by)
};
let batches = batches
.into_iter()
.map(|columns| {
let mut columns = columns
.into_iter()
.map(|values| Arc::new(Int64Array::from(values)) as ArrayRef)
.collect::<Vec<_>>();
columns.push(Arc::clone(columns.last().unwrap()));
RecordBatch::try_new(Arc::clone(&input_schema), columns)
})
.collect::<Result<Vec<_>, _>>()?;
let ordering =
LexOrdering::new([PhysicalSortExpr::new_default(col(keys[0], &input_schema)?)])
.unwrap();
let input = TestMemoryExec::try_new(&[batches], Arc::clone(&input_schema), None)?
.try_with_sort_information(vec![ordering])?;
AggregateExec::try_new(
mode,
group_by,
aggregate_exprs,
vec![None],
Arc::new(TestMemoryExec::update_cache(&Arc::new(input))),
schema,
)
}
async fn collect_with_held_memory(
plan: &AggregateExec,
pool: Arc<dyn MemoryPool>,
) -> Result<(Vec<RecordBatch>, usize)> {
let context = Arc::new(
TaskContext::default()
.with_session_config(
SessionConfig::new()
.set_bool("datafusion.execution.enable_migration_aggregate", true),
)
.with_runtime(
RuntimeEnvBuilder::new()
.with_memory_pool(Arc::clone(&pool))
.with_disk_manager_builder(
DiskManagerBuilder::default()
.with_mode(DiskManagerMode::Disabled),
)
.build_arc()?,
),
);
let mut stream = plan.execute(0, context)?;
let mut held = pool.reserved();
let mut output = vec![];
while let Some(batch) = stream.next().await.transpose()? {
held = held.max(pool.reserved());
output.push(batch);
}
Ok((output, held))
}
pub(super) async fn collect_under_limit(
plan: &AggregateExec,
limit: usize,
) -> Result<Vec<RecordBatch>> {
let (output, _) =
collect_with_held_memory(plan, Arc::new(GreedyMemoryPool::new(limit))).await?;
Ok(output)
}
pub(super) async fn held_memory(plan: &AggregateExec) -> Result<usize> {
let (_, held) =
collect_with_held_memory(plan, Arc::new(UnboundedMemoryPool::default())).await?;
Ok(held)
}
pub(super) async fn assert_completed_groups_emitted_under_limit(
plan: &AggregateExec,
) -> Result<()> {
let (expected, held) =
collect_with_held_memory(plan, Arc::new(UnboundedMemoryPool::default())).await?;
let (actual, _) =
collect_with_held_memory(plan, Arc::new(GreedyMemoryPool::new(held))).await?;
assert_eq!(batches_to_string(&actual), batches_to_string(&expected));
Ok(())
}
pub(super) async fn assert_output_unchanged_under_limit(
plan: &AggregateExec,
limit: usize,
) -> Result<()> {
let (expected, _) =
collect_with_held_memory(plan, Arc::new(UnboundedMemoryPool::default())).await?;
let (actual, _) =
collect_with_held_memory(plan, Arc::new(GreedyMemoryPool::new(limit))).await?;
assert_eq!(batches_to_string(&actual), batches_to_string(&expected));
Ok(())
}
pub(super) async fn assert_resources_exhausted_under_limit(
plan: &AggregateExec,
limit: usize,
) -> Result<()> {
let result =
collect_with_held_memory(plan, Arc::new(GreedyMemoryPool::new(limit))).await;
assert!(
matches!(result, Err(DataFusionError::ResourcesExhausted(_))),
"expected ResourcesExhausted, got {result:?}"
);
Ok(())
}