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

Function plan_builder_unnest

datafusion/expr/src/logical_plan/builder.rs:2629–2765  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2627
2628 #[test]
2629 fn plan_builder_unnest() -> Result<()> {
2630 // Cannot unnest on a scalar column
2631 let err = nested_table_scan("test_table")?
2632 .unnest_column("scalar")
2633 .unwrap_err();
2634
2635 let DataFusionError::Internal(desc) = err else {
2636 return plan_err!("Plan should have returned an DataFusionError::Internal");
2637 };
2638
2639 let desc = (*desc
2640 .split(DataFusionError::BACK_TRACE_SEP)
2641 .collect::<Vec<&str>>()
2642 .first()
2643 .unwrap_or(&""))
2644 .to_string();
2645
2646 assert_snapshot!(desc, @"trying to unnest on invalid data type UInt32");
2647
2648 // Unnesting the strings list.
2649 let plan = nested_table_scan("test_table")?
2650 .unnest_column("strings")?
2651 .build()?;
2652
2653 assert_snapshot!(plan, @r"
2654 Unnest: lists[test_table.strings|depth=1] structs[]
2655 TableScan: test_table
2656 ");
2657
2658 // Check unnested field is a scalar
2659 let field = plan.schema().field_with_name(None, "strings").unwrap();
2660 assert_eq!(&DataType::Utf8, field.data_type());
2661
2662 // Unnesting the singular struct column result into 2 new columns for each subfield
2663 let plan = nested_table_scan("test_table")?
2664 .unnest_column("struct_singular")?
2665 .build()?;
2666
2667 assert_snapshot!(plan, @r"
2668 Unnest: lists[] structs[test_table.struct_singular]
2669 TableScan: test_table
2670 ");
2671
2672 for field_name in &["a", "b"] {
2673 // Check unnested struct field is a scalar
2674 let field = plan
2675 .schema()
2676 .field_with_name(None, &format!("struct_singular.{field_name}"))
2677 .unwrap();
2678 assert_eq!(&DataType::UInt32, field.data_type());
2679 }
2680
2681 // Unnesting multiple fields in separate plans
2682 let plan = nested_table_scan("test_table")?
2683 .unnest_column("strings")?
2684 .unnest_column("structs")?
2685 .unnest_column("struct_singular")?
2686 .build()?;

Callers

nothing calls this directly

Calls 15

nested_table_scanFunction · 0.85
unwrap_errMethod · 0.80
unnest_columnMethod · 0.80
field_with_nameMethod · 0.80
collectMethod · 0.80
with_recursionsMethod · 0.80
to_stringMethod · 0.45
firstMethod · 0.45
splitMethod · 0.45
buildMethod · 0.45
schemaMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…