zeroshade commented on code in PR #43957:
URL: https://github.com/apache/arrow/pull/43957#discussion_r1773722574


##########
cpp/src/arrow/util/decimal.cc:
##########
@@ -697,8 +943,150 @@ Status DecimalFromString(const char* type_name, 
std::string_view s, Decimal* out
   return Status::OK();
 }
 
+template <typename DecimalClass>
+Status SimpleDecimalFromString(const char* type_name, std::string_view s,
+                               DecimalClass* out, int32_t* precision, int32_t* 
scale) {
+  if (s.empty()) {
+    return Status::Invalid("Empty string cannot be converted to ", type_name);
+  }
+
+  DecimalComponents dec;
+  if (!ParseDecimalComponents(s.data(), s.size(), &dec)) {
+    return Status::Invalid("The string '", s, "' is not a valid ", type_name, 
" number");
+  }
+
+  // count number of significant digits (without leading zeros)
+  size_t first_non_zero = dec.whole_digits.find_first_not_of('0');
+  size_t significant_digits = dec.fractional_digits.size();
+  if (first_non_zero != std::string::npos) {
+    significant_digits += dec.whole_digits.size() - first_non_zero;
+  }
+  int32_t parsed_precision = static_cast<int32_t>(significant_digits);
+
+  int32_t parsed_scale = 0;
+  if (dec.has_exponent) {
+    auto adjusted_exponent = dec.exponent;
+    parsed_scale =
+        -adjusted_exponent + 
static_cast<int32_t>(dec.fractional_digits.size());
+  } else {
+    parsed_scale = static_cast<int32_t>(dec.fractional_digits.size());
+  }
+
+  if (out != nullptr) {
+    if constexpr (std::is_same_v<Decimal32, DecimalClass>) {
+      uint32_t value{0};
+      ShiftAndAdd(dec.whole_digits, &value);
+      ShiftAndAdd(dec.fractional_digits, &value);
+      if (value > static_cast<uint32_t>(
+                      std::numeric_limits<typename 
DecimalClass::ValueType>::max())) {
+        return Status::Invalid("The string '", s, "' cannot be represented as 
",
+                               type_name);
+      }
+
+      *out = DecimalClass(value);
+      if (dec.sign == '-') {
+        out->Negate();
+      }
+    } else {

Review Comment:
   following the suggestion from 
https://github.com/apache/arrow/pull/43957#discussion_r1771711880, i was able 
to eliminate the branch there entirely. Now the `Decimal32` case just uses the 
`uint64_t` variant anyways, performs the overflow check and then constructs the 
decimal value from the `uint64_t` by downcasting. 
   
   So the codepaths have been unified



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