diff --git a/be/src/olap/field_info.h b/be/src/olap/field_info.h index 9bb8518e6e..b188109ac2 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/CMakeLists.txt b/be/test/olap/CMakeLists.txt index 5d169e490c..921c1dbedd 100644 --- a/be/test/olap/CMakeLists.txt +++ b/be/test/olap/CMakeLists.txt @@ -43,3 +43,4 @@ ADD_BE_TEST(delta_writer_test) ADD_BE_TEST(serialize_test) ADD_BE_TEST(olap_meta_test) ADD_BE_TEST(olap_header_manager_test) +ADD_BE_TEST(field_info_test) diff --git a/be/test/olap/field_info_test.cpp b/be/test/olap/field_info_test.cpp new file mode 100644 index 0000000000..adbd7194c9 --- /dev/null +++ b/be/test/olap/field_info_test.cpp @@ -0,0 +1,46 @@ +// 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 "olap/field_info.h" + +#include + +namespace doris { + +TEST(FieldInfoTest, HandleNoneZeroInput) { + int64_t a_integer = 9223372036854775806L; + int a_fraction = 1; + decimal12_t a(a_integer, a_fraction); + decimal12_t b(1, 0); + a += b; + ASSERT_EQ(a_integer + 1, a.integer); + ASSERT_EQ(a_fraction, a.fraction); + + a.integer = -9223372036854775807L; + a.fraction = -1; + b.integer = 0; + a += b; + ASSERT_EQ(-9223372036854775807L, a.integer); + ASSERT_EQ(-1, a.fraction); +} + +} // namespace doris + +int main(int argc, char **argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/run-ut.sh b/run-ut.sh index 6c2f06a983..6301d83552 100755 --- a/run-ut.sh +++ b/run-ut.sh @@ -217,6 +217,7 @@ ${DORIS_TEST_BINARY_DIR}/olap/serialize_test ${DORIS_TEST_BINARY_DIR}/olap/olap_header_manager_test ${DORIS_TEST_BINARY_DIR}/olap/olap_meta_test ${DORIS_TEST_BINARY_DIR}/olap/delta_writer_test +${DORIS_TEST_BINARY_DIR}/olap/field_info_test # Running routine load test ${DORIS_TEST_BINARY_DIR}/runtime/kafka_consumer_pipe_test