2015-01-21 11:39:48 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Dictionaries/IDictionary.h>
|
2015-01-29 13:53:48 +00:00
|
|
|
#include <DB/Dictionaries/IDictionarySource.h>
|
2015-01-29 14:46:15 +00:00
|
|
|
#include <DB/Dictionaries/DictionaryStructure.h>
|
2015-02-19 14:51:39 +00:00
|
|
|
#include <DB/Columns/ColumnString.h>
|
2015-04-16 06:12:35 +00:00
|
|
|
#include <DB/Common/Arena.h>
|
2015-10-05 00:33:43 +00:00
|
|
|
#include <ext/range.hpp>
|
2015-11-19 13:15:02 +00:00
|
|
|
#include <ext/size.hpp>
|
2015-05-08 12:31:00 +00:00
|
|
|
#include <atomic>
|
2015-01-21 11:39:48 +00:00
|
|
|
#include <vector>
|
2015-02-27 11:57:14 +00:00
|
|
|
#include <tuple>
|
2015-01-21 11:39:48 +00:00
|
|
|
|
2015-11-19 13:15:02 +00:00
|
|
|
|
2015-01-21 11:39:48 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2015-01-26 15:27:51 +00:00
|
|
|
class FlatDictionary final : public IDictionary
|
2015-01-21 11:39:48 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-01-29 15:47:21 +00:00
|
|
|
FlatDictionary(const std::string & name, const DictionaryStructure & dict_struct,
|
2016-06-07 21:07:44 +00:00
|
|
|
DictionarySourcePtr source_ptr, const DictionaryLifetime dict_lifetime, bool require_nonempty);
|
2015-01-22 14:32:38 +00:00
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
FlatDictionary(const FlatDictionary & other);
|
2015-01-30 13:43:16 +00:00
|
|
|
|
2015-06-09 16:12:51 +00:00
|
|
|
std::exception_ptr getCreationException() const override { return creation_exception; }
|
|
|
|
|
2015-01-29 15:47:21 +00:00
|
|
|
std::string getName() const override { return name; }
|
|
|
|
|
2015-03-24 12:26:19 +00:00
|
|
|
std::string getTypeName() const override { return "Flat"; }
|
2015-01-29 14:46:15 +00:00
|
|
|
|
2015-03-24 11:30:16 +00:00
|
|
|
std::size_t getBytesAllocated() const override { return bytes_allocated; }
|
|
|
|
|
2015-05-08 12:31:00 +00:00
|
|
|
std::size_t getQueryCount() const override { return query_count.load(std::memory_order_relaxed); }
|
|
|
|
|
2015-03-24 17:02:56 +00:00
|
|
|
double getHitRate() const override { return 1.0; }
|
|
|
|
|
|
|
|
std::size_t getElementCount() const override { return element_count; }
|
|
|
|
|
|
|
|
double getLoadFactor() const override { return static_cast<double>(element_count) / bucket_count; }
|
|
|
|
|
2015-01-29 15:47:21 +00:00
|
|
|
bool isCached() const override { return false; }
|
|
|
|
|
2015-02-10 21:10:58 +00:00
|
|
|
DictionaryPtr clone() const override { return std::make_unique<FlatDictionary>(*this); }
|
2015-01-30 13:43:16 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
const IDictionarySource * getSource() const override { return source_ptr.get(); }
|
2015-01-29 15:47:21 +00:00
|
|
|
|
2015-01-30 15:18:13 +00:00
|
|
|
const DictionaryLifetime & getLifetime() const override { return dict_lifetime; }
|
|
|
|
|
2015-03-24 13:59:19 +00:00
|
|
|
const DictionaryStructure & getStructure() const override { return dict_struct; }
|
|
|
|
|
2015-03-24 17:02:56 +00:00
|
|
|
std::chrono::time_point<std::chrono::system_clock> getCreationTime() const override
|
|
|
|
{
|
|
|
|
return creation_time;
|
|
|
|
}
|
|
|
|
|
2015-05-13 16:11:07 +00:00
|
|
|
bool isInjective(const std::string & attribute_name) const override
|
|
|
|
{
|
|
|
|
return dict_struct.attributes[&getAttribute(attribute_name) - attributes.data()].injective;
|
|
|
|
}
|
|
|
|
|
2015-01-29 13:53:48 +00:00
|
|
|
bool hasHierarchy() const override { return hierarchical_attribute; }
|
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
void toParent(const PaddedPODArray<Key> & ids, PaddedPODArray<Key> & out) const override;
|
2015-01-28 13:20:20 +00:00
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
#define DECLARE(TYPE)\
|
2016-08-07 09:09:18 +00:00
|
|
|
void get##TYPE(const std::string & attribute_name, const PaddedPODArray<Key> & ids, PaddedPODArray<TYPE> & out) const;
|
2015-11-20 15:53:23 +00:00
|
|
|
DECLARE(UInt8)
|
|
|
|
DECLARE(UInt16)
|
|
|
|
DECLARE(UInt32)
|
|
|
|
DECLARE(UInt64)
|
|
|
|
DECLARE(Int8)
|
|
|
|
DECLARE(Int16)
|
|
|
|
DECLARE(Int32)
|
|
|
|
DECLARE(Int64)
|
|
|
|
DECLARE(Float32)
|
|
|
|
DECLARE(Float64)
|
|
|
|
#undef DECLARE
|
2015-01-28 13:20:20 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
void getString(const std::string & attribute_name, const PaddedPODArray<Key> & ids, ColumnString * out) const;
|
2015-11-06 14:49:54 +00:00
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
#define DECLARE(TYPE)\
|
2015-11-06 14:49:54 +00:00
|
|
|
void get##TYPE(\
|
2016-08-07 09:09:18 +00:00
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, const PaddedPODArray<TYPE> & def,\
|
2016-06-07 21:07:44 +00:00
|
|
|
PaddedPODArray<TYPE> & out) const;
|
2015-11-20 15:53:23 +00:00
|
|
|
DECLARE(UInt8)
|
|
|
|
DECLARE(UInt16)
|
|
|
|
DECLARE(UInt32)
|
|
|
|
DECLARE(UInt64)
|
|
|
|
DECLARE(Int8)
|
|
|
|
DECLARE(Int16)
|
|
|
|
DECLARE(Int32)
|
|
|
|
DECLARE(Int64)
|
|
|
|
DECLARE(Float32)
|
|
|
|
DECLARE(Float64)
|
|
|
|
#undef DECLARE
|
2016-06-07 21:07:44 +00:00
|
|
|
|
2015-11-06 14:49:54 +00:00
|
|
|
void getString(
|
2016-08-07 09:09:18 +00:00
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, const ColumnString * const def,
|
2016-06-07 21:07:44 +00:00
|
|
|
ColumnString * const out) const;
|
2015-11-06 14:49:54 +00:00
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
#define DECLARE(TYPE)\
|
|
|
|
void get##TYPE(\
|
2016-08-07 09:09:18 +00:00
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, const TYPE def,\
|
2016-06-07 21:07:44 +00:00
|
|
|
PaddedPODArray<TYPE> & out) const;
|
2015-11-20 15:53:23 +00:00
|
|
|
DECLARE(UInt8)
|
|
|
|
DECLARE(UInt16)
|
|
|
|
DECLARE(UInt32)
|
|
|
|
DECLARE(UInt64)
|
|
|
|
DECLARE(Int8)
|
|
|
|
DECLARE(Int16)
|
|
|
|
DECLARE(Int32)
|
|
|
|
DECLARE(Int64)
|
|
|
|
DECLARE(Float32)
|
|
|
|
DECLARE(Float64)
|
|
|
|
#undef DECLARE
|
2016-06-07 21:07:44 +00:00
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
void getString(
|
2016-08-07 09:09:18 +00:00
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, const String & def,
|
2016-08-24 22:21:48 +00:00
|
|
|
ColumnString * const out) const;
|
2015-11-20 15:53:23 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
void has(const PaddedPODArray<Key> & ids, PaddedPODArray<UInt8> & out) const override;
|
2015-01-28 13:20:20 +00:00
|
|
|
|
2015-01-30 13:43:16 +00:00
|
|
|
private:
|
2016-04-15 00:33:21 +00:00
|
|
|
template <typename Value> using ContainerType = PaddedPODArray<Value>;
|
2015-11-19 13:15:02 +00:00
|
|
|
template <typename Value> using ContainerPtrType = std::unique_ptr<ContainerType<Value>>;
|
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
struct Attribute final
|
2015-01-22 14:32:38 +00:00
|
|
|
{
|
2015-03-20 15:21:29 +00:00
|
|
|
AttributeUnderlyingType type;
|
2015-11-19 13:15:02 +00:00
|
|
|
std::tuple<
|
|
|
|
UInt8, UInt16, UInt32, UInt64,
|
2015-02-27 11:57:14 +00:00
|
|
|
Int8, Int16, Int32, Int64,
|
|
|
|
Float32, Float64,
|
2016-08-24 22:21:48 +00:00
|
|
|
StringRef> null_values;
|
2015-11-19 13:15:02 +00:00
|
|
|
std::tuple<
|
|
|
|
ContainerPtrType<UInt8>, ContainerPtrType<UInt16>, ContainerPtrType<UInt32>, ContainerPtrType<UInt64>,
|
|
|
|
ContainerPtrType<Int8>, ContainerPtrType<Int16>, ContainerPtrType<Int32>, ContainerPtrType<Int64>,
|
|
|
|
ContainerPtrType<Float32>, ContainerPtrType<Float64>,
|
|
|
|
ContainerPtrType<StringRef>> arrays;
|
2015-01-26 15:27:51 +00:00
|
|
|
std::unique_ptr<Arena> string_arena;
|
2015-01-22 14:32:38 +00:00
|
|
|
};
|
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
void createAttributes();
|
|
|
|
void loadData();
|
2015-01-30 13:43:16 +00:00
|
|
|
|
2015-03-24 11:30:16 +00:00
|
|
|
template <typename T>
|
2016-08-07 09:09:18 +00:00
|
|
|
void addAttributeSize(const Attribute & attribute);
|
2015-03-24 11:30:16 +00:00
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
void calculateBytesAllocated();
|
2015-03-24 11:30:16 +00:00
|
|
|
|
2015-02-27 11:57:14 +00:00
|
|
|
template <typename T>
|
2016-08-07 09:09:18 +00:00
|
|
|
void createAttributeImpl(Attribute & attribute, const Field & null_value);
|
2015-01-22 14:32:38 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
Attribute createAttributeWithType(const AttributeUnderlyingType type, const Field & null_value);
|
2016-06-07 19:11:04 +00:00
|
|
|
|
|
|
|
template <typename OutputType, typename ValueSetter, typename DefaultGetter>
|
|
|
|
void getItemsNumber(
|
2016-08-07 09:09:18 +00:00
|
|
|
const Attribute & attribute,
|
|
|
|
const PaddedPODArray<Key> & ids,
|
2016-06-07 19:11:04 +00:00
|
|
|
ValueSetter && set_value,
|
2016-06-07 21:07:44 +00:00
|
|
|
DefaultGetter && get_default) const;
|
2016-06-07 19:11:04 +00:00
|
|
|
|
|
|
|
template <typename AttributeType, typename OutputType, typename ValueSetter, typename DefaultGetter>
|
|
|
|
void getItemsImpl(
|
2016-08-07 09:09:18 +00:00
|
|
|
const Attribute & attribute,
|
|
|
|
const PaddedPODArray<Key> & ids,
|
2016-06-07 19:11:04 +00:00
|
|
|
ValueSetter && set_value,
|
2016-06-07 21:07:44 +00:00
|
|
|
DefaultGetter && get_default) const;
|
2016-06-07 19:11:04 +00:00
|
|
|
|
2015-02-27 11:57:14 +00:00
|
|
|
template <typename T>
|
2016-08-24 22:21:48 +00:00
|
|
|
void resize(Attribute & attribute, const Key id);
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void setAttributeValueImpl(Attribute & attribute, const Key id, const T& value);
|
2015-02-27 11:57:14 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
void setAttributeValue(Attribute & attribute, const Key id, const Field & value);
|
2015-02-19 14:51:39 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
const Attribute & getAttribute(const std::string & attribute_name) const;
|
2015-01-22 14:32:38 +00:00
|
|
|
|
2015-11-19 13:15:02 +00:00
|
|
|
template <typename T>
|
2016-08-07 09:09:18 +00:00
|
|
|
void has(const Attribute & attribute, const PaddedPODArray<Key> & ids, PaddedPODArray<UInt8> & out) const;
|
2015-11-19 13:15:02 +00:00
|
|
|
|
2015-01-29 15:47:21 +00:00
|
|
|
const std::string name;
|
2015-01-30 13:43:16 +00:00
|
|
|
const DictionaryStructure dict_struct;
|
|
|
|
const DictionarySourcePtr source_ptr;
|
2015-01-30 15:18:13 +00:00
|
|
|
const DictionaryLifetime dict_lifetime;
|
2015-08-11 21:32:27 +00:00
|
|
|
const bool require_nonempty;
|
2015-01-30 13:43:16 +00:00
|
|
|
|
2015-01-28 13:20:20 +00:00
|
|
|
std::map<std::string, std::size_t> attribute_index_by_name;
|
2016-08-07 09:09:18 +00:00
|
|
|
std::vector<Attribute> attributes;
|
|
|
|
const Attribute * hierarchical_attribute = nullptr;
|
2016-08-24 22:21:48 +00:00
|
|
|
std::vector<bool> loaded_ids;
|
2015-03-24 11:30:16 +00:00
|
|
|
|
|
|
|
std::size_t bytes_allocated = 0;
|
2015-03-24 17:02:56 +00:00
|
|
|
std::size_t element_count = 0;
|
|
|
|
std::size_t bucket_count = 0;
|
2015-08-12 04:21:10 +00:00
|
|
|
mutable std::atomic<std::size_t> query_count{0};
|
2015-03-24 17:02:56 +00:00
|
|
|
|
|
|
|
std::chrono::time_point<std::chrono::system_clock> creation_time;
|
2015-05-08 12:31:00 +00:00
|
|
|
|
2015-06-09 16:12:51 +00:00
|
|
|
std::exception_ptr creation_exception;
|
2015-01-21 11:39:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|