Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions cpp/src/arrow/compute/expression_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,41 @@ TEST(Expression, ExecuteDictionaryTransparent) {
])"));
}

TEST(Expression, ExecuteDecimalDivide) {
// GH-40911 Decimal divide promotion rule output wrong precision and scale
// in expression Bind when scale1 >= scale2
//
// scale1 < scale2
ExpectExecute(
call("divide", {field_ref("a"), field_ref("b")}),
ArrayFromJSON(struct_({field("a", decimal128(6, 1)), field("b", decimal128(7, 2))}),
R"([
{"a": "0.7", "b": "-2.17"},
{"a": "2.9", "b": "9.11"},
{"a": "3.1", "b" : "12.31"}
])"));

// scale1 == scale2
ExpectExecute(
call("divide", {field_ref("a"), field_ref("b")}),
ArrayFromJSON(struct_({field("a", decimal128(6, 1)), field("b", decimal128(6, 1))}),
R"([
{"a": "0.7", "b": "-2.1"},
{"a": "2.9", "b": "9.1"},
{"a": "3.1", "b" : "12.3"}
])"));

// scale1 > scale2
ExpectExecute(
call("divide", {field_ref("a"), field_ref("b")}),
ArrayFromJSON(struct_({field("a", decimal128(6, 2)), field("b", decimal128(6, 1))}),
R"([
{"a": "0.71", "b": "-2.1"},
{"a": "2.92", "b": "9.1"},
{"a": "3.17", "b" : "12.3"}
])"));
}

void ExpectIdenticalIfUnchanged(Expression modified, Expression original) {
if (modified == original) {
// no change -> must be identical
Expand Down
8 changes: 2 additions & 6 deletions cpp/src/arrow/compute/kernels/codegen_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,9 @@ Status CastBinaryDecimalArgs(DecimalPromotion promotion, std::vector<TypeHolder>
right_scaleup = std::max(s1, s2) - s2;
break;
}
case DecimalPromotion::kMultiply: {
left_scaleup = right_scaleup = 0;
break;
}
case DecimalPromotion::kMultiply:
case DecimalPromotion::kDivide: {
left_scaleup = std::max(4, s1 + p2 - s2 + 1) + s2 - s1;
right_scaleup = 0;
left_scaleup = right_scaleup = 0;
break;
}
default:
Expand Down
9 changes: 2 additions & 7 deletions cpp/src/arrow/compute/kernels/scalar_arithmetic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -541,13 +541,8 @@ Result<TypeHolder> ResolveDecimalDivisionOutput(KernelContext*,
types,
[](int32_t p1, int32_t s1, int32_t p2,
int32_t s2) -> Result<std::pair<int32_t, int32_t>> {
if (s1 < s2) {
return Status::Invalid("Division of two decimal types scale1 < scale2. ", "(",
s1, s2, ").");
}
DCHECK_GE(s1, s2);
const int32_t scale = s1 - s2;
const int32_t precision = p1;
const int32_t scale = std::max(4, s1 + p2 - s2 + 1);
const int32_t precision = p1 - s1 + s2 + scale;
return std::make_pair(precision, scale);
});
}
Expand Down