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

Function test_dictionary_array

arrow-array/src/array/dictionary_array.rs:1068–1118  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1066
1067 #[test]
1068 fn test_dictionary_array() {
1069 // Construct a value array
1070 let value_data = ArrayData::builder(DataType::Int8)
1071 .len(8)
1072 .add_buffer(Buffer::from(
1073 [10_i8, 11, 12, 13, 14, 15, 16, 17].to_byte_slice(),
1074 ))
1075 .build()
1076 .unwrap();
1077
1078 // Construct a buffer for value offsets, for the nested array:
1079 let keys = Buffer::from([2_i16, 3, 4].to_byte_slice());
1080
1081 // Construct a dictionary array from the above two
1082 let key_type = DataType::Int16;
1083 let value_type = DataType::Int8;
1084 let dict_data_type = DataType::Dictionary(Box::new(key_type), Box::new(value_type));
1085 let dict_data = ArrayData::builder(dict_data_type.clone())
1086 .len(3)
1087 .add_buffer(keys.clone())
1088 .add_child_data(value_data.clone())
1089 .build()
1090 .unwrap();
1091 let dict_array = Int16DictionaryArray::from(dict_data);
1092
1093 let values = dict_array.values();
1094 assert_eq!(value_data, values.to_data());
1095 assert_eq!(DataType::Int8, dict_array.value_type());
1096 assert_eq!(3, dict_array.len());
1097
1098 // Null count only makes sense in terms of the component arrays.
1099 assert_eq!(0, dict_array.null_count());
1100 assert_eq!(0, dict_array.values().null_count());
1101 assert_eq!(dict_array.keys(), &Int16Array::from(vec![2_i16, 3, 4]));
1102
1103 // Now test with a non-zero offset
1104 let dict_data = ArrayData::builder(dict_data_type)
1105 .len(2)
1106 .offset(1)
1107 .add_buffer(keys)
1108 .add_child_data(value_data.clone())
1109 .build()
1110 .unwrap();
1111 let dict_array = Int16DictionaryArray::from(dict_data);
1112
1113 let values = dict_array.values();
1114 assert_eq!(value_data, values.to_data());
1115 assert_eq!(DataType::Int8, dict_array.value_type());
1116 assert_eq!(2, dict_array.len());
1117 assert_eq!(dict_array.keys(), &Int16Array::from(vec![3_i16, 4]));
1118 }
1119
1120 #[test]
1121 fn test_dictionary_builder_append_many() {

Callers

nothing calls this directly

Calls 8

add_bufferMethod · 0.80
to_byte_sliceMethod · 0.80
add_child_dataMethod · 0.80
buildMethod · 0.45
lenMethod · 0.45
cloneMethod · 0.45
valuesMethod · 0.45
offsetMethod · 0.45

Tested by

no test coverage detected