-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathtypes.h
More file actions
385 lines (286 loc) · 8.65 KB
/
types.h
File metadata and controls
385 lines (286 loc) · 8.65 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#pragma once
#include "absl/numeric/int128.h"
#include <atomic>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <stdexcept>
namespace clickhouse {
using Int128 = absl::int128;
using UInt128 = absl::uint128;
using Int64 = int64_t;
using TypeRef = std::shared_ptr<class Type>;
class Type {
public:
enum Code {
Void = 0,
Int8,
Int16,
Int32,
Int64,
Bool,
UInt8,
UInt16,
UInt32,
UInt64,
Float32,
Float64,
String,
FixedString,
DateTime,
Date,
Array,
Nullable,
Tuple,
Enum8,
Enum16,
UUID,
IPv4,
IPv6,
Int128,
UInt128,
Decimal,
Decimal32,
Decimal64,
Decimal128,
LowCardinality,
DateTime64,
Date32,
Map,
Point,
Ring,
Polygon,
MultiPolygon
};
using EnumItem = std::pair<std::string /* name */, int16_t /* value */>;
protected:
Type(const Code code);
public:
template <typename Derived>
auto* As() {
return static_cast<Derived*>(this);
}
template <typename Derived>
const auto* As() const {
return static_cast<const Derived*>(this);
}
/// Type's code.
Code GetCode() const { return code_; }
/// String representation of the type.
std::string GetName() const;
/// Is given type same as current one.
bool IsEqual(const Type& other) const {
// Types are equal only if both code_ and type_unique_id_ are equal.
return this == &other
// GetTypeUniqueId() is relatively heavy, so avoid calling it when comparing obviously different types.
|| (this->GetCode() == other.GetCode() && this->GetTypeUniqueId() == other.GetTypeUniqueId());
}
bool IsEqual(const TypeRef& other) const { return IsEqual(*other); }
/// Simple name, doesn't depend on parameters and\or nested types, caller MUST NOT free returned value.
static const char* TypeName(Code);
public:
static TypeRef CreateArray(TypeRef item_type);
static TypeRef CreateDate();
static TypeRef CreateDate32();
static TypeRef CreateDateTime(std::string timezone = std::string());
static TypeRef CreateDateTime64(size_t precision, std::string timezone = std::string());
static TypeRef CreateDecimal(size_t precision, size_t scale);
static TypeRef CreateIPv4();
static TypeRef CreateIPv6();
static TypeRef CreateNothing();
static TypeRef CreateNullable(TypeRef nested_type);
template <typename T>
static TypeRef CreateSimple();
static TypeRef CreateString();
static TypeRef CreateString(size_t n);
static TypeRef CreateTuple(const std::vector<TypeRef>& item_types);
static TypeRef CreateEnum8(const std::vector<EnumItem>& enum_items);
static TypeRef CreateEnum16(const std::vector<EnumItem>& enum_items);
static TypeRef CreateUUID();
static TypeRef CreateLowCardinality(TypeRef item_type);
static TypeRef CreateMap(TypeRef key_type, TypeRef value_type);
static TypeRef CreatePoint();
static TypeRef CreateRing();
static TypeRef CreatePolygon();
static TypeRef CreateMultiPolygon();
private:
uint64_t GetTypeUniqueId() const;
const Code code_;
mutable std::atomic<uint64_t> type_unique_id_;
};
inline bool operator==(const Type & left, const Type & right) {
if (&left == &right)
return true;
if (typeid(left) == typeid(right))
return left.IsEqual(right);
return false;
}
inline bool operator==(const TypeRef & left, const TypeRef & right) {
return *left == *right;
}
class ArrayType : public Type {
public:
explicit ArrayType(TypeRef item_type);
std::string GetName() const { return std::string("Array(") + item_type_->GetName() + ")"; }
/// Type of array's elements.
inline TypeRef GetItemType() const { return item_type_; }
private:
TypeRef item_type_;
};
class DecimalType : public Type {
public:
DecimalType(size_t precision, size_t scale);
std::string GetName() const;
friend class EnumType;
friend class DateTimeType;
inline size_t GetScale() const { return scale_; }
inline size_t GetPrecision() const { return precision_; }
private:
const size_t precision_, scale_;
};
namespace details
{
class TypeWithTimeZoneMixin
{
public:
TypeWithTimeZoneMixin(std::string timezone);
/// Timezone associated with a data column.
const std::string & Timezone() const;
private:
std::string timezone_;
};
}
class DateTimeType : public Type, public details::TypeWithTimeZoneMixin {
public:
explicit DateTimeType(std::string timezone);
std::string GetName() const;
};
class DateTime64Type: public Type, public details::TypeWithTimeZoneMixin {
public:
explicit DateTime64Type(size_t precision, std::string timezone_);
std::string GetName() const;
inline size_t GetPrecision() const { return precision_; }
private:
size_t precision_;
};
class EnumType : public Type {
public:
EnumType(Type::Code type, const std::vector<EnumItem>& items);
std::string GetName() const;
/// Methods to work with enum types.
std::string_view GetEnumName(int16_t value) const;
int16_t GetEnumValue(const std::string& name) const;
bool HasEnumName(const std::string& name) const;
bool HasEnumValue(int16_t value) const;
private:
using ValueToNameType = std::map<int16_t, std::string_view>;
using NameToValueType = std::map<std::string, int16_t>;
using ValueToNameIterator = ValueToNameType::const_iterator;
ValueToNameType value_to_name_;
NameToValueType name_to_value_;
public:
ValueToNameIterator BeginValueToName() const;
ValueToNameIterator EndValueToName() const;
};
class FixedStringType : public Type {
public:
explicit FixedStringType(size_t n);
std::string GetName() const { return std::string("FixedString(") + std::to_string(size_) + ")"; }
inline size_t GetSize() const { return size_; }
private:
size_t size_;
};
class NullableType : public Type {
public:
explicit NullableType(TypeRef nested_type);
std::string GetName() const { return std::string("Nullable(") + nested_type_->GetName() + ")"; }
/// Type of nested nullable element.
TypeRef GetNestedType() const { return nested_type_; }
private:
TypeRef nested_type_;
};
class TupleType : public Type {
public:
explicit TupleType(const std::vector<TypeRef>& item_types);
std::string GetName() const;
/// Type of nested Tuple element type.
std::vector<TypeRef> GetTupleType() const { return item_types_; }
private:
std::vector<TypeRef> item_types_;
};
class LowCardinalityType : public Type {
public:
explicit LowCardinalityType(TypeRef nested_type);
~LowCardinalityType();
std::string GetName() const { return std::string("LowCardinality(") + nested_type_->GetName() + ")"; }
/// Type of nested nullable element.
TypeRef GetNestedType() const { return nested_type_; }
private:
TypeRef nested_type_;
};
class MapType : public Type {
public:
explicit MapType(TypeRef key_type, TypeRef value_type);
std::string GetName() const;
/// Type of keys.
TypeRef GetKeyType() const { return key_type_; }
/// Type of values.
TypeRef GetValueType() const { return value_type_; }
private:
TypeRef key_type_;
TypeRef value_type_;
};
template <>
inline TypeRef Type::CreateSimple<int8_t>() {
return TypeRef(new Type(Int8));
}
template <>
inline TypeRef Type::CreateSimple<int16_t>() {
return TypeRef(new Type(Int16));
}
template <>
inline TypeRef Type::CreateSimple<int32_t>() {
return TypeRef(new Type(Int32));
}
template <>
inline TypeRef Type::CreateSimple<int64_t>() {
return TypeRef(new Type(Int64));
}
template <>
inline TypeRef Type::CreateSimple<Int128>() {
return TypeRef(new Type(Int128));
}
template <>
inline TypeRef Type::CreateSimple<UInt128>() {
return TypeRef(new Type(UInt128));
}
template <>
inline TypeRef Type::CreateSimple<bool>() {
return TypeRef(new Type(Bool));
}
template <>
inline TypeRef Type::CreateSimple<uint8_t>() {
return TypeRef(new Type(UInt8));
}
template <>
inline TypeRef Type::CreateSimple<uint16_t>() {
return TypeRef(new Type(UInt16));
}
template <>
inline TypeRef Type::CreateSimple<uint32_t>() {
return TypeRef(new Type(UInt32));
}
template <>
inline TypeRef Type::CreateSimple<uint64_t>() {
return TypeRef(new Type(UInt64));
}
template <>
inline TypeRef Type::CreateSimple<float>() {
return TypeRef(new Type(Float32));
}
template <>
inline TypeRef Type::CreateSimple<double>() {
return TypeRef(new Type(Float64));
}
} // namespace clickhouse