forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStatisticsDescription.cpp
More file actions
253 lines (205 loc) · 8.18 KB
/
Copy pathStatisticsDescription.cpp
File metadata and controls
253 lines (205 loc) · 8.18 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
#include <Storages/StatisticsDescription.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTStatisticsDeclaration.h>
#include <Parsers/ParserCreateQuery.h>
#include <Storages/ColumnsDescription.h>
namespace DB
{
namespace ErrorCodes
{
extern const int INCORRECT_QUERY;
extern const int ILLEGAL_STATISTICS;
extern const int LOGICAL_ERROR;
};
SingleStatisticsDescription & SingleStatisticsDescription::operator=(const SingleStatisticsDescription & other)
{
if (this == &other)
return *this;
type = other.type;
ast = other.ast ? other.ast->clone() : nullptr;
is_implicit = other.is_implicit;
return *this;
}
SingleStatisticsDescription & SingleStatisticsDescription::operator=(SingleStatisticsDescription && other) noexcept
{
if (this == &other)
return *this;
type = std::exchange(other.type, StatisticsType{});
ast = other.ast ? other.ast->clone() : nullptr;
is_implicit = other.is_implicit;
other.ast.reset();
return *this;
}
StatisticsType stringToStatisticsType(String type)
{
type = Poco::toLower(type);
if (type == "tdigest")
return StatisticsType::TDigest;
if (type == "uniq")
return StatisticsType::Uniq;
if (type == "countmin")
return StatisticsType::CountMinSketch;
if (type == "minmax")
return StatisticsType::MinMax;
if (type == "basic")
return StatisticsType::Basic;
if (type == "uniq_v2")
return StatisticsType::UniqV2;
throw Exception(ErrorCodes::INCORRECT_QUERY, "Unknown statistics type: {}. Supported statistics types are 'basic', 'countmin', 'minmax', 'tdigest', 'uniq' and 'uniq_v2'", type);
}
String statisticsTypeToString(StatisticsType type)
{
switch (type)
{
case StatisticsType::TDigest:
return "TDigest";
case StatisticsType::Uniq:
return "Uniq";
case StatisticsType::CountMinSketch:
return "countmin";
case StatisticsType::MinMax:
return "minmax";
case StatisticsType::Basic:
return "basic";
case StatisticsType::UniqV2:
return "uniq_v2";
default:
throw Exception(ErrorCodes::LOGICAL_ERROR, "Unknown statistics type: {}. Supported statistics types are 'basic', 'countmin', 'minmax', 'tdigest', 'uniq' and 'uniq_v2'", type);
}
}
String SingleStatisticsDescription::getTypeName() const
{
return statisticsTypeToString(type);
}
SingleStatisticsDescription::SingleStatisticsDescription(StatisticsType type_, ASTPtr ast_, bool is_implicit_)
: type(type_), ast(ast_), is_implicit(is_implicit_)
{}
bool SingleStatisticsDescription::operator==(const SingleStatisticsDescription & other) const
{
return type == other.type && is_implicit == other.is_implicit;
}
bool ColumnStatisticsDescription::operator==(const ColumnStatisticsDescription & other) const
{
if (!data_type)
return !other.data_type;
if (!other.data_type)
return false;
return types_to_desc == other.types_to_desc && data_type->equals(*other.data_type);
}
bool ColumnStatisticsDescription::empty() const
{
return types_to_desc.empty();
}
bool ColumnStatisticsDescription::hasExplicitStatistics() const
{
return std::any_of(types_to_desc.begin(), types_to_desc.end(), [](const auto & desc) { return !desc.second.is_implicit; });
}
bool ColumnStatisticsDescription::contains(const String & stat_type) const
{
return types_to_desc.contains(stringToStatisticsType(stat_type));
}
void ColumnStatisticsDescription::merge(const ColumnStatisticsDescription & other, const String & merging_column_name, DataTypePtr merging_column_type, bool if_not_exists)
{
chassert(merging_column_type);
data_type = merging_column_type;
for (const auto & [stats_type, stats_desc]: other.types_to_desc)
{
if (!if_not_exists && types_to_desc.contains(stats_type))
{
throw Exception(ErrorCodes::ILLEGAL_STATISTICS, "Statistics type name {} has existed in column {}", stats_type, merging_column_name);
}
if (!types_to_desc.contains(stats_type))
types_to_desc.emplace(stats_type, stats_desc);
}
}
void ColumnStatisticsDescription::assign(const ColumnStatisticsDescription & other)
{
types_to_desc = other.types_to_desc;
data_type = other.data_type;
}
void ColumnStatisticsDescription::clear()
{
types_to_desc.clear();
}
std::vector<std::pair<String, ColumnStatisticsDescription>> ColumnStatisticsDescription::fromAST(const ASTPtr & definition_ast, const ColumnsDescription & columns)
{
const auto * stat_definition_ast = definition_ast->as<ASTStatisticsDeclaration>();
if (!stat_definition_ast)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot cast AST to ASTSingleStatisticsDeclaration");
StatisticsTypeDescMap statistics_types;
for (const auto & stat_ast : stat_definition_ast->types->children)
{
String stat_type_name = stat_ast->as<ASTFunction &>().name;
auto stat_type = stringToStatisticsType(Poco::toLower(stat_type_name));
if (statistics_types.contains(stat_type))
throw Exception(ErrorCodes::INCORRECT_QUERY, "Statistics type {} was specified more than once", stat_type_name);
SingleStatisticsDescription stat(stat_type, stat_ast->clone(), false);
statistics_types.emplace(stat.type, std::move(stat));
}
std::vector<std::pair<String, ColumnStatisticsDescription>> result;
result.reserve(stat_definition_ast->columns->children.size());
for (const auto & column_ast : stat_definition_ast->columns->children)
{
ColumnStatisticsDescription stats;
String physical_column_name = column_ast->as<ASTIdentifier &>().name();
if (!columns.hasPhysical(physical_column_name))
throw Exception(ErrorCodes::INCORRECT_QUERY, "Incorrect column name {}", physical_column_name);
const auto & column = columns.getPhysical(physical_column_name);
stats.data_type = column.type;
stats.types_to_desc = statistics_types;
result.emplace_back(physical_column_name, stats);
}
if (result.empty())
throw Exception(ErrorCodes::INCORRECT_QUERY, "Empty statistics column list is not allowed.");
return result;
}
ColumnStatisticsDescription ColumnStatisticsDescription::fromStatisticsDescriptionAST(const ASTPtr & statistics_desc, const String & column_name, DataTypePtr data_type)
{
const auto & stat_type_list_ast = statistics_desc->as<ASTFunction &>().arguments;
if (stat_type_list_ast->children.empty())
throw Exception(ErrorCodes::INCORRECT_QUERY, "We expect at least one statistics type for column {}", column_name);
ColumnStatisticsDescription stats;
for (const auto & ast : stat_type_list_ast->children)
{
const auto & stat_type = ast->as<const ASTFunction &>().name;
SingleStatisticsDescription stat(stringToStatisticsType(Poco::toLower(stat_type)), ast->clone(), false);
if (stats.types_to_desc.contains(stat.type))
throw Exception(ErrorCodes::INCORRECT_QUERY, "Column {} already contains statistics type {}", column_name, stat_type);
stats.types_to_desc.emplace(stat.type, std::move(stat));
}
stats.data_type = data_type;
return stats;
}
ASTPtr ColumnStatisticsDescription::getAST() const
{
auto function_node = make_intrusive<ASTFunction>();
function_node->name = "STATISTICS";
function_node->setKind(ASTFunction::Kind::STATISTICS);
function_node->arguments = make_intrusive<ASTExpressionList>();
for (const auto & [type, desc] : types_to_desc)
{
if (desc.ast == nullptr)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Unknown ast");
if (!desc.is_implicit)
function_node->arguments->children.push_back(desc.ast);
}
function_node->children.push_back(function_node->arguments);
return function_node;
}
String ColumnStatisticsDescription::getNameForLogs() const
{
String ret;
for (const auto & [tp, desc] : types_to_desc)
{
ret += desc.getTypeName();
if (desc.is_implicit)
ret += "(auto)";
ret += ",";
}
if (!ret.empty())
ret.pop_back();
return ret;
}
}