1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
use arrow::record_batch::RecordBatch;
use crate::metrics;
/// Tracks if the aggregate should skip partial aggregations
///
/// Used by [`crate::aggregates::hash_stream::PartialHashAggregateStream`].
pub(super) struct SkipAggregationProbe {
// ========================================================================
// PROPERTIES:
// These fields are initialized at the start and remain constant throughout
// the execution.
// ========================================================================
/// Aggregation ratio check performed when the number of input rows exceeds
/// this threshold (from `SessionConfig`)
probe_rows_threshold: usize,
/// Maximum ratio of `num_groups` to `input_rows` for continuing aggregation
/// (from `SessionConfig`). If the ratio exceeds this value, aggregation
/// is skipped and input rows are directly converted to output
probe_ratio_threshold: f64,
// ========================================================================
// STATES:
// Fields changes during execution. Can be buffer, or state flags that
// influence the execution in the parent aggregate stream
// ========================================================================
/// Number of processed input rows (updated during probing)
input_rows: usize,
/// Number of total group values for `input_rows` (updated during probing)
num_groups: usize,
/// Flag indicating further data aggregation may be skipped (decision made
/// when probing complete)
should_skip: bool,
/// Flag indicating further updates of `SkipAggregationProbe` state won't
/// make any effect (set either while probing or on probing completion)
is_locked: bool,
// ========================================================================
// METRICS:
// ========================================================================
/// Number of rows where state was output without aggregation.
///
/// * If 0, all input rows were aggregated (should_skip was always false)
///
/// * if greater than zero, the number of rows which were output directly
/// without aggregation
skipped_aggregation_rows: metrics::Count,
}
impl SkipAggregationProbe {
pub(super) fn new(
probe_rows_threshold: usize,
probe_ratio_threshold: f64,
skipped_aggregation_rows: metrics::Count,
) -> Self {
Self {
input_rows: 0,
num_groups: 0,
probe_rows_threshold,
probe_ratio_threshold,
should_skip: false,
is_locked: false,
skipped_aggregation_rows,
}
}
/// Updates `SkipAggregationProbe` state:
/// - increments the number of input rows
/// - replaces the number of groups with the new value
/// - on `probe_rows_threshold` exceeded calculates
/// aggregation ratio and sets `should_skip` flag
/// - if `should_skip` is set, locks further state updates
pub(super) fn update_state(&mut self, input_rows: usize, num_groups: usize) {
if self.is_locked {
return;
}
self.input_rows += input_rows;
self.num_groups = num_groups;
if self.input_rows >= self.probe_rows_threshold {
self.should_skip = self.num_groups as f64 / self.input_rows as f64
> self.probe_ratio_threshold;
// Set is_locked to true only if we have decided to skip, otherwise we can try to skip
// during processing the next record_batch.
self.is_locked = self.should_skip;
}
}
pub(super) fn should_skip(&self) -> bool {
self.should_skip
}
/// Record the number of rows that were output directly without aggregation
pub(super) fn record_skipped(&mut self, batch: &RecordBatch) {
self.skipped_aggregation_rows.add(batch.num_rows());
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::aggregates::grouped_hash_stream::GroupedHashAggregateStream;
use crate::aggregates::{AggregateExec, AggregateMode, PhysicalGroupBy};
use crate::execution_plan::ExecutionPlan;
use crate::test::TestMemoryExec;
use std::sync::Arc;
use arrow::array::Int32Array;
use arrow::datatypes::{DataType, Field, Schema};
use datafusion_common::Result;
use datafusion_execution::TaskContext;
use datafusion_execution::runtime_env::RuntimeEnvBuilder;
use datafusion_functions_aggregate::count::count_udaf;
use datafusion_physical_expr::aggregate::AggregateExprBuilder;
use datafusion_physical_expr::expressions::col;
use futures::StreamExt;
// Migrated to PartialHashAggregateStream coverage in hash_stream.rs;
// kept here for the legacy GroupedHashAggregateStream implementation.
#[tokio::test]
async fn test_skip_aggregation_probe_not_locked_until_skip() -> Result<()> {
// Test that the probe is not locked until we actually decide to skip.
// This allows us to continue evaluating the skip condition across multiple batches.
//
// Scenario:
// - Batch 1: Hits rows threshold but NOT ratio threshold (low cardinality) -> don't skip
// - Batch 2: Now hits ratio threshold (high cardinality) -> skip
//
// Without the fix, the probe would be locked after batch 1, preventing the skip
// decision from being made on batch 2.
let schema = Arc::new(Schema::new(vec![
Field::new("group_col", DataType::Int32, false),
Field::new("value_col", DataType::Int32, false),
]));
// Configure thresholds:
// - probe_rows_threshold: 100 rows
// - probe_ratio_threshold: 0.8 (80%)
let probe_rows_threshold = 100;
let probe_ratio_threshold = 0.8;
// Batch 1: 100 rows with only 10 unique groups
// Ratio: 10/100 = 0.1 (10%) < 0.8 -> should NOT skip
// This will hit the rows threshold but not the ratio threshold
let batch1_rows = 100;
let batch1_groups = 10;
let mut group_ids_batch1 = Vec::new();
for i in 0..batch1_rows {
group_ids_batch1.push((i % batch1_groups) as i32);
}
let values_batch1: Vec<i32> = vec![1; batch1_rows];
let batch1 = RecordBatch::try_new(
Arc::clone(&schema),
vec![
Arc::new(Int32Array::from(group_ids_batch1)),
Arc::new(Int32Array::from(values_batch1)),
],
)?;
// Batch 2: 360 rows with 360 unique NEW groups (starting from group 10)
// After batch 2, total: 460 rows, 370 groups
// Ratio: 370/460 is about 0.804 (80.4%) > 0.8 -> SHOULD decide to skip
let batch2_rows = 360;
let batch2_groups = 360;
let group_ids_batch2: Vec<i32> = (batch1_groups..(batch1_groups + batch2_groups))
.map(|x| x as i32)
.collect();
let values_batch2: Vec<i32> = vec![1; batch2_rows];
let batch2 = RecordBatch::try_new(
Arc::clone(&schema),
vec![
Arc::new(Int32Array::from(group_ids_batch2)),
Arc::new(Int32Array::from(values_batch2)),
],
)?;
// Batch 3: This batch should be skipped since we decided to skip after batch 2
// 100 rows with 100 unique groups (continuing from where batch 2 left off)
let batch3_rows = 100;
let batch3_groups = 100;
let batch3_start_group = batch1_groups + batch2_groups;
let group_ids_batch3: Vec<i32> = (batch3_start_group
..(batch3_start_group + batch3_groups))
.map(|x| x as i32)
.collect();
let values_batch3: Vec<i32> = vec![1; batch3_rows];
let batch3 = RecordBatch::try_new(
Arc::clone(&schema),
vec![
Arc::new(Int32Array::from(group_ids_batch3)),
Arc::new(Int32Array::from(values_batch3)),
],
)?;
let input_partitions = vec![vec![batch1, batch2, batch3]];
let runtime = RuntimeEnvBuilder::default().build_arc()?;
let mut task_ctx = TaskContext::default().with_runtime(runtime);
// Configure skip aggregation settings
let mut session_config = task_ctx.session_config().clone();
session_config = session_config.set(
"datafusion.execution.skip_partial_aggregation_probe_rows_threshold",
&datafusion_common::ScalarValue::UInt64(Some(probe_rows_threshold)),
);
session_config = session_config.set(
"datafusion.execution.skip_partial_aggregation_probe_ratio_threshold",
&datafusion_common::ScalarValue::Float64(Some(probe_ratio_threshold)),
);
task_ctx = task_ctx.with_session_config(session_config);
let task_ctx = Arc::new(task_ctx);
// Create aggregate: COUNT(*) GROUP BY group_col
let group_expr = vec![(col("group_col", &schema)?, "group_col".to_string())];
let aggr_expr = vec![Arc::new(
AggregateExprBuilder::new(count_udaf(), vec![col("value_col", &schema)?])
.schema(Arc::clone(&schema))
.alias("count_value")
.build()?,
)];
let exec = TestMemoryExec::try_new(&input_partitions, Arc::clone(&schema), None)?;
let exec = Arc::new(TestMemoryExec::update_cache(&Arc::new(exec)));
// Use Partial mode
let aggregate_exec = AggregateExec::try_new(
AggregateMode::Partial,
PhysicalGroupBy::new_single(group_expr),
aggr_expr,
vec![None],
exec,
Arc::clone(&schema),
)?;
// Execute and collect results
let mut stream =
GroupedHashAggregateStream::new(&aggregate_exec, &Arc::clone(&task_ctx), 0)?;
let mut results = Vec::new();
while let Some(result) = stream.next().await {
let batch = result?;
results.push(batch);
}
// Check that skip aggregation actually happened.
// The key metric is skipped_aggregation_rows.
let metrics = aggregate_exec.metrics().unwrap();
let skipped_rows = metrics
.sum_by_name("skipped_aggregation_rows")
.map(|m| m.as_usize())
.unwrap_or(0);
// We expect batch 3's rows to be skipped (100 rows)
assert_eq!(
skipped_rows, batch3_rows,
"Expected batch 3's rows ({batch3_rows}) to be skipped",
);
Ok(())
}
#[test]
fn test_skip_aggregation_probe_equality_does_not_skip() {
// When num_groups / input_rows == probe_ratio_threshold, the `>` boundary
// means we must NOT skip: equality is not sufficient to trigger skip.
let threshold_ratio = 0.5_f64;
let threshold_rows = 10_usize;
let mut probe = SkipAggregationProbe::new(
threshold_rows,
threshold_ratio,
metrics::Count::new(),
);
// 10 rows, 5 groups: ratio = 5/10 = 0.5 exactly equals threshold
probe.update_state(10, 5);
assert!(
!probe.should_skip(),
"ratio == threshold should not trigger skip (boundary is exclusive)"
);
}
}