Skip to content

Commit f2f305d

Browse files
viiryaIsaac
andcommitted
test: cover accumulator scratch release in clustered single aggregation
The clustered final and single streams also call ClusteredAggregateTable::shrink_to_fit after the memory-pressure emit. Add a Single-mode APPROX_DISTINCT test, and let the clustered test helper take the aggregate function. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 86a9abb commit f2f305d

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

‎datafusion/physical-plan/src/aggregates/clustered_single_stream.rs‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -762,9 +762,10 @@ mod tests {
762762
use super::*;
763763
use crate::aggregates::PhysicalGroupBy;
764764
use crate::aggregates::clustered_test_util::{
765-
assert_completed_groups_emitted_under_limit, assert_output_unchanged_under_limit,
766-
assert_resources_exhausted_under_limit, distinct_key_batches, full_batches,
767-
held_memory, single_key_batches, sum_over_sorted_input,
765+
aggregate_over_sorted_input, assert_completed_groups_emitted_under_limit,
766+
assert_output_unchanged_under_limit, assert_resources_exhausted_under_limit,
767+
distinct_key_batches, full_batches, held_memory, single_key_batches,
768+
sum_over_sorted_input,
768769
};
769770
use crate::collect;
770771
use crate::test::TestMemoryExec;
@@ -776,6 +777,7 @@ mod tests {
776777
GreedyMemoryPool, MemoryPool, PeakRecordingPool,
777778
};
778779
use datafusion_execution::runtime_env::RuntimeEnvBuilder;
780+
use datafusion_functions_aggregate::approx_distinct::approx_distinct_udaf;
779781
use datafusion_functions_aggregate::sum::sum_udaf;
780782
use datafusion_physical_expr::PhysicalSortExpr;
781783
use datafusion_physical_expr::aggregate::AggregateExprBuilder;
@@ -874,6 +876,22 @@ mod tests {
874876
assert_output_unchanged_under_limit(&plan, 100 * 1024).await
875877
}
876878

879+
/// Like [`completed_groups_release_batch_capacity`], but `APPROX_DISTINCT`
880+
/// also keeps the hashes of the last batch (64 KB for 8192 rows), which the
881+
/// table must release too. The limit fits the active group, but not those
882+
/// hashes.
883+
#[tokio::test]
884+
async fn completed_groups_release_accumulator_scratch() -> Result<()> {
885+
let plan = aggregate_over_sorted_input(
886+
AggregateMode::Single,
887+
&["k"],
888+
distinct_key_batches(),
889+
approx_distinct_udaf(),
890+
)?;
891+
assert_eq!(plan.group_clustering_mode(), &GroupClusteringMode::Full);
892+
assert_output_unchanged_under_limit(&plan, 64 * 1024).await
893+
}
894+
877895
/// A single key never completes a group before the input ends. The limit
878896
/// fits only the initial table.
879897
#[tokio::test]

‎datafusion/physical-plan/src/aggregates/clustered_test_util.rs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use datafusion_execution::memory_pool::{
1818
GreedyMemoryPool, MemoryPool, UnboundedMemoryPool,
1919
};
2020
use datafusion_execution::runtime_env::RuntimeEnvBuilder;
21+
use datafusion_expr::AggregateUDF;
2122
use datafusion_functions_aggregate::sum::sum_udaf;
2223
use datafusion_physical_expr::PhysicalSortExpr;
2324
use datafusion_physical_expr::aggregate::AggregateExprBuilder;
@@ -63,6 +64,17 @@ pub(super) fn sum_over_sorted_input(
6364
mode: AggregateMode,
6465
keys: &[&str],
6566
batches: Vec<Vec<Vec<i64>>>,
67+
) -> Result<AggregateExec> {
68+
aggregate_over_sorted_input(mode, keys, batches, sum_udaf())
69+
}
70+
71+
/// Like [`sum_over_sorted_input`], but aggregates `v` with `udaf`. For `Final`
72+
/// mode, the state of `udaf` must be a single `Int64` column.
73+
pub(super) fn aggregate_over_sorted_input(
74+
mode: AggregateMode,
75+
keys: &[&str],
76+
batches: Vec<Vec<Vec<i64>>>,
77+
udaf: Arc<AggregateUDF>,
6678
) -> Result<AggregateExec> {
6779
let fields = keys
6880
.iter()
@@ -76,9 +88,9 @@ pub(super) fn sum_over_sorted_input(
7688
.collect::<Result<_>>()?,
7789
);
7890
let aggregate_exprs = vec![Arc::new(
79-
AggregateExprBuilder::new(sum_udaf(), vec![col("v", &schema)?])
91+
AggregateExprBuilder::new(Arc::clone(&udaf), vec![col("v", &schema)?])
8092
.schema(Arc::clone(&schema))
81-
.alias("sum")
93+
.alias(udaf.name())
8294
.build()?,
8395
)];
8496

0 commit comments

Comments
 (0)