lidavidm commented on a change in pull request #11045:
URL: https://github.com/apache/arrow/pull/11045#discussion_r700199554



##########
File path: cpp/src/arrow/compute/kernels/scalar_cast_test.cc
##########
@@ -587,6 +589,36 @@ TEST(Cast, Decimal256ToInt) {
   CheckCast(negative_scale, ArrayFromJSON(int64(), "[1234567890000, 
-120000]"), options);
 }
 
+TEST(Cast, IntegerToDecimal) {
+  for (auto decimal_type : {decimal128(21, 2), decimal256(21, 2)}) {
+    for (auto integer_type : kIntegerTypes) {
+      CheckCast(
+          ArrayFromJSON(integer_type, "[0, 7, null, 100, 99]"),
+          ArrayFromJSON(decimal_type, R"(["0.00", "7.00", null, "100.00", 
"99.00"])"));
+    }
+  }
+
+  // extreme value
+  for (auto decimal_type : {decimal128(19, 0), decimal256(19, 0)}) {
+    CheckCast(ArrayFromJSON(int64(), "[-9223372036854775808, 
9223372036854775807]"),
+              ArrayFromJSON(decimal_type,
+                            R"(["-9223372036854775808", 
"9223372036854775807"])"));
+    CheckCast(ArrayFromJSON(uint64(), "[0, 18446744073709551615]"),
+              ArrayFromJSON(decimal_type, R"(["0", "18446744073709551615"])"));
+  }
+
+  // insufficient output precision
+  {
+    CastOptions options;
+
+    options.to_type = decimal128(5, 3);
+    CheckCastFails(ArrayFromJSON(int8(), "[0]"), options);
+
+    options.to_type = decimal256(76, 67);
+    CheckCastFails(ArrayFromJSON(int32(), "[0]"), options);
+  }

Review comment:
       Should we file a follow up issue to allow truncation via an unsafe cast 
in these cases?

##########
File path: cpp/src/arrow/compute/kernels/scalar_cast_numeric.cc
##########
@@ -391,6 +391,49 @@ struct CastFunctor<O, I,
   }
 };
 
+// ----------------------------------------------------------------------
+// Integer to decimal
+
+struct IntegerToDecimal {
+  template <typename OutValue, typename IntegerType>
+  OutValue Call(KernelContext*, IntegerType val, Status* st) const {
+    auto maybe_decimal = OutValue(val).Rescale(0, out_scale_);
+    if (ARROW_PREDICT_TRUE(maybe_decimal.ok())) {
+      return maybe_decimal.MoveValueUnsafe();
+    }
+    *st = maybe_decimal.status();
+    return OutValue{};
+  }
+
+  int32_t out_scale_;
+};
+
+template <typename O, typename I>
+struct CastFunctor<O, I,
+                   enable_if_t<is_decimal_type<O>::value && 
is_integer_type<I>::value>> {
+  static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
+    const auto& out_type = checked_cast<const O&>(*out->type());
+    const auto out_scale = out_type.scale();
+    const auto out_precision = out_type.precision();
+
+    // verify precision and scale
+    if (out_scale < 0) {
+      return Status::Invalid("Scale must be non-negative");
+    }
+    // maximal number of decimal digits for int8/16/32/64
+    constexpr std::array<int, 4> decimal_digits{3, 5, 10, 19};
+    using ctype = typename I::c_type;
+    static_assert(sizeof(ctype) <= 8, "");
+    if (out_precision < decimal_digits[BitUtil::Log2(sizeof(ctype))] + 
out_scale) {
+      return Status::Invalid("Invalid output precision and scale");

Review comment:
       nit: in the error message, it might be good to clarify that 1) the 
precision is not great enough for the requested type and 2) include the minimal 
precision necessary.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to