You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/07/02 20:42:26 UTC

[GitHub] [arrow] wesm commented on a change in pull request #7612: ARROW-7011: [C++] Implement casts from float/double to decimal

wesm commented on a change in pull request #7612:
URL: https://github.com/apache/arrow/pull/7612#discussion_r449248816



##########
File path: cpp/src/arrow/compute/kernels/scalar_cast_numeric.cc
##########
@@ -467,6 +467,51 @@ struct CastFunctor<Decimal128Type, Decimal128Type> {
   }
 };
 
+// ----------------------------------------------------------------------
+// Real to decimal
+
+template <bool AllowTruncate>
+struct RealToDecimal {
+  template <typename OUT, typename RealType>
+  Decimal128 Call(KernelContext* ctx, RealType val) const {
+    auto result = Decimal128::FromReal(val, out_precision_, out_scale_);
+    if (ARROW_PREDICT_FALSE(!result.ok())) {
+      if (!AllowTruncate) {
+        ctx->SetStatus(result.status());
+      }
+      return Decimal128();  // Zero
+    } else {
+      return *std::move(result);
+    }
+  }
+
+  int32_t out_scale_, out_precision_;
+};
+
+using SafeRealToDecimal = RealToDecimal<true>;
+using UnsafeRealToDecimal = RealToDecimal<false>;

Review comment:
       I'm not sure the performance benefit of pruning the `AllowTruncate` branch merits the 2x code size -- what do you think about making this flag just a member of the functor?




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org