2015-07-13 16:18:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-10-08 19:45:17 +00:00
|
|
|
#include <Columns/ColumnDecimal.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2018-12-10 15:25:45 +00:00
|
|
|
#include <Common/HashTable/HashMap.h>
|
|
|
|
#include "DictionaryStructure.h"
|
|
|
|
#include "IDictionary.h"
|
|
|
|
#include "IDictionarySource.h"
|
2018-09-13 13:33:44 +00:00
|
|
|
|
2015-07-13 16:18:28 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
2018-11-11 19:29:52 +00:00
|
|
|
#include <variant>
|
2015-07-13 16:18:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
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(
|
2019-12-25 23:12:12 +00:00
|
|
|
const std::string & database_,
|
|
|
|
const std::string & name_,
|
2019-08-03 11:02:40 +00:00
|
|
|
const DictionaryStructure & dict_struct_,
|
|
|
|
DictionarySourcePtr source_ptr_,
|
|
|
|
const DictionaryLifetime dict_lifetime_,
|
|
|
|
bool require_nonempty_);
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2019-12-25 23:12:12 +00:00
|
|
|
const std::string & getDatabase() const override { return database; }
|
|
|
|
const std::string & getName() const override { return name; }
|
|
|
|
const std::string & getFullName() const override { return full_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
|
|
|
|
2019-06-02 12:11:01 +00:00
|
|
|
std::shared_ptr<const IExternalLoadable> clone() const override
|
2019-01-19 23:27:52 +00:00
|
|
|
{
|
2019-12-25 23:12:12 +00:00
|
|
|
return std::make_shared<RangeHashedDictionary>(database, name, dict_struct, source_ptr->clone(), dict_lifetime, require_nonempty);
|
2019-01-19 23:27:52 +00:00
|
|
|
}
|
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
|
|
|
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;
|
|
|
|
|
2018-10-08 19:45:17 +00:00
|
|
|
template <typename T>
|
|
|
|
using ResultArrayType = std::conditional_t<IsDecimalNumber<T>, DecimalPaddedPODArray<T>, PaddedPODArray<T>>;
|
|
|
|
|
2018-12-10 15:50:58 +00:00
|
|
|
#define DECLARE_MULTIPLE_GETTER(TYPE) \
|
|
|
|
void get##TYPE( \
|
|
|
|
const std::string & attribute_name, \
|
|
|
|
const PaddedPODArray<Key> & ids, \
|
2018-12-10 15:25:45 +00:00
|
|
|
const PaddedPODArray<RangeStorageType> & dates, \
|
2018-10-08 19:45:17 +00:00
|
|
|
ResultArrayType<TYPE> & out) const;
|
2017-04-01 07:20:54 +00:00
|
|
|
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)
|
2018-10-08 19:45:17 +00:00
|
|
|
DECLARE_MULTIPLE_GETTER(Decimal32)
|
|
|
|
DECLARE_MULTIPLE_GETTER(Decimal64)
|
|
|
|
DECLARE_MULTIPLE_GETTER(Decimal128)
|
2015-07-13 16:18:28 +00:00
|
|
|
#undef DECLARE_MULTIPLE_GETTER
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void getString(
|
2018-12-10 15:25:45 +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
|
|
|
|
2019-02-18 18:51:46 +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);
|
2018-12-10 15:25:45 +00:00
|
|
|
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
|
|
|
|
2018-12-10 15:25:45 +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;
|
2018-12-10 15:25:45 +00:00
|
|
|
std::variant<
|
|
|
|
UInt8,
|
|
|
|
UInt16,
|
|
|
|
UInt32,
|
|
|
|
UInt64,
|
|
|
|
UInt128,
|
|
|
|
Int8,
|
|
|
|
Int16,
|
|
|
|
Int32,
|
|
|
|
Int64,
|
|
|
|
Decimal32,
|
|
|
|
Decimal64,
|
|
|
|
Decimal128,
|
|
|
|
Float32,
|
|
|
|
Float64,
|
|
|
|
String>
|
|
|
|
null_values;
|
|
|
|
std::variant<
|
|
|
|
Ptr<UInt8>,
|
|
|
|
Ptr<UInt16>,
|
|
|
|
Ptr<UInt32>,
|
|
|
|
Ptr<UInt64>,
|
|
|
|
Ptr<UInt128>,
|
|
|
|
Ptr<Int8>,
|
|
|
|
Ptr<Int16>,
|
|
|
|
Ptr<Int32>,
|
|
|
|
Ptr<Int64>,
|
|
|
|
Ptr<Decimal32>,
|
|
|
|
Ptr<Decimal64>,
|
|
|
|
Ptr<Decimal128>,
|
|
|
|
Ptr<Float32>,
|
|
|
|
Ptr<Float64>,
|
|
|
|
Ptr<StringRef>>
|
|
|
|
maps;
|
2017-04-01 07:20:54 +00:00
|
|
|
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>
|
2018-12-10 15:25:45 +00:00
|
|
|
void getIdsAndDates(PaddedPODArray<Key> & ids, 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>
|
2018-12-10 15:25:45 +00:00
|
|
|
void getIdsAndDates(
|
|
|
|
const Attribute & attribute,
|
|
|
|
PaddedPODArray<Key> & ids,
|
|
|
|
PaddedPODArray<RangeType> & start_dates,
|
|
|
|
PaddedPODArray<RangeType> & end_dates) const;
|
2018-09-17 15:04:57 +00:00
|
|
|
|
|
|
|
template <typename RangeType>
|
2019-02-18 18:51:46 +00:00
|
|
|
BlockInputStreamPtr getBlockInputStreamImpl(const Names & column_names, size_t max_block_size) const;
|
2018-09-17 15:04:57 +00:00
|
|
|
|
|
|
|
friend struct RangeHashedDIctionaryCallGetBlockInputStreamImpl;
|
2017-05-04 18:14:23 +00:00
|
|
|
|
2019-12-25 23:12:12 +00:00
|
|
|
const std::string database;
|
|
|
|
const std::string name;
|
|
|
|
const std::string full_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
|
|
|
};
|
|
|
|
|
|
|
|
}
|