This is an automated email from the ASF dual-hosted git repository.

apitrou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new cedb4f8112 ARROW-16223: [C++] Fix decimal reduce scale rounding
cedb4f8112 is described below

commit cedb4f8112b9c622dad88e0b6e8e0600f7e52746
Author: Yibo Cai <[email protected]>
AuthorDate: Tue Apr 19 17:18:27 2022 +0200

    ARROW-16223: [C++] Fix decimal reduce scale rounding
    
    Closes #12917 from cyb70289/16223-decimal-round
    
    Authored-by: Yibo Cai <[email protected]>
    Signed-off-by: Antoine Pitrou <[email protected]>
---
 cpp/src/arrow/util/basic_decimal.cc | 12 ++----------
 cpp/src/arrow/util/decimal_test.cc  | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/cpp/src/arrow/util/basic_decimal.cc 
b/cpp/src/arrow/util/basic_decimal.cc
index 3d6edaebfd..116f6d1f0c 100644
--- a/cpp/src/arrow/util/basic_decimal.cc
+++ b/cpp/src/arrow/util/basic_decimal.cc
@@ -1122,11 +1122,7 @@ BasicDecimal128 BasicDecimal128::ReduceScaleBy(int32_t 
reduce_by, bool round) co
   if (round) {
     auto divisor_half = ScaleMultipliersHalf[reduce_by];
     if (remainder.Abs() >= divisor_half) {
-      if (result > 0) {
-        result += 1;
-      } else {
-        result -= 1;
-      }
+      result += Sign();
     }
   }
   return result;
@@ -1263,11 +1259,7 @@ BasicDecimal256 BasicDecimal256::ReduceScaleBy(int32_t 
reduce_by, bool round) co
   if (round) {
     auto divisor_half = ScaleMultipliersHalfDecimal256[reduce_by];
     if (remainder.Abs() >= divisor_half) {
-      if (result > 0) {
-        result += 1;
-      } else {
-        result -= 1;
-      }
+      result += Sign();
     }
   }
   return result;
diff --git a/cpp/src/arrow/util/decimal_test.cc 
b/cpp/src/arrow/util/decimal_test.cc
index 2be9cfd9fe..87c69e8d29 100644
--- a/cpp/src/arrow/util/decimal_test.cc
+++ b/cpp/src/arrow/util/decimal_test.cc
@@ -1510,6 +1510,14 @@ TEST(Decimal128Test, ReduceScaleAndRound) {
   ASSERT_OK(result.ToInteger(&out));
   ASSERT_EQ(12345, out);
 
+  result = Decimal128("5").ReduceScaleBy(1, true);
+  ASSERT_OK(result.ToInteger(&out));
+  ASSERT_EQ(1, out);
+
+  result = Decimal128("0").ReduceScaleBy(1, true);
+  ASSERT_OK(result.ToInteger(&out));
+  ASSERT_EQ(0, out);
+
   result = Decimal128("-123789").ReduceScaleBy(2, true);
   ASSERT_OK(result.ToInteger(&out));
   ASSERT_EQ(-1238, out);
@@ -1521,6 +1529,10 @@ TEST(Decimal128Test, ReduceScaleAndRound) {
   result = Decimal128("-123750").ReduceScaleBy(2, true);
   ASSERT_OK(result.ToInteger(&out));
   ASSERT_EQ(-1238, out);
+
+  result = Decimal128("-5").ReduceScaleBy(1, true);
+  ASSERT_OK(result.ToInteger(&out));
+  ASSERT_EQ(-1, out);
 }
 
 TEST(Decimal128Test, FitsInPrecision) {
@@ -1972,6 +1984,12 @@ TEST(Decimal256Test, ReduceScaleAndRound) {
   result = Decimal256("123451").ReduceScaleBy(1, true);
   ASSERT_EQ("12345", result.ToIntegerString());
 
+  result = Decimal256("5").ReduceScaleBy(1, true);
+  ASSERT_EQ("1", result.ToIntegerString());
+
+  result = Decimal256("0").ReduceScaleBy(1, true);
+  ASSERT_EQ("0", result.ToIntegerString());
+
   result = Decimal256("-123789").ReduceScaleBy(2, true);
   ASSERT_EQ("-1238", result.ToIntegerString());
 
@@ -1980,6 +1998,9 @@ TEST(Decimal256Test, ReduceScaleAndRound) {
 
   result = Decimal256("-123750").ReduceScaleBy(2, true);
   ASSERT_EQ("-1238", result.ToIntegerString());
+
+  result = Decimal256("-5").ReduceScaleBy(1, true);
+  ASSERT_EQ("-1", result.ToIntegerString());
 }
 
 TEST(Decimal256, FromBigEndianTest) {

Reply via email to