2015-01-29 13:53:48 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Dictionaries/IDictionary.h>
|
|
|
|
#include <DB/Dictionaries/IDictionarySource.h>
|
2015-01-29 14:46:15 +00:00
|
|
|
#include <DB/Dictionaries/DictionaryStructure.h>
|
2015-01-29 13:53:48 +00:00
|
|
|
#include <DB/Common/HashTable/HashMap.h>
|
2015-02-19 14:51:39 +00:00
|
|
|
#include <DB/Columns/ColumnString.h>
|
2015-10-05 00:33:43 +00:00
|
|
|
#include <ext/range.hpp>
|
2015-05-08 12:31:00 +00:00
|
|
|
#include <atomic>
|
2015-02-10 21:10:58 +00:00
|
|
|
#include <memory>
|
2015-03-02 13:16:11 +00:00
|
|
|
#include <tuple>
|
2015-01-29 13:53:48 +00:00
|
|
|
|
2015-07-10 14:43:49 +00:00
|
|
|
|
2015-01-29 13:53:48 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class HashedDictionary final : public IDictionary
|
|
|
|
{
|
|
|
|
public:
|
2015-01-29 15:47:21 +00:00
|
|
|
HashedDictionary(const std::string & name, const DictionaryStructure & dict_struct,
|
2016-06-07 21:07:44 +00:00
|
|
|
DictionarySourcePtr source_ptr, const DictionaryLifetime dict_lifetime, bool require_nonempty);
|
2015-01-29 13:53:48 +00:00
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
HashedDictionary(const HashedDictionary & other);
|
2015-01-30 13:43:16 +00:00
|
|
|
|
2015-06-09 16:12:51 +00:00
|
|
|
std::exception_ptr getCreationException() const override { return creation_exception; }
|
|
|
|
|
2015-01-29 15:47:21 +00:00
|
|
|
std::string getName() const override { return name; }
|
|
|
|
|
2015-03-24 12:26:19 +00:00
|
|
|
std::string getTypeName() const override { return "Hashed"; }
|
2015-01-29 14:46:15 +00:00
|
|
|
|
2015-03-24 12:12:48 +00:00
|
|
|
std::size_t getBytesAllocated() const override { return bytes_allocated; }
|
2015-03-24 11:30:16 +00:00
|
|
|
|
2015-05-08 12:31:00 +00:00
|
|
|
std::size_t getQueryCount() const override { return query_count.load(std::memory_order_relaxed); }
|
|
|
|
|
2015-03-24 17:02:56 +00:00
|
|
|
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; }
|
|
|
|
|
2015-01-29 15:47:21 +00:00
|
|
|
bool isCached() const override { return false; }
|
|
|
|
|
2015-02-10 21:10:58 +00:00
|
|
|
DictionaryPtr clone() const override { return std::make_unique<HashedDictionary>(*this); }
|
2015-01-30 13:43:16 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
const IDictionarySource * getSource() const override { return source_ptr.get(); }
|
2015-01-29 15:47:21 +00:00
|
|
|
|
2015-01-30 15:18:13 +00:00
|
|
|
const DictionaryLifetime & getLifetime() const override { return dict_lifetime; }
|
|
|
|
|
2015-03-24 13:59:19 +00:00
|
|
|
const DictionaryStructure & getStructure() const override { return dict_struct; }
|
|
|
|
|
2015-03-24 17:02:56 +00:00
|
|
|
std::chrono::time_point<std::chrono::system_clock> getCreationTime() const override
|
|
|
|
{
|
|
|
|
return creation_time;
|
|
|
|
}
|
|
|
|
|
2015-05-13 16:11:07 +00:00
|
|
|
bool isInjective(const std::string & attribute_name) const override
|
|
|
|
{
|
|
|
|
return dict_struct.attributes[&getAttribute(attribute_name) - attributes.data()].injective;
|
|
|
|
}
|
|
|
|
|
2015-01-29 13:53:48 +00:00
|
|
|
bool hasHierarchy() const override { return hierarchical_attribute; }
|
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
void toParent(const PaddedPODArray<Key> & ids, PaddedPODArray<Key> & out) const override;
|
2015-01-29 13:53:48 +00:00
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
#define DECLARE(TYPE)\
|
2016-08-07 09:09:18 +00:00
|
|
|
void get##TYPE(const std::string & attribute_name, const PaddedPODArray<Key> & ids, 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
|
2015-01-29 13:53:48 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
void getString(const std::string & attribute_name, const PaddedPODArray<Key> & ids, ColumnString * out) const;
|
2015-11-06 14:49:54 +00:00
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
#define DECLARE(TYPE)\
|
2015-11-06 14:49:54 +00:00
|
|
|
void get##TYPE(\
|
2016-08-07 09:09:18 +00:00
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, const PaddedPODArray<TYPE> & def,\
|
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-06 14:49:54 +00:00
|
|
|
void getString(
|
2016-08-07 09:09:18 +00:00
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, const ColumnString * const def,
|
2016-06-07 21:07:44 +00:00
|
|
|
ColumnString * const out) const;
|
2015-11-06 14:49:54 +00:00
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
#define DECLARE(TYPE)\
|
|
|
|
void get##TYPE(\
|
2016-08-07 09:09:18 +00:00
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, 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(
|
2016-08-07 09:09:18 +00:00
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, const String & def,
|
2016-06-07 21:07:44 +00:00
|
|
|
ColumnString * const out) const;
|
2015-11-20 15:53:23 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
void has(const PaddedPODArray<Key> & ids, PaddedPODArray<UInt8> & out) const override;
|
2015-01-29 13:53:48 +00:00
|
|
|
|
2015-01-30 13:43:16 +00:00
|
|
|
private:
|
2015-11-19 13:15:02 +00:00
|
|
|
template <typename Value> using CollectionType = HashMap<UInt64, Value>;
|
|
|
|
template <typename Value> using CollectionPtrType = std::unique_ptr<CollectionType<Value>>;
|
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
struct Attribute final
|
2015-01-29 13:53:48 +00:00
|
|
|
{
|
2015-03-20 15:21:29 +00:00
|
|
|
AttributeUnderlyingType type;
|
2015-11-19 13:15:02 +00:00
|
|
|
std::tuple<
|
|
|
|
UInt8, UInt16, UInt32, UInt64,
|
2015-03-02 13:16:11 +00:00
|
|
|
Int8, Int16, Int32, Int64,
|
|
|
|
Float32, Float64,
|
|
|
|
String> null_values;
|
2015-11-19 13:15:02 +00:00
|
|
|
std::tuple<
|
|
|
|
CollectionPtrType<UInt8>, CollectionPtrType<UInt16>, CollectionPtrType<UInt32>, CollectionPtrType<UInt64>,
|
|
|
|
CollectionPtrType<Int8>, CollectionPtrType<Int16>, CollectionPtrType<Int32>, CollectionPtrType<Int64>,
|
|
|
|
CollectionPtrType<Float32>, CollectionPtrType<Float64>,
|
|
|
|
CollectionPtrType<StringRef>> maps;
|
2015-01-29 13:53:48 +00:00
|
|
|
std::unique_ptr<Arena> string_arena;
|
|
|
|
};
|
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
void createAttributes();
|
2015-01-30 13:43:16 +00:00
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
void loadData();
|
2015-01-30 13:43:16 +00:00
|
|
|
|
2015-03-24 12:12:48 +00:00
|
|
|
template <typename T>
|
2016-08-07 09:09:18 +00:00
|
|
|
void addAttributeSize(const Attribute & attribute);
|
2015-03-24 12:12:48 +00:00
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
void calculateBytesAllocated();
|
2015-03-24 12:12:48 +00:00
|
|
|
|
2015-03-02 13:16:11 +00:00
|
|
|
template <typename T>
|
2016-08-07 09:09:18 +00:00
|
|
|
void createAttributeImpl(Attribute & attribute, const Field & null_value);
|
2015-01-29 13:53:48 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
Attribute createAttributeWithType(const AttributeUnderlyingType type, const Field & null_value);
|
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,
|
|
|
|
const PaddedPODArray<Key> & ids,
|
2016-06-07 19:11:04 +00:00
|
|
|
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,
|
|
|
|
const PaddedPODArray<Key> & ids,
|
2016-06-07 19:11:04 +00:00
|
|
|
ValueSetter && set_value,
|
2016-06-07 21:07:44 +00:00
|
|
|
DefaultGetter && get_default) const;
|
2016-06-07 19:11:04 +00:00
|
|
|
|
2015-03-02 13:16:11 +00:00
|
|
|
template <typename T>
|
2016-08-07 09:09:18 +00:00
|
|
|
void setAttributeValueImpl(Attribute & attribute, const Key id, const T value);
|
2015-03-02 13:16:11 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
void setAttributeValue(Attribute & attribute, const Key id, const Field & value);
|
2015-01-29 13:53:48 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
const Attribute & getAttribute(const std::string & attribute_name) const;
|
2015-02-19 14:51:39 +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 PaddedPODArray<Key> & ids, PaddedPODArray<UInt8> & out) const;
|
2015-11-19 13:15:02 +00:00
|
|
|
|
2015-01-29 15:47:21 +00:00
|
|
|
const std::string name;
|
2015-01-30 13:43:16 +00:00
|
|
|
const DictionaryStructure dict_struct;
|
|
|
|
const DictionarySourcePtr source_ptr;
|
2015-01-30 15:18:13 +00:00
|
|
|
const DictionaryLifetime dict_lifetime;
|
2015-08-11 21:32:27 +00:00
|
|
|
const bool require_nonempty;
|
2015-01-30 13:43:16 +00:00
|
|
|
|
2015-01-29 13:53: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;
|
|
|
|
const Attribute * hierarchical_attribute = nullptr;
|
2015-01-29 13:53:48 +00:00
|
|
|
|
2015-03-24 12:12:48 +00:00
|
|
|
std::size_t bytes_allocated = 0;
|
2015-03-24 17:02:56 +00:00
|
|
|
std::size_t element_count = 0;
|
|
|
|
std::size_t bucket_count = 0;
|
2015-08-12 04:21:10 +00:00
|
|
|
mutable std::atomic<std::size_t> query_count{0};
|
2015-03-24 17:02:56 +00:00
|
|
|
|
|
|
|
std::chrono::time_point<std::chrono::system_clock> creation_time;
|
2015-06-09 16:12:51 +00:00
|
|
|
|
|
|
|
std::exception_ptr creation_exception;
|
2015-01-29 13:53:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|