diff options
author | kniv <kniv@yandex-team.ru> | 2022-03-14 18:26:01 +0300 |
---|---|---|
committer | kniv <kniv@yandex-team.ru> | 2022-03-14 18:26:01 +0300 |
commit | 61e7c7a08a105da2e4c779cd6a9ed2fb0f0ca1ad (patch) | |
tree | b094528ed6877f7eb6119dd49a4f78d32ac09f71 | |
parent | c7783ffcebf50c210ca2b469f3a52f59eb9045f8 (diff) | |
download | ydb-61e7c7a08a105da2e4c779cd6a9ed2fb0f0ca1ad.tar.gz |
YQL-7418: Change ToUint64() prototype, add TryToUint64()
YQL-7418: Change ToUint64() prototype, add TryToUint64()
ref:cdb80c16a324a307acad2068a94234c98b61303a
-rw-r--r-- | ydb/library/yql/udfs/common/unicode_base/lib/unicode_base_udf.h | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/ydb/library/yql/udfs/common/unicode_base/lib/unicode_base_udf.h b/ydb/library/yql/udfs/common/unicode_base/lib/unicode_base_udf.h index 85867d84ec8..5a79da9e63c 100644 --- a/ydb/library/yql/udfs/common/unicode_base/lib/unicode_base_udf.h +++ b/ydb/library/yql/udfs/common/unicode_base/lib/unicode_base_udf.h @@ -53,21 +53,44 @@ namespace { return TUnboxedValuePod(static_cast<ui64>(result)); } - SIMPLE_UDF(TToUint64, ui64(TAutoMap<TUtf8>, ui16)) { + SIMPLE_UDF_OPTIONS(TToUint64, ui64(TAutoMap<TUtf8>, TOptional<ui16>), + builder.OptionalArgs(1)) { Y_UNUSED(valueBuilder); const TString inputStr(args[0].AsStringRef()); const char* input = inputStr.Data(); - const int base = static_cast<int>(args[1].Get<ui16>()); - char *pos = nullptr; + const int base = static_cast<int>(args[1].GetOrDefault<ui16>(0)); + char* pos = nullptr; unsigned long long res = std::strtoull(input, &pos, base); + ui64 ret = static_cast<ui64>(res); if (!res && pos == input) { UdfTerminate("Input string is not a number"); - } else if (res == ULLONG_MAX && errno == ERANGE) { + } else if ((res == ULLONG_MAX && errno == ERANGE) || ret != res) { UdfTerminate("Converted value falls out of Uint64 range"); } else if (*pos) { UdfTerminate("Input string contains junk after the number"); } - return TUnboxedValuePod(static_cast<ui64>(res)); + return TUnboxedValuePod(ret); + } + + SIMPLE_UDF_OPTIONS(TTryToUint64, TOptional<ui64>(TAutoMap<TUtf8>, TOptional<ui16>), + builder.OptionalArgs(1)) { + Y_UNUSED(valueBuilder); + const TString inputStr(args[0].AsStringRef()); + const char* input = inputStr.Data(); + const int base = static_cast<int>(args[1].GetOrDefault<ui16>(0)); + char* pos = nullptr; + unsigned long long res = std::strtoull(input, &pos, base); + ui64 ret = static_cast<ui64>(res); + if (!res && pos == input) { + return TUnboxedValuePod(); + } + if ((res == ULLONG_MAX && errno == ERANGE) || ret != res) { + return TUnboxedValuePod(); + } + if (*pos) { + return TUnboxedValuePod(); + } + return TUnboxedValuePod(ret); } SIMPLE_UDF_OPTIONS(TSubstring, TUtf8(TAutoMap<TUtf8>, TOptional<ui64>, TOptional<ui64>), @@ -452,5 +475,6 @@ namespace { TToLower, \ TToUpper, \ TToTitle, \ - TToUint64 + TToUint64, \ + TTryToUint64 } |