Repository: jorgecarleitao/arrow2
Pull request #310 (2021-08-21 15:55:03)

Fixed extend when additional is zero
Also added an extra test for it.

codecov[bot]: # [Codecov](https://codecov.io/gh/jorgecarleitao/arrow2/pull/310?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao) Report
> Merging [#310](https://codecov.io/gh/jorgecarleitao/arrow2/pull/310?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao) (9423aed) into [main](https://codecov.io/gh/jorgecarleitao/arrow2/commit/bbe607ccbf3d7079c1fac6ad53fc6e5d4368e165?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao) (bbe607c) will **increase** coverage by `0.01%`.
> The diff coverage is `95.65%`.

[![Impacted file tree graph](https://codecov.io/gh/jorgecarleitao/arrow2/pull/310/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/310?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     #310      +/-   ##
==========================================
+ Coverage   80.52%   80.54%   +0.01%     
==========================================
  Files         323      323              
  Lines       21135    21158      +23     
==========================================
+ Hits        17019    17041      +22     
- Misses       4116     4117       +1     
```


| [Impacted Files](https://codecov.io/gh/jorgecarleitao/arrow2/pull/310?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao) | Coverage Δ | |
|---|---|---|
| [src/bitmap/mutable.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/310/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-c3JjL2JpdG1hcC9tdXRhYmxlLnJz) | `89.27% <50.00%> (-0.34%)` | :arrow_down: |
| [tests/it/bitmap/mutable.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/310/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-dGVzdHMvaXQvYml0bWFwL211dGFibGUucnM=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/jorgecarleitao/arrow2/pull/310?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/310?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao). Last update [bbe607c...9423aed](https://codecov.io/gh/jorgecarleitao/arrow2/pull/310?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/bitmap/mutable.rs b/src/bitmap/mutable.rs
index eed57fcdfc..f3ec798866 100644
--- a/src/bitmap/mutable.rs
+++ b/src/bitmap/mutable.rs
@@ -148,7 +148,7 @@ impl MutableBitmap {
         additional = additional.saturating_sub(added);
         if additional > 0 {
             debug_assert_eq!(self.length % 8, 0);
-            let existing = self.buffer.len();
+            let existing = self.length.saturating_add(7) / 8;
             let required = (self.length + additional).saturating_add(7) / 8;
             // add remaining as full bytes
             self.buffer.extend_from_trusted_len_iter(
@@ -182,6 +182,10 @@ impl MutableBitmap {
     /// Extends [`MutableBitmap`] by `additional` values of constant `value`.
     #[inline]
     pub fn extend_constant(&mut self, additional: usize, value: bool) {
+        if additional == 0 {
+            return;
+        }
+
         if value {
             self.extend_set(additional)
         } else {
diff --git a/src/compute/cast/mod.rs b/src/compute/cast/mod.rs
index eca849ea48..9dd5ef5f33 100644
--- a/src/compute/cast/mod.rs
+++ b/src/compute/cast/mod.rs
@@ -239,9 +239,11 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
         (Int32, Date32) => true,
         (Int32, Time32(_)) => true,
         (Date32, Int32) => true,
+        (Date32, Int64) => true,
         (Time32(_), Int32) => true,
         (Int64, Date64) => true,
         (Int64, Time64(_)) => true,
+        (Date64, Int32) => true,
         (Date64, Int64) => true,
         (Time64(_), Int64) => true,
         (Date32, Date64) => true,
@@ -674,6 +676,7 @@ fn cast_with_options(
         }
         // No support for microsecond/nanosecond with i32
         (Date32, Int32) => primitive_to_same_primitive_dyn::<i32>(array, to_type),
+        (Date32, Int64) => primitive_to_primitive_dyn::<i32, i64>(array, to_type, options),
         (Time32(_), Int32) => primitive_to_same_primitive_dyn::<i32>(array, to_type),
         (Int64, Date64) => primitive_to_same_primitive_dyn::<i64>(array, to_type),
         // No support for second/milliseconds with i64
@@ -684,6 +687,7 @@ fn cast_with_options(
             primitive_to_same_primitive_dyn::<i64>(array, to_type)
         }
 
+        (Date64, Int32) => primitive_to_primitive_dyn::<i64, i32>(array, to_type, options),
         (Date64, Int64) => primitive_to_same_primitive_dyn::<i64>(array, to_type),
         (Time64(_), Int64) => primitive_to_same_primitive_dyn::<i64>(array, to_type),
         (Date32, Date64) => primitive_dyn!(array, date32_to_date64),


Test patch:
diff --git a/tests/it/bitmap/mutable.rs b/tests/it/bitmap/mutable.rs
index fd3d719fbf..234365d67f 100644
--- a/tests/it/bitmap/mutable.rs
+++ b/tests/it/bitmap/mutable.rs
@@ -224,6 +224,11 @@ fn extend_set() {
     assert_eq!(b.as_slice(), &[0b00110000]);
     assert_eq!(b.len(), 4 + 2);
 
+    let mut b = MutableBitmap::from(&[false, false, false, false]);
+    b.extend_constant(8, true);
+    assert_eq!(b.as_slice(), &[0b11110000, 0b11111111]);
+    assert_eq!(b.len(), 4 + 8);
+
     let mut b = MutableBitmap::from(&[true, true]);
     b.extend_constant(3, true);
     assert_eq!(b.as_slice(), &[0b00011111]);
@@ -275,3 +280,35 @@ fn extend_bitmap() {
     assert_eq!(b.as_slice(), &[0b01111111, 0b00000001]);
     assert_eq!(b.len(), 5 + 4);
 }
+
+#[test]
+fn extend_constant1() {
+    use std::iter::FromIterator;
+    for i in 0..64 {
+        for j in 0..64 {
+            let mut b = MutableBitmap::new();
+            b.extend_constant(i, false);
+            b.extend_constant(j, true);
+            assert_eq!(
+                b,
+                MutableBitmap::from_iter(
+                    std::iter::repeat(false)
+                        .take(i)
+                        .chain(std::iter::repeat(true).take(j))
+                )
+            );
+
+            let mut b = MutableBitmap::new();
+            b.extend_constant(i, true);
+            b.extend_constant(j, false);
+            assert_eq!(
+                b,
+                MutableBitmap::from_iter(
+                    std::iter::repeat(true)
+                        .take(i)
+                        .chain(std::iter::repeat(false).take(j))
+                )
+            );
+        }
+    }
+}
diff --git a/tests/it/compute/cast.rs b/tests/it/compute/cast.rs
index 8abb8afc29..36ebfde4f9 100644
--- a/tests/it/compute/cast.rs
+++ b/tests/it/compute/cast.rs
@@ -378,6 +378,26 @@ fn date32_to_int32() {
     );
 }
 
+#[test]
+fn date64_to_int32() {
+    test_primitive_to_primitive(
+        &[10000i64, 17890],
+        DataType::Date64,
+        &[10000i32, 17890],
+        DataType::Int32,
+    );
+}
+
+#[test]
+fn date32_to_int64() {
+    test_primitive_to_primitive(
+        &[10000i32, 17890],
+        DataType::Date32,
+        &[10000i64, 17890],
+        DataType::Int64,
+    );
+}
+
 #[test]
 fn int32_to_date32() {
     test_primitive_to_primitive(