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

Function test_dictionary_buffer

parquet/src/arrow/buffer/dictionary_buffer.rs:283–367  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

281
282 #[test]
283 fn test_dictionary_buffer() {
284 let dict_type =
285 ArrowType::Dictionary(Box::new(ArrowType::Int32), Box::new(ArrowType::Utf8));
286
287 let d1: ArrayRef = Arc::new(StringArray::from(vec!["hello", "world", "", "a", "b"]));
288
289 let mut buffer = DictionaryBuffer::<i32, i32>::with_capacity(0);
290
291 // Read some data preserving the dictionary
292 let values = &[1, 0, 3, 2, 4];
293 buffer.as_keys(&d1).unwrap().extend_from_slice(values);
294
295 let mut valid = vec![false, false, true, true, false, true, true, true];
296 let valid_buffer = Buffer::from_iter(valid.iter().cloned());
297 buffer.pad_nulls(0, values.len(), valid.len(), valid_buffer.as_slice());
298
299 // Read some data not preserving the dictionary
300
301 let values = buffer.spill_values().unwrap();
302 let read_offset = values.len();
303 values.try_push("bingo".as_bytes(), false).unwrap();
304 values.try_push("bongo".as_bytes(), false).unwrap();
305
306 valid.extend_from_slice(&[false, false, true, false, true]);
307 let null_buffer = Buffer::from_iter(valid.iter().cloned());
308 buffer.pad_nulls(read_offset, 2, 5, null_buffer.as_slice());
309
310 assert_eq!(buffer.len(), 13);
311 let split = std::mem::replace(&mut buffer, DictionaryBuffer::with_capacity(0));
312
313 let array = split.into_array(Some(null_buffer), &dict_type).unwrap();
314 assert_eq!(array.data_type(), &dict_type);
315
316 let strings = cast(&array, &ArrowType::Utf8).unwrap();
317 let strings = strings.as_any().downcast_ref::<StringArray>().unwrap();
318 assert_eq!(
319 strings.iter().collect::<Vec<_>>(),
320 vec![
321 None,
322 None,
323 Some("world"),
324 Some("hello"),
325 None,
326 Some("a"),
327 Some(""),
328 Some("b"),
329 None,
330 None,
331 Some("bingo"),
332 None,
333 Some("bongo")
334 ]
335 );
336
337 // Can recreate with new dictionary as values is empty
338 assert!(matches!(&buffer, DictionaryBuffer::Values { .. }));
339 assert_eq!(buffer.len(), 0);
340 let d2 = Arc::new(StringArray::from(vec!["bingo", ""])) as ArrayRef;

Callers

nothing calls this directly

Calls 13

castFunction · 0.85
extend_from_sliceMethod · 0.80
as_keysMethod · 0.80
spill_valuesMethod · 0.80
try_pushMethod · 0.80
from_iterFunction · 0.50
iterMethod · 0.45
pad_nullsMethod · 0.45
lenMethod · 0.45
as_sliceMethod · 0.45
as_bytesMethod · 0.45
into_arrayMethod · 0.45

Tested by

no test coverage detected