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

Write validity buffer for UnionArray in V4 IPC message #1794

Merged
merged 4 commits into from
Jun 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 80 additions & 6 deletions arrow/src/ipc/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ impl IpcDataGenerator {
offset,
array.len(),
array.null_count(),
write_options,
);
}

Expand Down Expand Up @@ -389,6 +390,7 @@ impl IpcDataGenerator {
0,
array_data.len(),
array_data.null_count(),
write_options,
);

// write data
Expand Down Expand Up @@ -849,7 +851,18 @@ fn write_continuation<W: Write>(
Ok(written)
}

/// In V4, null types have no validity bitmap
/// In V5 and later, null and union types have no validity bitmap
fn has_validity_bitmap(data_type: &DataType, write_options: &IpcWriteOptions) -> bool {
if write_options.metadata_version < ipc::MetadataVersion::V5 {
!matches!(data_type, DataType::Null)
} else {
!matches!(data_type, DataType::Null | DataType::Union(_, _, _))
}
}

/// Write array data to a vector of bytes
#[allow(clippy::too_many_arguments)]
fn write_array_data(
array_data: &ArrayData,
buffers: &mut Vec<ipc::Buffer>,
Expand All @@ -858,6 +871,7 @@ fn write_array_data(
offset: i64,
num_rows: usize,
null_count: usize,
write_options: &IpcWriteOptions,
) -> i64 {
let mut offset = offset;
if !matches!(array_data.data_type(), DataType::Null) {
Expand All @@ -867,12 +881,7 @@ fn write_array_data(
// where null_count is always 0.
nodes.push(ipc::FieldNode::new(num_rows as i64, num_rows as i64));
}
// NullArray does not have any buffers, thus the null buffer is not generated
// UnionArray does not have a validity buffer
if !matches!(
array_data.data_type(),
DataType::Null | DataType::Union(_, _, _)
) {
if has_validity_bitmap(array_data.data_type(), write_options) {
// write null buffer if exists
let null_buffer = match array_data.null_buffer() {
None => {
Expand Down Expand Up @@ -904,6 +913,7 @@ fn write_array_data(
offset,
data_ref.len(),
data_ref.null_count(),
write_options,
);
});
}
Expand Down Expand Up @@ -1433,4 +1443,68 @@ mod tests {
},
);
}

fn write_union_file(options: IpcWriteOptions) {
let schema = Schema::new(vec![Field::new(
"union",
DataType::Union(
vec![
Field::new("a", DataType::Int32, false),
Field::new("c", DataType::Float64, false),
],
vec![0, 1],
UnionMode::Sparse,
),
true,
)]);
let mut builder = UnionBuilder::new_sparse(5);
builder.append::<Int32Type>("a", 1).unwrap();
builder.append_null::<Int32Type>("a").unwrap();
builder.append::<Float64Type>("c", 3.0).unwrap();
builder.append_null::<Float64Type>("c").unwrap();
builder.append::<Int32Type>("a", 4).unwrap();
let union = builder.build().unwrap();

let batch = RecordBatch::try_new(
Arc::new(schema.clone()),
vec![Arc::new(union) as ArrayRef],
)
.unwrap();
let file_name = "target/debug/testdata/union.arrow_file";
{
let file = File::create(&file_name).unwrap();
let mut writer =
FileWriter::try_new_with_options(file, &schema, options).unwrap();

writer.write(&batch).unwrap();
writer.finish().unwrap();
}

{
let file = File::open(&file_name).unwrap();
let reader = FileReader::try_new(file, None).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure how hard this would be to test, but perhaps we should test what null mask is actually written (I think the reader simply ignores it)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea, the reader just ignores it like C++ reader does. So I skip null mask test.

reader.for_each(|maybe_batch| {
maybe_batch
.unwrap()
.columns()
.iter()
.zip(batch.columns())
.for_each(|(a, b)| {
assert_eq!(a.data_type(), b.data_type());
assert_eq!(a.len(), b.len());
assert_eq!(a.null_count(), b.null_count());
});
});
}
}

#[test]
fn test_write_union_file_v4_v5() {
write_union_file(
IpcWriteOptions::try_new(8, false, MetadataVersion::V4).unwrap(),
);
write_union_file(
IpcWriteOptions::try_new(8, false, MetadataVersion::V5).unwrap(),
);
}
}