Skip to content

Commit a836df5

Browse files
branch-2.1: [fix](decimal)Fix the issue where decimal multiplication produces incorrect results due to mul_overflow error #51533 (#51563)
Cherry-picked from #51533 Co-authored-by: Mryange <[email protected]>
1 parent c8daf2e commit a836df5

File tree

4 files changed

+109
-7
lines changed

4 files changed

+109
-7
lines changed

be/src/vec/common/arithmetic_overflow.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,7 @@ inline bool mul_overflow(long long x, long long y, long long& res) {
117117

118118
template <>
119119
inline bool mul_overflow(__int128 x, __int128 y, __int128& res) {
120-
res = static_cast<unsigned __int128>(x) *
121-
static_cast<unsigned __int128>(y); /// Avoid signed integer overflow.
122-
if (!x || !y) return false;
123-
124-
unsigned __int128 a = (x > 0) ? x : -x;
125-
unsigned __int128 b = (y > 0) ? y : -y;
126-
return ((a * b) / b != a) || (x > 0 && y > 0 && res < 0) || (x < 0 && y < 0 && res < 0);
120+
return __builtin_mul_overflow(x, y, &res);
127121
}
128122

129123
template <>

be/test/common/check_overflow.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#include <gtest/gtest.h>
19+
20+
#include "vec/core/types.h"
21+
#include "vec/io/io_helper.h"
22+
#include "vec/io/reader_buffer.h"
23+
24+
namespace doris::vectorized {
25+
26+
struct CheckOverFlowTest : public testing::Test {
27+
void SetUp() override {
28+
// This function is called before each test.
29+
}
30+
31+
void TearDown() override {
32+
// This function is called after each test.
33+
}
34+
35+
Int128 to_i128(std::string str) {
36+
ReadBuffer rb = ReadBuffer(str.data(), str.size());
37+
Int128 val;
38+
EXPECT_TRUE(read_int_text_impl(val, rb));
39+
return val;
40+
};
41+
42+
wide::Int256 to_i256(std::string str) { return wide::Int256::_impl::from_str(str.c_str()); };
43+
};
44+
45+
TEST_F(CheckOverFlowTest, test_overflow_int128) {
46+
{
47+
Int128 a = to_i128("-15687000000000000000000");
48+
Int128 b = to_i128("11000000000000000");
49+
Int128 c;
50+
EXPECT_TRUE(common::mul_overflow(a, b, c));
51+
}
52+
53+
{
54+
Int128 a = to_i128("-15687000000000000000000");
55+
Int128 b = to_i128("-11000000000000000");
56+
Int128 c;
57+
EXPECT_TRUE(common::mul_overflow(a, b, c));
58+
}
59+
60+
{
61+
Int128 a = to_i128("1000");
62+
Int128 b = to_i128("12000");
63+
Int128 c;
64+
EXPECT_FALSE(common::mul_overflow(a, b, c));
65+
}
66+
}
67+
68+
TEST_F(CheckOverFlowTest, test_overflow_int256) {
69+
{
70+
wide::Int256 a =
71+
to_i256("-11579208923731619542357098500868790785326998466564056403945758400791");
72+
wide::Int256 b = to_i256("1157920892373161954235709850086879078532699846656405640394575");
73+
wide::Int256 c;
74+
EXPECT_TRUE(common::mul_overflow(a, b, c));
75+
}
76+
77+
{
78+
wide::Int256 a = to_i256("-1157920892373161954235709850086879078532699846656405640394");
79+
wide::Int256 b = to_i256("-1157920892373161954235709850086879078532699846656405640394");
80+
wide::Int256 c;
81+
EXPECT_TRUE(common::mul_overflow(a, b, c));
82+
}
83+
84+
{
85+
wide::Int256 a = to_i256("1000");
86+
wide::Int256 b = to_i256("12000");
87+
wide::Int256 c;
88+
EXPECT_FALSE(common::mul_overflow(a, b, c));
89+
}
90+
}
91+
92+
} // namespace doris::vectorized

regression-test/data/datatype_p0/decimalv3/test_decimalv3_cast3.out

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,12 @@
134134
-170141183460469231731687303715884105728
135135
170141183460469231731687303715884105727
136136

137+
-- !sql --
138+
-172.557000000000000000000000000000000
139+
140+
-- !sql --
141+
-172.557000000000000000000000000000000
142+
143+
-- !sql --
144+
-172.557000000000000000000000000000000
145+

regression-test/suites/datatype_p0/decimalv3/test_decimalv3_cast3.groovy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,4 +505,11 @@ suite("test_decimalv3_cast3") {
505505
exception "Arithmetic overflow"
506506
}
507507
sql "set enable_decimal256=false;"
508+
509+
510+
sql """ set debug_skip_fold_constant = true;"""
511+
qt_sql """ select -15687.000000000000000000 * 0.01100000000000000; """
512+
qt_sql """ select -15687.000000000000000000 * 0.011000000000000000; """
513+
qt_sql """ select -15687.000000000000000000 * 0.0110000000000000000; """
514+
sql """ set debug_skip_fold_constant = false;"""
508515
}

0 commit comments

Comments
 (0)