Repository: jorgecarleitao/arrow2
Pull request #308 (2021-08-21 12:14:35)

add cast `date32 -> i64` and `date64 -> i32`
This add casting the primitive values of datetypes, date32, date64 to integers.

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

[![Impacted file tree graph](https://codecov.io/gh/jorgecarleitao/arrow2/pull/308/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/308?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     #308      +/-   ##
==========================================
+ Coverage   80.57%   80.59%   +0.01%     
==========================================
  Files         324      324              
  Lines       21221    21231      +10     
==========================================
+ Hits        17099    17111      +12     
+ Misses       4122     4120       -2     
```


| [Impacted Files](https://codecov.io/gh/jorgecarleitao/arrow2/pull/308?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao) | Coverage Δ | |
|---|---|---|
| [src/compute/cast/mod.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/308/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-c3JjL2NvbXB1dGUvY2FzdC9tb2QucnM=) | `89.31% <100.00%> (+0.08%)` | :arrow_up: |
| [tests/it/compute/cast.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/308/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-dGVzdHMvaXQvY29tcHV0ZS9jYXN0LnJz) | `99.32% <100.00%> (+0.01%)` | :arrow_up: |
| [src/io/json/read/reader.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/308/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-c3JjL2lvL2pzb24vcmVhZC9yZWFkZXIucnM=) | `75.67% <0.00%> (+1.35%)` | :arrow_up: |
| [src/io/json\_integration/write.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/308/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-c3JjL2lvL2pzb25faW50ZWdyYXRpb24vd3JpdGUucnM=) | `6.25% <0.00%> (+6.25%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/jorgecarleitao/arrow2/pull/308?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/308?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao). Last update [2873e1a...524ea7f](https://codecov.io/gh/jorgecarleitao/arrow2/pull/308?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..51d9e5492b 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(
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/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(