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


##########
cpp/src/arrow/util/decimal.cc:
##########
@@ -742,6 +1106,78 @@ static inline uint64_t UInt64FromBigEndian(const uint8_t* 
bytes, int32_t length)
   return ::arrow::bit_util::FromBigEndian(result);
 }
 
+Result<Decimal32> Decimal32::FromBigEndian(const uint8_t* bytes, int32_t 
length) {
+  static constexpr int32_t kMinDecimalBytes = 1;
+  static constexpr int32_t kMaxDecimalBytes = 4;
+
+  if (ARROW_PREDICT_FALSE(length < kMinDecimalBytes || length > 
kMaxDecimalBytes)) {
+    return Status::Invalid("Length of byte array passed to 
Decimal32::FromBigEndian was ",
+                           length, ", but must be between ", kMinDecimalBytes, 
" and ",
+                           kMaxDecimalBytes);
+  }
+
+  const bool is_negative = static_cast<int8_t>(bytes[0]) < 0;
+
+  uint32_t result = 0;
+  memcpy(reinterpret_cast<uint8_t*>(&result) + kMaxDecimalBytes - length, 
bytes, length);
+
+  const auto value = bit_util::FromBigEndian(result);
+  const int32_t bits_offset = std::max(0, length - kMaxDecimalBytes);
+  if (bits_offset == 8) {

Review Comment:
   i've added some comments to better explain this



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