Skip to content

Commit

Permalink
ARROW-6197: [GLib] Add garrow_decimal128_rescale()
Browse files Browse the repository at this point in the history
Closes apache#5057 from kou/glib-decimal128-rescale and squashes the following commits:

487b167 <Sutou Kouhei>  Add garrow_decimal128_rescale()

Authored-by: Sutou Kouhei <[email protected]>
Signed-off-by: Yosuke Shiro <[email protected]>
  • Loading branch information
kou authored and shiro615 committed Aug 11, 2019
1 parent 102ba09 commit 803c818
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
33 changes: 32 additions & 1 deletion c_glib/arrow-glib/decimal128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ garrow_decimal128_divide(GArrowDecimal128 *left,
arrow_decimal_left->Divide(*arrow_decimal_right,
&arrow_result_raw,
&arrow_remainder_raw);
if (garrow_error_check(error, status, "[decimal][divide]")) {
if (garrow_error_check(error, status, "[decimal128][divide]")) {
if (remainder) {
auto arrow_remainder =
std::make_shared<arrow::Decimal128>(arrow_remainder_raw);
Expand All @@ -436,6 +436,37 @@ garrow_decimal128_divide(GArrowDecimal128 *left,
}
}

/**
* garrow_decimal128_rescale:
* @decimal: A #GArrowDecimal128.
* @original_scale: A scale to be converted from.
* @new_scale: A scale to be converted to.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable) (transfer full): The rescaled decimal or %NULL on error.
*
* Since: 0.15.0
*/
GArrowDecimal128 *
garrow_decimal128_rescale(GArrowDecimal128 *decimal,
gint32 original_scale,
gint32 new_scale,
GError **error)
{
auto arrow_decimal = garrow_decimal128_get_raw(decimal);
arrow::Decimal128 arrow_rescaled_decimal_raw;
auto status = arrow_decimal->Rescale(original_scale,
new_scale,
&arrow_rescaled_decimal_raw);
if (garrow_error_check(error, status, "[decimal128][rescale]")) {
auto arrow_rescaled_decimal =
std::make_shared<arrow::Decimal128>(arrow_rescaled_decimal_raw);
return garrow_decimal128_new_raw(&arrow_rescaled_decimal);
} else {
return NULL;
}
}

G_END_DECLS

GArrowDecimal128 *
Expand Down
6 changes: 6 additions & 0 deletions c_glib/arrow-glib/decimal128.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,11 @@ GArrowDecimal128 *garrow_decimal128_divide(GArrowDecimal128 *left,
GArrowDecimal128 *right,
GArrowDecimal128 **remainder,
GError **error);
GARROW_AVAILABLE_IN_0_15
GArrowDecimal128 *
garrow_decimal128_rescale(GArrowDecimal128 *decimal,
gint32 original_scale,
gint32 new_scale,
GError **error);

G_END_DECLS
18 changes: 17 additions & 1 deletion c_glib/test/test-decimal128.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_divide_zero
decimal1 = Arrow::Decimal128.new(23423445)
decimal2 = Arrow::Decimal128.new(0)
message =
"[decimal][divide]: Invalid: Division by 0 in Decimal128"
"[decimal128][divide]: Invalid: Division by 0 in Decimal128"
assert_raise(Arrow::Error::Invalid.new(message)) do
decimal1.divide(decimal2)
end
Expand Down Expand Up @@ -203,4 +203,20 @@ def test_greater_than_or_equal
decimal >= decimal
])
end

def test_rescale
decimal = Arrow::Decimal128.new(10)
assert_equal(Arrow::Decimal128.new(1000),
decimal.rescale(1, 3))
end

def test_rescale_fail
decimal = Arrow::Decimal128.new(10)
message =
"[decimal128][rescale]: Invalid: " +
"Rescaling decimal value would cause data loss"
assert_raise(Arrow::Error::Invalid.new(message)) do
decimal.rescale(1, -1)
end
end
end

0 comments on commit 803c818

Please sign in to comment.