Skip to content

Commit

Permalink
Fix merge for empty HyperLogLog (#6053)
Browse files Browse the repository at this point in the history
Summary:
Resolves prestodb/presto#20533

Pull Request resolved: #6053

Reviewed By: kgpai

Differential Revision: D48372690

Pulled By: Yuhta

fbshipit-source-id: 774ec01b9cf4021167b25677d73adebb45ff3b8f
  • Loading branch information
pramodsatya authored and facebook-github-bot committed Aug 18, 2023
1 parent 68b4834 commit eab7c05
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
17 changes: 14 additions & 3 deletions velox/common/hyperloglog/SparseHll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,26 @@ SparseHll::SparseHll(const char* serialized, HashStringAllocator* allocator)
}

void SparseHll::mergeWith(const SparseHll& other) {
mergeWith(other.entries_.size(), other.entries_.data());
auto size = other.entries_.size();
// This check prevents merge aggregation from being performed on
// empty_approx_set(), an empty HyperLogLog. The merge function typically does
// not take an empty HyperLogLog structure as an argument.
if (size) {
mergeWith(size, other.entries_.data());
}
}

void SparseHll::mergeWith(const char* serialized) {
auto stream = initializeInputStream(serialized);

auto size = stream.read<int16_t>();
mergeWith(
size, reinterpret_cast<const uint32_t*>(serialized + stream.offset()));
// This check prevents merge aggregation from being performed on
// empty_approx_set(), an empty HyperLogLog. The merge function typically does
// not take an empty HyperLogLog structure as an argument.
if (size) {
mergeWith(
size, reinterpret_cast<const uint32_t*>(serialized + stream.offset()));
}
}

void SparseHll::mergeWith(size_t otherSize, const uint32_t* otherEntries) {
Expand Down
4 changes: 4 additions & 0 deletions velox/common/hyperloglog/tests/SparseHllTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ TEST_F(SparseHllTest, mergeWith) {

// idempotent
testMergeWith(sequence(0, 100), sequence(0, 100));

// empty sequence
testMergeWith(sequence(0, 100), {});
testMergeWith({}, sequence(100, 300));
}

class SparseHllToDenseTest : public ::testing::TestWithParam<int8_t> {
Expand Down

0 comments on commit eab7c05

Please sign in to comment.