2015-07-13 16:18:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-01-23 13:18:24 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
|
|
|
#include <variant>
|
|
|
|
#include <optional>
|
|
|
|
|
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>
|
2022-01-11 10:15:12 +00:00
|
|
|
#include <Common/IntervalTree.h>
|
|
|
|
|
2021-08-11 12:32:01 +00:00
|
|
|
#include <Dictionaries/DictionaryStructure.h>
|
|
|
|
#include <Dictionaries/IDictionary.h>
|
|
|
|
#include <Dictionaries/IDictionarySource.h>
|
|
|
|
#include <Dictionaries/DictionaryHelpers.h>
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2022-01-11 10:15:12 +00:00
|
|
|
|
2015-07-13 16:18:28 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2021-08-12 21:39:20 +00:00
|
|
|
|
|
|
|
using RangeStorageType = Int64;
|
|
|
|
|
|
|
|
template <DictionaryKeyType dictionary_key_type>
|
2021-03-24 16:31:00 +00:00
|
|
|
class RangeHashedDictionary final : public IDictionary
|
2015-07-13 16:18:28 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-08-17 17:35:43 +00:00
|
|
|
using KeyType = std::conditional_t<dictionary_key_type == DictionaryKeyType::Simple, UInt64, StringRef>;
|
2021-08-12 21:39:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
RangeHashedDictionary(
|
2020-07-14 18:46:29 +00:00
|
|
|
const StorageID & dict_id_,
|
2019-08-03 11:02:40 +00:00
|
|
|
const DictionaryStructure & dict_struct_,
|
|
|
|
DictionarySourcePtr source_ptr_,
|
|
|
|
const DictionaryLifetime dict_lifetime_,
|
2021-12-03 11:06:58 +00:00
|
|
|
bool require_nonempty_,
|
|
|
|
BlockPtr update_field_loaded_block_ = nullptr);
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2022-01-23 17:20:37 +00:00
|
|
|
std::string getTypeName() const override
|
|
|
|
{
|
|
|
|
if (dictionary_key_type == DictionaryKeyType::Simple)
|
|
|
|
return "RangeHashed";
|
|
|
|
else
|
|
|
|
return "ComplexKeyRangeHashed";
|
|
|
|
}
|
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
|
|
|
|
2021-05-05 07:56:21 +00:00
|
|
|
double getFoundRate() const override
|
|
|
|
{
|
|
|
|
size_t queries = query_count.load(std::memory_order_relaxed);
|
|
|
|
if (!queries)
|
|
|
|
return 0;
|
|
|
|
return static_cast<double>(found_count.load(std::memory_order_relaxed)) / queries;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-12-03 11:06:58 +00:00
|
|
|
return std::make_shared<RangeHashedDictionary>(getDictionaryID(), dict_struct, source_ptr->clone(), dict_lifetime, require_nonempty, update_field_loaded_block);
|
2019-01-19 23:27:52 +00:00
|
|
|
}
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2021-12-15 12:55:28 +00:00
|
|
|
DictionarySourcePtr getSource() const override { return source_ptr; }
|
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
|
|
|
|
{
|
2021-08-12 21:39:20 +00:00
|
|
|
return dict_struct.getAttribute(attribute_name).injective;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2021-08-17 17:35:43 +00:00
|
|
|
DictionaryKeyType getKeyType() const override { return dictionary_key_type; }
|
|
|
|
|
|
|
|
DictionarySpecialKeyType getSpecialKeyType() const override { return DictionarySpecialKeyType::Range;}
|
2018-09-13 13:33:44 +00:00
|
|
|
|
2020-12-20 20:11:28 +00:00
|
|
|
ColumnPtr getColumn(
|
|
|
|
const std::string& attribute_name,
|
|
|
|
const DataTypePtr & result_type,
|
|
|
|
const Columns & key_columns,
|
|
|
|
const DataTypes & key_types,
|
2021-02-16 21:33:02 +00:00
|
|
|
const ColumnPtr & default_values_column) const override;
|
2020-12-20 20:11:28 +00:00
|
|
|
|
2021-01-23 13:18:24 +00:00
|
|
|
ColumnUInt8::Ptr hasKeys(const Columns & key_columns, const DataTypes & key_types) const override;
|
2020-12-20 20:11:28 +00:00
|
|
|
|
2021-10-21 14:17:53 +00:00
|
|
|
Pipe read(const Names & column_names, size_t max_block_size, size_t num_streams) const override;
|
2017-05-04 18:14:23 +00:00
|
|
|
|
2017-12-25 19:00:48 +00:00
|
|
|
private:
|
2022-01-11 10:15:12 +00:00
|
|
|
|
|
|
|
using RangeInterval = Interval<RangeStorageType>;
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2022-01-23 17:20:37 +00:00
|
|
|
using IntervalMap = IntervalMap<RangeInterval, size_t>;
|
2021-08-12 21:39:20 +00:00
|
|
|
|
2022-01-23 17:20:37 +00:00
|
|
|
using KeyContainerType = std::conditional_t<
|
2021-08-17 17:35:43 +00:00
|
|
|
dictionary_key_type == DictionaryKeyType::Simple,
|
2022-01-23 17:20:37 +00:00
|
|
|
HashMap<UInt64, IntervalMap, DefaultHash<UInt64>>,
|
|
|
|
HashMapWithSavedHash<StringRef, IntervalMap, DefaultHash<StringRef>>>;
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2022-01-23 17:20:37 +00:00
|
|
|
template <typename Value>
|
|
|
|
using AttributeContainerType = std::conditional_t<std::is_same_v<Value, Array>, std::vector<Value>, PaddedPODArray<Value>>;
|
2022-01-22 20:01:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
struct Attribute final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AttributeUnderlyingType type;
|
2021-01-04 21:07:45 +00:00
|
|
|
|
2018-12-10 15:25:45 +00:00
|
|
|
std::variant<
|
2022-01-23 17:20:37 +00:00
|
|
|
AttributeContainerType<UInt8>,
|
|
|
|
AttributeContainerType<UInt16>,
|
|
|
|
AttributeContainerType<UInt32>,
|
|
|
|
AttributeContainerType<UInt64>,
|
|
|
|
AttributeContainerType<UInt128>,
|
|
|
|
AttributeContainerType<UInt256>,
|
|
|
|
AttributeContainerType<Int8>,
|
|
|
|
AttributeContainerType<Int16>,
|
|
|
|
AttributeContainerType<Int32>,
|
|
|
|
AttributeContainerType<Int64>,
|
|
|
|
AttributeContainerType<Int128>,
|
|
|
|
AttributeContainerType<Int256>,
|
|
|
|
AttributeContainerType<Decimal32>,
|
|
|
|
AttributeContainerType<Decimal64>,
|
|
|
|
AttributeContainerType<Decimal128>,
|
|
|
|
AttributeContainerType<Decimal256>,
|
|
|
|
AttributeContainerType<DateTime64>,
|
|
|
|
AttributeContainerType<Float32>,
|
|
|
|
AttributeContainerType<Float64>,
|
|
|
|
AttributeContainerType<UUID>,
|
|
|
|
AttributeContainerType<StringRef>,
|
|
|
|
AttributeContainerType<Array>>
|
|
|
|
container;
|
|
|
|
|
|
|
|
std::optional<std::vector<bool>> is_value_nullable;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct KeyAttribute final
|
|
|
|
{
|
|
|
|
|
|
|
|
KeyContainerType container;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
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
|
|
|
void calculateBytesAllocated();
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2021-06-12 10:53:03 +00:00
|
|
|
static Attribute createAttribute(const DictionaryAttribute & dictionary_attribute);
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2021-06-12 10:53:03 +00:00
|
|
|
template <typename AttributeType, bool is_nullable, typename ValueSetter, typename DefaultValueExtractor>
|
2017-04-01 07:20:54 +00:00
|
|
|
void getItemsImpl(
|
|
|
|
const Attribute & attribute,
|
2020-12-20 20:11:28 +00:00
|
|
|
const Columns & key_columns,
|
|
|
|
ValueSetter && set_value,
|
2021-01-23 16:47:33 +00:00
|
|
|
DefaultValueExtractor & default_value_extractor) const;
|
2016-06-07 19:11:04 +00:00
|
|
|
|
2021-12-03 11:06:58 +00:00
|
|
|
void updateData();
|
|
|
|
|
|
|
|
void blockToAttributes(const Block & block);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename T>
|
2022-01-23 17:20:37 +00:00
|
|
|
void setAttributeValueImpl(Attribute & attribute, const Field & value);
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2022-01-23 17:20:37 +00:00
|
|
|
void setAttributeValue(Attribute & attribute, const Field & value);
|
2015-07-14 13:06:25 +00:00
|
|
|
|
2018-09-17 15:04:57 +00:00
|
|
|
template <typename RangeType>
|
2022-01-23 17:20:37 +00:00
|
|
|
void getKeysAndRangeValues(
|
2021-08-12 21:39:20 +00:00
|
|
|
PaddedPODArray<KeyType> & keys,
|
2022-01-23 17:20:37 +00:00
|
|
|
PaddedPODArray<RangeType> & range_start_values,
|
|
|
|
PaddedPODArray<RangeType> & range_end_value) const;
|
2018-09-17 15:04:57 +00:00
|
|
|
|
|
|
|
template <typename RangeType>
|
2022-01-23 17:20:37 +00:00
|
|
|
PaddedPODArray<Int64> makeKeysValues(
|
|
|
|
const PaddedPODArray<RangeType> & range_start_values,
|
|
|
|
const PaddedPODArray<RangeType> & range_end_values) const;
|
2018-09-17 15:04:57 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const DictionaryStructure dict_struct;
|
|
|
|
const DictionarySourcePtr source_ptr;
|
|
|
|
const DictionaryLifetime dict_lifetime;
|
|
|
|
const bool require_nonempty;
|
2021-12-03 11:06:58 +00:00
|
|
|
BlockPtr update_field_loaded_block;
|
2015-07-13 16:18:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::vector<Attribute> attributes;
|
2022-01-23 17:20:37 +00:00
|
|
|
KeyAttribute key_attribute;
|
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};
|
2021-05-05 07:56:21 +00:00
|
|
|
mutable std::atomic<size_t> found_count{0};
|
2022-01-08 10:26:11 +00:00
|
|
|
Arena string_arena;
|
2015-07-13 16:18:28 +00:00
|
|
|
};
|
|
|
|
|
2021-08-06 08:41:45 +00:00
|
|
|
}
|