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

GH-42198: [C++] Fix GetRecordBatchPayload crashes for device data #42199

Merged
merged 7 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
43 changes: 43 additions & 0 deletions cpp/src/arrow/gpu/cuda_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,49 @@ TEST_F(TestCudaArrowIpc, BasicWriteRead) {
CompareBatch(*batch, *cpu_batch);
}

TEST_F(TestCudaArrowIpc, WriteIpcString) {
auto values = ArrayFromJSON(utf8(), R"(["foo", null, "quux"])");
ASSERT_OK_AND_ASSIGN(auto values_device, values->CopyTo(mm_));
auto batch = RecordBatch::Make(schema({field("vals", utf8())}), 3,
{values_device->data()}, DeviceAllocationType::kCUDA);

ipc::IpcPayload payload;
ASSERT_OK(
ipc::GetRecordBatchPayload(*batch, ipc::IpcWriteOptions::Defaults(), &payload));

ASSERT_EQ(values_device->data()->buffers[0]->address(),
payload.body_buffers[0]->address());
ASSERT_EQ(values_device->data()->buffers[1]->address(),
payload.body_buffers[1]->address());
}

TEST_F(TestCudaArrowIpc, WriteIpcList) {
auto values =
ArrayFromJSON(list(utf8()), R"([["foo", null], null, ["quux", "bar", "baz"]])");
ASSERT_OK_AND_ASSIGN(auto values_device, values->CopyTo(mm_));
auto batch = RecordBatch::Make(schema({field("vals", list(utf8()))}), 3,
{values_device->data()}, DeviceAllocationType::kCUDA);

ipc::IpcPayload payload;
ASSERT_OK(
ipc::GetRecordBatchPayload(*batch, ipc::IpcWriteOptions::Defaults(), &payload));

ASSERT_EQ(values_device->data()->buffers[0]->address(),
payload.body_buffers[0]->address());
}

TEST_F(TestCudaArrowIpc, WriteIpcSlicedRecord) {
std::shared_ptr<RecordBatch> batch;
ASSERT_OK(ipc::test::MakeListRecordBatch(&batch));

ASSERT_OK_AND_ASSIGN(auto batch_device, batch->CopyTo(mm_));
auto sliced_batch_device = batch_device->Slice(10);

ipc::IpcPayload payload;
ASSERT_NOT_OK(ipc::GetRecordBatchPayload(*sliced_batch_device,
ipc::IpcWriteOptions::Defaults(), &payload));
}

TEST_F(TestCudaArrowIpc, DictionaryWriteRead) {
std::shared_ptr<RecordBatch> batch;
ASSERT_OK(ipc::test::MakeDictionary(&batch));
Expand Down
26 changes: 23 additions & 3 deletions cpp/src/arrow/ipc/writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ class RecordBatchSerializer {
return Status::CapacityError("Cannot write arrays larger than 2^31 - 1 in length");
}

if (arr.offset() != 0 && arr.device_type() != DeviceAllocationType::kCPU) {
// https://github.com/apache/arrow/issues/43029
return Status::NotImplemented("Cannot compute null count for non-cpu sliced array");
Copy link
Member

Choose a reason for hiding this comment

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

Can we link this to a follow up ticket?

Copy link
Member Author

Choose a reason for hiding this comment

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

I created a ticket #43029 and added a comment here to link to it

}

// push back all common elements
field_nodes_.push_back({arr.length(), arr.null_count(), 0});

Expand Down Expand Up @@ -449,14 +454,22 @@ class RecordBatchSerializer {

template <typename T>
enable_if_base_binary<typename T::TypeClass, Status> Visit(const T& array) {
using offset_type = typename T::offset_type;

std::shared_ptr<Buffer> value_offsets;
RETURN_NOT_OK(GetZeroBasedValueOffsets<T>(array, &value_offsets));
auto data = array.value_data();

int64_t total_data_bytes = 0;
if (value_offsets) {
total_data_bytes = array.value_offset(array.length()) - array.value_offset(0);
offset_type last_offset_value;
RETURN_NOT_OK(MemoryManager::CopyBufferSliceToCPU(
value_offsets, array.length() * sizeof(offset_type), sizeof(offset_type),
reinterpret_cast<uint8_t*>(&last_offset_value)));

total_data_bytes = last_offset_value;
Copy link
Member

Choose a reason for hiding this comment

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

Why don't we take into account offset #0 here anymore?

Copy link
Member Author

Choose a reason for hiding this comment

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

just above this we use GetZeroBasedValueOffsets and populate value_offsets with zero-indexed offsets. This means that if we grab the very last offset in that buffer (array.length() * sizeof(offset_type)) we have the full number of data bytes and we can save the extra copy to grab offset #0

}

if (NeedTruncate(array.offset(), data.get(), total_data_bytes)) {
// Slice the data buffer to include only the range we need now
const int64_t start_offset = array.value_offset(0);
Expand Down Expand Up @@ -495,8 +508,15 @@ class RecordBatchSerializer {
offset_type values_offset = 0;
offset_type values_length = 0;
if (value_offsets) {
values_offset = array.value_offset(0);
values_length = array.value_offset(array.length()) - values_offset;
RETURN_NOT_OK(MemoryManager::CopyBufferSliceToCPU(
array.value_offsets(), array.offset() * sizeof(offset_type),
sizeof(offset_type), reinterpret_cast<uint8_t*>(&values_offset)));
offset_type last_values_offset = 0;
RETURN_NOT_OK(MemoryManager::CopyBufferSliceToCPU(
array.value_offsets(), (array.offset() + array.length()) * sizeof(offset_type),
sizeof(offset_type), reinterpret_cast<uint8_t*>(&last_values_offset)));

values_length = last_values_offset - values_offset;
}

if (array.offset() != 0 || values_length < values->length()) {
Expand Down
Loading