DictionaryStructure.h 5.5 KB
Newer Older
1 2
#pragma once

3
#include <map>
4
#include <optional>
P
proller 已提交
5 6
#include <string>
#include <vector>
7

8

M
Maksim Kita 已提交
9 10 11 12 13 14 15
#include <Poco/Util/AbstractConfiguration.h>

#include <Core/Field.h>
#include <IO/ReadBufferFromString.h>
#include <DataTypes/IDataType.h>
#include <Interpreters/IExternalLoadable.h>

16 17
namespace DB
{
18

19
enum class AttributeUnderlyingType
20
{
K
kreuzerkrieg 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    utUInt8,
    utUInt16,
    utUInt32,
    utUInt64,
    utUInt128,
    utInt8,
    utInt16,
    utInt32,
    utInt64,
    utFloat32,
    utFloat64,
    utDecimal32,
    utDecimal64,
    utDecimal128,
    utString
36 37
};

38

39
AttributeUnderlyingType getAttributeUnderlyingType(const std::string & type);
40

41
std::string toString(const AttributeUnderlyingType type);
42

43
/// Min and max lifetimes for a dictionary or it's entry
44
using DictionaryLifetime = ExternalLoadableLifetime;
45

46
/** Holds the description of a single dictionary attribute:
47 48
*    - name, used for lookup into dictionary and source;
*    - type, used in conjunction with DataTypeFactory and getAttributeUnderlyingTypeByname;
M
Maksim Kita 已提交
49
*    - nested_type, contains nested type of complex type like Nullable, Array
50 51 52 53
*    - null_value, used as a default value for non-existent entries in the dictionary,
*        decimal representation for numeric attributes;
*    - hierarchical, whether this attribute defines a hierarchy;
*    - injective, whether the mapping to parent is injective (can be used for optimization of GROUP BY?)
54
*    - is_object_id, used in mongo dictionary, converts string key to objectid
55
*/
56
struct DictionaryAttribute final
57
{
58 59 60
    const std::string name;
    const AttributeUnderlyingType underlying_type;
    const DataTypePtr type;
A
Anton Popov 已提交
61
    const SerializationPtr serialization;
M
Maksim Kita 已提交
62
    const DataTypePtr nested_type;
63 64 65 66
    const std::string expression;
    const Field null_value;
    const bool hierarchical;
    const bool injective;
67
    const bool is_object_id;
68 69 70 71 72 73 74 75
    const bool is_nullable;
    const bool is_array;
};

template <typename Type>
struct DictionaryAttributeType
{
    using AttributeType = Type;
76 77
};

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
template <typename F>
void callOnDictionaryAttributeType(AttributeUnderlyingType type, F&& func)
{
    switch (type)
    {
        case AttributeUnderlyingType::utUInt8:
            func(DictionaryAttributeType<UInt8>());
            break;
        case AttributeUnderlyingType::utUInt16:
            func(DictionaryAttributeType<UInt16>());
            break;
        case AttributeUnderlyingType::utUInt32:
            func(DictionaryAttributeType<UInt32>());
            break;
        case AttributeUnderlyingType::utUInt64:
            func(DictionaryAttributeType<UInt64>());
            break;
        case AttributeUnderlyingType::utUInt128:
            func(DictionaryAttributeType<UInt128>());
            break;
        case AttributeUnderlyingType::utInt8:
            func(DictionaryAttributeType<Int8>());
            break;
        case AttributeUnderlyingType::utInt16:
            func(DictionaryAttributeType<Int16>());
            break;
        case AttributeUnderlyingType::utInt32:
            func(DictionaryAttributeType<Int32>());
            break;
        case AttributeUnderlyingType::utInt64:
            func(DictionaryAttributeType<Int64>());
            break;
        case AttributeUnderlyingType::utFloat32:
            func(DictionaryAttributeType<Float32>());
            break;
        case AttributeUnderlyingType::utFloat64:
            func(DictionaryAttributeType<Float64>());
            break;
        case AttributeUnderlyingType::utString:
            func(DictionaryAttributeType<String>());
            break;
        case AttributeUnderlyingType::utDecimal32:
            func(DictionaryAttributeType<Decimal32>());
            break;
        case AttributeUnderlyingType::utDecimal64:
            func(DictionaryAttributeType<Decimal64>());
            break;
        case AttributeUnderlyingType::utDecimal128:
            func(DictionaryAttributeType<Decimal128>());
            break;
    }
};
130

131 132
struct DictionarySpecialAttribute final
{
133 134
    const std::string name;
    const std::string expression;
135

136
    DictionarySpecialAttribute(const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix);
137 138
};

139 140 141 142 143 144 145
struct DictionaryTypedSpecialAttribute final
{
    const std::string name;
    const std::string expression;
    const DataTypePtr type;
};

146

147
/// Name of identifier plus list of attributes
148
struct DictionaryStructure final
149
{
150 151
    std::optional<DictionarySpecialAttribute> id;
    std::optional<std::vector<DictionaryAttribute>> key;
152
    std::vector<DictionaryAttribute> attributes;
M
Maksim Kita 已提交
153
    std::unordered_map<std::string, size_t> attribute_name_to_index;
154 155
    std::optional<DictionaryTypedSpecialAttribute> range_min;
    std::optional<DictionaryTypedSpecialAttribute> range_max;
156
    bool has_expressions = false;
157
    bool access_to_key_from_attributes = false;
158

159
    DictionaryStructure(const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix);
160

161
    void validateKeyTypes(const DataTypes & key_types) const;
M
Maksim Kita 已提交
162 163 164

    const DictionaryAttribute & getAttribute(const std::string & attribute_name) const;
    const DictionaryAttribute & getAttribute(const std::string & attribute_name, const DataTypePtr & type) const;
M
Maksim Kita 已提交
165 166

    Strings getKeysNames() const;
M
Maksim Kita 已提交
167 168
    size_t getKeysSize() const;

169 170
    std::string getKeyDescription() const;
    bool isKeySizeFixed() const;
171

172
private:
173
    /// range_min and range_max have to be parsed before this function call
174
    std::vector<DictionaryAttribute> getAttributes(
P
proller 已提交
175 176
        const Poco::Util::AbstractConfiguration & config,
        const std::string & config_prefix,
M
Maksim Kita 已提交
177
        bool complex_key_attributes);
178 179 180
};

}