MCPcopy Create free account
hub / github.com/apache/arrow-rs / test_generic_list

Function test_generic_list

arrow-array/src/ffi.rs:791–842  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

789 }
790
791 fn test_generic_list<Offset: OffsetSizeTrait>() -> Result<()> {
792 // Construct a value array
793 let value_data = ArrayData::builder(DataType::Int32)
794 .len(8)
795 .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
796 .build()
797 .unwrap();
798
799 // Construct a buffer for value offsets, for the nested array:
800 // [[0, 1, 2], [3, 4, 5], [6, 7]]
801 let value_offsets = [0_usize, 3, 6, 8]
802 .iter()
803 .map(|i| Offset::from_usize(*i).unwrap())
804 .collect::<Buffer>();
805
806 // Construct a list array from the above two
807 let list_data_type = GenericListArray::<Offset>::DATA_TYPE_CONSTRUCTOR(Arc::new(
808 Field::new_list_field(DataType::Int32, false),
809 ));
810
811 let list_data = ArrayData::builder(list_data_type)
812 .len(3)
813 .add_buffer(value_offsets)
814 .add_child_data(value_data)
815 .build()
816 .unwrap();
817
818 // create an array natively
819 let array = GenericListArray::<Offset>::from(list_data.clone());
820
821 // export it
822 let (array, schema) = to_ffi(&array.to_data())?;
823
824 // (simulate consumer) import it
825 let data = unsafe { from_ffi(array, &schema) }?;
826 let array = make_array(data);
827
828 // downcast
829 let array = array
830 .as_any()
831 .downcast_ref::<GenericListArray<Offset>>()
832 .unwrap();
833
834 // verify
835 let expected = GenericListArray::<Offset>::from(list_data);
836 assert_eq!(&array.value(0), &expected.value(0));
837 assert_eq!(&array.value(1), &expected.value(1));
838 assert_eq!(&array.value(2), &expected.value(2));
839
840 // (drop/release)
841 Ok(())
842 }
843
844 #[test]
845 fn test_list() -> Result<()> {

Callers

nothing calls this directly

Calls 11

to_ffiFunction · 0.85
from_ffiFunction · 0.85
add_bufferMethod · 0.80
add_child_dataMethod · 0.80
make_arrayFunction · 0.50
buildMethod · 0.45
lenMethod · 0.45
iterMethod · 0.45
cloneMethod · 0.45
to_dataMethod · 0.45
as_anyMethod · 0.45

Tested by

no test coverage detected