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

Function test_map_array

arrow-array/src/array/map_array.rs:534–637  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

532
533 #[test]
534 fn test_map_array() {
535 // Construct key and values
536 let key_data = ArrayData::builder(DataType::Int32)
537 .len(8)
538 .add_buffer(Buffer::from([0, 1, 2, 3, 4, 5, 6, 7].to_byte_slice()))
539 .build()
540 .unwrap();
541 let value_data = ArrayData::builder(DataType::UInt32)
542 .len(8)
543 .add_buffer(Buffer::from(
544 [0u32, 10, 20, 0, 40, 0, 60, 70].to_byte_slice(),
545 ))
546 .null_bit_buffer(Some(Buffer::from(&[0b11010110])))
547 .build()
548 .unwrap();
549
550 // Construct a buffer for value offsets, for the nested array:
551 // [[0, 1, 2], [3, 4, 5], [6, 7]]
552 let entry_offsets = Buffer::from([0, 3, 6, 8].to_byte_slice());
553
554 let keys_field = Arc::new(Field::new("keys", DataType::Int32, false));
555 let values_field = Arc::new(Field::new("values", DataType::UInt32, true));
556 let entry_struct = StructArray::from(vec![
557 (keys_field.clone(), make_array(key_data)),
558 (values_field.clone(), make_array(value_data.clone())),
559 ]);
560
561 // Construct a map array from the above two
562 let map_data_type = DataType::Map(
563 Arc::new(Field::new(
564 "entries",
565 entry_struct.data_type().clone(),
566 false,
567 )),
568 false,
569 );
570 let map_data = ArrayData::builder(map_data_type)
571 .len(3)
572 .add_buffer(entry_offsets)
573 .add_child_data(entry_struct.into_data())
574 .build()
575 .unwrap();
576 let map_array = MapArray::from(map_data);
577
578 assert_eq!(value_data, map_array.values().to_data());
579 assert_eq!(&DataType::UInt32, map_array.value_type());
580 assert_eq!(3, map_array.len());
581 assert_eq!(0, map_array.null_count());
582 assert_eq!(6, map_array.value_offsets()[2]);
583 assert_eq!(2, map_array.value_length(2));
584
585 let key_array = Arc::new(Int32Array::from(vec![0, 1, 2])) as ArrayRef;
586 let value_array =
587 Arc::new(UInt32Array::from(vec![None, Some(10u32), Some(20)])) as ArrayRef;
588 let struct_array = StructArray::from(vec![
589 (keys_field.clone(), key_array),
590 (values_field.clone(), value_array),
591 ]);

Callers

nothing calls this directly

Calls 11

add_bufferMethod · 0.80
to_byte_sliceMethod · 0.80
add_child_dataMethod · 0.80
MapClass · 0.50
buildMethod · 0.45
lenMethod · 0.45
null_bit_bufferMethod · 0.45
cloneMethod · 0.45
data_typeMethod · 0.45
into_dataMethod · 0.45
sliceMethod · 0.45

Tested by

no test coverage detected