diff --git a/be/src/vec/common/hash_table/ph_hash_map.h b/be/src/vec/common/hash_table/ph_hash_map.h index da51f31cf93fee..61e7375cb86e2b 100644 --- a/be/src/vec/common/hash_table/ph_hash_map.h +++ b/be/src/vec/common/hash_table/ph_hash_map.h @@ -129,11 +129,16 @@ class PHHashMap : private boost::noncopyable { void ALWAYS_INLINE emplace(KeyHolder&& key_holder, LookupResult& it, bool& inserted) { const auto& key = key_holder_get_key(key_holder); inserted = false; - it = &*_hash_map.lazy_emplace(key, [&](const auto& ctor) { - inserted = true; - key_holder_persist_key(key_holder); - ctor(key_holder_get_key(key_holder), nullptr); - }); + try { + it = &*_hash_map.lazy_emplace(key, [&](const auto& ctor) { + inserted = true; + key_holder_persist_key(key_holder); + ctor(key_holder_get_key(key_holder), nullptr); + }); + } catch (std::bad_alloc const& e) { + _hash_map.erase(key); + throw e; + } if constexpr (PartitionedHashTable) { _check_if_need_partition(); @@ -143,10 +148,15 @@ class PHHashMap : private boost::noncopyable { template void ALWAYS_INLINE lazy_emplace(KeyHolder&& key_holder, LookupResult& it, Func&& f) { const auto& key = key_holder_get_key(key_holder); - it = &*_hash_map.lazy_emplace(key, [&](const auto& ctor) { - key_holder_persist_key(key_holder); - f(ctor, key); - }); + try { + it = &*_hash_map.lazy_emplace(key, [&](const auto& ctor) { + key_holder_persist_key(key_holder); + f(ctor, key); + }); + } catch (std::bad_alloc const& e) { + _hash_map.erase(key); + throw e; + } if constexpr (PartitionedHashTable) { _check_if_need_partition(); @@ -158,11 +168,16 @@ class PHHashMap : private boost::noncopyable { bool& inserted) { const auto& key = key_holder_get_key(key_holder); inserted = false; - it = &*_hash_map.lazy_emplace_with_hash(key, hash_value, [&](const auto& ctor) { - inserted = true; - key_holder_persist_key(key_holder); - ctor(key, nullptr); - }); + try { + it = &*_hash_map.lazy_emplace_with_hash(key, hash_value, [&](const auto& ctor) { + inserted = true; + key_holder_persist_key(key_holder); + ctor(key, nullptr); + }); + } catch (std::bad_alloc const& e) { + _hash_map.erase(key); + throw e; + } if constexpr (PartitionedHashTable) { _check_if_need_partition(); @@ -174,10 +189,15 @@ class PHHashMap : private boost::noncopyable { Func&& f) { const auto& key = key_holder_get_key(key_holder); - it = &*_hash_map.lazy_emplace_with_hash(key, hash_value, [&](const auto& ctor) { - key_holder_persist_key(key_holder); - f(ctor, key); - }); + try { + it = &*_hash_map.lazy_emplace_with_hash(key, hash_value, [&](const auto& ctor) { + key_holder_persist_key(key_holder); + f(ctor, key); + }); + } catch (std::bad_alloc const& e) { + _hash_map.erase(key); + throw e; + } if constexpr (PartitionedHashTable) { _check_if_need_partition();