#pragma once #include #include #include #include #include #include #include #include #include #include #include #include namespace DB { using RangeStorageType = Int64; template class RangeHashedDictionary final : public IDictionary { public: using KeyType = std::conditional_t; RangeHashedDictionary( const StorageID & dict_id_, const DictionaryStructure & dict_struct_, DictionarySourcePtr source_ptr_, const DictionaryLifetime dict_lifetime_, bool require_nonempty_, BlockPtr update_field_loaded_block_ = nullptr); std::string getTypeName() const override { if (dictionary_key_type == DictionaryKeyType::Simple) return "RangeHashed"; else return "ComplexKeyRangeHashed"; } size_t getBytesAllocated() const override { return bytes_allocated; } size_t getQueryCount() const override { return query_count.load(std::memory_order_relaxed); } double getFoundRate() const override { size_t queries = query_count.load(std::memory_order_relaxed); if (!queries) return 0; return static_cast(found_count.load(std::memory_order_relaxed)) / queries; } double getHitRate() const override { return 1.0; } size_t getElementCount() const override { return element_count; } double getLoadFactor() const override { return static_cast(element_count) / bucket_count; } std::shared_ptr clone() const override { return std::make_shared(getDictionaryID(), dict_struct, source_ptr->clone(), dict_lifetime, require_nonempty, update_field_loaded_block); } DictionarySourcePtr getSource() const override { return source_ptr; } const DictionaryLifetime & getLifetime() const override { return dict_lifetime; } const DictionaryStructure & getStructure() const override { return dict_struct; } bool isInjective(const std::string & attribute_name) const override { return dict_struct.getAttribute(attribute_name).injective; } DictionaryKeyType getKeyType() const override { return dictionary_key_type; } DictionarySpecialKeyType getSpecialKeyType() const override { return DictionarySpecialKeyType::Range;} ColumnPtr getColumn( const std::string& attribute_name, const DataTypePtr & result_type, const Columns & key_columns, const DataTypes & key_types, const ColumnPtr & default_values_column) const override; ColumnUInt8::Ptr hasKeys(const Columns & key_columns, const DataTypes & key_types) const override; Pipe read(const Names & column_names, size_t max_block_size, size_t num_streams) const override; private: using RangeInterval = Interval; using IntervalMap = IntervalMap; using KeyContainerType = std::conditional_t< dictionary_key_type == DictionaryKeyType::Simple, HashMap>, HashMapWithSavedHash>>; template using AttributeContainerType = std::conditional_t, std::vector, PaddedPODArray>; struct Attribute final { public: AttributeUnderlyingType type; std::variant< AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType, AttributeContainerType> container; std::optional> is_value_nullable; }; struct KeyAttribute final { KeyContainerType container; }; void createAttributes(); void loadData(); void calculateBytesAllocated(); static Attribute createAttribute(const DictionaryAttribute & dictionary_attribute); template void getItemsImpl( const Attribute & attribute, const Columns & key_columns, ValueSetter && set_value, DefaultValueExtractor & default_value_extractor) const; void updateData(); void blockToAttributes(const Block & block); template void setAttributeValueImpl(Attribute & attribute, const Field & value); void setAttributeValue(Attribute & attribute, const Field & value); template void getKeysAndRangeValues( PaddedPODArray & keys, PaddedPODArray & range_start_values, PaddedPODArray & range_end_value) const; template PaddedPODArray makeKeysValues( const PaddedPODArray & range_start_values, const PaddedPODArray & range_end_values) const; const DictionaryStructure dict_struct; const DictionarySourcePtr source_ptr; const DictionaryLifetime dict_lifetime; const bool require_nonempty; BlockPtr update_field_loaded_block; std::vector attributes; KeyAttribute key_attribute; size_t bytes_allocated = 0; size_t element_count = 0; size_t bucket_count = 0; mutable std::atomic query_count{0}; mutable std::atomic found_count{0}; Arena string_arena; }; }