Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions be/src/common/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ namespace ErrorCode {
E(COPY_FILE_ERROR, -121, true); \
E(FILE_ALREADY_EXIST, -122, true); \
E(BAD_CAST, -123, true); \
E(ARITHMETIC_OVERFLOW_ERRROR, -124, false); \
E(CALL_SEQUENCE_ERROR, -202, true); \
E(BUFFER_OVERFLOW, -204, true); \
E(CONFIG_ERROR, -205, true); \
Expand Down
4 changes: 2 additions & 2 deletions be/src/runtime/type_limit.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ namespace doris {

template <typename T>
struct type_limit {
static T min() { return std::numeric_limits<T>::lowest(); }
static T max() { return std::numeric_limits<T>::max(); }
static constexpr T min() { return std::numeric_limits<T>::lowest(); }
static constexpr T max() { return std::numeric_limits<T>::max(); }
};

template <>
Expand Down
63 changes: 3 additions & 60 deletions be/src/util/string_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "vec/core/extended_types.h"
#include "vec/core/wide_integer.h"
#include "vec/data_types/data_type_decimal.h"
#include "vec/data_types/number_traits.h"

namespace doris {
namespace vectorized {
Expand Down Expand Up @@ -72,14 +73,6 @@ class StringParser {
public:
enum ParseResult { PARSE_SUCCESS = 0, PARSE_FAILURE, PARSE_OVERFLOW, PARSE_UNDERFLOW };

template <typename T>
class StringParseTraits {
public:
/// Returns the maximum ascii string length for this type.
/// e.g. the max/min int8_t has 3 characters.
static int max_ascii_len();
};

template <typename T>
static T numeric_limits(bool negative) {
if constexpr (std::is_same_v<T, __int128>) {
Expand Down Expand Up @@ -282,7 +275,7 @@ T StringParser::string_to_int_internal(const char* s, int len, ParseResult* resu
}

// This is the fast path where the string cannot overflow.
if (LIKELY(len - i < StringParseTraits<T>::max_ascii_len())) {
if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
return static_cast<T>(negative ? -val : val);
}
Expand Down Expand Up @@ -329,7 +322,7 @@ T StringParser::string_to_unsigned_int_internal(const char* s, int len, ParseRes

typedef typename std::make_signed<T>::type signedT;
// This is the fast path where the string cannot overflow.
if (LIKELY(len - i < StringParseTraits<signedT>::max_ascii_len())) {
if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<signedT>())) {
val = string_to_int_no_overflow<T>(s + i, len - i, result);
return val;
}
Expand Down Expand Up @@ -526,56 +519,6 @@ inline bool StringParser::string_to_bool_internal(const char* s, int len, ParseR
return false;
}

template <>
inline int StringParser::StringParseTraits<uint8_t>::max_ascii_len() {
return 3;
}

template <>
inline int StringParser::StringParseTraits<uint16_t>::max_ascii_len() {
return 5;
}

template <>
inline int StringParser::StringParseTraits<uint32_t>::max_ascii_len() {
return 10;
}

template <>
inline int StringParser::StringParseTraits<uint64_t>::max_ascii_len() {
return 20;
}

template <>
inline int StringParser::StringParseTraits<int8_t>::max_ascii_len() {
return 3;
}

template <>
inline int StringParser::StringParseTraits<int16_t>::max_ascii_len() {
return 5;
}

template <>
inline int StringParser::StringParseTraits<int32_t>::max_ascii_len() {
return 10;
}

template <>
inline int StringParser::StringParseTraits<int64_t>::max_ascii_len() {
return 19;
}

template <>
inline int StringParser::StringParseTraits<__int128>::max_ascii_len() {
return 39;
}

template <>
inline int StringParser::StringParseTraits<wide::Int256>::max_ascii_len() {
return 78;
}

template <PrimitiveType P, typename T>
T StringParser::string_to_decimal(const char* s, int len, int type_precision, int type_scale,
ParseResult* result) {
Expand Down
2 changes: 1 addition & 1 deletion be/src/vec/common/arithmetic_overflow.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ inline bool mul_overflow(__int128 x, __int128 y, __int128& res) {

template <>
inline bool mul_overflow(wide::Int256 x, wide::Int256 y, wide::Int256& res) {
res = x * y;
res = static_cast<wide::UInt256>(x) * static_cast<wide::UInt256>(y);
if (!x || !y) return false;
wide::UInt256 a = (x > 0) ? x : -x;
wide::UInt256 b = (y > 0) ? y : -y;
Expand Down
183 changes: 183 additions & 0 deletions be/src/vec/common/int_exp.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,187 @@ inline wide::Int256 exp10_i256(int x) {
return values[x];
}

constexpr inline int max_i32(int digit_count) {
if (digit_count < 0) {
return 0;
}
if (digit_count > 9) {
return std::numeric_limits<int>::max();
}
constexpr int values[] = {0, 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999};
return values[digit_count];
}

constexpr inline int64_t max_i64(int digit_count) {
if (digit_count < 0) {
return 0;
}
if (digit_count > 18) {
return std::numeric_limits<int64_t>::max();
}

constexpr int64_t values[] = {1LL,
9LL,
99LL,
999LL,
9999LL,
99999LL,
999999LL,
9999999LL,
99999999LL,
999999999LL,
9999999999LL,
99999999999LL,
999999999999LL,
9999999999999LL,
99999999999999LL,
999999999999999LL,
9999999999999999LL,
99999999999999999LL,
999999999999999999LL};
return values[digit_count];
}

constexpr inline __int128 max_i128(int digit_count) {
DCHECK(digit_count > 0);
constexpr __int128 values[] = {
static_cast<__int128>(0LL),
static_cast<__int128>(9LL),
static_cast<__int128>(99LL),
static_cast<__int128>(999LL),
static_cast<__int128>(9999LL),
static_cast<__int128>(99999LL),
static_cast<__int128>(999999LL),
static_cast<__int128>(9999999LL),
static_cast<__int128>(99999999LL),
static_cast<__int128>(999999999LL),
static_cast<__int128>(9999999999LL),
static_cast<__int128>(99999999999LL),
static_cast<__int128>(999999999999LL),
static_cast<__int128>(9999999999999LL),
static_cast<__int128>(99999999999999LL),
static_cast<__int128>(999999999999999LL),
static_cast<__int128>(9999999999999999LL),
static_cast<__int128>(99999999999999999LL),
static_cast<__int128>(999999999999999999LL),
static_cast<__int128>(1000000000000000000LL) * 10LL - 1,
static_cast<__int128>(1000000000000000000LL) * 100LL - 1,
static_cast<__int128>(1000000000000000000LL) * 1000LL - 1,
static_cast<__int128>(1000000000000000000LL) * 10000LL - 1,
static_cast<__int128>(1000000000000000000LL) * 100000LL - 1,
static_cast<__int128>(1000000000000000000LL) * 1000000LL - 1,
static_cast<__int128>(1000000000000000000LL) * 10000000LL - 1,
static_cast<__int128>(1000000000000000000LL) * 100000000LL - 1,
static_cast<__int128>(1000000000000000000LL) * 1000000000LL - 1,
static_cast<__int128>(1000000000000000000LL) * 10000000000LL - 1,
static_cast<__int128>(1000000000000000000LL) * 100000000000LL - 1,
static_cast<__int128>(1000000000000000000LL) * 1000000000000LL - 1,
static_cast<__int128>(1000000000000000000LL) * 10000000000000LL - 1,
static_cast<__int128>(1000000000000000000LL) * 100000000000000LL - 1,
static_cast<__int128>(1000000000000000000LL) * 1000000000000000LL - 1,
static_cast<__int128>(1000000000000000000LL) * 10000000000000000LL - 1,
static_cast<__int128>(1000000000000000000LL) * 100000000000000000LL - 1,
static_cast<__int128>(1000000000000000000LL) * 100000000000000000LL * 10LL - 1,
static_cast<__int128>(1000000000000000000LL) * 100000000000000000LL * 100LL - 1,
static_cast<__int128>(1000000000000000000LL) * 100000000000000000LL * 1000LL - 1};
return values[digit_count];
}

inline wide::Int256 max_i256(int digit_count) {
if (digit_count < 0) {
return 0;
}
if (digit_count > 76) {
return std::numeric_limits<wide::Int256>::max();
}

static constexpr wide::Int256 i10e18 {1000000000000000000LL};
static const wide::Int256 values[] = {
static_cast<wide::Int256>(0LL),
static_cast<wide::Int256>(9LL),
static_cast<wide::Int256>(99LL),
static_cast<wide::Int256>(999LL),
static_cast<wide::Int256>(9999LL),
static_cast<wide::Int256>(99999LL),
static_cast<wide::Int256>(999999LL),
static_cast<wide::Int256>(9999999LL),
static_cast<wide::Int256>(99999999LL),
static_cast<wide::Int256>(999999999LL),
static_cast<wide::Int256>(9999999999LL),
static_cast<wide::Int256>(99999999999LL),
static_cast<wide::Int256>(999999999999LL),
static_cast<wide::Int256>(9999999999999LL),
static_cast<wide::Int256>(99999999999999LL),
static_cast<wide::Int256>(999999999999999LL),
static_cast<wide::Int256>(9999999999999999LL),
static_cast<wide::Int256>(99999999999999999LL),
i10e18 - 1,
i10e18 * 10LL - 1,
i10e18 * 100LL - 1,
i10e18 * 1000LL - 1,
i10e18 * 10000LL - 1,
i10e18 * 100000LL - 1,
i10e18 * 1000000LL - 1,
i10e18 * 10000000LL - 1,
i10e18 * 100000000LL - 1,
i10e18 * 1000000000LL - 1,
i10e18 * 10000000000LL - 1,
i10e18 * 100000000000LL - 1,
i10e18 * 1000000000000LL - 1,
i10e18 * 10000000000000LL - 1,
i10e18 * 100000000000000LL - 1,
i10e18 * 1000000000000000LL - 1,
i10e18 * 10000000000000000LL - 1,
i10e18 * 100000000000000000LL - 1,
i10e18 * 100000000000000000LL * 10LL - 1,
i10e18 * 100000000000000000LL * 100LL - 1,
i10e18 * 100000000000000000LL * 1000LL - 1,
i10e18 * 100000000000000000LL * 10000LL - 1,
i10e18 * 100000000000000000LL * 100000LL - 1,
i10e18 * 100000000000000000LL * 1000000LL - 1,
i10e18 * 100000000000000000LL * 10000000LL - 1,
i10e18 * 100000000000000000LL * 100000000LL - 1,
i10e18 * 100000000000000000LL * 1000000000LL - 1,
i10e18 * 100000000000000000LL * 10000000000LL - 1,
i10e18 * 100000000000000000LL * 100000000000LL - 1,
i10e18 * 100000000000000000LL * 1000000000000LL - 1,
i10e18 * 100000000000000000LL * 10000000000000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000LL - 1,
i10e18 * 100000000000000000LL * 1000000000000000LL - 1,
i10e18 * 100000000000000000LL * 10000000000000000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 10LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 100LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 1000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 10000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 100000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 1000000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 10000000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 100000000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 1000000000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 10000000000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 100000000000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 1000000000000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 10000000000000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 100000000000000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 1000000000000000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 10000000000000000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 100000000000000000LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 100000000000000000LL * 10LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 100000000000000000LL * 100LL - 1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 100000000000000000LL * 1000LL -
1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 100000000000000000LL * 10000LL -
1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 100000000000000000LL * 100000LL -
1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 100000000000000000LL *
1000000LL -
1,
i10e18 * 100000000000000000LL * 100000000000000000LL * 100000000000000000LL *
10000000LL -
1,
};
return values[digit_count];
}
} // namespace common
Loading