diff --git a/dbms/include/DB/Common/HashTable/HashMap.h b/dbms/include/DB/Common/HashTable/HashMap.h index b36d62e9539..20cbb616aa2 100644 --- a/dbms/include/DB/Common/HashTable/HashMap.h +++ b/dbms/include/DB/Common/HashTable/HashMap.h @@ -74,6 +74,25 @@ struct HashMapCell }; +template +struct HashMapCellWithSavedHash : public HashMapCell +{ + typedef HashMapCell Base; + + size_t saved_hash; + + HashMapCellWithSavedHash() : Base() {} + HashMapCellWithSavedHash(const Key & key_, const typename Base::State & state) : Base(key_, state) {} + HashMapCellWithSavedHash(const typename Base::value_type & value_, const typename Base::State & state) : Base(value_, state) {} + + bool keyEquals(const Key & key_) const { return this->value.first == key_; } + bool keyEquals(const HashMapCellWithSavedHash & other) const { return saved_hash == other.saved_hash && this->value.first == other.value.first; } + + void setHash(size_t hash_value) { saved_hash = hash_value; } + size_t getHash(const Hash & hash) const { return saved_hash; } +}; + + template < typename Key, @@ -112,3 +131,14 @@ template typename Allocator = HashTableAllocator > using HashMap = HashMapTable, Hash, Grower, Allocator>; + + +template +< + typename Key, + typename Mapped, + typename Hash = DefaultHash, + typename Grower = HashTableGrower<>, + typename Allocator = HashTableAllocator +> +using HashMapWithSavedHash = HashMapTable, Hash, Grower, Allocator>; diff --git a/dbms/include/DB/Common/HashTable/HashSet.h b/dbms/include/DB/Common/HashTable/HashSet.h index c9ce37c6627..6c53d34f9f2 100644 --- a/dbms/include/DB/Common/HashTable/HashSet.h +++ b/dbms/include/DB/Common/HashTable/HashSet.h @@ -14,15 +14,16 @@ template < typename Key, + typename TCell, typename Hash = DefaultHash, typename Grower = HashTableGrower<>, typename Allocator = HashTableAllocator > -class HashSet : public HashTable, Hash, Grower, Allocator> +class HashSetTable : public HashTable { public: - typedef HashSet Self; - typedef HashTableCell Cell; + typedef HashSetTable Self; + typedef TCell Cell; void merge(const Self & rhs) { @@ -55,3 +56,41 @@ public: } } }; + + +template +struct HashSetCellWithSavedHash : public HashTableCell +{ + typedef HashTableCell Base; + + size_t saved_hash; + + HashSetCellWithSavedHash() : Base() {} + HashSetCellWithSavedHash(const Key & key_, const typename Base::State & state) : Base(key_, state) {} + + bool keyEquals(const Key & key_) const { return this->key == key_; } + bool keyEquals(const HashSetCellWithSavedHash & other) const { return saved_hash == other.saved_hash && this->key == other.key; } + + void setHash(size_t hash_value) { saved_hash = hash_value; } + size_t getHash(const Hash & hash) const { return saved_hash; } +}; + + +template +< + typename Key, + typename Hash = DefaultHash, + typename Grower = HashTableGrower<>, + typename Allocator = HashTableAllocator +> +using HashSet = HashSetTable, Hash, Grower, Allocator>; + + +template +< + typename Key, + typename Hash = DefaultHash, + typename Grower = HashTableGrower<>, + typename Allocator = HashTableAllocator +> +using HashSetWithSavedHash = HashSetTable, Hash, Grower, Allocator>; diff --git a/dbms/include/DB/Interpreters/Aggregator.h b/dbms/include/DB/Interpreters/Aggregator.h index a79858db5bb..25f6cca3d39 100644 --- a/dbms/include/DB/Interpreters/Aggregator.h +++ b/dbms/include/DB/Interpreters/Aggregator.h @@ -48,7 +48,7 @@ typedef std::vector AggregateDescriptions; */ typedef AggregateDataPtr AggregatedDataWithoutKey; typedef HashMap AggregatedDataWithUInt64Key; -typedef HashMap AggregatedDataWithStringKey; +typedef HashMapWithSavedHash AggregatedDataWithStringKey; typedef HashMap AggregatedDataWithKeys128; typedef HashMap, UInt128TrivialHash> AggregatedDataHashed; diff --git a/dbms/include/DB/Interpreters/Set.h b/dbms/include/DB/Interpreters/Set.h index 384fde072ed..91393c32d80 100644 --- a/dbms/include/DB/Interpreters/Set.h +++ b/dbms/include/DB/Interpreters/Set.h @@ -94,7 +94,7 @@ private: * одного или нескольких столбцов значений множеству. */ typedef HashSet SetUInt64; - typedef HashSet SetString; + typedef HashSetWithSavedHash SetString; typedef HashSet SetHashed; BlockInputStreamPtr source;