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

[C++] Fix BufferBuilder edge case where all inputs were length-0 #86

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions cpp/src/feather/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,20 @@ class BufferBuilder {
RETURN_NOT_OK(buffer_->Resize(capacity_));
data_ = buffer_->mutable_data();
}
memcpy(data_ + size_, data, length);
size_ += length;
if (length > 0) {
memcpy(data_ + size_, data, length);
size_ += length;
}
return Status::OK();
}

std::shared_ptr<Buffer> Finish() {
auto result = buffer_;
std::shared_ptr<Buffer> result;
if (data_ == nullptr) {
result = std::make_shared<Buffer>(nullptr, 0);
} else {
result = buffer_;
}
buffer_ = nullptr;
return result;
}
Expand Down
13 changes: 13 additions & 0 deletions cpp/src/feather/tests/io-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,17 @@ TEST(FileOutputStream, NonExistentDirectory) {
ASSERT_TRUE(s.IsIOError());
}

TEST(BufferBuilder, EmptyStrings) {
BufferBuilder builder;

builder.Append(nullptr, 0);
builder.Append(nullptr, 0);
builder.Append(nullptr, 0);

std::shared_ptr<Buffer> result = builder.Finish();

ASSERT_EQ(nullptr, result->data());
ASSERT_EQ(0, result->size());
}

} // namespace feather
4 changes: 4 additions & 0 deletions python/feather/tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ def test_strings(self):
expected = pd.DataFrame({'strings': values * repeats})
self._check_pandas_roundtrip(df, expected)

def test_empty_strings(self):
df = pd.DataFrame({'strings': [''] * 10})
self._check_pandas_roundtrip(df)

def test_nan_as_null(self):
# Create a nan that is not numpy.nan
values = np.array(['foo', np.nan, np.nan * 2, 'bar'] * 10)
Expand Down