#pragma once #include #include #include #include #include #include #include #include #include #include #include #include namespace DB { using RangeStorageType = Int64; struct Range { RangeStorageType left; RangeStorageType right; static bool isCorrectDate(const RangeStorageType & date); bool contains(const RangeStorageType & value) const; }; 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_); std::string getTypeName() const override { return "RangeHashed"; } 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); } const IDictionarySource * getSource() const override { return source_ptr.get(); } 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) const override; private: template struct Value final { Range range; std::optional value; }; template using Values = std::vector>; template using CollectionType = std::conditional_t< dictionary_key_type == DictionaryKeyType::Simple, HashMap>, HashMapWithSavedHash, DefaultHash>>; struct Attribute final { public: AttributeUnderlyingType type; bool is_nullable; std::variant< CollectionType, CollectionType, CollectionType, CollectionType, CollectionType, CollectionType, CollectionType, CollectionType, CollectionType, CollectionType, CollectionType, CollectionType, CollectionType, CollectionType, CollectionType, CollectionType, CollectionType, CollectionType, CollectionType, CollectionType, CollectionType> maps; std::unique_ptr string_arena; }; 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; template static void setAttributeValueImpl(Attribute & attribute, KeyType key, const Range & range, const Field & value); static void setAttributeValue(Attribute & attribute, KeyType key, const Range & range, const Field & value); template void getKeysAndDates( PaddedPODArray & keys, PaddedPODArray & start_dates, PaddedPODArray & end_dates) const; template void getKeysAndDates( const Attribute & attribute, PaddedPODArray & keys, PaddedPODArray & start_dates, PaddedPODArray & end_dates) const; template Pipe readImpl(const Names & column_names, size_t max_block_size) const; StringRef copyKeyInArena(StringRef key); template friend struct RangeHashedDictionaryCallGetSourceImpl; const DictionaryStructure dict_struct; const DictionarySourcePtr source_ptr; const DictionaryLifetime dict_lifetime; const bool require_nonempty; std::map attribute_index_by_name; std::vector attributes; Arena complex_key_arena; 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}; }; }