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
2 changes: 1 addition & 1 deletion be/src/exprs/math_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ double MathFunctions::my_double_round(double value, int64_t dec, bool dec_unsign

if (dec_negative && std::isinf(tmp)) {
tmp2 = 0.0;
} else if (!dec_negative && std::isinf(value_mul_tmp)) {
} else if (!dec_negative && !std::isfinite(value_mul_tmp)) {
tmp2 = value;
} else if (truncate) {
if (value >= 0.0) {
Expand Down
50 changes: 47 additions & 3 deletions be/src/vec/functions/function_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -3365,6 +3365,44 @@ struct FormatRoundDoubleImpl {
return {std::make_shared<DataTypeFloat64>(), std::make_shared<vectorized::DataTypeInt32>()};
}

static std::string add_thousands_separator(const std::string& formatted_num) {
// Find the position of the decimal point
size_t dot_pos = formatted_num.find('.');
if (dot_pos == std::string::npos) {
dot_pos = formatted_num.size();
}

// Handle the integer part
int start = (formatted_num[0] == '-') ? 1 : 0;
int digit_count = dot_pos - start;

// There is no need to add commas.
if (digit_count <= 3) {
return formatted_num;
}

std::string result;

if (start == 1) result += '-';

// Add the integer part (with comma)
int first_group = digit_count % 3;
if (first_group == 0) first_group = 3;
result.append(formatted_num, start, first_group);

for (size_t i = start + first_group; i < dot_pos; i += 3) {
result += ',';
result.append(formatted_num, i, 3);
}

// Add the decimal part (keep as it is)
if (dot_pos != formatted_num.size()) {
result.append(formatted_num, dot_pos);
}

return result;
}

template <bool is_const>
static Status execute(FunctionContext* context, ColumnString* result_column,
const ColumnPtr col_ptr, ColumnPtr decimal_places_col_ptr,
Expand All @@ -3382,9 +3420,15 @@ struct FormatRoundDoubleImpl {
// round to `decimal_places` decimal places
double value = MathFunctions::my_double_round(data_column->get_element(i),
decimal_places, false, false);
StringRef str = FormatRound::do_format_round(
context, fmt::format("{:.{}f}", value, decimal_places), decimal_places);
result_column->insert_data(str.data, str.size);
std::string formatted_value = fmt::format("{:.{}f}", value, decimal_places);
if (std::isfinite(value)) {
auto res_str = add_thousands_separator(formatted_value);
result_column->insert_data(res_str.data(), res_str.size());
} else {
// if value is not finite, we just insert the original formatted value
// e.g. "inf", "-inf", "nan"
result_column->insert_data(formatted_value.data(), formatted_value.size());
}
}
return Status::OK();
}
Expand Down
87 changes: 87 additions & 0 deletions be/test/exprs/math_functions_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include "exprs/math_functions.h"

#include <gtest/gtest.h>

#include <cmath>
#include <limits>
namespace doris {

struct MathFunctionsTest : public ::testing::Test {};

// Regular rounding test (truncate = false)
TEST_F(MathFunctionsTest, DoubleRoundBasic) {
// Positive number rounding
EXPECT_DOUBLE_EQ(123.46, MathFunctions::my_double_round(123.456, 2, false, false));
EXPECT_DOUBLE_EQ(123.45, MathFunctions::my_double_round(123.454, 2, false, false));

// Negative number rounding
EXPECT_DOUBLE_EQ(-123.46, MathFunctions::my_double_round(-123.456, 2, false, false));
EXPECT_DOUBLE_EQ(-123.45, MathFunctions::my_double_round(-123.454, 2, false, false));

// Integer place rounding
EXPECT_DOUBLE_EQ(100.0,
MathFunctions::my_double_round(123.456, -2, false, false)); // Hundreds place
EXPECT_DOUBLE_EQ(120.0,
MathFunctions::my_double_round(123.456, -1, false, false)); // Tens place
EXPECT_DOUBLE_EQ(-100.0, MathFunctions::my_double_round(
-123.456, -2, false, false)); // Negative number hundreds place
}

// Truncation mode test (truncate = true)
TEST_F(MathFunctionsTest, DoubleRoundTruncate) {
// Positive number truncation
EXPECT_DOUBLE_EQ(123.45, MathFunctions::my_double_round(123.456, 2, false, true));
EXPECT_DOUBLE_EQ(123.0, MathFunctions::my_double_round(123.789, 0, false, true));

// Negative number truncation (towards zero)
EXPECT_DOUBLE_EQ(-123.45, MathFunctions::my_double_round(-123.456, 2, false, true));
EXPECT_DOUBLE_EQ(-100.0, MathFunctions::my_double_round(-123.456, -2, false, true));
}

// Special value handling (Infinity, NaN)
TEST_F(MathFunctionsTest, DoubleRoundSpecialValues) {
const double inf = std::numeric_limits<double>::infinity();
const double nan = std::numeric_limits<double>::quiet_NaN();

// Infinity remains unchanged
EXPECT_DOUBLE_EQ(inf, MathFunctions::my_double_round(inf, 2, false, false));
EXPECT_DOUBLE_EQ(-inf, MathFunctions::my_double_round(-inf, -3, true, true));

// NaN returns NaN
EXPECT_TRUE(std::isnan(MathFunctions::my_double_round(nan, 2, false, false)));

// Large precision causing overflow
EXPECT_DOUBLE_EQ(0.0, MathFunctions::my_double_round(123.456, -1000, false, false));
EXPECT_DOUBLE_EQ(123.456, MathFunctions::my_double_round(123.456, -1000, true,
false)); // dec_unsigned handling
}

// Zero and boundary precision test
TEST_F(MathFunctionsTest, DoubleRoundEdgeCases) {
// Zero value handling
EXPECT_DOUBLE_EQ(0.0, MathFunctions::my_double_round(0.0, 3, false, false));
EXPECT_DOUBLE_EQ(0.0, MathFunctions::my_double_round(-0.0, 2, true, true));

// Zero precision
EXPECT_DOUBLE_EQ(123.0, MathFunctions::my_double_round(123.456, 0, false, false));
EXPECT_DOUBLE_EQ(124.0, MathFunctions::my_double_round(123.789, 0, false, false)); // Rounding
}

}; // namespace doris
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,177 @@
123,456.123457
34.123457

-- !format_round_14 --
9,876

-- !format_round_15 --
0.0000001

-- !format_round_16 --
999,999,999.999999

-- !format_round_17 --
-123.457

-- !format_round_18 --
1.1949288396

-- !format_round_19 --
0.00

-- !format_round_20 --
1,234,567,890.123456789

-- !format_round_21 --
0.0000000001

-- !format_round_22 --
-999,999,999.999999

-- !format_round_23 --
123.5

-- !format_round_24 --
123.00000000002147026859

-- !format_round_25 --
179,769,313,486,231,570,814,527,423,731,704,356,798,070,567,525,844,996,598,917,476,803,157,260,780,028,538,760,589,558,632,766,878,171,540,458,953,514,382,464,234,321,326,889,464,182,768,467,546,703,537,516,986,049,910,576,551,282,076,245,490,090,389,328,944,075,868,508,455,133,942,304,583,236,903,222,948,165,808,559,332,123,348,274,797,826,204,144,723,168,738,177,180,919,299,881,250,404,026,184,124,858,368.00

-- !format_round_26 --
0.00000000000000000000

-- !format_round_27 --
0

-- !format_round_28 --
0.0000000000

-- !format_round_29 --
1

-- !format_round_30 --
1.0000000000

-- !format_round_31 --
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

-- !format_round_32 --
179,769,313,486,231,570,814,527,423,731,704,356,798,070,567,525,844,996,598,917,476,803,157,260,780,028,538,760,589,558,632,766,878,171,540,458,953,514,382,464,234,321,326,889,464,182,768,467,546,703,537,516,986,049,910,576,551,282,076,245,490,090,389,328,944,075,868,508,455,133,942,304,583,236,903,222,948,165,808,559,332,123,348,274,797,826,204,144,723,168,738,177,180,919,299,881,250,404,026,184,124,858,368

-- !format_round_33 --
0

-- !format_round_34 --
179,769,313,486,231,570,814,527,423,731,704,356,798,070,567,525,844,996,598,917,476,803,157,260,780,028,538,760,589,558,632,766,878,171,540,458,953,514,382,464,234,321,326,889,464,182,768,467,546,703,537,516,986,049,910,576,551,282,076,245,490,090,389,328,944,075,868,508,455,133,942,304,583,236,903,222,948,165,808,559,332,123,348,274,797,826,204,144,723,168,738,177,180,919,299,881,250,404,026,184,124,858,368.0000000000

-- !format_round_35 --
0.0000000000

-- !format_round_36 --
179,769,313,486,231,570,814,527,423,731,704,356,798,070,567,525,844,996,598,917,476,803,157,260,780,028,538,760,589,558,632,766,878,171,540,458,953,514,382,464,234,321,326,889,464,182,768,467,546,703,537,516,986,049,910,576,551,282,076,245,490,090,389,328,944,075,868,508,455,133,942,304,583,236,903,222,948,165,808,559,332,123,348,274,797,826,204,144,723,168,738,177,180,919,299,881,250,404,026,184,124,858,368.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

-- !format_round_37 --
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072014

-- !format_round_38 --
1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

-- !format_round_39 --
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

-- !format_round_40 --
0

-- !format_round_41 --
0.0000000000

-- !format_round_42 --
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

-- !format_round_43 --
1

-- !format_round_44 --
1.0000000000

-- !format_round_45 --
1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

-- !format_round_46 --
0

-- !format_round_47 --
0.0000000000

-- !format_round_48 --
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

-- !format_round_49 --
179,769,313,486,231,570,814,527,423,731,704,356,798,070,567,525,844,996,598,917,476,803,157,260,780,028,538,760,589,558,632,766,878,171,540,458,953,514,382,464,234,321,326,889,464,182,768,467,546,703,537,516,986,049,910,576,551,282,076,245,490,090,389,328,944,075,868,508,455,133,942,304,583,236,903,222,948,165,808,559,332,123,348,274,797,826,204,144,723,168,738,177,180,919,299,881,250,404,026,184,124,858,368

-- !format_round_50 --
179,769,313,486,231,570,814,527,423,731,704,356,798,070,567,525,844,996,598,917,476,803,157,260,780,028,538,760,589,558,632,766,878,171,540,458,953,514,382,464,234,321,326,889,464,182,768,467,546,703,537,516,986,049,910,576,551,282,076,245,490,090,389,328,944,075,868,508,455,133,942,304,583,236,903,222,948,165,808,559,332,123,348,274,797,826,204,144,723,168,738,177,180,919,299,881,250,404,026,184,124,858,368.0000000000

-- !format_round_51 --
179,769,313,486,231,570,814,527,423,731,704,356,798,070,567,525,844,996,598,917,476,803,157,260,780,028,538,760,589,558,632,766,878,171,540,458,953,514,382,464,234,321,326,889,464,182,768,467,546,703,537,516,986,049,910,576,551,282,076,245,490,090,389,328,944,075,868,508,455,133,942,304,583,236,903,222,948,165,808,559,332,123,348,274,797,826,204,144,723,168,738,177,180,919,299,881,250,404,026,184,124,858,368.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

-- !format_round_52 --
0

-- !format_round_53 --
0.0000000000

-- !format_round_54 --
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072014

-- !format_round_55 --
0

-- !format_round_56 --
0.0000000000

-- !format_round_57 --
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

-- !format_round_58 --
1

-- !format_round_59 --
1.0000000000

-- !format_round_60 --
1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

-- !format_round_61 --
0

-- !format_round_62 --
0.0000000000

-- !format_round_63 --
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

-- !format_round_64 --
179,769,313,486,231,570,814,527,423,731,704,356,798,070,567,525,844,996,598,917,476,803,157,260,780,028,538,760,589,558,632,766,878,171,540,458,953,514,382,464,234,321,326,889,464,182,768,467,546,703,537,516,986,049,910,576,551,282,076,245,490,090,389,328,944,075,868,508,455,133,942,304,583,236,903,222,948,165,808,559,332,123,348,274,797,826,204,144,723,168,738,177,180,919,299,881,250,404,026,184,124,858,368

-- !format_round_65 --
179,769,313,486,231,570,814,527,423,731,704,356,798,070,567,525,844,996,598,917,476,803,157,260,780,028,538,760,589,558,632,766,878,171,540,458,953,514,382,464,234,321,326,889,464,182,768,467,546,703,537,516,986,049,910,576,551,282,076,245,490,090,389,328,944,075,868,508,455,133,942,304,583,236,903,222,948,165,808,559,332,123,348,274,797,826,204,144,723,168,738,177,180,919,299,881,250,404,026,184,124,858,368.0000000000

-- !format_round_66 --
179,769,313,486,231,570,814,527,423,731,704,356,798,070,567,525,844,996,598,917,476,803,157,260,780,028,538,760,589,558,632,766,878,171,540,458,953,514,382,464,234,321,326,889,464,182,768,467,546,703,537,516,986,049,910,576,551,282,076,245,490,090,389,328,944,075,868,508,455,133,942,304,583,236,903,222,948,165,808,559,332,123,348,274,797,826,204,144,723,168,738,177,180,919,299,881,250,404,026,184,124,858,368.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

-- !format_round_67 --
0

-- !format_round_68 --
0.0000000000

-- !format_round_69 --
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072014

-- !format_round_70 --
0

-- !format_round_71 --
0.0000000000

Loading
Loading