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-35521: [C++] Hash null bitmap only if null count is 0 #35522

Merged
merged 4 commits into from
May 11, 2023
Merged
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
5 changes: 3 additions & 2 deletions cpp/src/arrow/scalar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ struct ScalarHashImpl {

Status ArrayHash(const ArrayData& a) {
RETURN_NOT_OK(StdHash(a.length) & StdHash(a.GetNullCount()));
if (a.buffers[0] != nullptr) {
if (a.GetNullCount() != 0 && a.buffers[0] != nullptr) {
Copy link
Member

Choose a reason for hiding this comment

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

IIRC, it's also possible that a.buffers[0] == nullptr if all the elements are valid. Is it possible that we still get differing hashes in this case?

  • All elements valid and equal and validity bitmap present (would hash the validity bitmap)
  • All elements valid and equal and validity bitmap missing (would not hash the validity bitmap)

Copy link
Contributor Author

@micah-white micah-white May 10, 2023

Choose a reason for hiding this comment

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

Yes, that's the bug that I am trying to fix. Since both cases have the same semantic value, their hashes should be the same.

// We can't visit values without unboxing the whole array, so only hash
// the null bitmap for now.
// the null bitmap for now. Only hash the null bitmap if the null count
// is not 0 to ensure hash consistency.
RETURN_NOT_OK(BufferHash(*a.buffers[0]));
}
for (const auto& child : a.child_data) {
Expand Down
21 changes: 20 additions & 1 deletion cpp/src/arrow/scalar_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,6 @@ class TestListScalar : public ::testing::Test {
using ScalarType = typename TypeTraits<T>::ScalarType;

void SetUp() {
// type_ = std::make_shared<T>(int16());
type_ = MakeListType<T>(int16(), 3);
value_ = ArrayFromJSON(int16(), "[1, 2, null]");
}
Expand Down Expand Up @@ -1106,6 +1105,24 @@ class TestListScalar : public ::testing::Test {
ASSERT_RAISES(Invalid, scalar.ValidateFull());
}

void TestHashing() {
// GH-35521: the hash value of a non-null list scalar should not
// depend on the presence or absence of a null bitmap in the underlying
// list values.
ScalarType empty_bitmap_scalar(ArrayFromJSON(int16(), "[1, 2, 3]"));
ASSERT_OK(empty_bitmap_scalar.ValidateFull());
// Underlying list array doesn't have a null bitmap
ASSERT_EQ(empty_bitmap_scalar.value->data()->buffers[0], nullptr);

auto list_array = ArrayFromJSON(type_, "[[1, 2, 3], [4, 5, null]]");
ASSERT_OK_AND_ASSIGN(auto set_bitmap_scalar_uncasted, list_array->GetScalar(0));
auto set_bitmap_scalar = checked_pointer_cast<ScalarType>(set_bitmap_scalar_uncasted);
// Underlying list array has a null bitmap
ASSERT_NE(set_bitmap_scalar->value->data()->buffers[0], nullptr);
// ... yet it's hashing equal to the other scalar
ASSERT_EQ(empty_bitmap_scalar.hash(), set_bitmap_scalar->hash());
}

protected:
std::shared_ptr<DataType> type_;
std::shared_ptr<Array> value_;
Expand All @@ -1119,6 +1136,8 @@ TYPED_TEST(TestListScalar, Basics) { this->TestBasics(); }

TYPED_TEST(TestListScalar, ValidateErrors) { this->TestValidateErrors(); }

TYPED_TEST(TestListScalar, TestHashing) { this->TestHashing(); }

TEST(TestFixedSizeListScalar, ValidateErrors) {
const auto ty = fixed_size_list(int16(), 3);
FixedSizeListScalar scalar(ArrayFromJSON(int16(), "[1, 2, 5]"), ty);
Expand Down