Skip to content

Commit

Permalink
wip: add a kinda hack-ish (?) way to get the hash table key
Browse files Browse the repository at this point in the history
  • Loading branch information
akuzm committed Sep 19, 2019
1 parent acf3fd2 commit c00ddeb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
4 changes: 2 additions & 2 deletions dbms/src/Common/ColumnsHashingImpl.h
Expand Up @@ -191,13 +191,13 @@ class HashMethodBase

if constexpr (has_mapped)
{
cache.value.first = keyHolderGetKey(key_holder);
cache.value.first = Data::getKeyByMapped(it);
cache.value.second = *it;
cached = &cache.value.second;
}
else
{
cache.value = keyHolderGetKey(key_holder);
cache.value = Data::getKeyByMapped(it);
}
}

Expand Down
6 changes: 6 additions & 0 deletions dbms/src/Common/HashTable/HashMap.h
Expand Up @@ -56,6 +56,7 @@ struct HashMapCell

const value_type & getValue() const { return value; }

Key getKey() const { return value.first; }
static const Key & getKey(const value_type & value) { return value.first; }

bool keyEquals(const Key & key_) const { return value.first == key_; }
Expand All @@ -79,6 +80,11 @@ struct HashMapCell

mapped_type * getMapped() { return &value.second; }
const mapped_type * getMapped() const { return &value.second; }
static HashMapCell * getCellByMapped(mapped_type * mapped)
{
static constexpr auto offset = offsetof(HashMapCell, value) + offsetof(value_type, second);
return reinterpret_cast<HashMapCell *>(reinterpret_cast<char *>(mapped) - offset);
}

/// Serialization, in binary and text form.
void write(DB::WriteBuffer & wb) const
Expand Down
10 changes: 10 additions & 0 deletions dbms/src/Common/HashTable/HashTable.h
Expand Up @@ -103,6 +103,7 @@ struct HashTableCell
const value_type & getValue() const { return key; }

/// Get the key.
Key getKey() const { return key; }
static const Key & getKey(const value_type & value) { return value; }

/// Are the keys at the cells equal?
Expand Down Expand Up @@ -134,6 +135,10 @@ struct HashTableCell
bool isDeleted() const { return false; }

void * getMapped() { return this; }
static HashTableCell * getCellByMapped(void * mapped)
{
return reinterpret_cast<HashTableCell *>(mapped);
}

/// Serialization, in binary and text form.
void write(DB::WriteBuffer & wb) const { DB::writeBinary(key, wb); }
Expand Down Expand Up @@ -788,6 +793,11 @@ class HashTable :
emplaceNonZero(key_holder, it, inserted, hash_value);
}

static Key getKeyByMapped(MappedPtr mapped)
{
return Cell::getCellByMapped(mapped)->getKey();
}

/// Copy the cell from another hash table. It is assumed that the cell is not zero, and also that there was no such key in the table yet.
void ALWAYS_INLINE insertUniqueNonZero(const Cell * cell, size_t hash_value)
{
Expand Down
5 changes: 5 additions & 0 deletions dbms/src/Common/HashTable/TwoLevelHashTable.h
Expand Up @@ -256,6 +256,11 @@ class TwoLevelHashTable :
it = impl_it;
}

static Key getKeyByMapped(MappedPtr mapped)
{
return Cell::getCellByMapped(mapped)->getKey();
}

MappedPtr ALWAYS_INLINE find(Key x, size_t hash_value)
{
size_t buck = getBucketFromHash(hash_value);
Expand Down
12 changes: 6 additions & 6 deletions dbms/src/Interpreters/SetVariants.h
Expand Up @@ -22,15 +22,15 @@ namespace DB


/// For the case where there is one numeric key.
template <typename FieldType, typename TData> /// UInt8/16/32/64 for any types with corresponding bit width.
template <typename FieldType, typename TData, bool use_cache = true> /// UInt8/16/32/64 for any types with corresponding bit width.
struct SetMethodOneNumber
{
using Data = TData;
using Key = typename Data::key_type;

Data data;

using State = ColumnsHashing::HashMethodOneNumber<typename Data::value_type, void, FieldType>;
using State = ColumnsHashing::HashMethodOneNumber<typename Data::value_type, void, FieldType, use_cache>;
};

/// For the case where there is one string key.
Expand Down Expand Up @@ -183,8 +183,8 @@ struct SetMethodHashed
*/
struct NonClearableSet
{
std::unique_ptr<SetMethodOneNumber<UInt8, FixedHashSet<UInt8>>> key8;
std::unique_ptr<SetMethodOneNumber<UInt16, FixedHashSet<UInt16>>> key16;
std::unique_ptr<SetMethodOneNumber<UInt8, FixedHashSet<UInt8>, false>> key8;
std::unique_ptr<SetMethodOneNumber<UInt16, FixedHashSet<UInt16>, false>> key16;

/** Also for the experiment was tested the ability to use SmallSet,
* as long as the number of elements in the set is small (and, if necessary, converted to a full-fledged HashSet).
Expand All @@ -209,8 +209,8 @@ struct NonClearableSet

struct ClearableSet
{
std::unique_ptr<SetMethodOneNumber<UInt8, FixedClearableHashSet<UInt8>>> key8;
std::unique_ptr<SetMethodOneNumber<UInt16, FixedClearableHashSet<UInt16>>> key16;
std::unique_ptr<SetMethodOneNumber<UInt8, FixedClearableHashSet<UInt8>, false>> key8;
std::unique_ptr<SetMethodOneNumber<UInt16, FixedClearableHashSet<UInt16>, false>> key16;

std::unique_ptr<SetMethodOneNumber<UInt32, ClearableHashSet<UInt32, HashCRC32<UInt32>>>> key32;
std::unique_ptr<SetMethodOneNumber<UInt64, ClearableHashSet<UInt64, HashCRC32<UInt64>>>> key64;
Expand Down

0 comments on commit c00ddeb

Please sign in to comment.