2015-11-12 16:28:48 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Dictionaries/IDictionary.h>
|
|
|
|
#include <DB/Dictionaries/IDictionarySource.h>
|
|
|
|
#include <DB/Dictionaries/DictionaryStructure.h>
|
|
|
|
#include <DB/Core/StringRef.h>
|
|
|
|
#include <DB/Common/HashTable/HashMap.h>
|
|
|
|
#include <DB/Columns/ColumnString.h>
|
2015-11-17 16:08:31 +00:00
|
|
|
#include <DB/Common/Arena.h>
|
2015-11-12 16:28:48 +00:00
|
|
|
#include <ext/range.hpp>
|
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2015-11-13 11:21:40 +00:00
|
|
|
class ComplexKeyHashedDictionary final : public IDictionaryBase
|
2015-11-12 16:28:48 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-11-13 11:21:40 +00:00
|
|
|
ComplexKeyHashedDictionary(
|
2015-11-12 16:28:48 +00:00
|
|
|
const std::string & name, const DictionaryStructure & dict_struct, DictionarySourcePtr source_ptr,
|
2016-06-07 21:07:44 +00:00
|
|
|
const DictionaryLifetime dict_lifetime, bool require_nonempty);
|
2015-11-12 16:28:48 +00:00
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
ComplexKeyHashedDictionary(const ComplexKeyHashedDictionary & other);
|
2015-11-12 16:28:48 +00:00
|
|
|
|
2015-11-13 01:44:41 +00:00
|
|
|
std::string getKeyDescription() const { return key_description; };
|
|
|
|
|
2015-11-12 16:28:48 +00:00
|
|
|
std::exception_ptr getCreationException() const override { return creation_exception; }
|
|
|
|
|
|
|
|
std::string getName() const override { return name; }
|
|
|
|
|
2015-11-13 11:21:40 +00:00
|
|
|
std::string getTypeName() const override { return "ComplexKeyHashed"; }
|
2015-11-12 16:28:48 +00:00
|
|
|
|
|
|
|
std::size_t getBytesAllocated() const override { return bytes_allocated; }
|
|
|
|
|
|
|
|
std::size_t getQueryCount() const override { return query_count.load(std::memory_order_relaxed); }
|
|
|
|
|
|
|
|
double getHitRate() const override { return 1.0; }
|
|
|
|
|
|
|
|
std::size_t getElementCount() const override { return element_count; }
|
|
|
|
|
|
|
|
double getLoadFactor() const override { return static_cast<double>(element_count) / bucket_count; }
|
|
|
|
|
|
|
|
bool isCached() const override { return false; }
|
|
|
|
|
2015-11-13 11:21:40 +00:00
|
|
|
DictionaryPtr clone() const override { return std::make_unique<ComplexKeyHashedDictionary>(*this); }
|
2015-11-12 16:28:48 +00:00
|
|
|
|
|
|
|
const IDictionarySource * getSource() const override { return source_ptr.get(); }
|
|
|
|
|
|
|
|
const DictionaryLifetime & getLifetime() const override { return dict_lifetime; }
|
|
|
|
|
|
|
|
const DictionaryStructure & getStructure() const override { return dict_struct; }
|
|
|
|
|
|
|
|
std::chrono::time_point<std::chrono::system_clock> getCreationTime() const override
|
|
|
|
{
|
|
|
|
return creation_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isInjective(const std::string & attribute_name) const override
|
|
|
|
{
|
|
|
|
return dict_struct.attributes[&getAttribute(attribute_name) - attributes.data()].injective;
|
|
|
|
}
|
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
#define DECLARE(TYPE)\
|
2015-11-13 01:44:41 +00:00
|
|
|
void get##TYPE(\
|
|
|
|
const std::string & attribute_name, const ConstColumnPlainPtrs & key_columns, const DataTypes & key_types,\
|
2016-06-07 21:07:44 +00:00
|
|
|
PaddedPODArray<TYPE> & out) const;
|
2015-11-20 15:53:23 +00:00
|
|
|
DECLARE(UInt8)
|
|
|
|
DECLARE(UInt16)
|
|
|
|
DECLARE(UInt32)
|
|
|
|
DECLARE(UInt64)
|
|
|
|
DECLARE(Int8)
|
|
|
|
DECLARE(Int16)
|
|
|
|
DECLARE(Int32)
|
|
|
|
DECLARE(Int64)
|
|
|
|
DECLARE(Float32)
|
|
|
|
DECLARE(Float64)
|
|
|
|
#undef DECLARE
|
2016-06-07 21:07:44 +00:00
|
|
|
|
2015-11-13 01:44:41 +00:00
|
|
|
void getString(
|
|
|
|
const std::string & attribute_name, const ConstColumnPlainPtrs & key_columns, const DataTypes & key_types,
|
2016-06-07 21:07:44 +00:00
|
|
|
ColumnString * out) const;
|
2015-11-12 16:28:48 +00:00
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
#define DECLARE(TYPE)\
|
2015-11-12 16:28:48 +00:00
|
|
|
void get##TYPE(\
|
2015-11-13 01:44:41 +00:00
|
|
|
const std::string & attribute_name, const ConstColumnPlainPtrs & key_columns, const DataTypes & key_types,\
|
2016-06-07 21:07:44 +00:00
|
|
|
const PaddedPODArray<TYPE> & def, PaddedPODArray<TYPE> & out) const;
|
2015-11-20 15:53:23 +00:00
|
|
|
DECLARE(UInt8)
|
|
|
|
DECLARE(UInt16)
|
|
|
|
DECLARE(UInt32)
|
|
|
|
DECLARE(UInt64)
|
|
|
|
DECLARE(Int8)
|
|
|
|
DECLARE(Int16)
|
|
|
|
DECLARE(Int32)
|
|
|
|
DECLARE(Int64)
|
|
|
|
DECLARE(Float32)
|
|
|
|
DECLARE(Float64)
|
|
|
|
#undef DECLARE
|
2016-06-07 21:07:44 +00:00
|
|
|
|
2015-11-12 16:28:48 +00:00
|
|
|
void getString(
|
2015-11-13 01:44:41 +00:00
|
|
|
const std::string & attribute_name, const ConstColumnPlainPtrs & key_columns, const DataTypes & key_types,
|
2016-06-07 21:07:44 +00:00
|
|
|
const ColumnString * const def, ColumnString * const out) const;
|
2015-11-12 16:28:48 +00:00
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
#define DECLARE(TYPE)\
|
|
|
|
void get##TYPE(\
|
|
|
|
const std::string & attribute_name, const ConstColumnPlainPtrs & key_columns, const DataTypes & key_types,\
|
2016-06-07 21:07:44 +00:00
|
|
|
const TYPE def, PaddedPODArray<TYPE> & out) const;
|
2015-11-20 15:53:23 +00:00
|
|
|
DECLARE(UInt8)
|
|
|
|
DECLARE(UInt16)
|
|
|
|
DECLARE(UInt32)
|
|
|
|
DECLARE(UInt64)
|
|
|
|
DECLARE(Int8)
|
|
|
|
DECLARE(Int16)
|
|
|
|
DECLARE(Int32)
|
|
|
|
DECLARE(Int64)
|
|
|
|
DECLARE(Float32)
|
|
|
|
DECLARE(Float64)
|
|
|
|
#undef DECLARE
|
2016-06-07 21:07:44 +00:00
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
void getString(
|
|
|
|
const std::string & attribute_name, const ConstColumnPlainPtrs & key_columns, const DataTypes & key_types,
|
2016-06-07 21:07:44 +00:00
|
|
|
const String & def, ColumnString * const out) const;
|
2015-11-20 15:53:23 +00:00
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
void has(const ConstColumnPlainPtrs & key_columns, const DataTypes & key_types, PaddedPODArray<UInt8> & out) const;
|
2015-11-19 13:15:02 +00:00
|
|
|
|
2015-11-12 16:28:48 +00:00
|
|
|
private:
|
2015-11-19 13:15:02 +00:00
|
|
|
template <typename Value> using ContainerType = HashMapWithSavedHash<StringRef, Value, StringRefHash>;
|
|
|
|
template <typename Value> using ContainerPtrType = std::unique_ptr<ContainerType<Value>>;
|
2015-11-12 16:28:48 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
struct Attribute final
|
2015-11-12 16:28:48 +00:00
|
|
|
{
|
|
|
|
AttributeUnderlyingType type;
|
|
|
|
std::tuple<
|
|
|
|
UInt8, UInt16, UInt32, UInt64,
|
|
|
|
Int8, Int16, Int32, Int64,
|
|
|
|
Float32, Float64,
|
|
|
|
String> null_values;
|
|
|
|
std::tuple<
|
2015-11-19 13:15:02 +00:00
|
|
|
ContainerPtrType<UInt8>, ContainerPtrType<UInt16>, ContainerPtrType<UInt32>, ContainerPtrType<UInt64>,
|
|
|
|
ContainerPtrType<Int8>, ContainerPtrType<Int16>, ContainerPtrType<Int32>, ContainerPtrType<Int64>,
|
|
|
|
ContainerPtrType<Float32>, ContainerPtrType<Float64>,
|
|
|
|
ContainerPtrType<StringRef>> maps;
|
2015-11-12 16:28:48 +00:00
|
|
|
std::unique_ptr<Arena> string_arena;
|
|
|
|
};
|
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
void createAttributes();
|
2015-11-12 16:28:48 +00:00
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
void loadData();
|
2015-11-12 16:28:48 +00:00
|
|
|
|
|
|
|
template <typename T>
|
2016-08-07 09:09:18 +00:00
|
|
|
void addAttributeSize(const Attribute & attribute);
|
2015-11-12 16:28:48 +00:00
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
void calculateBytesAllocated();
|
2015-11-12 16:28:48 +00:00
|
|
|
|
|
|
|
template <typename T>
|
2016-08-07 09:09:18 +00:00
|
|
|
void createAttributeImpl(Attribute & attribute, const Field & null_value);
|
2015-11-12 16:28:48 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
Attribute createAttributeWithType(const AttributeUnderlyingType type, const Field & null_value);
|
2015-11-13 01:44:41 +00:00
|
|
|
|
2016-06-07 19:11:04 +00:00
|
|
|
|
|
|
|
template <typename OutputType, typename ValueSetter, typename DefaultGetter>
|
|
|
|
void getItemsNumber(
|
2016-08-07 09:09:18 +00:00
|
|
|
const Attribute & attribute,
|
2016-06-07 19:11:04 +00:00
|
|
|
const ConstColumnPlainPtrs & key_columns,
|
|
|
|
ValueSetter && set_value,
|
2016-06-07 21:07:44 +00:00
|
|
|
DefaultGetter && get_default) const;
|
2016-06-07 19:11:04 +00:00
|
|
|
|
|
|
|
template <typename AttributeType, typename OutputType, typename ValueSetter, typename DefaultGetter>
|
|
|
|
void getItemsImpl(
|
2016-08-07 09:09:18 +00:00
|
|
|
const Attribute & attribute,
|
2016-06-07 19:11:04 +00:00
|
|
|
const ConstColumnPlainPtrs & key_columns,
|
|
|
|
ValueSetter && set_value,
|
2016-06-07 21:07:44 +00:00
|
|
|
DefaultGetter && get_default) const;
|
2015-11-12 16:28:48 +00:00
|
|
|
|
2016-06-07 19:11:04 +00:00
|
|
|
|
2015-11-12 16:28:48 +00:00
|
|
|
template <typename T>
|
2016-08-07 09:09:18 +00:00
|
|
|
bool setAttributeValueImpl(Attribute & attribute, const StringRef key, const T value);
|
2015-11-12 16:28:48 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
bool setAttributeValue(Attribute & attribute, const StringRef key, const Field & value);
|
2015-11-12 16:28:48 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
const Attribute & getAttribute(const std::string & attribute_name) const;
|
2015-11-12 16:28:48 +00:00
|
|
|
|
2015-11-13 01:44:41 +00:00
|
|
|
static StringRef placeKeysInPool(
|
2016-06-07 21:07:44 +00:00
|
|
|
const std::size_t row, const ConstColumnPlainPtrs & key_columns, StringRefs & keys, Arena & pool);
|
2015-11-12 16:28:48 +00:00
|
|
|
|
2015-11-19 13:15:02 +00:00
|
|
|
template <typename T>
|
2016-08-07 09:09:18 +00:00
|
|
|
void has(const Attribute & attribute, const ConstColumnPlainPtrs & key_columns, PaddedPODArray<UInt8> & out) const;
|
2015-11-19 13:15:02 +00:00
|
|
|
|
2015-11-12 16:28:48 +00:00
|
|
|
const std::string name;
|
|
|
|
const DictionaryStructure dict_struct;
|
|
|
|
const DictionarySourcePtr source_ptr;
|
|
|
|
const DictionaryLifetime dict_lifetime;
|
|
|
|
const bool require_nonempty;
|
2015-11-20 16:20:54 +00:00
|
|
|
const std::string key_description{dict_struct.getKeyDescription()};
|
2015-11-12 16:28:48 +00:00
|
|
|
|
|
|
|
std::map<std::string, std::size_t> attribute_index_by_name;
|
2016-08-07 09:09:18 +00:00
|
|
|
std::vector<Attribute> attributes;
|
2015-11-12 16:28:48 +00:00
|
|
|
Arena keys_pool;
|
|
|
|
|
|
|
|
std::size_t bytes_allocated = 0;
|
|
|
|
std::size_t element_count = 0;
|
|
|
|
std::size_t bucket_count = 0;
|
|
|
|
mutable std::atomic<std::size_t> query_count{0};
|
|
|
|
|
|
|
|
std::chrono::time_point<std::chrono::system_clock> creation_time;
|
|
|
|
|
|
|
|
std::exception_ptr creation_exception;
|
|
|
|
};
|
|
|
|
|
2015-11-13 01:44:41 +00:00
|
|
|
|
2015-11-12 16:28:48 +00:00
|
|
|
}
|