forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSettingsFields.cpp
More file actions
654 lines (565 loc) · 19 KB
/
Copy pathSettingsFields.cpp
File metadata and controls
654 lines (565 loc) · 19 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#include <Core/ProtocolDefines.h>
#include <Columns/IColumn.h>
#include <Core/AccurateComparison.h>
#include <Core/Field.h>
#include <Core/SettingsFields.h>
#include <DataTypes/DataTypeMap.h>
#include <DataTypes/DataTypeString.h>
#include <IO/ReadBufferFromString.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h>
#include <Common/getNumberOfCPUCoresToUse.h>
#include <Common/logger_useful.h>
#include <boost/algorithm/string/predicate.hpp>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
#include <cctz/time_zone.h>
#pragma clang diagnostic pop
#include <cmath>
#include <limits>
namespace DB
{
namespace ErrorCodes
{
extern const int SIZE_OF_FIXED_STRING_DOESNT_MATCH;
extern const int CANNOT_PARSE_BOOL;
extern const int CANNOT_PARSE_NUMBER;
extern const int CANNOT_CONVERT_TYPE;
extern const int BAD_ARGUMENTS;
}
bool stringToBool(const String & str)
{
if (str == "0")
return false;
if (str == "1")
return true;
if (boost::iequals(str, "false"))
return false;
if (boost::iequals(str, "true"))
return true;
throw Exception(ErrorCodes::CANNOT_PARSE_BOOL, "Cannot parse bool from string '{}'", str);
}
namespace
{
template<typename T>
void validateFloatingPointSettingValue(T value)
{
if constexpr (std::is_floating_point_v<T>)
{
if (!std::isfinite(value))
throw Exception(ErrorCodes::CANNOT_PARSE_NUMBER,
"Float setting value must be finite, got {}", value);
}
}
template <typename T>
T stringToNumber(const String & str)
{
if constexpr (std::is_same_v<T, bool>)
{
return stringToBool(str);
}
else
{
T value = parseWithSizeSuffix<T>(str);
validateFloatingPointSettingValue(value);
return value;
}
}
template <typename T>
T fieldToNumber(const Field & f)
{
if (f.getType() == Field::Types::String)
{
return stringToNumber<T>(f.safeGet<String>());
}
if (f.getType() == Field::Types::UInt64)
{
T result;
if (!accurate::convertNumeric(f.safeGet<UInt64>(), result))
throw Exception(ErrorCodes::CANNOT_CONVERT_TYPE,
"Field value {} is out of range of {} type", f, demangle(typeid(T).name()));
validateFloatingPointSettingValue(result);
return result;
}
if (f.getType() == Field::Types::Int64)
{
T result;
if (!accurate::convertNumeric(f.safeGet<Int64>(), result))
throw Exception(ErrorCodes::CANNOT_CONVERT_TYPE,
"Field value {} is out of range of {} type", f, demangle(typeid(T).name()));
validateFloatingPointSettingValue(result);
return result;
}
if (f.getType() == Field::Types::Bool)
{
return T(f.safeGet<bool>());
}
if (f.getType() == Field::Types::Float64)
{
Float64 x = f.safeGet<Float64>();
validateFloatingPointSettingValue(x);
if constexpr (std::is_floating_point_v<T>)
{
return T(x);
}
else
{
if (!isFinite(x))
{
/// Conversion of infinite values to integer is undefined.
throw Exception(ErrorCodes::CANNOT_CONVERT_TYPE, "Cannot convert infinite value to integer type");
}
/// Use precision-correct float-vs-integer comparison via `accurate::greaterOp` / `accurate::lessOp`.
/// A naive `x > Float64(numeric_limits<T>::max())` is wrong for wide integer types like `UInt64`:
/// `Float64(numeric_limits<UInt64>::max())` rounds UP to `2^64`, so a `Float64` value equal to
/// that rounded-up boundary slips through the check and produces undefined behavior in the
/// subsequent `static_cast<T>(x)`. See issue #103817.
///
/// Bool is special-cased: `numeric_limits<bool>` is exactly representable in `Float64`, and
/// `accurate::lessOp` would fail to instantiate for `bool` (`make_unsigned_t<bool>` is ill-formed).
if constexpr (std::is_same_v<T, bool>)
{
if (x > Float64(std::numeric_limits<T>::max()) || x < Float64(std::numeric_limits<T>::lowest()))
throw Exception(ErrorCodes::CANNOT_CONVERT_TYPE, "Cannot convert out of range floating point value to integer type");
}
else if (accurate::greaterOp(x, std::numeric_limits<T>::max())
|| accurate::lessOp(x, std::numeric_limits<T>::lowest()))
{
throw Exception(ErrorCodes::CANNOT_CONVERT_TYPE, "Cannot convert out of range floating point value to integer type");
}
return T(x);
}
}
else
throw Exception(
ErrorCodes::CANNOT_CONVERT_TYPE, "Invalid value {} of the setting, which needs {}", f, demangle(typeid(T).name()));
}
Map stringToMap(const String & str)
{
/// Allow empty string as an empty map
if (str.empty())
return {};
auto type_string = std::make_shared<DataTypeString>();
DataTypeMap type_map(type_string, type_string);
auto serialization = type_map.getDefaultSerialization();
auto column = type_map.createColumn();
ReadBufferFromString buf(str);
serialization->deserializeTextEscaped(*column, buf, {});
return (*column)[0].safeGet<Map>();
}
[[maybe_unused]] Map fieldToMap(const Field & f)
{
if (f.getType() == Field::Types::String)
{
/// Allow to parse Map from string field. For the convenience.
const auto & str = f.safeGet<String>();
return stringToMap(str);
}
return f.safeGet<Map>();
}
}
template <typename T>
SettingFieldNumber<T>::SettingFieldNumber(Type x)
{
validateFloatingPointSettingValue(x);
value = x;
};
template <typename T>
SettingFieldNumber<T> & SettingFieldNumber<T>::operator=(Type x)
{
validateFloatingPointSettingValue(x);
value = x;
changed = true;
return *this;
}
template <typename T>
SettingFieldNumber<T>::SettingFieldNumber(const Field & f) : SettingFieldNumber(fieldToNumber<T>(f))
{
}
template <typename T>
SettingFieldNumber<T> & SettingFieldNumber<T>::operator=(const Field & f)
{
*this = fieldToNumber<T>(f);
return *this;
}
template <typename T>
String SettingFieldNumber<T>::toString() const
{
return ::DB::toString(value);
}
template <typename T>
void SettingFieldNumber<T>::parseFromString(const String & str)
{
*this = stringToNumber<T>(str);
}
template <typename T>
void SettingFieldNumber<T>::writeBinary(WriteBuffer & out) const
{
if constexpr (std::is_integral_v<T> && is_unsigned_v<T>)
writeVarUInt(static_cast<UInt64>(value), out);
else if constexpr (std::is_integral_v<T> && is_signed_v<T>)
writeVarInt(static_cast<Int64>(value), out);
else
{
static_assert(std::is_floating_point_v<T>);
writeStringBinary(::DB::toString(value), out);
}
}
template <typename T>
void SettingFieldNumber<T>::readBinary(ReadBuffer & in)
{
if constexpr (std::is_integral_v<T> && is_unsigned_v<T>)
{
UInt64 x = 0;
readVarUInt(x, in);
*this = static_cast<T>(x);
}
else if constexpr (std::is_integral_v<T> && is_signed_v<T>)
{
Int64 x = 0;
readVarInt(x, in);
*this = static_cast<T>(value);
}
else
{
static_assert(std::is_floating_point_v<T>);
String str;
readStringBinary(str, in);
*this = ::DB::parseFromString<T>(str);
}
}
template struct SettingFieldNumber<UInt64>;
template struct SettingFieldNumber<Int64>;
template struct SettingFieldNumber<float>;
template struct SettingFieldNumber<bool>;
template struct SettingFieldNumber<Int32>;
template struct SettingFieldNumber<UInt32>;
template struct SettingFieldNumber<double>;
template struct SettingAutoWrapper<SettingFieldNumber<UInt64>>;
template struct SettingAutoWrapper<SettingFieldNumber<Int64>>;
template struct SettingAutoWrapper<SettingFieldNumber<float>>;
template struct SettingAutoWrapper<SettingFieldNumber<UInt32>>;
template struct SettingAutoWrapper<SettingFieldNumber<Int32>>;
template struct SettingAutoWrapper<SettingFieldNumber<double>>;
namespace
{
UInt64 stringToMaxThreads(const String & str)
{
/// Accept both the clean `auto(N)` form and the legacy `'auto(N)'` form (quotes included in the
/// value). The latter is what older replicas send over the wire; keeping it parseable is what lets
/// `toString` emit the clean form without breaking mixed-version clusters. Do not remove it.
if (startsWith(str, "auto") || startsWith(str, "'auto"))
return 0;
return parseFromString<UInt64>(str);
}
UInt64 fieldToMaxThreads(const Field & f)
{
if (f.getType() == Field::Types::String)
return stringToMaxThreads(f.safeGet<String>());
return fieldToNumber<UInt64>(f);
}
}
SettingFieldMaxThreads::SettingFieldMaxThreads(const Field & f) : SettingFieldMaxThreads(fieldToMaxThreads(f))
{
}
SettingFieldMaxThreads & SettingFieldMaxThreads::operator=(const Field & f)
{
*this = fieldToMaxThreads(f);
return *this;
}
String SettingFieldMaxThreads::toString() const
{
if (is_auto)
/// The surrounding quotes are an unfortunate historical artifact: for a long time this returned the
/// string `'auto(N)'` (quotes included in the value itself), which leaks into `system.settings` and
/// looks like garbage. We emit the clean `auto(N)` form now. This is safe across versions because
/// `stringToMaxThreads` accepts both `auto(...)` and the legacy `'auto(...)'` form, so a server
/// receiving settings from an older replica still parses them, and every released version can parse
/// the unquoted form we send (see issue #68748 and the history below).
return "auto(" + ::DB::toString(value) + ")";
return ::DB::toString(value);
}
void SettingFieldMaxThreads::parseFromString(const String & str)
{
*this = stringToMaxThreads(str);
}
void SettingFieldMaxThreads::writeBinary(WriteBuffer & out) const
{
writeVarUInt(is_auto ? 0 : value, out);
}
void SettingFieldMaxThreads::readBinary(ReadBuffer & in)
{
UInt64 x = 0;
readVarUInt(x, in);
*this = x;
}
UInt64 SettingFieldMaxThreads::getAuto()
{
return getNumberOfCPUCoresToUse();
}
namespace
{
Int64 float64AsSecondsToTimespan(Float64 d)
{
if (d != 0.0 && !std::isnormal(d))
throw Exception(
ErrorCodes::CANNOT_PARSE_NUMBER, "A setting's value in seconds must be a normal floating point number or zero. Got {}", d);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wimplicit-const-int-float-conversion"
if (d * 1000000 > std::numeric_limits<Int64>::max() || d * 1000000 < std::numeric_limits<Int64>::min())
throw Exception(
ErrorCodes::BAD_ARGUMENTS, "Cannot convert seconds to microseconds: the setting's value in seconds is too big: {}", d);
#pragma clang diagnostic pop
return static_cast<Int64>(d * 1000000);
}
}
template <>
SettingFieldSeconds::SettingFieldTimespan(const Field & f)
: SettingFieldTimespan(Poco::Timespan{float64AsSecondsToTimespan(fieldToNumber<Float64>(f))})
{
}
template <>
SettingFieldMilliseconds::SettingFieldTimespan(const Field & f) : SettingFieldTimespan(fieldToNumber<UInt64>(f))
{
}
template <>
SettingFieldTimespan<SettingFieldTimespanUnit::Second> & SettingFieldSeconds::operator=(const Field & f)
{
*this = Poco::Timespan{float64AsSecondsToTimespan(fieldToNumber<Float64>(f))};
return *this;
}
template <>
SettingFieldTimespan<SettingFieldTimespanUnit::Millisecond> & SettingFieldMilliseconds::operator=(const Field & f)
{
*this = fieldToNumber<UInt64>(f);
return *this;
}
template <>
String SettingFieldSeconds::toString() const
{
return ::DB::toString(static_cast<Float64>(microseconds) / microseconds_per_unit);
}
template <>
String SettingFieldMilliseconds::toString() const
{
return ::DB::toString(operator UInt64());
}
template <>
SettingFieldSeconds::operator Field() const
{
return static_cast<Float64>(microseconds) / microseconds_per_unit;
}
template <>
SettingFieldMilliseconds::operator Field() const
{
return operator UInt64();
}
template <>
void SettingFieldSeconds::parseFromString(const String & str)
{
Float64 n = parse<Float64>(str.data(), str.size());
*this = Poco::Timespan{float64AsSecondsToTimespan(n)};
}
template <>
void SettingFieldMilliseconds::parseFromString(const String & str)
{
*this = stringToNumber<UInt64>(str);
}
template <SettingFieldTimespanUnit unit_>
Int64 SettingFieldTimespan<unit_>::microsecondsFromUnits(UInt64 units)
{
constexpr std::string_view unit_name = unit == SettingFieldTimespanUnit::Millisecond ? "milliseconds" : "seconds";
if (units > static_cast<UInt64>(std::numeric_limits<Int64>::max() / static_cast<Int64>(microseconds_per_unit)))
throw Exception(
ErrorCodes::BAD_ARGUMENTS,
"Cannot convert {} to microseconds: the setting's value in {} is too big: {}",
unit_name,
unit_name,
units);
return static_cast<Int64>(units * microseconds_per_unit);
}
template <SettingFieldTimespanUnit unit_>
void SettingFieldTimespan<unit_>::writeBinary(WriteBuffer & out) const
{
/// Note that this returns an UInt64 (for both seconds and milliseconds units) for compatibility reasons as the value
/// for seconds used to be a integer (now a Float64)
/// This method is only used to communicate with clients or servers older than DBMS_MIN_REVISION_WITH_SETTINGS_SERIALIZED_AS_STRINGS
/// in which the value was passed as binary (as a UInt64)
/// Later versions pass the setting values as String (using toString() and parseFromString()) and there passing "1.2" will
/// lead to `1` on releases with integer seconds or `1.2` on more recent releases
/// See https://github.com/ClickHouse/ClickHouse/issues/36940 for more details
auto num_units = operator UInt64();
writeVarUInt(num_units, out);
}
template <SettingFieldTimespanUnit unit_>
void SettingFieldTimespan<unit_>::readBinary(ReadBuffer & in)
{
UInt64 num_units = 0;
readVarUInt(num_units, in);
*this = num_units;
}
template struct SettingFieldTimespan<SettingFieldTimespanUnit::Second>;
template struct SettingFieldTimespan<SettingFieldTimespanUnit::Millisecond>;
void SettingFieldString::writeBinary(WriteBuffer & out) const
{
writeStringBinary(value, out);
}
void SettingFieldString::readBinary(ReadBuffer & in)
{
String str;
readStringBinary(str, in);
*this = std::move(str);
}
SettingFieldMap::SettingFieldMap(const Field & f) : value(fieldToMap(f)) {}
String SettingFieldMap::toString() const
{
auto type_string = std::make_shared<DataTypeString>();
DataTypeMap type_map(type_string, type_string);
auto serialization = type_map.getDefaultSerialization();
auto column = type_map.createColumn();
column->insert(value);
WriteBufferFromOwnString out;
serialization->serializeTextEscaped(*column, 0, out, {});
return out.str();
}
SettingFieldMap & SettingFieldMap::operator =(const Field & f)
{
*this = fieldToMap(f);
return *this;
}
void SettingFieldMap::parseFromString(const String & str)
{
*this = stringToMap(str);
}
void SettingFieldMap::writeBinary(WriteBuffer & out) const
{
DB::writeBinary(value, out);
}
void SettingFieldMap::readBinary(ReadBuffer & in)
{
Map map;
DB::readBinary(map, in);
*this = map;
}
namespace
{
char stringToChar(const String & str)
{
if (str.size() > 1)
throw Exception(ErrorCodes::SIZE_OF_FIXED_STRING_DOESNT_MATCH, "A setting's value string has to be an exactly one character long");
if (str.empty())
return '\0';
return str[0];
}
char fieldToChar(const Field & f)
{
return stringToChar(f.safeGet<String>());
}
}
SettingFieldChar::SettingFieldChar(const Field & f) : SettingFieldChar(fieldToChar(f))
{
}
SettingFieldChar & SettingFieldChar::operator =(const Field & f)
{
*this = fieldToChar(f);
return *this;
}
void SettingFieldChar::parseFromString(const String & str)
{
*this = stringToChar(str);
}
void SettingFieldChar::writeBinary(WriteBuffer & out) const
{
writeStringBinary(toString(), out);
}
void SettingFieldChar::readBinary(ReadBuffer & in)
{
String str;
readStringBinary(str, in);
*this = stringToChar(str);
}
void SettingFieldURI::writeBinary(WriteBuffer & out) const
{
writeStringBinary(value.toString(), out);
}
void SettingFieldURI::readBinary(ReadBuffer & in)
{
String str;
readStringBinary(str, in);
*this = Poco::URI{str};
}
void SettingFieldEnumHelpers::writeBinary(std::string_view str, WriteBuffer & out)
{
writeStringBinary(str, out);
}
String SettingFieldEnumHelpers::readBinary(ReadBuffer & in)
{
String str;
readStringBinary(str, in);
return str;
}
void SettingFieldTimezone::writeBinary(WriteBuffer & out) const
{
writeStringBinary(value, out);
}
void SettingFieldTimezone::readBinary(ReadBuffer & in)
{
String str;
readStringBinary(str, in);
*this = std::move(str);
}
void SettingFieldTimezone::validateTimezone(const std::string & tz_str)
{
cctz::time_zone validated_tz;
if (!tz_str.empty() && !cctz::load_time_zone(tz_str, &validated_tz))
throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Invalid time zone: {}", tz_str);
}
String SettingFieldCustom::toString() const
{
return value.dump();
}
void SettingFieldCustom::parseFromString(const String & str)
{
*this = Field::restoreFromDump(str);
}
void SettingFieldCustom::writeBinary(WriteBuffer & out) const
{
writeStringBinary(toString(), out);
}
void SettingFieldCustom::readBinary(ReadBuffer & in)
{
String str;
readStringBinary(str, in);
parseFromString(str);
}
SettingFieldNonZeroUInt64::SettingFieldNonZeroUInt64(UInt64 x) : SettingFieldUInt64(x)
{
checkValueNonZero();
}
SettingFieldNonZeroUInt64::SettingFieldNonZeroUInt64(const DB::Field & f) : SettingFieldUInt64(f)
{
checkValueNonZero();
}
SettingFieldNonZeroUInt64 & SettingFieldNonZeroUInt64::operator=(UInt64 x)
{
SettingFieldUInt64::operator=(x);
checkValueNonZero();
return *this;
}
SettingFieldNonZeroUInt64 & SettingFieldNonZeroUInt64::operator=(const DB::Field & f)
{
SettingFieldUInt64::operator=(f);
checkValueNonZero();
return *this;
}
void SettingFieldNonZeroUInt64::parseFromString(const String & str)
{
SettingFieldUInt64::parseFromString(str);
checkValueNonZero();
}
void SettingFieldNonZeroUInt64::checkValueNonZero() const
{
if (value == 0)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "A setting's value has to be greater than 0");
}
}