-
Notifications
You must be signed in to change notification settings - Fork 9k
Expand file tree
/
Copy pathDataTypesCache.cpp
More file actions
196 lines (161 loc) · 7.17 KB
/
Copy pathDataTypesCache.cpp
File metadata and controls
196 lines (161 loc) · 7.17 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
#include <DataTypes/DataTypesCache.h>
#include <DataTypes/DataTypeFactory.h>
#include <Core/Settings.h>
#include <Interpreters/Context.h>
#include <Common/CurrentThread.h>
namespace DB
{
namespace Setting
{
extern const SettingsTimezone session_timezone;
}
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;
}
SerializationPtr DataTypesCache::getSerialization(const String & type_name, const DataTypePtr & type)
{
/// Check the thread-local cache of simple types first.
if (const auto * elem = getSimpleDataTypesCache().findByName(type_name))
return elem->serialization;
return getCacheElement(type_name, &type).serialization;
}
void DataTypesCache::clearIfQueryContextChanged()
{
ContextPtr current_query_context = CurrentThread::tryGetQueryContext();
/// Owner-based identity comparison. It does not lock the weak_ptr, and since we hold
/// the weak_ptr (keeping the control block alive), an expired context cannot be
/// confused with a new one allocated at the same address.
bool same_query_context = !query_context.owner_before(current_query_context) && !current_query_context.owner_before(query_context);
/// Context identity is not enough: clickhouse-client keeps one long-lived client context
/// (attached to the client thread by a single query scope) for the whole session and
/// mutates `session_timezone` in place between queries (see `ClientBase::onTimezoneUpdate`),
/// so also track the value of the setting the cached types may have captured.
const String * current_session_timezone
= current_query_context ? ¤t_query_context->getSettingsRef()[Setting::session_timezone].value : nullptr;
bool same_session_timezone
= current_session_timezone ? *current_session_timezone == session_timezone : session_timezone.empty();
if (same_query_context && same_session_timezone)
return;
/// The thread is now serving a different query, or `session_timezone` changed in place
/// on the same context. Cached types/serializations may depend on the previous query's
/// context (e.g. `session_timezone` captured by DateTime types), so they must not be reused.
cache.clear();
query_context = current_query_context;
session_timezone = current_session_timezone ? *current_session_timezone : String();
}
const DataTypesCache::Element & DataTypesCache::getCacheElement(const String & type_name, const DataTypePtr * known_type)
{
clearIfQueryContextChanged();
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 = known_type ? *known_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;
}
}