-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Expand file tree
/
Copy pathDataTypesCache.cpp
More file actions
148 lines (122 loc) · 4.94 KB
/
Copy pathDataTypesCache.cpp
File metadata and controls
148 lines (122 loc) · 4.94 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
#include <DataTypes/DataTypesCache.h>
#include <DataTypes/DataTypeFactory.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
SimpleDataTypesCache::SimpleDataTypesCache()
{
addSimpleType(BinaryTypeIndex::Nothing, "Nothing");
addSimpleType(BinaryTypeIndex::UInt8, "UInt8");
addSimpleType(BinaryTypeIndex::UInt16, "UInt16");
addSimpleType(BinaryTypeIndex::UInt32, "UInt32");
addSimpleType(BinaryTypeIndex::UInt64, "UInt64");
addSimpleType(BinaryTypeIndex::UInt128, "UInt128");
addSimpleType(BinaryTypeIndex::UInt256, "UInt256");
addSimpleType(BinaryTypeIndex::Int8, "Int8");
addSimpleType(BinaryTypeIndex::Int16, "Int16");
addSimpleType(BinaryTypeIndex::Int32, "Int32");
addSimpleType(BinaryTypeIndex::Int64, "Int64");
addSimpleType(BinaryTypeIndex::Int128, "Int128");
addSimpleType(BinaryTypeIndex::Int256, "Int256");
addSimpleType(BinaryTypeIndex::BFloat16, "BFloat16");
addSimpleType(BinaryTypeIndex::Float32, "Float32");
addSimpleType(BinaryTypeIndex::Float64, "Float64");
addSimpleType(BinaryTypeIndex::Date, "Date");
addSimpleType(BinaryTypeIndex::Date32, "Date32");
addSimpleType(BinaryTypeIndex::String, "String");
addSimpleType(BinaryTypeIndex::UUID, "UUID");
addSimpleType(BinaryTypeIndex::IPv4, "IPv4");
addSimpleType(BinaryTypeIndex::IPv6, "IPv6");
addSimpleType(BinaryTypeIndex::Bool, "Bool");
}
void SimpleDataTypesCache::addSimpleType(BinaryTypeIndex index, const String & type_name)
{
auto type = DataTypeFactory::instance().get(type_name);
Element element{type_name, type, type->getDefaultSerialization()};
by_index[static_cast<uint8_t>(index)] = element;
by_name.emplace(type_name, std::move(element));
}
bool SimpleDataTypesCache::hasElement(BinaryTypeIndex index) const
{
uint8_t index_value = static_cast<uint8_t>(index);
return index_value < by_index.size() && by_index[index_value].type != nullptr;
}
const SimpleDataTypesCache::Element & SimpleDataTypesCache::getElement(BinaryTypeIndex index) const
{
uint8_t index_value = static_cast<uint8_t>(index);
if (index_value >= by_index.size() || by_index[index_value].type == nullptr)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Invalid binary type index: {}", index);
return by_index[index_value];
}
DataTypePtr SimpleDataTypesCache::getType(BinaryTypeIndex index) const
{
uint8_t index_value = static_cast<uint8_t>(index);
if (index_value >= by_index.size() || by_index[index_value].type == nullptr)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Invalid binary type index: {}", index);
return by_index[index_value].type;
}
SerializationPtr SimpleDataTypesCache::getSerialization(BinaryTypeIndex index) const
{
uint8_t index_value = static_cast<uint8_t>(index);
if (index_value >= by_index.size() || by_index[index_value].serialization == nullptr)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Invalid binary type index: {}", index);
return by_index[index_value].serialization;
}
const SimpleDataTypesCache::Element * SimpleDataTypesCache::findByName(const String & type_name) const
{
auto it = by_name.find(type_name);
return it != by_name.end() ? &it->second : nullptr;
}
DataTypePtr SimpleDataTypesCache::getType(const String & type_name) const
{
if (const auto * elem = findByName(type_name))
return elem->type;
return DataTypeFactory::instance().get(type_name);
}
SerializationPtr SimpleDataTypesCache::getSerialization(const String & type_name) const
{
if (const auto * elem = findByName(type_name))
return elem->serialization;
auto type = DataTypeFactory::instance().get(type_name);
return type->getDefaultSerialization();
}
const SimpleDataTypesCache & getSimpleDataTypesCache()
{
thread_local SimpleDataTypesCache cache;
return cache;
}
DataTypePtr DataTypesCache::getType(const String & type_name)
{
/// Check the thread-local cache of simple types first.
if (const auto * elem = getSimpleDataTypesCache().findByName(type_name))
return elem->type;
return getCacheElement(type_name).type;
}
SerializationPtr DataTypesCache::getSerialization(const String & type_name)
{
/// Check the thread-local cache of simple types first.
if (const auto * elem = getSimpleDataTypesCache().findByName(type_name))
return elem->serialization;
return getCacheElement(type_name).serialization;
}
const DataTypesCache::Element & DataTypesCache::getCacheElement(const String & type_name)
{
auto it = cache.find(type_name);
if (it != cache.end())
return it->second;
/// If cache is full, just clear it.
if (cache.size() >= MAX_ELEMENTS)
cache.clear();
auto type = DataTypeFactory::instance().get(type_name);
it = cache.emplace(type_name, Element{type, type->getDefaultSerialization()}).first;
return it->second;
}
DataTypesCache & getDataTypesCache()
{
thread_local static DataTypesCache data_types_cache;
return data_types_cache;
}
}