MCPcopy Create free account
hub / github.com/apache/datafusion / unnest_array_agg

Function unnest_array_agg

datafusion/core/tests/dataframe/mod.rs:4639–4738  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4637
4638#[tokio::test]
4639async fn unnest_array_agg() -> Result<()> {
4640 let mut shape_id_builder = UInt32Builder::new();
4641 let mut tag_id_builder = UInt32Builder::new();
4642
4643 for shape_id in 1..=3 {
4644 for tag_id in 1..=3 {
4645 shape_id_builder.append_value(shape_id as u32);
4646 tag_id_builder.append_value((shape_id * 10 + tag_id) as u32);
4647 }
4648 }
4649
4650 let batch = RecordBatch::try_from_iter(vec![
4651 ("shape_id", Arc::new(shape_id_builder.finish()) as ArrayRef),
4652 ("tag_id", Arc::new(tag_id_builder.finish()) as ArrayRef),
4653 ])?;
4654
4655 let ctx = SessionContext::new();
4656 ctx.register_batch("shapes", batch)?;
4657 let df = ctx.table("shapes").await?;
4658
4659 let results = df.clone().collect().await?;
4660
4661 // Assert that there are no empty batches in result
4662 for rb in results.clone() {
4663 assert!(rb.num_rows() > 0);
4664 }
4665
4666 assert_snapshot!(
4667 batches_to_sort_string(&results),
4668 @r"
4669 +----------+--------+
4670 | shape_id | tag_id |
4671 +----------+--------+
4672 | 1 | 11 |
4673 | 1 | 12 |
4674 | 1 | 13 |
4675 | 2 | 21 |
4676 | 2 | 22 |
4677 | 2 | 23 |
4678 | 3 | 31 |
4679 | 3 | 32 |
4680 | 3 | 33 |
4681 +----------+--------+
4682 "
4683 );
4684
4685 // Doing an `array_agg` by `shape_id` produces:
4686 let results = df
4687 .clone()
4688 .aggregate(
4689 vec![col("shape_id")],
4690 vec![array_agg(col("tag_id")).alias("tag_id")],
4691 )?
4692 .collect()
4693 .await?;
4694 assert_snapshot!(
4695 batches_to_sort_string(&results),
4696 @r"

Callers

nothing calls this directly

Calls 8

newFunction · 0.85
collectMethod · 0.80
unnest_columnsMethod · 0.80
append_valueMethod · 0.45
register_batchMethod · 0.45
tableMethod · 0.45
cloneMethod · 0.45
aggregateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…