1 | #pragma once |
2 | |
3 | #include <DataTypes/IDataType.h> |
4 | #include <Columns/ColumnVector.h> |
5 | #include <Columns/ColumnConst.h> |
6 | #include <Common/HashTable/HashMap.h> |
7 | #include <vector> |
8 | #include <unordered_map> |
9 | |
10 | |
11 | namespace DB |
12 | { |
13 | |
14 | namespace ErrorCodes |
15 | { |
16 | extern const int LOGICAL_ERROR; |
17 | } |
18 | |
19 | |
20 | class IDataTypeEnum : public IDataType |
21 | { |
22 | public: |
23 | virtual Field castToName(const Field & value_or_name) const = 0; |
24 | virtual Field castToValue(const Field & value_or_name) const = 0; |
25 | |
26 | bool isParametric() const override { return true; } |
27 | bool haveSubtypes() const override { return false; } |
28 | bool isValueRepresentedByNumber() const override { return true; } |
29 | bool isValueRepresentedByInteger() const override { return true; } |
30 | bool isValueUnambiguouslyRepresentedInContiguousMemoryRegion() const override { return true; } |
31 | bool haveMaximumSizeOfValue() const override { return true; } |
32 | bool isCategorial() const override { return true; } |
33 | bool canBeInsideNullable() const override { return true; } |
34 | bool isComparable() const override { return true; } |
35 | }; |
36 | |
37 | |
38 | template <typename Type> |
39 | class DataTypeEnum final : public IDataTypeEnum |
40 | { |
41 | public: |
42 | using FieldType = Type; |
43 | using ColumnType = ColumnVector<FieldType>; |
44 | using Value = std::pair<std::string, FieldType>; |
45 | using Values = std::vector<Value>; |
46 | using NameToValueMap = HashMap<StringRef, FieldType, StringRefHash>; |
47 | using ValueToNameMap = std::unordered_map<FieldType, StringRef>; |
48 | |
49 | static constexpr bool is_parametric = true; |
50 | |
51 | private: |
52 | Values values; |
53 | NameToValueMap name_to_value_map; |
54 | ValueToNameMap value_to_name_map; |
55 | std::string type_name; |
56 | |
57 | static std::string generateName(const Values & values); |
58 | void fillMaps(); |
59 | |
60 | public: |
61 | explicit DataTypeEnum(const Values & values_); |
62 | |
63 | const Values & getValues() const { return values; } |
64 | std::string doGetName() const override { return type_name; } |
65 | const char * getFamilyName() const override; |
66 | |
67 | TypeIndex getTypeId() const override { return sizeof(FieldType) == 1 ? TypeIndex::Enum8 : TypeIndex::Enum16; } |
68 | |
69 | const StringRef & getNameForValue(const FieldType & value) const |
70 | { |
71 | const auto it = value_to_name_map.find(value); |
72 | if (it == std::end(value_to_name_map)) |
73 | throw Exception{"Unexpected value " + toString(value) + " for type " + getName(), ErrorCodes::LOGICAL_ERROR}; |
74 | |
75 | return it->second; |
76 | } |
77 | |
78 | FieldType getValue(StringRef field_name) const |
79 | { |
80 | const auto it = name_to_value_map.find(field_name); |
81 | if (!it) |
82 | throw Exception{"Unknown element '" + field_name.toString() + "' for type " + getName(), ErrorCodes::LOGICAL_ERROR}; |
83 | |
84 | return it->getMapped(); |
85 | } |
86 | |
87 | Field castToName(const Field & value_or_name) const override; |
88 | Field castToValue(const Field & value_or_name) const override; |
89 | |
90 | void serializeBinary(const Field & field, WriteBuffer & ostr) const override; |
91 | void deserializeBinary(Field & field, ReadBuffer & istr) const override; |
92 | void serializeBinary(const IColumn & column, size_t row_num, WriteBuffer & ostr) const override; |
93 | void deserializeBinary(IColumn & column, ReadBuffer & istr) const override; |
94 | void serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override; |
95 | void serializeTextEscaped(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override; |
96 | void deserializeTextEscaped(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override; |
97 | void serializeTextQuoted(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override; |
98 | void deserializeTextQuoted(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override; |
99 | void deserializeWholeText(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override; |
100 | |
101 | void serializeTextJSON(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override; |
102 | void deserializeTextJSON(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override; |
103 | void serializeTextXML(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override; |
104 | void serializeTextCSV(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override; |
105 | void deserializeTextCSV(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const override; |
106 | |
107 | void serializeBinaryBulk(const IColumn & column, WriteBuffer & ostr, const size_t offset, size_t limit) const override; |
108 | void deserializeBinaryBulk(IColumn & column, ReadBuffer & istr, const size_t limit, const double avg_value_size_hint) const override; |
109 | |
110 | void serializeProtobuf(const IColumn & column, size_t row_num, ProtobufWriter & protobuf, size_t & value_index) const override; |
111 | void deserializeProtobuf(IColumn & column, ProtobufReader & protobuf, bool allow_add_row, bool & row_added) const override; |
112 | |
113 | MutableColumnPtr createColumn() const override { return ColumnType::create(); } |
114 | |
115 | Field getDefault() const override; |
116 | void insertDefaultInto(IColumn & column) const override; |
117 | |
118 | bool equals(const IDataType & rhs) const override; |
119 | |
120 | bool textCanContainOnlyValidUTF8() const override; |
121 | size_t getSizeOfValueInMemory() const override { return sizeof(FieldType); } |
122 | }; |
123 | |
124 | |
125 | using DataTypeEnum8 = DataTypeEnum<Int8>; |
126 | using DataTypeEnum16 = DataTypeEnum<Int16>; |
127 | |
128 | } |
129 | |