|
1 | 1 | #include <Core/SettingsFields.h> |
2 | | - |
3 | 2 | #include <Core/Field.h> |
| 3 | +#include <Core/AccurateComparison.h> |
4 | 4 | #include <Common/getNumberOfPhysicalCPUCores.h> |
5 | | -#include <Common/FieldVisitorConvertToNumber.h> |
6 | 5 | #include <Common/logger_useful.h> |
7 | 6 | #include <DataTypes/DataTypeMap.h> |
8 | 7 | #include <DataTypes/DataTypeString.h> |
|
13 | 12 |
|
14 | 13 | #include <cmath> |
15 | 14 |
|
| 15 | + |
16 | 16 | namespace DB |
17 | 17 | { |
18 | 18 | namespace ErrorCodes |
19 | 19 | { |
20 | 20 | extern const int SIZE_OF_FIXED_STRING_DOESNT_MATCH; |
21 | 21 | extern const int CANNOT_PARSE_BOOL; |
22 | 22 | extern const int CANNOT_PARSE_NUMBER; |
| 23 | + extern const int CANNOT_CONVERT_TYPE; |
23 | 24 | } |
24 | 25 |
|
25 | 26 |
|
@@ -48,9 +49,51 @@ namespace |
48 | 49 | T fieldToNumber(const Field & f) |
49 | 50 | { |
50 | 51 | if (f.getType() == Field::Types::String) |
| 52 | + { |
51 | 53 | return stringToNumber<T>(f.get<const String &>()); |
| 54 | + } |
| 55 | + else if (f.getType() == Field::Types::UInt64) |
| 56 | + { |
| 57 | + T result; |
| 58 | + if (!accurate::convertNumeric(f.get<UInt64>(), result)) |
| 59 | + throw Exception(ErrorCodes::CANNOT_CONVERT_TYPE, "Field value {} is out of range of {} type", f, demangle(typeid(T).name())); |
| 60 | + return result; |
| 61 | + } |
| 62 | + else if (f.getType() == Field::Types::Int64) |
| 63 | + { |
| 64 | + T result; |
| 65 | + if (!accurate::convertNumeric(f.get<Int64>(), result)) |
| 66 | + throw Exception(ErrorCodes::CANNOT_CONVERT_TYPE, "Field value {} is out of range of {} type", f, demangle(typeid(T).name())); |
| 67 | + return result; |
| 68 | + } |
| 69 | + else if (f.getType() == Field::Types::Bool) |
| 70 | + { |
| 71 | + return T(f.get<bool>()); |
| 72 | + } |
| 73 | + else if (f.getType() == Field::Types::Float64) |
| 74 | + { |
| 75 | + Float64 x = f.get<Float64>(); |
| 76 | + if constexpr (std::is_floating_point_v<T>) |
| 77 | + { |
| 78 | + return T(x); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + if (!isFinite(x)) |
| 83 | + { |
| 84 | + /// Conversion of infinite values to integer is undefined. |
| 85 | + throw Exception(ErrorCodes::CANNOT_CONVERT_TYPE, "Cannot convert infinite value to integer type"); |
| 86 | + } |
| 87 | + else if (x > Float64(std::numeric_limits<T>::max()) || x < Float64(std::numeric_limits<T>::lowest())) |
| 88 | + { |
| 89 | + throw Exception(ErrorCodes::CANNOT_CONVERT_TYPE, "Cannot convert out of range floating point value to integer type"); |
| 90 | + } |
| 91 | + else |
| 92 | + return T(x); |
| 93 | + } |
| 94 | + } |
52 | 95 | else |
53 | | - return applyVisitor(FieldVisitorConvertToNumber<T>(), f); |
| 96 | + throw Exception(ErrorCodes::CANNOT_CONVERT_TYPE, "Invalid value {} of the setting, which needs {}", f, demangle(typeid(T).name())); |
54 | 97 | } |
55 | 98 |
|
56 | 99 | Map stringToMap(const String & str) |
@@ -174,7 +217,7 @@ namespace |
174 | 217 | if (f.getType() == Field::Types::String) |
175 | 218 | return stringToMaxThreads(f.get<const String &>()); |
176 | 219 | else |
177 | | - return applyVisitor(FieldVisitorConvertToNumber<UInt64>(), f); |
| 220 | + return fieldToNumber<UInt64>(f); |
178 | 221 | } |
179 | 222 | } |
180 | 223 |
|
|
0 commit comments