yiguolei commented on code in PR #27787: URL: https://github.com/apache/doris/pull/27787#discussion_r1410083307
########## be/src/vec/core/types.h: ########## @@ -700,83 +700,9 @@ struct Decimal<wide::Int256> { return *this; } - static constexpr int max_string_length() { - constexpr auto precision = - std::is_same_v<T, Int32> - ? BeConsts::MAX_DECIMAL32_PRECISION - : (std::is_same_v<T, Int64> - ? BeConsts::MAX_DECIMAL64_PRECISION - : (std::is_same_v<T, Int128> - ? BeConsts::MAX_DECIMAL128_PRECISION - : BeConsts::MAX_DECIMAL256_PRECISION)); - return precision + 1 // Add a space for decimal place - + 1 // Add a space for leading 0 - + 1; // Add a space for negative sign - } + static constexpr int max_string_length() { return max_decimal_string_length<T>(); } - std::string to_string(UInt32 scale) const { - if (value == std::numeric_limits<T>::min()) { - if constexpr (std::is_same_v<T, wide::Int256>) { - std::string res {wide::to_string(value)}; - res.insert(res.size() - scale, "."); - return res; - } else { - fmt::memory_buffer buffer; - fmt::format_to(buffer, "{}", value); - std::string res {buffer.data(), buffer.size()}; - res.insert(res.size() - scale, "."); - return res; - } - } - - static constexpr auto precision = - std::is_same_v<T, Int32> - ? BeConsts::MAX_DECIMAL32_PRECISION - : (std::is_same_v<T, Int64> - ? BeConsts::MAX_DECIMAL64_PRECISION - : (std::is_same_v<T, Int128> - ? BeConsts::MAX_DECIMAL128_PRECISION - : BeConsts::MAX_DECIMAL256_PRECISION)); - bool is_nagetive = value < 0; - int max_result_length = precision + (scale > 0) // Add a space for decimal place - + (scale == precision) // Add a space for leading 0 - + (is_nagetive); // Add a space for negative sign - std::string str = std::string(max_result_length, '0'); - - T abs_value = value; - int pos = 0; - - if (is_nagetive) { - abs_value = -value; - str[pos++] = '-'; - } - - T whole_part = abs_value; - T frac_part; - if (scale) { - whole_part = abs_value / decimal_scale_multiplier<T>(scale); - frac_part = abs_value % decimal_scale_multiplier<T>(scale); - } - if constexpr (std::is_same_v<T, wide::Int256>) { - std::string num_str {wide::to_string(whole_part)}; - auto end = fmt::format_to(str.data() + pos, "{}", num_str); - pos = end - str.data(); - } else { - auto end = fmt::format_to(str.data() + pos, "{}", whole_part); - pos = end - str.data(); - } - - if (scale) { - str[pos++] = '.'; - for (auto end_pos = pos + scale - 1; end_pos >= pos && frac_part > 0; - --end_pos, frac_part /= 10) { - str[end_pos] += (int)(frac_part % 10); - } - } - - str.resize(pos + scale); - return str; - } + std::string to_string(UInt32 scale) const { return decimal_to_string(value, scale); } Review Comment: maybe not refactor these code, because we need pick this PR to 2.0 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org