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

Function test_list_array

arrow-array/src/array/list_array.rs:744–811  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

742
743 #[test]
744 fn test_list_array() {
745 // Construct a value array
746 let value_data = ArrayData::builder(DataType::Int32)
747 .len(8)
748 .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
749 .build()
750 .unwrap();
751
752 // Construct a buffer for value offsets, for the nested array:
753 // [[0, 1, 2], [3, 4, 5], [6, 7]]
754 let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
755
756 // Construct a list array from the above two
757 let list_data_type =
758 DataType::List(Arc::new(Field::new_list_field(DataType::Int32, false)));
759 let list_data = ArrayData::builder(list_data_type.clone())
760 .len(3)
761 .add_buffer(value_offsets.clone())
762 .add_child_data(value_data.clone())
763 .build()
764 .unwrap();
765 let list_array = ListArray::from(list_data);
766
767 let values = list_array.values();
768 assert_eq!(value_data, values.to_data());
769 assert_eq!(DataType::Int32, list_array.value_type());
770 assert_eq!(3, list_array.len());
771 assert_eq!(0, list_array.null_count());
772 assert_eq!(6, list_array.value_offsets()[2]);
773 assert_eq!(2, list_array.value_length(2));
774 assert_eq!(0, list_array.value(0).as_primitive::<Int32Type>().value(0));
775 assert_eq!(
776 0,
777 unsafe { list_array.value_unchecked(0) }
778 .as_primitive::<Int32Type>()
779 .value(0)
780 );
781 for i in 0..3 {
782 assert!(list_array.is_valid(i));
783 assert!(!list_array.is_null(i));
784 }
785
786 // Now test with a non-zero offset (skip first element)
787 // [[3, 4, 5], [6, 7]]
788 let list_data = ArrayData::builder(list_data_type)
789 .len(2)
790 .offset(1)
791 .add_buffer(value_offsets)
792 .add_child_data(value_data.clone())
793 .build()
794 .unwrap();
795 let list_array = ListArray::from(list_data);
796
797 let values = list_array.values();
798 assert_eq!(value_data, values.to_data());
799 assert_eq!(DataType::Int32, list_array.value_type());
800 assert_eq!(2, list_array.len());
801 assert_eq!(0, list_array.null_count());

Callers

nothing calls this directly

Calls 8

add_bufferMethod · 0.80
add_child_dataMethod · 0.80
ListClass · 0.50
buildMethod · 0.45
lenMethod · 0.45
cloneMethod · 0.45
valuesMethod · 0.45
offsetMethod · 0.45

Tested by

no test coverage detected