Skip to content

Commit

Permalink
Support binary data type in build_struct_array. (#702)
Browse files Browse the repository at this point in the history
* Support binary data type in `build_struct_array`.

* Modify test case.

* cargo fmt

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
  • Loading branch information
zijie0 and alamb authored Aug 21, 2021
1 parent 38e5d22 commit 8308615
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion arrow/src/array/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ mod tests {
"entry",
DataType::Struct(vec![
Field::new("key", DataType::Utf8, false),
Field::new("key", DataType::Int32, true),
Field::new("value", DataType::Int32, true),
]),
false,
)),
Expand Down
40 changes: 40 additions & 0 deletions arrow/src/json/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,14 @@ impl Decoder {
})
.collect::<StringArray>(),
) as ArrayRef),
DataType::Binary => Ok(Arc::new(
rows.iter()
.map(|row| {
let maybe_value = row.get(field.name());
maybe_value.and_then(|value| value.as_str())
})
.collect::<BinaryArray>(),
) as ArrayRef),
DataType::List(ref list_field) => {
match list_field.data_type() {
DataType::Dictionary(ref key_ty, _) => {
Expand Down Expand Up @@ -3140,6 +3148,38 @@ mod tests {
assert_eq!(batch.num_rows(), 3);
}

#[test]
fn test_json_read_binary_structs() {
let schema = Schema::new(vec![Field::new("c1", DataType::Binary, true)]);
let decoder = Decoder::new(Arc::new(schema), 1024, None);
let batch = decoder
.next_batch(
&mut vec![
Ok(serde_json::json!({
"c1": "₁₂₃",
})),
Ok(serde_json::json!({
"c1": "foo",
})),
]
.into_iter(),
)
.unwrap()
.unwrap();
let data = batch.columns().iter().collect::<Vec<_>>();

let schema = Schema::new(vec![Field::new("c1", DataType::Binary, true)]);
let binary_values = BinaryArray::from(vec!["₁₂₃".as_bytes(), "foo".as_bytes()]);
let expected_batch =
RecordBatch::try_new(Arc::new(schema), vec![Arc::new(binary_values)])
.unwrap();
let expected_data = expected_batch.columns().iter().collect::<Vec<_>>();

assert_eq!(data, expected_data);
assert_eq!(batch.num_columns(), 1);
assert_eq!(batch.num_rows(), 2);
}

#[test]
fn test_json_iterator() {
let builder = ReaderBuilder::new().infer_schema(None).with_batch_size(5);
Expand Down

0 comments on commit 8308615

Please sign in to comment.