2015-07-13 16:18:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Dictionaries/IDictionary.h>
|
|
|
|
#include <Dictionaries/IDictionarySource.h>
|
|
|
|
#include <Dictionaries/DictionaryStructure.h>
|
|
|
|
#include <Common/HashTable/HashMap.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
2018-09-13 13:33:44 +00:00
|
|
|
|
2015-07-13 16:18:28 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2015-07-13 17:10:16 +00:00
|
|
|
class RangeHashedDictionary final : public IDictionaryBase
|
2015-07-13 16:18:28 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
RangeHashedDictionary(
|
2018-08-27 17:42:13 +00:00
|
|
|
const std::string & dictionary_name, const DictionaryStructure & dict_struct, DictionarySourcePtr source_ptr,
|
2017-04-01 07:20:54 +00:00
|
|
|
const DictionaryLifetime dict_lifetime, bool require_nonempty);
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
RangeHashedDictionary(const RangeHashedDictionary & other);
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::exception_ptr getCreationException() const override { return creation_exception; }
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2018-08-27 17:42:13 +00:00
|
|
|
std::string getName() const override { return dictionary_name; }
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string getTypeName() const override { return "RangeHashed"; }
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
size_t getBytesAllocated() const override { return bytes_allocated; }
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
size_t getQueryCount() const override { return query_count.load(std::memory_order_relaxed); }
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
double getHitRate() const override { return 1.0; }
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
size_t getElementCount() const override { return element_count; }
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
double getLoadFactor() const override { return static_cast<double>(element_count) / bucket_count; }
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool isCached() const override { return false; }
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-10-26 13:19:58 +00:00
|
|
|
std::unique_ptr<IExternalLoadable> clone() const override { return std::make_unique<RangeHashedDictionary>(*this); }
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const IDictionarySource * getSource() const override { return source_ptr.get(); }
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const DictionaryLifetime & getLifetime() const override { return dict_lifetime; }
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const DictionaryStructure & getStructure() const override { return dict_struct; }
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::chrono::time_point<std::chrono::system_clock> getCreationTime() const override
|
|
|
|
{
|
|
|
|
return creation_time;
|
|
|
|
}
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool isInjective(const std::string & attribute_name) const override
|
|
|
|
{
|
|
|
|
return dict_struct.attributes[&getAttribute(attribute_name) - attributes.data()].injective;
|
|
|
|
}
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2018-09-13 13:33:44 +00:00
|
|
|
typedef Int64 RangeStorageType;
|
|
|
|
|
2015-07-13 16:18:28 +00:00
|
|
|
#define DECLARE_MULTIPLE_GETTER(TYPE)\
|
2017-04-01 07:20:54 +00:00
|
|
|
void get##TYPE(\
|
2018-09-13 13:33:44 +00:00
|
|
|
const std::string & attribute_name,\
|
|
|
|
const PaddedPODArray<Key> & ids,\
|
|
|
|
const PaddedPODArray<RangeStorageType> & dates,\
|
2017-04-01 07:20:54 +00:00
|
|
|
PaddedPODArray<TYPE> & out) const;
|
|
|
|
DECLARE_MULTIPLE_GETTER(UInt8)
|
|
|
|
DECLARE_MULTIPLE_GETTER(UInt16)
|
|
|
|
DECLARE_MULTIPLE_GETTER(UInt32)
|
|
|
|
DECLARE_MULTIPLE_GETTER(UInt64)
|
2017-11-14 00:08:54 +00:00
|
|
|
DECLARE_MULTIPLE_GETTER(UInt128)
|
2017-04-01 07:20:54 +00:00
|
|
|
DECLARE_MULTIPLE_GETTER(Int8)
|
|
|
|
DECLARE_MULTIPLE_GETTER(Int16)
|
|
|
|
DECLARE_MULTIPLE_GETTER(Int32)
|
|
|
|
DECLARE_MULTIPLE_GETTER(Int64)
|
|
|
|
DECLARE_MULTIPLE_GETTER(Float32)
|
|
|
|
DECLARE_MULTIPLE_GETTER(Float64)
|
2015-07-13 16:18:28 +00:00
|
|
|
#undef DECLARE_MULTIPLE_GETTER
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void getString(
|
2018-09-13 13:33:44 +00:00
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, const PaddedPODArray<RangeStorageType> & dates,
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnString * out) const;
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-05-29 17:26:45 +00:00
|
|
|
BlockInputStreamPtr getBlockInputStream(const Names & column_names, size_t max_block_size) const override;
|
2017-05-04 18:14:23 +00:00
|
|
|
|
2018-09-13 13:33:44 +00:00
|
|
|
struct Range
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-09-13 13:33:44 +00:00
|
|
|
RangeStorageType left;
|
|
|
|
RangeStorageType right;
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2018-09-13 13:33:44 +00:00
|
|
|
static bool isCorrectDate(const RangeStorageType & date);
|
|
|
|
bool contains(const RangeStorageType& value) const;
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-12-25 19:00:48 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename T>
|
|
|
|
struct Value final
|
|
|
|
{
|
|
|
|
Range range;
|
|
|
|
T value;
|
|
|
|
};
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename T> using Values = std::vector<Value<T>>;
|
|
|
|
template <typename T> using Collection = HashMap<UInt64, Values<T>>;
|
|
|
|
template <typename T> using Ptr = std::unique_ptr<Collection<T>>;
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
struct Attribute final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AttributeUnderlyingType type;
|
|
|
|
std::tuple<UInt8, UInt16, UInt32, UInt64,
|
2017-11-14 00:08:54 +00:00
|
|
|
UInt128,
|
2017-04-01 07:20:54 +00:00
|
|
|
Int8, Int16, Int32, Int64,
|
|
|
|
Float32, Float64,
|
|
|
|
String> null_values;
|
|
|
|
std::tuple<Ptr<UInt8>, Ptr<UInt16>, Ptr<UInt32>, Ptr<UInt64>,
|
2017-11-14 00:08:54 +00:00
|
|
|
Ptr<UInt128>,
|
2017-04-01 07:20:54 +00:00
|
|
|
Ptr<Int8>, Ptr<Int16>, Ptr<Int32>, Ptr<Int64>,
|
|
|
|
Ptr<Float32>, Ptr<Float64>, Ptr<StringRef>> maps;
|
|
|
|
std::unique_ptr<Arena> string_arena;
|
|
|
|
};
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void createAttributes();
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void loadData();
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename T>
|
|
|
|
void addAttributeSize(const Attribute & attribute);
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void calculateBytesAllocated();
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename T>
|
|
|
|
void createAttributeImpl(Attribute & attribute, const Field & null_value);
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Attribute createAttributeWithType(const AttributeUnderlyingType type, const Field & null_value);
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2016-06-07 19:11:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename OutputType>
|
|
|
|
void getItems(
|
|
|
|
const Attribute & attribute,
|
|
|
|
const PaddedPODArray<Key> & ids,
|
2018-09-13 13:33:44 +00:00
|
|
|
const PaddedPODArray<RangeStorageType> & dates,
|
2017-04-01 07:20:54 +00:00
|
|
|
PaddedPODArray<OutputType> & out) const;
|
2016-06-07 19:11:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename AttributeType, typename OutputType>
|
|
|
|
void getItemsImpl(
|
|
|
|
const Attribute & attribute,
|
|
|
|
const PaddedPODArray<Key> & ids,
|
2018-09-13 13:33:44 +00:00
|
|
|
const PaddedPODArray<RangeStorageType> & dates,
|
2017-04-01 07:20:54 +00:00
|
|
|
PaddedPODArray<OutputType> & out) const;
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2016-06-07 19:11:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename T>
|
|
|
|
void setAttributeValueImpl(Attribute & attribute, const Key id, const Range & range, const T value);
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void setAttributeValue(Attribute & attribute, const Key id, const Range & range, const Field & value);
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const Attribute & getAttribute(const std::string & attribute_name) const;
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const Attribute & getAttributeWithType(const std::string & name, const AttributeUnderlyingType type) const;
|
2015-07-14 13:06:25 +00:00
|
|
|
|
2018-09-17 15:04:57 +00:00
|
|
|
template <typename RangeType>
|
2017-05-04 18:14:23 +00:00
|
|
|
void getIdsAndDates(PaddedPODArray<Key> & ids,
|
2018-09-17 15:04:57 +00:00
|
|
|
PaddedPODArray<RangeType> & start_dates, PaddedPODArray<RangeType> & end_dates) const;
|
2017-05-04 18:14:23 +00:00
|
|
|
|
2018-09-17 15:04:57 +00:00
|
|
|
template <typename T, typename RangeType>
|
2017-05-04 18:14:23 +00:00
|
|
|
void getIdsAndDates(const Attribute & attribute, PaddedPODArray<Key> & ids,
|
2018-09-17 15:04:57 +00:00
|
|
|
PaddedPODArray<RangeType> & start_dates, PaddedPODArray<RangeType> & end_dates) const;
|
|
|
|
|
|
|
|
template <typename RangeType>
|
|
|
|
BlockInputStreamPtr getBlockInputStreamImpl(const Names & column_names, size_t max_block_size) const;
|
|
|
|
|
|
|
|
friend struct RangeHashedDIctionaryCallGetBlockInputStreamImpl;
|
2017-05-04 18:14:23 +00:00
|
|
|
|
2018-08-27 17:42:13 +00:00
|
|
|
const std::string dictionary_name;
|
2017-04-01 07:20:54 +00:00
|
|
|
const DictionaryStructure dict_struct;
|
|
|
|
const DictionarySourcePtr source_ptr;
|
|
|
|
const DictionaryLifetime dict_lifetime;
|
|
|
|
const bool require_nonempty;
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
std::map<std::string, size_t> attribute_index_by_name;
|
2017-04-01 07:20:54 +00:00
|
|
|
std::vector<Attribute> attributes;
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
size_t bytes_allocated = 0;
|
|
|
|
size_t element_count = 0;
|
|
|
|
size_t bucket_count = 0;
|
|
|
|
mutable std::atomic<size_t> query_count{0};
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::chrono::time_point<std::chrono::system_clock> creation_time;
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::exception_ptr creation_exception;
|
2015-07-13 16:18:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|