2016-12-21 12:10:31 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/AggregationCommon.h>
|
2019-02-04 14:36:15 +00:00
|
|
|
#include <Common/ColumnsHashing.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Arena.h>
|
|
|
|
#include <Common/HashTable/HashSet.h>
|
2017-04-07 16:13:25 +00:00
|
|
|
#include <Common/HashTable/ClearableHashSet.h>
|
2019-02-28 09:35:38 +00:00
|
|
|
#include <Common/HashTable/FixedClearableHashSet.h>
|
|
|
|
#include <Common/HashTable/FixedHashSet.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/UInt128.h>
|
2016-12-21 12:10:31 +00:00
|
|
|
|
2017-03-03 21:15:46 +00:00
|
|
|
|
2016-12-21 12:10:31 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
2016-12-21 12:10:31 +00:00
|
|
|
|
2017-03-03 21:15:46 +00:00
|
|
|
/** Methods for different implementations of sets (used in right hand side of IN or for DISTINCT).
|
|
|
|
* To use as template parameter.
|
2016-12-21 12:10:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// For the case where there is one numeric key.
|
2019-08-20 09:58:44 +00:00
|
|
|
template <typename FieldType, typename TData, bool use_cache = true> /// UInt8/16/32/64 for any types with corresponding bit width.
|
2016-12-21 12:10:31 +00:00
|
|
|
struct SetMethodOneNumber
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using Data = TData;
|
|
|
|
using Key = typename Data::key_type;
|
|
|
|
|
|
|
|
Data data;
|
|
|
|
|
2019-08-20 09:58:44 +00:00
|
|
|
using State = ColumnsHashing::HashMethodOneNumber<typename Data::value_type,
|
|
|
|
void, FieldType, use_cache>;
|
2016-12-21 12:10:31 +00:00
|
|
|
};
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// For the case where there is one string key.
|
2016-12-21 12:10:31 +00:00
|
|
|
template <typename TData>
|
|
|
|
struct SetMethodString
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using Data = TData;
|
|
|
|
using Key = typename Data::key_type;
|
|
|
|
|
|
|
|
Data data;
|
|
|
|
|
2019-02-05 11:14:09 +00:00
|
|
|
using State = ColumnsHashing::HashMethodString<typename Data::value_type, void, true, false>;
|
2016-12-21 12:10:31 +00:00
|
|
|
};
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// For the case when there is one fixed-length string key.
|
2016-12-21 12:10:31 +00:00
|
|
|
template <typename TData>
|
|
|
|
struct SetMethodFixedString
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using Data = TData;
|
|
|
|
using Key = typename Data::key_type;
|
|
|
|
|
|
|
|
Data data;
|
|
|
|
|
2019-02-05 11:14:09 +00:00
|
|
|
using State = ColumnsHashing::HashMethodFixedString<typename Data::value_type, void, true, false>;
|
2016-12-21 12:10:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace set_impl
|
|
|
|
{
|
|
|
|
|
|
|
|
/// This class is designed to provide the functionality that is required for
|
|
|
|
/// supporting nullable keys in SetMethodKeysFixed. If there are
|
|
|
|
/// no nullable keys, this class is merely implemented as an empty shell.
|
|
|
|
template <typename Key, bool has_nullable_keys>
|
|
|
|
class BaseStateKeysFixed;
|
|
|
|
|
|
|
|
/// Case where nullable keys are supported.
|
|
|
|
template <typename Key>
|
|
|
|
class BaseStateKeysFixed<Key, true>
|
|
|
|
{
|
|
|
|
protected:
|
2017-12-13 01:27:53 +00:00
|
|
|
void init(const ColumnRawPtrs & key_columns)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
null_maps.reserve(key_columns.size());
|
|
|
|
actual_columns.reserve(key_columns.size());
|
|
|
|
|
|
|
|
for (const auto & col : key_columns)
|
|
|
|
{
|
2019-06-27 18:50:20 +00:00
|
|
|
if (auto * nullable = checkAndGetColumn<ColumnNullable>(*col))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-06-26 17:20:33 +00:00
|
|
|
actual_columns.push_back(&nullable->getNestedColumn());
|
|
|
|
null_maps.push_back(&nullable->getNullMapColumn());
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
actual_columns.push_back(col);
|
|
|
|
null_maps.push_back(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the columns which actually contain the values of the keys.
|
|
|
|
/// For a given key column, if it is nullable, we return its nested
|
|
|
|
/// column. Otherwise we return the key column itself.
|
2017-12-13 01:27:53 +00:00
|
|
|
inline const ColumnRawPtrs & getActualColumns() const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return actual_columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a bitmap that indicates whether, for a particular row,
|
|
|
|
/// a key column bears a null value or not.
|
|
|
|
KeysNullMap<Key> createBitmap(size_t row) const
|
|
|
|
{
|
|
|
|
KeysNullMap<Key> bitmap{};
|
|
|
|
|
|
|
|
for (size_t k = 0; k < null_maps.size(); ++k)
|
|
|
|
{
|
|
|
|
if (null_maps[k] != nullptr)
|
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
const auto & null_map = assert_cast<const ColumnUInt8 &>(*null_maps[k]).getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
if (null_map[row] == 1)
|
|
|
|
{
|
|
|
|
size_t bucket = k / 8;
|
|
|
|
size_t offset = k % 8;
|
|
|
|
bitmap[bucket] |= UInt8(1) << offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
}
|
2016-12-21 12:10:31 +00:00
|
|
|
|
|
|
|
private:
|
2017-12-13 01:27:53 +00:00
|
|
|
ColumnRawPtrs actual_columns;
|
|
|
|
ColumnRawPtrs null_maps;
|
2016-12-21 12:10:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Case where nullable keys are not supported.
|
|
|
|
template <typename Key>
|
|
|
|
class BaseStateKeysFixed<Key, false>
|
|
|
|
{
|
|
|
|
protected:
|
2017-12-13 01:27:53 +00:00
|
|
|
void init(const ColumnRawPtrs &)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
throw Exception{"Internal error: calling init() for non-nullable"
|
|
|
|
" keys is forbidden", ErrorCodes::LOGICAL_ERROR};
|
|
|
|
}
|
|
|
|
|
2017-12-13 01:27:53 +00:00
|
|
|
const ColumnRawPtrs & getActualColumns() const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
throw Exception{"Internal error: calling getActualColumns() for non-nullable"
|
|
|
|
" keys is forbidden", ErrorCodes::LOGICAL_ERROR};
|
|
|
|
}
|
|
|
|
|
2017-12-01 19:34:51 +00:00
|
|
|
KeysNullMap<Key> createBitmap(size_t) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
throw Exception{"Internal error: calling createBitmap() for non-nullable keys"
|
|
|
|
" is forbidden", ErrorCodes::LOGICAL_ERROR};
|
|
|
|
}
|
2016-12-21 12:10:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// For the case when all keys are of fixed length, and they fit in N (for example, 128) bits.
|
2016-12-21 12:10:31 +00:00
|
|
|
template <typename TData, bool has_nullable_keys_ = false>
|
|
|
|
struct SetMethodKeysFixed
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using Data = TData;
|
|
|
|
using Key = typename Data::key_type;
|
|
|
|
static constexpr bool has_nullable_keys = has_nullable_keys_;
|
|
|
|
|
|
|
|
Data data;
|
|
|
|
|
2019-02-04 14:36:15 +00:00
|
|
|
using State = ColumnsHashing::HashMethodKeysFixed<typename Data::value_type, Key, void, has_nullable_keys, false>;
|
2016-12-21 12:10:31 +00:00
|
|
|
};
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// For other cases. 128 bit hash from the key.
|
2016-12-21 12:10:31 +00:00
|
|
|
template <typename TData>
|
|
|
|
struct SetMethodHashed
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using Data = TData;
|
|
|
|
using Key = typename Data::key_type;
|
|
|
|
|
|
|
|
Data data;
|
|
|
|
|
2019-02-04 14:36:15 +00:00
|
|
|
using State = ColumnsHashing::HashMethodHashed<typename Data::value_type, void>;
|
2016-12-21 12:10:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/** Different implementations of the set.
|
2016-12-21 12:10:31 +00:00
|
|
|
*/
|
2017-04-07 16:13:25 +00:00
|
|
|
struct NonClearableSet
|
2016-12-21 12:10:31 +00:00
|
|
|
{
|
2019-08-20 09:58:44 +00:00
|
|
|
/*
|
|
|
|
* As in Aggregator, using consecutive keys cache doesn't improve performance
|
|
|
|
* for FixedHashTables.
|
|
|
|
*/
|
2020-04-14 22:25:31 +00:00
|
|
|
std::unique_ptr<SetMethodOneNumber<UInt8, FixedHashSet<UInt8>, false /* use_cache */>> key8;
|
|
|
|
std::unique_ptr<SetMethodOneNumber<UInt16, FixedHashSet<UInt16>, false /* use_cache */>> key16;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/** 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).
|
|
|
|
* But this experiment showed that there is an advantage only in rare cases.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2020-04-14 22:25:31 +00:00
|
|
|
std::unique_ptr<SetMethodOneNumber<UInt32, HashSet<UInt32, HashCRC32<UInt32>>>> key32;
|
|
|
|
std::unique_ptr<SetMethodOneNumber<UInt64, HashSet<UInt64, HashCRC32<UInt64>>>> key64;
|
|
|
|
std::unique_ptr<SetMethodString<HashSetWithSavedHash<StringRef>>> key_string;
|
|
|
|
std::unique_ptr<SetMethodFixedString<HashSetWithSavedHash<StringRef>>> key_fixed_string;
|
|
|
|
std::unique_ptr<SetMethodKeysFixed<HashSet<UInt128, UInt128HashCRC32>>> keys128;
|
|
|
|
std::unique_ptr<SetMethodKeysFixed<HashSet<UInt256, UInt256HashCRC32>>> keys256;
|
|
|
|
std::unique_ptr<SetMethodHashed<HashSet<UInt128, UInt128TrivialHash>>> hashed;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Support for nullable keys (for DISTINCT implementation).
|
2020-04-14 22:25:31 +00:00
|
|
|
std::unique_ptr<SetMethodKeysFixed<HashSet<UInt128, UInt128HashCRC32>, true>> nullable_keys128;
|
|
|
|
std::unique_ptr<SetMethodKeysFixed<HashSet<UInt256, UInt256HashCRC32>, true>> nullable_keys256;
|
2017-06-02 21:37:28 +00:00
|
|
|
/** Unlike Aggregator, `concat` method is not used here.
|
|
|
|
* This is done because `hashed` method, although slower, but in this case, uses less RAM.
|
|
|
|
* since when you use it, the key values themselves are not stored.
|
2017-04-07 16:13:25 +00:00
|
|
|
*/
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ClearableSet
|
|
|
|
{
|
2020-04-14 22:25:31 +00:00
|
|
|
std::unique_ptr<SetMethodOneNumber<UInt8, FixedClearableHashSet<UInt8>, false /* use_cache */>> key8;
|
|
|
|
std::unique_ptr<SetMethodOneNumber<UInt16, FixedClearableHashSet<UInt16>, false /*use_cache */>> key16;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-04-14 22:25:31 +00:00
|
|
|
std::unique_ptr<SetMethodOneNumber<UInt32, ClearableHashSet<UInt32, HashCRC32<UInt32>>>> key32;
|
|
|
|
std::unique_ptr<SetMethodOneNumber<UInt64, ClearableHashSet<UInt64, HashCRC32<UInt64>>>> key64;
|
|
|
|
std::unique_ptr<SetMethodString<ClearableHashSetWithSavedHash<StringRef>>> key_string;
|
|
|
|
std::unique_ptr<SetMethodFixedString<ClearableHashSetWithSavedHash<StringRef>>> key_fixed_string;
|
|
|
|
std::unique_ptr<SetMethodKeysFixed<ClearableHashSet<UInt128, UInt128HashCRC32>>> keys128;
|
|
|
|
std::unique_ptr<SetMethodKeysFixed<ClearableHashSet<UInt256, UInt256HashCRC32>>> keys256;
|
|
|
|
std::unique_ptr<SetMethodHashed<ClearableHashSet<UInt128, UInt128TrivialHash>>> hashed;
|
2017-04-07 16:13:25 +00:00
|
|
|
|
|
|
|
/// Support for nullable keys (for DISTINCT implementation).
|
2020-04-14 22:25:31 +00:00
|
|
|
std::unique_ptr<SetMethodKeysFixed<ClearableHashSet<UInt128, UInt128HashCRC32>, true>> nullable_keys128;
|
|
|
|
std::unique_ptr<SetMethodKeysFixed<ClearableHashSet<UInt256, UInt256HashCRC32>, true>> nullable_keys256;
|
2017-06-02 21:37:28 +00:00
|
|
|
/** Unlike Aggregator, `concat` method is not used here.
|
|
|
|
* This is done because `hashed` method, although slower, but in this case, uses less RAM.
|
|
|
|
* since when you use it, the key values themselves are not stored.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2017-04-07 16:13:25 +00:00
|
|
|
};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-07 16:13:25 +00:00
|
|
|
template <typename Variant>
|
|
|
|
struct SetVariantsTemplate: public Variant
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Arena string_pool;
|
|
|
|
|
|
|
|
#define APPLY_FOR_SET_VARIANTS(M) \
|
2017-04-07 16:13:25 +00:00
|
|
|
M(key8) \
|
|
|
|
M(key16) \
|
|
|
|
M(key32) \
|
|
|
|
M(key64) \
|
|
|
|
M(key_string) \
|
|
|
|
M(key_fixed_string) \
|
|
|
|
M(keys128) \
|
|
|
|
M(keys256) \
|
|
|
|
M(nullable_keys128) \
|
|
|
|
M(nullable_keys256) \
|
2017-04-01 07:20:54 +00:00
|
|
|
M(hashed)
|
|
|
|
|
2017-04-07 16:13:25 +00:00
|
|
|
#define M(NAME) using Variant::NAME;
|
|
|
|
APPLY_FOR_SET_VARIANTS(M)
|
|
|
|
#undef M
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
enum class Type
|
|
|
|
{
|
|
|
|
EMPTY,
|
|
|
|
|
|
|
|
#define M(NAME) NAME,
|
|
|
|
APPLY_FOR_SET_VARIANTS(M)
|
|
|
|
#undef M
|
|
|
|
};
|
|
|
|
|
|
|
|
Type type = Type::EMPTY;
|
|
|
|
|
|
|
|
bool empty() const { return type == Type::EMPTY; }
|
|
|
|
|
2017-12-13 01:27:53 +00:00
|
|
|
static Type chooseMethod(const ColumnRawPtrs & key_columns, Sizes & key_sizes);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
void init(Type type_);
|
|
|
|
|
|
|
|
size_t getTotalRowCount() const;
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Counts the size in bytes of the Set buffer and the size of the `string_pool`
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t getTotalByteCount() const;
|
2016-12-21 12:10:31 +00:00
|
|
|
};
|
|
|
|
|
2017-04-07 16:13:25 +00:00
|
|
|
using SetVariants = SetVariantsTemplate<NonClearableSet>;
|
|
|
|
using ClearableSetVariants = SetVariantsTemplate<ClearableSet>;
|
|
|
|
|
2016-12-21 12:10:31 +00:00
|
|
|
}
|