Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](money_format) fix money_format #31883

Merged
merged 1 commit into from
Mar 7, 2024
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
2 changes: 1 addition & 1 deletion be/src/vec/functions/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Status PreparedFunctionImpl::default_implementation_for_constant_arguments(
return Status::OK();
}

// now all columns is const.
// now all columns are const.
Block temporary_block;

size_t arguments_size = args.size();
Expand Down
30 changes: 21 additions & 9 deletions be/src/vec/functions/function_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <array>
#include <boost/iterator/iterator_facade.hpp>
#include <cstddef>
#include <cstdlib>
#include <iomanip>
#include <memory>
#include <ostream>
Expand All @@ -36,6 +37,7 @@
#include <vector>

#include "common/compiler_util.h" // IWYU pragma: keep
#include "common/exception.h"
#include "common/status.h"
#include "gutil/strings/numbers.h"
#include "gutil/strings/substitute.h"
Expand Down Expand Up @@ -2941,10 +2943,15 @@ template <typename Impl>
class FunctionMoneyFormat : public IFunction {
public:
static constexpr auto name = "money_format";
static FunctionPtr create() { return std::make_shared<FunctionMoneyFormat<Impl>>(); }
static FunctionPtr create() { return std::make_shared<FunctionMoneyFormat>(); }
String get_name() const override { return name; }

DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
if (arguments.size() != 1) {
throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
"Function {} requires exactly 1 argument", name);
}

return std::make_shared<DataTypeString>();
}
DataTypes get_variadic_argument_types_impl() const override {
Expand Down Expand Up @@ -3017,9 +3024,11 @@ struct MoneyFormatDoubleImpl {
static void execute(FunctionContext* context, ColumnString* result_column,
const ColumnPtr col_ptr, size_t input_rows_count) {
const auto* data_column = assert_cast<const ColumnVector<Float64>*>(col_ptr.get());
// when scale is above 38, we will go here
for (size_t i = 0; i < input_rows_count; i++) {
// truncate to 2 decimal places, keep same with mysql
double value =
MathFunctions::my_double_round(data_column->get_element(i), 2, false, false);
MathFunctions::my_double_round(data_column->get_element(i), 2, false, true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should do round instead of truncate

StringRef str = MoneyFormat::do_money_format(context, fmt::format("{:.2f}", value));
result_column->insert_data(str.data, str.size);
}
Expand Down Expand Up @@ -3076,8 +3085,8 @@ struct MoneyFormatDecimalImpl {
} else if (auto* decimal32_column =
check_and_get_column<ColumnDecimal<Decimal32>>(*col_ptr)) {
const UInt32 scale = decimal32_column->get_scale();
const auto multiplier =
scale > 2 ? common::exp10_i32(scale - 2) : common::exp10_i32(2 - scale);
// scale is up to 9, so exp10_i32 is enough
const auto multiplier = common::exp10_i32(std::abs(static_cast<int>(scale - 2)));
for (size_t i = 0; i < input_rows_count; i++) {
Decimal32 frac_part = decimal32_column->get_fractional_part(i);
if (scale > 2) {
Expand All @@ -3095,8 +3104,8 @@ struct MoneyFormatDecimalImpl {
} else if (auto* decimal64_column =
check_and_get_column<ColumnDecimal<Decimal64>>(*col_ptr)) {
const UInt32 scale = decimal64_column->get_scale();
const auto multiplier =
scale > 2 ? common::exp10_i32(scale - 2) : common::exp10_i32(2 - scale);
// 9 < scale <= 18
const auto multiplier = common::exp10_i64(std::abs(static_cast<int>(scale - 2)));
for (size_t i = 0; i < input_rows_count; i++) {
Decimal64 frac_part = decimal64_column->get_fractional_part(i);
if (scale > 2) {
Expand All @@ -3114,8 +3123,8 @@ struct MoneyFormatDecimalImpl {
} else if (auto* decimal128_column =
check_and_get_column<ColumnDecimal<Decimal128V3>>(*col_ptr)) {
const UInt32 scale = decimal128_column->get_scale();
const auto multiplier =
scale > 2 ? common::exp10_i32(scale - 2) : common::exp10_i32(2 - scale);
// 18 < scale <= 38
const auto multiplier = common::exp10_i128(std::abs(static_cast<int>(scale - 2)));
for (size_t i = 0; i < input_rows_count; i++) {
Decimal128V3 frac_part = decimal128_column->get_fractional_part(i);
if (scale > 2) {
Expand All @@ -3130,6 +3139,9 @@ struct MoneyFormatDecimalImpl {

result_column->insert_data(str.data, str.size);
}
} else {
throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
"Not supported input argument type {}", col_ptr->get_name());
}
// TODO: decimal256
/* else if (auto* decimal256_column =
Expand Down Expand Up @@ -3635,7 +3647,7 @@ class FunctionConvertTo : public IFunction {
// +---------------------------------------------------------------------------------------------+
// | char(0xe5, 0xa4, 0x9a, 0xe7, 0x9d, 0xbf, 0xe4, 0xb8, 0x9d, 68, 111, 114, 105, 115 using utf8) |
// +---------------------------------------------------------------------------------------------+
// | 多睿丝Doris |
// | 多睿丝 Doris |
// +---------------------------------------------------------------------------------------------+
// mysql> select char(68, 111, 114, 0, 105, null, 115 using utf8);
// +--------------------------------------------------+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,30 @@ ab d
-- !sql --
1,123.40

-- !sql --
1.12

-- !sql_decimal32 --
1.12

-- !sql_decimal64 --
1.12

-- !sql_decimal64 --
1.12

-- !sql_decimal128 --
1.12

-- !sql_decimal128 --
1.12

-- !sql_float64 --
1.12

-- !sql_float64 --
1.12

-- !sql --
true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ suite("test_string_function") {
qt_sql "select money_format(17014116);"
qt_sql "select money_format(1123.456);"
qt_sql "select money_format(1123.4);"
qt_sql "select money_format(1.1249);"
qt_sql_decimal32 "select money_format(cast(concat('1.124', repeat('9', 5)) as DECIMAL(9, 8)));"
qt_sql_decimal64 "select money_format(cast(concat('1.124', repeat('9', 6)) as DECIMAL(10, 9)));"
qt_sql_decimal64 "select money_format(cast(concat('1.124', repeat('9', 14)) as DECIMAL(18, 17)));"
qt_sql_decimal128 "select money_format(cast(concat('1.124', repeat('9', 15)) as DECIMAL(19, 18)));"
qt_sql_decimal128 "select money_format(cast(concat('1.124', repeat('9', 34)) as DECIMAL(38, 37)));"
qt_sql_float64 "select money_format(cast(concat('1.124', repeat('9', 35)) as DOUBLE));"
qt_sql_float64 "select money_format(cast(concat('1.124', repeat('9', 70)) as DOUBLE));"

qt_sql "select null_or_empty(null);"
qt_sql "select null_or_empty(\"\");"
Expand Down
Loading