jacktengg commented on code in PR #34680: URL: https://github.com/apache/doris/pull/34680#discussion_r1599312403
########## be/src/vec/functions/function_string.h: ########## @@ -2961,18 +2965,77 @@ class FunctionMoneyFormat : public IFunction { namespace MoneyFormat { +constexpr size_t MAX_FORMAT_LEN_DEC32() { + // Decimal(9, 0) + // Double the size to avoid some unexpected bug. + return 2 * (1 + 9 + (9 / 3) + 3); +} + +constexpr size_t MAX_FORMAT_LEN_DEC64() { + // Decimal(18, 0) + // Double the size to avoid some unexpected bug. + return 2 * (1 + 18 + (18 / 3) + 3); +} + +constexpr size_t MAX_FORMAT_LEN_DEC128V2() { + // DecimalV2 has at most 27 digits + // Double the size to avoid some unexpected bug. + return 2 * (1 + 27 + (27 / 3) + 3); +} + +constexpr size_t MAX_FORMAT_LEN_DEC128V3() { + // Decimal(38, 0) + // Double the size to avoid some unexpected bug. + return 2 * (1 + 39 + (39 / 3) + 3); +} + template <typename T, size_t N> -StringRef do_money_format(FunctionContext* context, const T int_value, - const int32_t frac_value = 0) { +StringRef do_money_format(FunctionContext* context, UInt32 scale, T int_value, T frac_value, + bool do_truncate = true) { + static_assert(std::is_integral<T>::value); + const bool is_negative = int_value < 0 || frac_value < 0; + + bool append_sign_manually = false; + if (is_negative && int_value == 0) { + // when int_value is 0, result of SimpleItoaWithCommas will contains just zero + // for Decimal like -0.1234, this will leads to problem, because negative sign is discarded. + // this is why we introduce argument append_sing_manually. + append_sign_manually = true; + } + + // for DecimalV2, it has already been truncated, so we need this flag + if (do_truncate) { + // magic number 2: since we need to truncate frac_part to 2 digits + if (scale > 2) { + DCHECK(scale <= 38); + // do truncate + auto multiplier = common::exp10_i128(std::abs(static_cast<int>(scale - 2))); + frac_value = std::abs(frac_value / multiplier); Review Comment: 这里小数部分四舍五入是不是给弄没了呀? -- 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