Repository: jorgecarleitao/arrow2
Pull request #304 (2021-08-20 07:11:44)

Added support to read json to `BinaryArray`
To keep up with the arrow-rs implementation (https://github.com/apache/arrow-rs/pull/702)

codecov[bot]: # [Codecov](https://codecov.io/gh/jorgecarleitao/arrow2/pull/304?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao) Report
> Merging [#304](https://codecov.io/gh/jorgecarleitao/arrow2/pull/304?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao) (fb8b447) 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.00%`.
> The diff coverage is `92.30%`.

[![Impacted file tree graph](https://codecov.io/gh/jorgecarleitao/arrow2/pull/304/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/304?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     #304   +/-   ##
=======================================
  Coverage   80.52%   80.53%           
=======================================
  Files         323      323           
  Lines       21135    21147   +12     
=======================================
+ Hits        17019    17030   +11     
- Misses       4116     4117    +1     
```


| [Impacted Files](https://codecov.io/gh/jorgecarleitao/arrow2/pull/304?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao) | Coverage Δ | |
|---|---|---|
| [src/io/json/read/deserialize.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/304/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-c3JjL2lvL2pzb24vcmVhZC9kZXNlcmlhbGl6ZS5ycw==) | `71.64% <85.71%> (+0.77%)` | :arrow_up: |
| [tests/it/io/json/mod.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/304/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-dGVzdHMvaXQvaW8vanNvbi9tb2QucnM=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/jorgecarleitao/arrow2/pull/304?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/304?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao). Last update [bbe607c...fb8b447](https://codecov.io/gh/jorgecarleitao/arrow2/pull/304?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/io/json/read/deserialize.rs b/src/io/json/read/deserialize.rs
index 18c7499861..03fb257a27 100644
--- a/src/io/json/read/deserialize.rs
+++ b/src/io/json/read/deserialize.rs
@@ -25,10 +25,7 @@ use serde_json::Value;
 
 use crate::types::NaturalDataType;
 use crate::{
-    array::{
-        Array, BooleanArray, DictionaryArray, DictionaryKey, ListArray, NullArray, Offset,
-        PrimitiveArray, StructArray, Utf8Array,
-    },
+    array::*,
     bitmap::MutableBitmap,
     buffer::MutableBuffer,
     datatypes::{DataType, IntervalUnit},
@@ -90,6 +87,14 @@ fn read_primitive<T: NativeType + NaturalDataType + NumCast>(
     PrimitiveArray::from_trusted_len_iter(iter).to(data_type)
 }
 
+fn read_binary<O: Offset>(rows: &[&Value]) -> BinaryArray<O> {
+    let iter = rows.iter().map(|row| match row {
+        Value::String(v) => Some(v.as_bytes()),
+        _ => None,
+    });
+    BinaryArray::from_trusted_len_iter(iter)
+}
+
 fn read_boolean(rows: &[&Value]) -> BooleanArray {
     let iter = rows.iter().map(|row| match row {
         Value::Bool(v) => Some(v),
@@ -236,9 +241,8 @@ pub fn read(rows: &[&Value], data_type: DataType) -> Arc<dyn Array> {
         DataType::LargeUtf8 => Arc::new(read_utf8::<i64>(rows)),
         DataType::List(_) => Arc::new(read_list::<i32>(rows, data_type)),
         DataType::LargeList(_) => Arc::new(read_list::<i64>(rows, data_type)),
-        // unsupported
-        //DataType::Binary => Box::new(BinaryArray::<i32>::new_empty()),
-        //DataType::LargeBinary => Box::new(BinaryArray::<i64>::new_empty()),
+        DataType::Binary => Arc::new(read_binary::<i32>(rows)),
+        DataType::LargeBinary => Arc::new(read_binary::<i64>(rows)),
         DataType::Struct(_) => Arc::new(read_struct(rows, data_type)),
         DataType::Dictionary(key_type, _) => match key_type.as_ref() {
             DataType::Int8 => Arc::new(read_dictionary::<i8>(rows, data_type)),


Test patch:
diff --git a/tests/it/io/json/mod.rs b/tests/it/io/json/mod.rs
index 37810e9ec3..b8bc9a5f7b 100644
--- a/tests/it/io/json/mod.rs
+++ b/tests/it/io/json/mod.rs
@@ -167,8 +167,8 @@ fn case_basics() -> (String, Schema, Vec<Box<dyn Array>>) {
 }
 
 fn case_basics_schema() -> (String, Schema, Vec<Box<dyn Array>>) {
-    let data = r#"{"a":1, "b":2.0, "c":false, "d":"4"}
-    {"a":10, "b":-3.5, "c":true, "d":null}
+    let data = r#"{"a":1, "b":2.0, "c":false, "d":"4", "e":"4"}
+    {"a":10, "b":-3.5, "c":true, "d":null, "e":"text"}
     {"a":100000000, "b":0.6, "d":"text"}"#
         .to_string();
     let schema = Schema::new(vec![
@@ -176,11 +176,17 @@ fn case_basics_schema() -> (String, Schema, Vec<Box<dyn Array>>) {
         Field::new("b", DataType::Float32, true),
         Field::new("c", DataType::Boolean, true),
         // note how "d" is not here
+        Field::new("e", DataType::Binary, true),
     ]);
     let columns = vec![
         Box::new(UInt32Array::from_slice(&[1, 10, 100000000])) as Box<dyn Array>,
         Box::new(Float32Array::from_slice(&[2.0, -3.5, 0.6])),
         Box::new(BooleanArray::from(&[Some(false), Some(true), None])),
+        Box::new(BinaryArray::<i32>::from(&[
+            Some(b"4".as_ref()),
+            Some(b"text".as_ref()),
+            None,
+        ])),
     ];
     (data, schema, columns)
 }