Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Commit

Permalink
use decimal ops from arrow-3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rui-mo committed Mar 4, 2021
1 parent f3cfd91 commit 703a436
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
35 changes: 23 additions & 12 deletions cpp/src/third_party/gandiva/decimal_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
#include <cmath>
#include <limits>

#include "arrow/util/logging.h"
#include "gandiva/decimal_type_util.h"
#include "gandiva/decimal_xlarge.h"
#include "gandiva/gdv_function_stubs.h"
#include "gandiva/logging.h"

// Several operations (multiply, divide, mod, ..) require converting to 256-bit, and we
// use the boost library for doing 256-bit operations. To avoid references to boost from
Expand Down Expand Up @@ -658,24 +658,35 @@ static BasicDecimal128 RoundWithNegativeScale(const BasicDecimalScalar128& x,
return scaled + delta;
}

BasicDecimal128 Round(const BasicDecimalScalar128& x, int32_t out_scale, bool* overflow) {
if (out_scale < 0) {
return RoundWithNegativeScale(x, x.precision(), out_scale,
BasicDecimal128 Round(const BasicDecimalScalar128& x, int32_t out_precision,
int32_t out_scale, int32_t rounding_scale, bool* overflow) {
// no-op if target scale is same as arg scale
if (x.scale() == out_scale && rounding_scale >= 0) {
return x.value();
}

if (rounding_scale < 0) {
return RoundWithNegativeScale(x, out_precision, rounding_scale,
RoundType::kRoundTypeHalfRoundUp, overflow);
} else {
return RoundWithPositiveScale(x, x.precision(), out_scale,
return RoundWithPositiveScale(x, out_precision, rounding_scale,
RoundType::kRoundTypeHalfRoundUp, overflow);
}
}

BasicDecimal128 Truncate(const BasicDecimalScalar128& x, int32_t out_scale,
bool* overflow) {
if (out_scale < 0) {
return RoundWithNegativeScale(x, x.precision(), out_scale, RoundType::kRoundTypeTrunc,
overflow);
BasicDecimal128 Truncate(const BasicDecimalScalar128& x, int32_t out_precision,
int32_t out_scale, int32_t rounding_scale, bool* overflow) {
// no-op if target scale is same as arg scale
if (x.scale() == out_scale && rounding_scale >= 0) {
return x.value();
}

if (rounding_scale < 0) {
return RoundWithNegativeScale(x, out_precision, rounding_scale,
RoundType::kRoundTypeTrunc, overflow);
} else {
return RoundWithPositiveScale(x, x.precision(), out_scale, RoundType::kRoundTypeTrunc,
overflow);
return RoundWithPositiveScale(x, out_precision, rounding_scale,
RoundType::kRoundTypeTrunc, overflow);
}
}

Expand Down
7 changes: 4 additions & 3 deletions cpp/src/third_party/gandiva/decimal_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ BasicDecimal128 Convert(const BasicDecimalScalar128& x, int32_t out_precision,
int32_t out_scale, bool* overflow);

/// round decimal.
BasicDecimal128 Round(const BasicDecimalScalar128& x, int32_t out_scale, bool* overflow);
BasicDecimal128 Round(const BasicDecimalScalar128& x, int32_t out_precision,
int32_t out_scale, int32_t rounding_scale, bool* overflow);

/// truncate decimal.
BasicDecimal128 Truncate(const BasicDecimalScalar128& x, int32_t out_scale,
bool* overflow);
BasicDecimal128 Truncate(const BasicDecimalScalar128& x, int32_t out_precision,
int32_t out_scale, int32_t rounding_scale, bool* overflow);

/// ceil decimal
BasicDecimal128 Ceil(const BasicDecimalScalar128& x, bool* overflow);
Expand Down

0 comments on commit 703a436

Please sign in to comment.