|
11 | 11 | #include "jaro_winkler.hpp" |
12 | 12 | #include "utf8proc_wrapper.hpp" |
13 | 13 | #include "duckdb/common/types/string_type.hpp" |
| 14 | +#include "duckdb/common/operator/cast_operators.hpp" |
14 | 15 |
|
15 | 16 | #include <algorithm> |
16 | 17 | #include <cctype> |
@@ -266,6 +267,89 @@ string StringUtil::BytesToHumanReadableString(idx_t bytes, idx_t multiplier) { |
266 | 267 | return to_string(array[0]) + (bytes == 1 ? " byte" : " bytes"); |
267 | 268 | } |
268 | 269 |
|
| 270 | +string StringUtil::TryParseFormattedBytes(const string &arg, idx_t &result) { |
| 271 | + // split based on the number/non-number |
| 272 | + idx_t idx = 0; |
| 273 | + while (StringUtil::CharacterIsSpace(arg[idx])) { |
| 274 | + idx++; |
| 275 | + } |
| 276 | + idx_t num_start = idx; |
| 277 | + while ((arg[idx] >= '0' && arg[idx] <= '9') || arg[idx] == '.' || arg[idx] == 'e' || arg[idx] == 'E' || |
| 278 | + arg[idx] == '-') { |
| 279 | + idx++; |
| 280 | + } |
| 281 | + if (idx == num_start) { |
| 282 | + return "Memory must have a number (e.g. 1GB)"; |
| 283 | + } |
| 284 | + string number = arg.substr(num_start, idx - num_start); |
| 285 | + |
| 286 | + // try to parse the number |
| 287 | + double limit; |
| 288 | + bool success = TryCast::Operation<string_t, double>(string_t(number), limit); |
| 289 | + if (!success) { |
| 290 | + return StringUtil::Format("Invalid memory limit: '%s'", number); |
| 291 | + } |
| 292 | + |
| 293 | + // now parse the memory limit unit (e.g. bytes, gb, etc) |
| 294 | + while (StringUtil::CharacterIsSpace(arg[idx])) { |
| 295 | + idx++; |
| 296 | + } |
| 297 | + idx_t start = idx; |
| 298 | + while (idx < arg.size() && !StringUtil::CharacterIsSpace(arg[idx])) { |
| 299 | + idx++; |
| 300 | + } |
| 301 | + |
| 302 | + if (limit < 0) { |
| 303 | + return "Memory cannot be negative"; |
| 304 | + } |
| 305 | + |
| 306 | + string unit = StringUtil::Lower(arg.substr(start, idx - start)); |
| 307 | + idx_t multiplier; |
| 308 | + if (unit == "byte" || unit == "bytes" || unit == "b") { |
| 309 | + multiplier = 1; |
| 310 | + } else if (unit == "kilobyte" || unit == "kilobytes" || unit == "kb" || unit == "k") { |
| 311 | + multiplier = 1000LL; |
| 312 | + } else if (unit == "megabyte" || unit == "megabytes" || unit == "mb" || unit == "m") { |
| 313 | + multiplier = 1000LL * 1000LL; |
| 314 | + } else if (unit == "gigabyte" || unit == "gigabytes" || unit == "gb" || unit == "g") { |
| 315 | + multiplier = 1000LL * 1000LL * 1000LL; |
| 316 | + } else if (unit == "terabyte" || unit == "terabytes" || unit == "tb" || unit == "t") { |
| 317 | + multiplier = 1000LL * 1000LL * 1000LL * 1000LL; |
| 318 | + } else if (unit == "kib") { |
| 319 | + multiplier = 1024LL; |
| 320 | + } else if (unit == "mib") { |
| 321 | + multiplier = 1024LL * 1024LL; |
| 322 | + } else if (unit == "gib") { |
| 323 | + multiplier = 1024LL * 1024LL * 1024LL; |
| 324 | + } else if (unit == "tib") { |
| 325 | + multiplier = 1024LL * 1024LL * 1024LL * 1024LL; |
| 326 | + } else { |
| 327 | + return StringUtil::Format("Unknown unit for memory: '%s' (expected: KB, MB, GB, TB for 1000^i units or KiB, " |
| 328 | + "MiB, GiB, TiB for 1024^i units)", |
| 329 | + unit); |
| 330 | + } |
| 331 | + |
| 332 | + // Make sure the result is not greater than `idx_t` max value |
| 333 | + constexpr double max_value = static_cast<double>(NumericLimits<idx_t>::Maximum()); |
| 334 | + const double double_multiplier = static_cast<double>(multiplier); |
| 335 | + |
| 336 | + if (limit > (max_value / double_multiplier)) { |
| 337 | + return "Memory value out of range: value is too large"; |
| 338 | + } |
| 339 | + |
| 340 | + result = LossyNumericCast<idx_t>(static_cast<double>(multiplier) * limit); |
| 341 | + return string(); |
| 342 | +} |
| 343 | + |
| 344 | +idx_t StringUtil::ParseFormattedBytes(const string &arg) { |
| 345 | + idx_t result; |
| 346 | + const string error = TryParseFormattedBytes(arg, result); |
| 347 | + if (!error.empty()) { |
| 348 | + throw InvalidInputException(error); |
| 349 | + } |
| 350 | + return result; |
| 351 | +} |
| 352 | + |
269 | 353 | string StringUtil::Upper(const string &str) { |
270 | 354 | string copy(str); |
271 | 355 | transform(copy.begin(), copy.end(), copy.begin(), [](unsigned char c) { return std::toupper(c); }); |
|
0 commit comments