diff --git a/be/src/olap/field_info.h b/be/src/olap/field_info.h index 9bb8518e6eef2d..b188109ac24af6 100644 --- a/be/src/olap/field_info.h +++ b/be/src/olap/field_info.h @@ -123,6 +123,7 @@ inline std::ostream& operator<<(std::ostream& os, const uint24_t& val) { return os; } +// the sign of integer must be same as fraction struct decimal12_t { decimal12_t() : integer(0), fraction(0) {} decimal12_t(int64_t int_part, int32_t frac_part) { @@ -147,7 +148,8 @@ struct decimal12_t { fraction += FRAC_RATIO; } - if (fraction * integer < 0) { + // if sign of fraction is different from integer + if ((fraction ^ integer) < 0) { bool sign = integer < 0; integer += (sign ? 1 : -1); fraction += (sign ? -FRAC_RATIO : FRAC_RATIO); diff --git a/be/test/olap/field_info_test.cpp b/be/test/olap/field_info_test.cpp new file mode 100644 index 00000000000000..33259f131adad2 --- /dev/null +++ b/be/test/olap/field_info_test.cpp @@ -0,0 +1,15 @@ +#include +#include + +TEST_F(){ + decimal12_t a = decimal12_t(INT_MAX + 1, INT_MAX); + decimal12_t b = decimal12_t(0,0); + + ASSERT_EQ(a, a+=b); + delete +} + +int main(int argc, char **argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file