Skip to content

Commit c8fd71b

Browse files
authored
test: cover unsorted contiguous groups in one partition (apache#24737)
## Which issue does this PR relate to? - Part of apache#24438. ## Rationale for this change A source can concatenate several sorted logical runs into one DataFusion output partition. The resulting stream may be globally unsorted while every distinct `(key, time_bin)` tuple still occupies one contiguous range. This PR records how aggregate planning handles that layout before grouped input properties are available. It provides the behavioral baseline for the remaining PRs in the stack. ## What changes are included in this PR? - Add a single-partition `TestMemoryExec` fixture containing two sorted logical runs whose `(key, time_bin)` order resets at the record-batch boundary. - Aggregate by the complete `(key, time_bin)` tuple and verify the result. - Assert that planning selects `InputOrderMode::Linear`, `EmissionType::Final`, and `SingleHashAggregateStream`. ## Stack 1. [apache#24737 — test: cover unsorted contiguous groups in one partition](apache#24737) ← **this PR** 2. [apache#24697 — refactor: separate aggregate group completion from input ordering](apache#24697) 3. [apache#24698 — feat: add grouped equivalence properties](apache#24698) 4. [apache#24497 — feat: stream aggregates over grouped input](apache#24497) ## Are these changes tested? The new aggregate test executes the single-partition input and snapshot-checks all four grouped sums. ## Are there any user-facing changes? No. ## Review this layer [View only this PR layer](apache@36969e7) (cherry picked from commit 59d74ca)
1 parent e7af2a2 commit c8fd71b

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

  • datafusion/physical-plan/src/aggregates

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4372,6 +4372,83 @@ mod tests {
43724372
Ok(())
43734373
}
43744374

4375+
#[tokio::test]
4376+
async fn unsorted_contiguous_groups_use_final_emission() -> Result<()> {
4377+
let schema = Arc::new(Schema::new(vec![
4378+
Field::new("key", DataType::Int32, false),
4379+
Field::new("time_bin", DataType::Int64, false),
4380+
Field::new("value", DataType::Int64, false),
4381+
]));
4382+
// Two sorted logical runs are emitted as batches in one DataFusion
4383+
// partition. Every distinct grouping tuple occupies one contiguous range,
4384+
// but tuple order resets at the batch boundary, so (key, time_bin) is not
4385+
// globally sorted.
4386+
let input_batches = vec![
4387+
RecordBatch::try_new(
4388+
Arc::clone(&schema),
4389+
vec![
4390+
Arc::new(Int32Array::from(vec![1, 1, 2, 2])),
4391+
Arc::new(Int64Array::from(vec![20, 20, 20, 20])),
4392+
Arc::new(Int64Array::from(vec![10, 20, 30, 40])),
4393+
],
4394+
)?,
4395+
RecordBatch::try_new(
4396+
Arc::clone(&schema),
4397+
vec![
4398+
Arc::new(Int32Array::from(vec![1, 1, 2, 2])),
4399+
Arc::new(Int64Array::from(vec![0, 0, 0, 0])),
4400+
Arc::new(Int64Array::from(vec![50, 60, 70, 80])),
4401+
],
4402+
)?,
4403+
];
4404+
let group_by = PhysicalGroupBy::new_single(vec![
4405+
(col("key", &schema)?, "key".to_string()),
4406+
(col("time_bin", &schema)?, "time_bin".to_string()),
4407+
]);
4408+
let aggr_expr = Arc::new(
4409+
AggregateExprBuilder::new(sum_udaf(), vec![col("value", &schema)?])
4410+
.schema(Arc::clone(&schema))
4411+
.alias("SUM(value)")
4412+
.build()?,
4413+
);
4414+
let input: Arc<dyn ExecutionPlan> =
4415+
TestMemoryExec::try_new_exec(&[input_batches], Arc::clone(&schema), None)?;
4416+
assert_eq!(input.output_partitioning().partition_count(), 1);
4417+
4418+
let aggregate = AggregateExec::try_new(
4419+
AggregateMode::Single,
4420+
group_by,
4421+
vec![aggr_expr],
4422+
vec![None],
4423+
input,
4424+
schema,
4425+
)?;
4426+
4427+
assert_eq!(aggregate.input_order_mode(), &InputOrderMode::Linear);
4428+
// This captures the behavior before #24438. When the source can declare
4429+
// `(key, time_bin)` group-contiguous, the corresponding case can use
4430+
// `EmissionType::Incremental`.
4431+
assert_eq!(aggregate.cache().emission_type, EmissionType::Final);
4432+
4433+
let task_ctx = new_migrated_hash_ctx(1024);
4434+
let stream = aggregate.execute_typed(0, &task_ctx)?;
4435+
assert!(matches!(stream, StreamType::SingleHash(_)));
4436+
let stream: SendableRecordBatchStream = stream.into();
4437+
let output = collect(stream).await?;
4438+
assert_snapshot!(batches_to_sort_string(&output), @r"
4439+
+-----+----------+------------+
4440+
| key | time_bin | SUM(value) |
4441+
+-----+----------+------------+
4442+
| 1 | 0 | 110 |
4443+
| 1 | 20 | 30 |
4444+
| 2 | 0 | 150 |
4445+
| 2 | 20 | 70 |
4446+
+-----+----------+------------+
4447+
");
4448+
4449+
Ok(())
4450+
}
4451+
43754452
/// Ensures for ordered input, `OrderedPartialAggregateStream` is used.
43764453
#[tokio::test]
43774454
async fn ordered_partial_aggregate_planning() -> Result<()> {

0 commit comments

Comments
 (0)