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 += decimal error #1266

Closed
wants to merge 1 commit into from
Closed
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/olap/field_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ struct decimal12_t {
}

// if sign of fraction is different from integer
if ((fraction ^ integer) < 0) {
if ((fraction != 0) && (integer != 0) && (fraction ^ integer) < 0) {
bool sign = integer < 0;
integer += (sign ? 1 : -1);
fraction += (sign ? -FRAC_RATIO : FRAC_RATIO);
Expand Down
26 changes: 25 additions & 1 deletion be/test/olap/field_info_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace doris {

TEST(FieldInfoTest, HandleNoneZeroInput) {
TEST(FieldInfoTest, Add) {
int64_t a_integer = 9223372036854775806L;
int a_fraction = 1;
decimal12_t a(a_integer, a_fraction);
Expand All @@ -36,6 +36,30 @@ TEST(FieldInfoTest, HandleNoneZeroInput) {
a += b;
ASSERT_EQ(-9223372036854775807L, a.integer);
ASSERT_EQ(-1, a.fraction);

a.integer = -1;
a.fraction = 0;
b.integer = -7;
b.fraction = 0;
a += b;
ASSERT_EQ(-8, a.integer);
ASSERT_EQ(0, a.fraction);

a.integer = 0;
a.fraction = -1;
b.integer = 0;
b.fraction = -2;
a +=b;
ASSERT_EQ(0, a.integer);
ASSERT_EQ(-3, a.fraction);

a.integer = 0;
a.fraction = -999999999;
b.integer = 0;
b.fraction = -1;
a += b;
ASSERT_EQ(-1, a.integer);
ASSERT_EQ(0, a.fraction);
}

} // namespace doris
Expand Down