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
|
#include <IO/ReadHelpers.h>
#include <Access/IAccessStorage.h>
#include <Parsers/ASTIdentifier_fwd.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/Access/ASTCreateQuotaQuery.h>
#include <Parsers/Access/ASTRolesOrUsersSet.h>
#include <Parsers/Access/ParserCreateQuotaQuery.h>
#include <Parsers/Access/ParserRolesOrUsersSet.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/ExpressionListParsers.h>
#include <Parsers/parseIdentifierOrStringLiteral.h>
#include <Parsers/parseIntervalKind.h>
#include <base/range.h>
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <Common/FieldVisitorConvertToNumber.h>
namespace DB
{
namespace ErrorCodes
{
extern const int SYNTAX_ERROR;
}
namespace
{
bool parseRenameTo(IParserBase::Pos & pos, Expected & expected, String & new_name)
{
return IParserBase::wrapParseImpl(pos, [&]
{
if (!ParserKeyword{"RENAME TO"}.ignore(pos, expected))
return false;
return parseIdentifierOrStringLiteral(pos, expected, new_name);
});
}
bool parseKeyType(IParserBase::Pos & pos, Expected & expected, QuotaKeyType & key_type)
{
return IParserBase::wrapParseImpl(pos, [&]
{
if (ParserKeyword{"NOT KEYED"}.ignore(pos, expected))
{
key_type = QuotaKeyType::NONE;
return true;
}
if (!ParserKeyword{"KEY BY"}.ignore(pos, expected) && !ParserKeyword{"KEYED BY"}.ignore(pos, expected))
return false;
Strings names;
if (!parseIdentifiersOrStringLiterals(pos, expected, names))
return false;
String name = boost::algorithm::join(names, "_or_");
boost::to_lower(name);
boost::replace_all(name, " ", "_");
for (auto kt : collections::range(QuotaKeyType::MAX))
{
if (QuotaKeyTypeInfo::get(kt).name == name)
{
key_type = kt;
return true;
}
}
String all_types_str;
for (auto kt : collections::range(QuotaKeyType::MAX))
all_types_str += String(all_types_str.empty() ? "" : ", ") + "'" + QuotaKeyTypeInfo::get(kt).name + "'";
throw Exception(ErrorCodes::SYNTAX_ERROR, "Quota cannot be keyed by '{}'. Expected one of the following identifiers: {}", name, all_types_str);
});
}
bool parseQuotaType(IParserBase::Pos & pos, Expected & expected, QuotaType & quota_type)
{
return IParserBase::wrapParseImpl(pos, [&]
{
for (auto qt : collections::range(QuotaType::MAX))
{
if (ParserKeyword{QuotaTypeInfo::get(qt).keyword}.ignore(pos, expected))
{
quota_type = qt;
return true;
}
}
ASTPtr ast;
if (!ParserIdentifier{}.parse(pos, ast, expected))
return false;
String name = getIdentifierName(ast);
for (auto qt : collections::range(QuotaType::MAX))
{
if (QuotaTypeInfo::get(qt).name == name)
{
quota_type = qt;
return true;
}
}
return false;
});
}
template <typename T>
requires std::same_as<T, double> || std::same_as<T, QuotaValue>
T fieldToNumber(const Field & f)
{
if (f.getType() == Field::Types::String)
return parseWithSizeSuffix<QuotaValue>(boost::algorithm::trim_copy(f.get<std::string>()));
else
return applyVisitor(FieldVisitorConvertToNumber<T>(), f);
}
bool parseMaxValue(IParserBase::Pos & pos, Expected & expected, QuotaType quota_type, QuotaValue & max_value)
{
ASTPtr ast;
if (!ParserNumber{}.parse(pos, ast, expected) && !ParserStringLiteral{}.parse(pos, ast, expected))
return false;
const Field & max_field = ast->as<ASTLiteral &>().value;
const auto & type_info = QuotaTypeInfo::get(quota_type);
if (type_info.output_denominator == 1)
max_value = fieldToNumber<QuotaValue>(max_field);
else
max_value = static_cast<QuotaValue>(fieldToNumber<double>(max_field) * type_info.output_denominator);
return true;
}
bool parseLimits(IParserBase::Pos & pos, Expected & expected, std::vector<std::pair<QuotaType, QuotaValue>> & limits)
{
std::vector<std::pair<QuotaType, QuotaValue>> res_limits;
bool max_prefix_encountered = false;
auto parse_limit = [&]
{
max_prefix_encountered |= ParserKeyword{"MAX"}.ignore(pos, expected);
QuotaType quota_type;
if (!parseQuotaType(pos, expected, quota_type))
return false;
if (max_prefix_encountered)
{
ParserToken{TokenType::Equals}.ignore(pos, expected);
}
else
{
if (!ParserKeyword{"MAX"}.ignore(pos, expected))
return false;
}
QuotaValue max_value;
if (!parseMaxValue(pos, expected, quota_type, max_value))
return false;
res_limits.emplace_back(quota_type, max_value);
return true;
};
if (!ParserList::parseUtil(pos, expected, parse_limit, false))
return false;
limits = std::move(res_limits);
return true;
}
bool parseIntervalsWithLimits(IParserBase::Pos & pos, Expected & expected, std::vector<ASTCreateQuotaQuery::Limits> & all_limits)
{
std::vector<ASTCreateQuotaQuery::Limits> res_all_limits;
auto parse_interval_with_limits = [&]
{
if (!ParserKeyword{"FOR"}.ignore(pos, expected))
return false;
ASTCreateQuotaQuery::Limits limits;
limits.randomize_interval = ParserKeyword{"RANDOMIZED"}.ignore(pos, expected);
ParserKeyword{"INTERVAL"}.ignore(pos, expected);
ASTPtr num_intervals_ast;
if (!ParserNumber{}.parse(pos, num_intervals_ast, expected))
return false;
double num_intervals = applyVisitor(FieldVisitorConvertToNumber<double>(), num_intervals_ast->as<ASTLiteral &>().value);
IntervalKind interval_kind;
if (!parseIntervalKind(pos, expected, interval_kind))
return false;
limits.duration = std::chrono::seconds(static_cast<UInt64>(num_intervals * interval_kind.toAvgSeconds()));
std::vector<std::pair<QuotaType, QuotaValue>> new_limits;
if (ParserKeyword{"NO LIMITS"}.ignore(pos, expected))
{
limits.drop = true;
}
else if (ParserKeyword{"TRACKING ONLY"}.ignore(pos, expected))
{
}
else if (parseLimits(pos, expected, new_limits))
{
for (const auto & [quota_type, max_value] : new_limits)
limits.max[static_cast<size_t>(quota_type)] = max_value;
}
else
return false;
res_all_limits.emplace_back(std::move(limits));
return true;
};
if (!ParserList::parseUtil(pos, expected, parse_interval_with_limits, false))
return false;
all_limits = std::move(res_all_limits);
return true;
}
bool parseToRoles(IParserBase::Pos & pos, Expected & expected, bool id_mode, std::shared_ptr<ASTRolesOrUsersSet> & roles)
{
return IParserBase::wrapParseImpl(pos, [&]
{
ASTPtr node;
ParserRolesOrUsersSet roles_p;
roles_p.allowAll().allowRoles().allowUsers().allowCurrentUser().useIDMode(id_mode);
if (!ParserKeyword{"TO"}.ignore(pos, expected) || !roles_p.parse(pos, node, expected))
return false;
roles = std::static_pointer_cast<ASTRolesOrUsersSet>(node);
return true;
});
}
bool parseOnCluster(IParserBase::Pos & pos, Expected & expected, String & cluster)
{
return IParserBase::wrapParseImpl(pos, [&]
{
return ParserKeyword{"ON"}.ignore(pos, expected) && ASTQueryWithOnCluster::parse(pos, cluster, expected);
});
}
}
bool ParserCreateQuotaQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
bool alter = false;
if (attach_mode)
{
if (!ParserKeyword{"ATTACH QUOTA"}.ignore(pos, expected))
return false;
}
else
{
if (ParserKeyword{"ALTER QUOTA"}.ignore(pos, expected))
alter = true;
else if (!ParserKeyword{"CREATE QUOTA"}.ignore(pos, expected))
return false;
}
bool if_exists = false;
bool if_not_exists = false;
bool or_replace = false;
if (alter)
{
if (ParserKeyword{"IF EXISTS"}.ignore(pos, expected))
if_exists = true;
}
else
{
if (ParserKeyword{"IF NOT EXISTS"}.ignore(pos, expected))
if_not_exists = true;
else if (ParserKeyword{"OR REPLACE"}.ignore(pos, expected))
or_replace = true;
}
Strings names;
if (!parseIdentifiersOrStringLiterals(pos, expected, names))
return false;
String new_name;
std::optional<QuotaKeyType> key_type;
std::vector<ASTCreateQuotaQuery::Limits> all_limits;
String cluster;
String storage_name;
while (true)
{
if (alter && new_name.empty() && (names.size() == 1) && parseRenameTo(pos, expected, new_name))
continue;
if (!key_type)
{
QuotaKeyType new_key_type;
if (parseKeyType(pos, expected, new_key_type))
{
key_type = new_key_type;
continue;
}
}
if (parseIntervalsWithLimits(pos, expected, all_limits))
continue;
if (cluster.empty() && parseOnCluster(pos, expected, cluster))
continue;
if (storage_name.empty() && ParserKeyword{"IN"}.ignore(pos, expected) && parseAccessStorageName(pos, expected, storage_name))
continue;
break;
}
std::shared_ptr<ASTRolesOrUsersSet> roles;
parseToRoles(pos, expected, attach_mode, roles);
if (cluster.empty())
parseOnCluster(pos, expected, cluster);
auto query = std::make_shared<ASTCreateQuotaQuery>();
node = query;
query->alter = alter;
query->if_exists = if_exists;
query->if_not_exists = if_not_exists;
query->or_replace = or_replace;
query->cluster = std::move(cluster);
query->names = std::move(names);
query->new_name = std::move(new_name);
query->key_type = key_type;
query->all_limits = std::move(all_limits);
query->roles = std::move(roles);
query->storage_name = std::move(storage_name);
return true;
}
}
|