-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathtypes.h
325 lines (243 loc) · 7.12 KB
/
types.h
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
#pragma once
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <stdexcept>
namespace absl
{
class int128;
}
namespace clickhouse {
using Int128 = absl::int128;
using Int64 = int64_t;
using TypeRef = std::shared_ptr<class Type>;
class Type {
public:
enum Code {
Void = 0,
Int8,
Int16,
Int32,
Int64,
UInt8,
UInt16,
UInt32,
UInt64,
Float32,
Float64,
String,
FixedString,
DateTime,
Date,
Array,
Nullable,
Tuple,
Enum8,
Enum16,
UUID,
IPv4,
IPv6,
Int128,
Decimal,
Decimal32,
Decimal64,
Decimal128,
LowCardinality,
DateTime64,
};
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 { return this->GetName() == other.GetName(); }
bool IsEqual(const TypeRef& other) const { return IsEqual(*other); }
public:
static TypeRef CreateArray(TypeRef item_type);
static TypeRef CreateDate();
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);
private:
const Code code_;
};
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.
const std::string& 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>;
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_) + ")"; }
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_;
};
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<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