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

ARROW-6046: [C++] Do not write excess varbinary offsets in IPC messages from sliced BinaryArray #5126

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions cpp/src/arrow/ipc/read_write_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,26 @@ TEST_P(TestIpcRoundTrip, ZeroLengthArrays) {
CheckRoundtrip(bin_array2, 1 << 20);
}

TEST_F(TestWriteRecordBatch, SliceTruncatesBinaryOffsets) {
// ARROW-6046
std::shared_ptr<Array> array;
ASSERT_OK(MakeRandomStringArray(500, false, default_memory_pool(), &array));

auto f0 = field("f0", array->type());
auto schema = ::arrow::schema({f0});
auto batch = RecordBatch::Make(schema, array->length(), {array});
auto sliced_batch = batch->Slice(0, 5);

std::stringstream ss;
ss << "test-truncate-offsets";
ASSERT_OK(
io::MemoryMapFixture::InitMemoryMap(/*buffer_size=*/1 << 20, ss.str(), &mmap_));
DictionaryMemo dictionary_memo;
std::shared_ptr<RecordBatch> result;
ASSERT_OK(DoStandardRoundTrip(*sliced_batch, &dictionary_memo, &result));
ASSERT_EQ(6 * sizeof(int32_t), result->column(0)->data()->buffers[1]->size());
}

TEST_F(TestWriteRecordBatch, SliceTruncatesBuffers) {
auto CheckArray = [this](const std::shared_ptr<Array>& array) {
auto f0 = field("f0", array->type());
Expand Down
11 changes: 8 additions & 3 deletions cpp/src/arrow/ipc/writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,14 @@ class RecordBatchSerializer : public ArrayVisitor {

auto offsets = array.value_offsets();

int64_t required_bytes = sizeof(offset_type) * (array.length() + 1);
if (array.offset() != 0) {
// If we have a non-zero offset, then the value offsets do not start at
// zero. We must a) create a new offsets array with shifted offsets and
// b) slice the values array accordingly

std::shared_ptr<Buffer> shifted_offsets;
RETURN_NOT_OK(AllocateBuffer(pool_, sizeof(offset_type) * (array.length() + 1),
&shifted_offsets));
RETURN_NOT_OK(AllocateBuffer(pool_, required_bytes, &shifted_offsets));

offset_type* dest_offsets =
reinterpret_cast<offset_type*>(shifted_offsets->mutable_data());
Expand All @@ -249,8 +249,13 @@ class RecordBatchSerializer : public ArrayVisitor {
// Final offset
dest_offsets[array.length()] = array.value_offset(array.length()) - start_offset;
offsets = shifted_offsets;
} else {
// ARROW-6046: Slice offsets to used extent, in case we have a truncated
// slice
if (offsets != nullptr && offsets->size() > required_bytes) {
offsets = SliceBuffer(offsets, 0, required_bytes);
}
}

*value_offsets = offsets;
return Status::OK();
}
Expand Down