forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumValues.cpp
More file actions
283 lines (247 loc) · 9.8 KB
/
Copy pathEnumValues.cpp
File metadata and controls
283 lines (247 loc) · 9.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <DataTypes/EnumValues.h>
#include <boost/algorithm/string.hpp>
#include <base/sort.h>
#include <Core/Field.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h>
#include <algorithm>
#include <numeric>
namespace DB
{
namespace ErrorCodes
{
extern const int SYNTAX_ERROR;
extern const int EMPTY_DATA_PASSED;
extern const int UNKNOWN_ELEMENT_OF_ENUM;
}
template <typename T>
EnumValues<T>::EnumValues(const Values & values_, ValidationMode validation_mode)
: values(values_)
{
if (values.empty())
throw Exception(ErrorCodes::EMPTY_DATA_PASSED, "DataTypeEnum enumeration cannot be empty");
/// Temporary `ADD ENUM VALUES` entries carry per-element relative flags, so their order
/// must stay exactly as parsed until `mergeEnumTypes` consumes those flags.
if (validation_mode == ValidationMode::Normal)
{
/// Sort values by numeric value (for getValues() compatibility)
::sort(std::begin(values), std::end(values), [](const auto & left, const auto & right)
{
return left.second < right.second;
});
}
buildLookupStructures(validation_mode);
}
template <typename T>
EnumValues<T>::~EnumValues() = default;
template <typename T>
void EnumValues<T>::buildLookupStructures(ValidationMode validation_mode)
{
const size_t n = values.size();
/// Build name-sorted index for binary search on names
name_sorted_index.resize(n);
std::iota(name_sorted_index.begin(), name_sorted_index.end(), static_cast<uint16_t>(0));
::sort(name_sorted_index.begin(), name_sorted_index.end(), [this](uint16_t a, uint16_t b)
{
return values[a].first < values[b].first;
});
/// Check for duplicate names
for (size_t i = 1; i < n; ++i)
{
if (values[name_sorted_index[i - 1]].first == values[name_sorted_index[i]].first)
{
const auto & dup_name = values[name_sorted_index[i]].first;
throw Exception(ErrorCodes::SYNTAX_ERROR, "Duplicate names in enum: '{}' = {} and {}",
dup_name,
toString(values[name_sorted_index[i - 1]].second),
toString(values[name_sorted_index[i]].second));
}
}
/// `ADD ENUM VALUES` may temporarily rewrite a leading implicit prefix to `1..N`. These numbers are
/// only relative placeholders until `mergeEnumTypes` remaps them, so a `TemporaryAdd` instance keeps
/// its values in parser order (not sorted by value) and may contain duplicate placeholder values.
/// Skip duplicate-value validation and the value-to-name lookup structures: such an instance is only
/// used to read back `getValues()` and is never queried by value, and building the value-to-name
/// lookup here would be incorrect (and could even overflow the range) on unsorted values.
if (validation_mode == ValidationMode::TemporaryAdd)
return;
/// Check for duplicate values (values are already sorted by value)
for (size_t i = 1; i < n; ++i)
{
if (values[i - 1].second == values[i].second)
{
throw Exception(ErrorCodes::SYNTAX_ERROR, "Duplicate values in enum: '{}' = {} and '{}'",
values[i].first, toString(values[i].second), values[i - 1].first);
}
}
/// Decide strategy for value-to-name lookup
/// Cast to Int32 first to avoid signed overflow when computing range (e.g., 127 - (-128) for Enum8)
T min_val = values.front().second;
T max_val = values.back().second;
size_t range = static_cast<size_t>(static_cast<Int32>(max_val) - static_cast<Int32>(min_val)) + 1;
if constexpr (sizeof(T) == 1)
{
/// Enum8: always use direct lookup (max 512 bytes)
use_direct_value_lookup = true;
}
else
{
/// Enum16: use direct lookup if range is small enough
use_direct_value_lookup = (range <= DIRECT_LOOKUP_THRESHOLD);
}
if (use_direct_value_lookup)
{
/// Build direct lookup array
value_to_index.resize(range, INVALID_INDEX);
for (size_t i = 0; i < n; ++i)
{
/// Cast to Int32 first to avoid signed overflow (e.g., 1 - (-128) for Enum8)
size_t idx = static_cast<size_t>(static_cast<Int32>(values[i].second) - static_cast<Int32>(min_val));
value_to_index[idx] = static_cast<uint16_t>(i);
}
}
/// For non-direct lookup, we search directly on values (already sorted by value)
}
template <typename T>
bool EnumValues<T>::hasValue(T value) const
{
std::string_view ignored;
return getNameForValue(value, ignored);
}
template <typename T>
std::string_view EnumValues<T>::getNameForValue(T value) const
{
std::string_view result;
if (!getNameForValue(value, result))
throw Exception(ErrorCodes::UNKNOWN_ELEMENT_OF_ENUM, "Unexpected value {} in enum", toString(value));
return result;
}
template <typename T>
bool EnumValues<T>::getNameForValue(T value, std::string_view & result) const
{
if (use_direct_value_lookup)
{
T min_val = values.front().second;
T max_val = values.back().second;
if (value < min_val || value > max_val)
return false;
/// Cast to Int32 first to avoid signed overflow
size_t arr_idx = static_cast<size_t>(static_cast<Int32>(value) - static_cast<Int32>(min_val));
uint16_t idx = value_to_index[arr_idx];
if (idx == INVALID_INDEX)
return false;
result = values[idx].first;
return true;
}
else
{
/// Early bounds check
if (value < values.front().second || value > values.back().second)
return false;
/// Binary search on values (already sorted by value)
auto it = std::lower_bound(values.begin(), values.end(), value,
[](const Value & v, T val) { return v.second < val; });
if (it != values.end() && it->second == value)
{
result = it->first;
return true;
}
return false;
}
}
template <typename T>
T EnumValues<T>::getValue(std::string_view field_name) const
{
T x;
if (tryGetValue(x, field_name))
return x;
auto hints = this->getHints(std::string{field_name});
auto hints_string = !hints.empty() ? ", maybe you meant: " + toString(hints) : "";
throw Exception(ErrorCodes::UNKNOWN_ELEMENT_OF_ENUM, "Unknown element '{}' for enum{}", field_name, hints_string);
}
template <typename T>
bool EnumValues<T>::findValueByName(std::string_view field_name, T & result) const
{
/// Binary search on name-sorted index
auto it = std::lower_bound(name_sorted_index.begin(), name_sorted_index.end(), field_name,
[this](uint16_t idx, std::string_view name) { return values[idx].first < name; });
if (it != name_sorted_index.end() && values[*it].first == field_name)
{
result = values[*it].second;
return true;
}
return false;
}
template <typename T>
bool EnumValues<T>::tryGetValue(T & x, std::string_view field_name) const
{
if (findValueByName(field_name, x))
return true;
/// Fallback: try parsing as numeric value
if (tryParse(x, field_name.data(), field_name.size()) && hasValue(x))
return true;
return false;
}
template <typename T>
size_t EnumValues<T>::allocatedBytes() const
{
size_t bytes = values.capacity() * sizeof(typename Values::value_type);
for (const auto & [name, _] : values)
bytes += name.capacity();
bytes += name_sorted_index.capacity() * sizeof(typename decltype(name_sorted_index)::value_type);
bytes += value_to_index.capacity() * sizeof(typename decltype(value_to_index)::value_type);
return bytes;
}
template <typename T>
VectorWithMemoryTracking<String> EnumValues<T>::getAllRegisteredNames() const
{
VectorWithMemoryTracking<String> result;
result.reserve(values.size());
for (const auto & value : values)
result.emplace_back(value.first);
return result;
}
template <typename T>
std::unordered_set<String> EnumValues<T>::getSetOfAllNames(bool to_lower) const
{
std::unordered_set<String> result;
for (const auto & value : values)
result.insert(to_lower ? boost::algorithm::to_lower_copy(value.first) : value.first);
return result;
}
template <typename T>
std::unordered_set<T> EnumValues<T>::getSetOfAllValues() const
{
std::unordered_set<T> result;
for (const auto & value : values)
result.insert(value.second);
return result;
}
template <typename T>
template <typename TValues>
bool EnumValues<T>::containsAll(const TValues & rhs_values) const
{
auto check = [&](const auto & value)
{
/// Look up by exact name only. We must NOT fall back to numeric-string parsing here:
/// `containsAll` is a compatibility check between enums, and a literal name like "1" must
/// not be reinterpreted as the numeric value 1 (which would change behavior compared to
/// the previous hash-map-based implementation).
T found_value;
if (findValueByName(value.first, found_value))
{
/// If we have this name, it should have the same value
return found_value == value.second;
}
/// If we don't have this name, check if the value exists
return hasValue(static_cast<T>(value.second));
};
return std::all_of(rhs_values.begin(), rhs_values.end(), check);
}
template bool EnumValues<Int8>::containsAll<EnumValues<Int8>::Values>(const EnumValues<Int8>::Values & rhs_values) const;
template bool EnumValues<Int8>::containsAll<EnumValues<Int16>::Values>(const EnumValues<Int16>::Values & rhs_values) const;
template bool EnumValues<Int16>::containsAll<EnumValues<Int8>::Values>(const EnumValues<Int8>::Values & rhs_values) const;
template bool EnumValues<Int16>::containsAll<EnumValues<Int16>::Values>(const EnumValues<Int16>::Values & rhs_values) const;
template class EnumValues<Int8>;
template class EnumValues<Int16>;
}