Skip to content

Commit 25c6a79

Browse files
tcm-marcelpmenon
authored andcommitted
Fix HashTable::BuildLazy if it contains zero emelemts (cmu-db#1408)
* Fix HashTable::BuildLazy if it contains zero emelemts * Add test case for empty hash table
1 parent ad83af4 commit 25c6a79

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/codegen/util/hash_table.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ char *HashTable::Insert(uint64_t hash) {
191191
}
192192

193193
void HashTable::BuildLazy() {
194+
// Early exit if no elements have been added (hash table is still valid)
195+
if (num_elems_ == 0) return;
196+
194197
// Grab entry head
195198
Entry *head = directory_[0];
196199

test/codegen/hash_table_test.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,33 @@ TEST_F(HashTableTest, CanInsertUniqueKeys) {
105105
}
106106
}
107107

108+
TEST_F(HashTableTest, BuildEmptyHashTable) {
109+
codegen::util::HashTable table{GetMemPool(), sizeof(Key), sizeof(Value)};
110+
111+
std::vector<Key> keys;
112+
113+
// Insert keys
114+
for (uint32_t i = 0; i < 10; i++) {
115+
Key k{1, i};
116+
// do NOT insert into hash table
117+
118+
keys.emplace_back(k);
119+
}
120+
121+
EXPECT_EQ(0, table.NumElements());
122+
123+
// Build lazy
124+
table.BuildLazy();
125+
126+
// Lookups should succeed
127+
for (const auto &key : keys) {
128+
std::function<void(const Value &v)> f =
129+
[](UNUSED_ATTRIBUTE const Value &v) {};
130+
auto ret = table.TypedProbe(key.Hash(), key, f);
131+
EXPECT_EQ(false, ret);
132+
}
133+
}
134+
108135
TEST_F(HashTableTest, CanInsertDuplicateKeys) {
109136
codegen::util::HashTable table{GetMemPool(), sizeof(Key), sizeof(Value)};
110137

0 commit comments

Comments
 (0)