Skip to content

Commit b024f06

Browse files
xavleeTristan1900
authored andcommitted
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 17a947e commit b024f06

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
@@ -4519,6 +4519,83 @@ mod tests {
45194519
Ok(())
45204520
}
45214521

4522+
#[tokio::test]
4523+
async fn unsorted_contiguous_groups_use_final_emission() -> Result<()> {
4524+
let schema = Arc::new(Schema::new(vec![
4525+
Field::new("key", DataType::Int32, false),
4526+
Field::new("time_bin", DataType::Int64, false),
4527+
Field::new("value", DataType::Int64, false),
4528+
]));
4529+
// Two sorted logical runs are emitted as batches in one DataFusion
4530+
// partition. Every distinct grouping tuple occupies one contiguous range,
4531+
// but tuple order resets at the batch boundary, so (key, time_bin) is not
4532+
// globally sorted.
4533+
let input_batches = vec![
4534+
RecordBatch::try_new(
4535+
Arc::clone(&schema),
4536+
vec![
4537+
Arc::new(Int32Array::from(vec![1, 1, 2, 2])),
4538+
Arc::new(Int64Array::from(vec![20, 20, 20, 20])),
4539+
Arc::new(Int64Array::from(vec![10, 20, 30, 40])),
4540+
],
4541+
)?,
4542+
RecordBatch::try_new(
4543+
Arc::clone(&schema),
4544+
vec![
4545+
Arc::new(Int32Array::from(vec![1, 1, 2, 2])),
4546+
Arc::new(Int64Array::from(vec![0, 0, 0, 0])),
4547+
Arc::new(Int64Array::from(vec![50, 60, 70, 80])),
4548+
],
4549+
)?,
4550+
];
4551+
let group_by = PhysicalGroupBy::new_single(vec![
4552+
(col("key", &schema)?, "key".to_string()),
4553+
(col("time_bin", &schema)?, "time_bin".to_string()),
4554+
]);
4555+
let aggr_expr = Arc::new(
4556+
AggregateExprBuilder::new(sum_udaf(), vec![col("value", &schema)?])
4557+
.schema(Arc::clone(&schema))
4558+
.alias("SUM(value)")
4559+
.build()?,
4560+
);
4561+
let input: Arc<dyn ExecutionPlan> =
4562+
TestMemoryExec::try_new_exec(&[input_batches], Arc::clone(&schema), None)?;
4563+
assert_eq!(input.output_partitioning().partition_count(), 1);
4564+
4565+
let aggregate = AggregateExec::try_new(
4566+
AggregateMode::Single,
4567+
group_by,
4568+
vec![aggr_expr],
4569+
vec![None],
4570+
input,
4571+
schema,
4572+
)?;
4573+
4574+
assert_eq!(aggregate.input_order_mode(), &InputOrderMode::Linear);
4575+
// This captures the behavior before #24438. When the source can declare
4576+
// `(key, time_bin)` group-contiguous, the corresponding case can use
4577+
// `EmissionType::Incremental`.
4578+
assert_eq!(aggregate.cache().emission_type, EmissionType::Final);
4579+
4580+
let task_ctx = new_migrated_hash_ctx(1024);
4581+
let stream = aggregate.execute_typed(0, &task_ctx)?;
4582+
assert!(matches!(stream, StreamType::SingleHash(_)));
4583+
let stream: SendableRecordBatchStream = stream.into();
4584+
let output = collect(stream).await?;
4585+
assert_snapshot!(batches_to_sort_string(&output), @r"
4586+
+-----+----------+------------+
4587+
| key | time_bin | SUM(value) |
4588+
+-----+----------+------------+
4589+
| 1 | 0 | 110 |
4590+
| 1 | 20 | 30 |
4591+
| 2 | 0 | 150 |
4592+
| 2 | 20 | 70 |
4593+
+-----+----------+------------+
4594+
");
4595+
4596+
Ok(())
4597+
}
4598+
45224599
/// Ensures for ordered input, `OrderedPartialAggregateStream` is used.
45234600
#[tokio::test]
45244601
async fn ordered_partial_aggregate_planning() -> Result<()> {

0 commit comments

Comments
 (0)