From bc3ef33c47d9746be43e3a8de1e2d876c05007e8 Mon Sep 17 00:00:00 2001 From: "Jorge C. Leitao" Date: Sun, 6 Dec 2020 06:54:06 +0100 Subject: [PATCH] Fixed error in MutableArrayData --- rust/arrow/src/array/transform/mod.rs | 28 ++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/rust/arrow/src/array/transform/mod.rs b/rust/arrow/src/array/transform/mod.rs index 9c4149ee5f2..074d6acaf11 100644 --- a/rust/arrow/src/array/transform/mod.rs +++ b/rust/arrow/src/array/transform/mod.rs @@ -285,10 +285,16 @@ impl<'a> MutableArrayData<'a> { /// `use_nulls` is a flag used to optimize insertions. It should be `false` if the only source of nulls /// are the arrays themselves and `true` if the user plans to call [MutableArrayData::extend_nulls]. /// In other words, if `use_nulls` is `false`, calling [MutableArrayData::extend_nulls] should not be used. - pub fn new(arrays: Vec<&'a ArrayData>, use_nulls: bool, capacity: usize) -> Self { + pub fn new(arrays: Vec<&'a ArrayData>, mut use_nulls: bool, capacity: usize) -> Self { let data_type = arrays[0].data_type(); use crate::datatypes::*; + // if any of the arrays has nulls, insertions from any array requires setting bits + // as there is at least one array with nulls. + if arrays.iter().any(|array| array.null_count() > 0) { + use_nulls = true; + }; + let buffers = match &data_type { DataType::Boolean => { let bytes = bit_util::ceil(capacity, 8); @@ -614,6 +620,26 @@ mod tests { assert_eq!(result, expected); } + #[test] + fn test_multiple_with_nulls() { + let array1 = StringArray::from(vec!["hello", "world"]).data(); + let array2 = StringArray::from(vec![Some("1"), None]).data(); + + let arrays = vec![array1.as_ref(), array2.as_ref()]; + + let mut mutable = MutableArrayData::new(arrays, false, 5); + + mutable.extend(0, 0, 2); + mutable.extend(1, 0, 2); + + let result = mutable.freeze(); + let result = StringArray::from(Arc::new(result)); + + let expected = + StringArray::from(vec![Some("hello"), Some("world"), Some("1"), None]); + assert_eq!(result, expected); + } + #[test] fn test_string_null_offset_nulls() { let array =