Repository: jorgecarleitao/arrow2
Pull request #294 (2021-08-17 17:15:02)

Added more tests to MutablePrimitiveArray
Also used more efficient APIs internally.

codecov[bot]: # [Codecov](https://codecov.io/gh/jorgecarleitao/arrow2/pull/294?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao) Report
> Merging [#294](https://codecov.io/gh/jorgecarleitao/arrow2/pull/294?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao) (c48b06e) into [main](https://codecov.io/gh/jorgecarleitao/arrow2/commit/0742edddce26df26c10bddf53f9450a420e4e77c?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao) (0742edd) will **increase** coverage by `0.14%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/jorgecarleitao/arrow2/pull/294/graphs/tree.svg?width=650&height=150&src=pr&token=AgyTF60R3D&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao)](https://codecov.io/gh/jorgecarleitao/arrow2/pull/294?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao)

```diff
@@            Coverage Diff             @@
##             main     #294      +/-   ##
==========================================
+ Coverage   77.25%   77.39%   +0.14%     
==========================================
  Files         315      315              
  Lines       20791    20833      +42     
==========================================
+ Hits        16062    16124      +62     
+ Misses       4729     4709      -20     
```


| [Impacted Files](https://codecov.io/gh/jorgecarleitao/arrow2/pull/294?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao) | Coverage Δ | |
|---|---|---|
| [src/array/primitive/mutable.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/294/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-c3JjL2FycmF5L3ByaW1pdGl2ZS9tdXRhYmxlLnJz) | `70.81% <100.00%> (+11.57%)` | :arrow_up: |
| [tests/it/array/primitive/mutable.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/294/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-dGVzdHMvaXQvYXJyYXkvcHJpbWl0aXZlL211dGFibGUucnM=) | `100.00% <100.00%> (ø)` | |
| [src/io/json/read/reader.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/294/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-c3JjL2lvL2pzb24vcmVhZC9yZWFkZXIucnM=) | `74.32% <0.00%> (-1.36%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/jorgecarleitao/arrow2/pull/294?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/jorgecarleitao/arrow2/pull/294?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao). Last update [0742edd...c48b06e](https://codecov.io/gh/jorgecarleitao/arrow2/pull/294?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao).


Patch:
diff --git a/src/array/primitive/mutable.rs b/src/array/primitive/mutable.rs
index 69220c3cfa..2e80840249 100644
--- a/src/array/primitive/mutable.rs
+++ b/src/array/primitive/mutable.rs
@@ -222,9 +222,9 @@ impl<T: NativeType> MutablePrimitiveArray<T> {
         if value.is_none() && self.validity.is_none() {
             // When the validity is None, all elements so far are valid. When one of the elements is set fo null,
             // the validity must be initialized.
-            self.validity = Some(MutableBitmap::from_trusted_len_iter(
-                std::iter::repeat(true).take(self.len()),
-            ));
+            let mut validity = MutableBitmap::new();
+            validity.extend_constant(self.len(), true);
+            self.validity = Some(validity);
         }
         if let Some(x) = self.validity.as_mut() {
             x.set(index, value.is_some())


Test patch:
diff --git a/tests/it/array/primitive/mutable.rs b/tests/it/array/primitive/mutable.rs
index 8dbff29823..0adb4a534d 100644
--- a/tests/it/array/primitive/mutable.rs
+++ b/tests/it/array/primitive/mutable.rs
@@ -2,19 +2,60 @@ use arrow2::{
     array::*,
     bitmap::{Bitmap, MutableBitmap},
     buffer::MutableBuffer,
+    datatypes::DataType,
 };
 use std::iter::FromIterator;
 
+#[test]
+fn from_and_into_data() {
+    let a = MutablePrimitiveArray::from_data(
+        DataType::Int32,
+        MutableBuffer::from([1i32, 0]),
+        Some(MutableBitmap::from([true, false])),
+    );
+    assert_eq!(a.len(), 2);
+    let (a, b, c) = a.into_data();
+    assert_eq!(a, DataType::Int32);
+    assert_eq!(b, MutableBuffer::from([1i32, 0]));
+    assert_eq!(c, Some(MutableBitmap::from([true, false])));
+}
+
+#[test]
+fn to() {
+    let a = MutablePrimitiveArray::from_data(
+        DataType::Int32,
+        MutableBuffer::from([1i32, 0]),
+        Some(MutableBitmap::from([true, false])),
+    );
+    let a = a.to(DataType::Date32);
+    assert_eq!(a.data_type(), &DataType::Date32);
+}
+
+#[test]
+fn values_mut_slice() {
+    let mut a = MutablePrimitiveArray::from_data(
+        DataType::Int32,
+        MutableBuffer::from([1i32, 0]),
+        Some(MutableBitmap::from([true, false])),
+    );
+    let values = a.values_mut_slice();
+
+    values[0] = 10;
+    assert_eq!(a.values()[0], 10);
+}
+
 #[test]
 fn push() {
     let mut a = MutablePrimitiveArray::<i32>::new();
     a.push(Some(1));
     a.push(None);
-    assert_eq!(a.len(), 2);
+    a.push_null();
+    assert_eq!(a.len(), 3);
     assert!(a.is_valid(0));
     assert!(!a.is_valid(1));
+    assert!(!a.is_valid(2));
 
-    assert_eq!(a.values(), &MutableBuffer::from([1, 0]));
+    assert_eq!(a.values(), &MutableBuffer::from([1, 0, 0]));
 }
 
 #[test]
@@ -29,6 +70,17 @@ fn set() {
     assert!(a.is_valid(1));
 
     assert_eq!(a.values(), &MutableBuffer::from([2, 1]));
+
+    let mut a = MutablePrimitiveArray::<i32>::from_slice([1, 2]);
+
+    a.set(0, Some(2));
+    a.set(1, None);
+
+    assert_eq!(a.len(), 2);
+    assert!(a.is_valid(0));
+    assert!(!a.is_valid(1));
+
+    assert_eq!(a.values(), &MutableBuffer::from([2, 0]));
 }
 
 #[test]
@@ -72,3 +124,19 @@ fn extend_trusted_len() {
     );
     assert_eq!(a.values(), &MutableBuffer::<i32>::from([1, 2, 0, 4]));
 }
+
+#[test]
+fn set_validity() {
+    let mut a = MutablePrimitiveArray::<i32>::new();
+    a.extend_trusted_len(vec![Some(1), Some(2)].into_iter());
+    assert_eq!(a.validity(), &None);
+    a.set_validity(Some(MutableBitmap::from([false, true])));
+    assert_eq!(a.validity(), &Some(MutableBitmap::from([false, true])));
+}
+
+#[test]
+fn set_values() {
+    let mut a = MutablePrimitiveArray::<i32>::from_slice([1, 2]);
+    a.set_values(MutableBuffer::from([1, 3]));
+    assert_eq!(a.values().as_slice(), [1, 3]);
+}