| 586 | |
| 587 | #[test] |
| 588 | fn cast_struct_by_field_name() { |
| 589 | let source_fields = Fields::from(vec![ |
| 590 | Field::new("b", DataType::Int32, true), |
| 591 | Field::new("a", DataType::Int32, true), |
| 592 | ]); |
| 593 | |
| 594 | let target_fields = Fields::from(vec![ |
| 595 | Field::new("a", DataType::Int32, true), |
| 596 | Field::new("b", DataType::Int32, true), |
| 597 | ]); |
| 598 | |
| 599 | let struct_array = StructArray::new( |
| 600 | source_fields, |
| 601 | vec![ |
| 602 | Arc::new(Int32Array::from(vec![Some(3)])), |
| 603 | Arc::new(Int32Array::from(vec![Some(4)])), |
| 604 | ], |
| 605 | None, |
| 606 | ); |
| 607 | |
| 608 | let value = ColumnarValue::Array(Arc::new(struct_array)); |
| 609 | let casted = value |
| 610 | .cast_to(&DataType::Struct(target_fields.clone()), None) |
| 611 | .expect("struct cast should succeed"); |
| 612 | |
| 613 | let ColumnarValue::Array(arr) = casted else { |
| 614 | panic!("expected array after cast"); |
| 615 | }; |
| 616 | |
| 617 | let struct_array = arr |
| 618 | .as_any() |
| 619 | .downcast_ref::<StructArray>() |
| 620 | .expect("expected StructArray"); |
| 621 | |
| 622 | let field_a = struct_array |
| 623 | .column_by_name("a") |
| 624 | .expect("expected field a in cast result"); |
| 625 | let field_b = struct_array |
| 626 | .column_by_name("b") |
| 627 | .expect("expected field b in cast result"); |
| 628 | |
| 629 | assert_eq!( |
| 630 | field_a |
| 631 | .as_any() |
| 632 | .downcast_ref::<Int32Array>() |
| 633 | .expect("expected Int32 array") |
| 634 | .value(0), |
| 635 | 4 |
| 636 | ); |
| 637 | assert_eq!( |
| 638 | field_b |
| 639 | .as_any() |
| 640 | .downcast_ref::<Int32Array>() |
| 641 | .expect("expected Int32 array") |
| 642 | .value(0), |
| 643 | 3 |
| 644 | ); |
| 645 | } |