diff --git a/src/Dictionaries/HashedDictionary.cpp b/src/Dictionaries/HashedDictionary.cpp index aebaffca1f1..913f06b5e92 100644 --- a/src/Dictionaries/HashedDictionary.cpp +++ b/src/Dictionaries/HashedDictionary.cpp @@ -567,7 +567,11 @@ void HashedDictionary::calculateBytesAllocated() if constexpr (sparse || std::is_same_v) { - bytes_allocated += container.max_size() * (sizeof(KeyType) + sizeof(AttributeValueType)); + /// bucket_count() - Returns table size, that includes empty and deleted + /// size() - Returns table size, w/o empty and deleted + /// and since this is sparsehash, empty cells should not be significant, + /// and since items cannot be removed from the dictionary, deleted is also not important. + bytes_allocated += container.size() * (sizeof(KeyType) + sizeof(AttributeValueType)); bucket_count = container.bucket_count(); } else diff --git a/tests/queries/0_stateless/01802_dict_sparse_hashed_bytes_allocated.reference b/tests/queries/0_stateless/01802_dict_sparse_hashed_bytes_allocated.reference new file mode 100644 index 00000000000..c4844ab2c99 --- /dev/null +++ b/tests/queries/0_stateless/01802_dict_sparse_hashed_bytes_allocated.reference @@ -0,0 +1 @@ +4422 diff --git a/tests/queries/0_stateless/01802_dict_sparse_hashed_bytes_allocated.sql b/tests/queries/0_stateless/01802_dict_sparse_hashed_bytes_allocated.sql new file mode 100644 index 00000000000..1b69de273a2 --- /dev/null +++ b/tests/queries/0_stateless/01802_dict_sparse_hashed_bytes_allocated.sql @@ -0,0 +1,30 @@ +DROP TABLE IF EXISTS data_01802; +DROP DICTIONARY IF EXISTS dict_01802; + +CREATE TABLE data_01802 +( + id UInt64, + value UInt16 +) +ENGINE = Memory(); + +INSERT INTO data_01802 VALUES(0, 0); +INSERT INTO data_01802 VALUES(1, 0); +INSERT INTO data_01802 VALUES(2, 0); + +CREATE DICTIONARY dict_01802 +( + id UInt64, + value UInt16 +) +PRIMARY KEY id +SOURCE(CLICKHOUSE(HOST 'localhost' PORT tcpPort() USER 'default' TABLE 'data_01802')) +LIFETIME(MIN 1 MAX 1000) +LAYOUT(SPARSE_HASHED()); + +SYSTEM RELOAD DICTIONARY dict_01802; + +SELECT bytes_allocated FROM system.dictionaries WHERE database = currentDatabase() AND name = 'dict_01802'; + +DROP TABLE data_01802; +DROP DICTIONARY dict_01802;