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



##########
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{};

Review comment:
       Nit: Other casting kernels simply use
   ```c++
   return {};  // zero
   ```

##########
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};

Review comment:
       Since `decimal_digits` is a constant array, I would make it a `static` 
data member of `CastFunctor`. Also, you can use a standard array where the size 
would be implicit and is more lightweight than `std::array`:
   ```c++
   static constexpr int decimal_digits[]{3,5, 10, 19};
   ```

##########
File path: cpp/src/arrow/compute/kernels/scalar_cast_numeric.cc
##########
@@ -641,6 +684,12 @@ std::shared_ptr<CastFunction> GetCastToDecimal128() {
   DCHECK_OK(func->AddKernel(Type::DOUBLE, {float64()}, sig_out_ty,
                             CastFunctor<Decimal128Type, DoubleType>::Exec));
 
+  // Cast from integer
+  for (const std::shared_ptr<DataType>& in_ty : IntTypes()) {
+    auto exec = GenerateInteger<CastFunctor, Decimal128Type>(in_ty->id());
+    DCHECK_OK(func->AddKernel(in_ty->id(), {in_ty}, sig_out_ty, 
std::move(exec)));
+  }

Review comment:
       No need to `std::move(exec)` as it is already an rvalue (function 
pointer).

##########
File path: cpp/src/arrow/compute/kernels/scalar_cast_numeric.cc
##########
@@ -664,6 +713,12 @@ std::shared_ptr<CastFunction> GetCastToDecimal256() {
   DCHECK_OK(func->AddKernel(Type::DOUBLE, {float64()}, sig_out_ty,
                             CastFunctor<Decimal256Type, DoubleType>::Exec));
 
+  // Cast from integer
+  for (const std::shared_ptr<DataType>& in_ty : IntTypes()) {
+    auto exec = GenerateInteger<CastFunctor, Decimal256Type>(in_ty->id());
+    DCHECK_OK(func->AddKernel(in_ty->id(), {in_ty}, sig_out_ty, 
std::move(exec)));
+  }

Review comment:
       Same here, no need to `std::move(exec)`

##########
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};

Review comment:
       Agree, local variable is better in this scenario.




-- 
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