Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read/write nested dictionary under map in ipc stream reader/writer #1583

Merged
merged 4 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion arrow/src/datatypes/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl Field {
DataType::List(field)
| DataType::LargeList(field)
| DataType::FixedSizeList(field, _)
| DataType::Map(field, _) => collected_fields.push(field),
| DataType::Map(field, _) => collected_fields.extend(field.fields()),
DataType::Dictionary(_, value_field) => {
collected_fields.append(&mut self._fields(value_field.as_ref()))
}
Expand Down
51 changes: 51 additions & 0 deletions arrow/src/ipc/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1481,4 +1481,55 @@ mod tests {
let output_batch = roundtrip_ipc_stream(&input_batch);
assert_eq!(input_batch, output_batch);
}

#[test]
fn test_roundtrip_stream_nested_dict_dict_in_map() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn test_roundtrip_stream_nested_dict_dict_in_map() {
fn test_roundtrip_stream_nested_dict_of_map_of_dict() {

let values = StringArray::from_iter_values(["a", "b", "c"]);
let keys = Int8Array::from_iter_values([0, 0, 1, 2, 0, 1]);
let dict_array = DictionaryArray::<Int8Type>::try_new(&keys, &values).unwrap();

let keys_array = Int32Array::from_iter_values([0, 0, 1, 2, 0, 1]);
let keys_field = Field::new("keys", DataType::Int32, false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to test dictionary encoded keys as well

let values_field = Field::new_dict(
"values",
DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Utf8)),
false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also test some NULLs

1,
false,
);
let entry_struct = StructArray::from(vec![
(keys_field, make_array(keys_array.data().clone())),
(values_field, make_array(dict_array.data().clone())),
]);
let map_data_type = DataType::Map(
Box::new(Field::new(
"entries",
entry_struct.data_type().clone(),
true,
)),
false,
);

let entry_offsets = Buffer::from_slice_ref(&[0, 2, 4, 6]);
let map_data = ArrayData::builder(map_data_type)
.len(3)
.add_buffer(entry_offsets)
.add_child_data(entry_struct.data().clone())
.build()
.unwrap();
let map_array = MapArray::from(map_data);

let dict_dict_array =
DictionaryArray::<Int8Type>::try_new(&keys, &map_array).unwrap();

let schema = Arc::new(Schema::new(vec![Field::new(
"f1",
dict_dict_array.data_type().clone(),
false,
)]));
let input_batch =
RecordBatch::try_new(schema, vec![Arc::new(dict_dict_array)]).unwrap();
let output_batch = roundtrip_ipc_stream(&input_batch);
assert_eq!(input_batch, output_batch);
}
}
33 changes: 31 additions & 2 deletions arrow/src/ipc/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ use std::io::{BufWriter, Write};
use flatbuffers::FlatBufferBuilder;

use crate::array::{
as_list_array, as_struct_array, as_union_array, make_array, ArrayData, ArrayRef,
as_list_array, as_map_array, as_struct_array, as_union_array, make_array, Array,
ArrayData, ArrayRef,
};
use crate::buffer::{Buffer, MutableBuffer};
use crate::datatypes::*;
Expand Down Expand Up @@ -146,7 +147,7 @@ impl IpcDataGenerator {
dictionary_tracker: &mut DictionaryTracker,
write_options: &IpcWriteOptions,
) -> Result<()> {
// TODO: Handle other nested types (map, etc)
// TODO: Handle other nested types (LargeList, FixedSizeList)
match column.data_type() {
DataType::Struct(fields) => {
let s = as_struct_array(column);
Expand All @@ -170,6 +171,34 @@ impl IpcDataGenerator {
write_options,
)?;
}
DataType::Map(field, _) => {
let map_array = as_map_array(column);

let (keys, values) = match field.data_type() {
DataType::Struct(fields) if fields.len() == 2 => {
(&fields[0], &fields[1])
}
_ => panic!("Incorrect field data type {:?}", field.data_type()),
};

// keys
self.encode_dictionaries(
keys,
&map_array.keys(),
encoded_dictionaries,
dictionary_tracker,
write_options,
)?;

// values
self.encode_dictionaries(
values,
&map_array.values(),
encoded_dictionaries,
dictionary_tracker,
write_options,
)?;
}
DataType::Union(fields, _) => {
let union = as_union_array(column);
for (field, ref column) in fields
Expand Down