EnricoMi commented on code in PR #48458:
URL: https://github.com/apache/arrow/pull/48458#discussion_r2616305054


##########
python/pyarrow/src/arrow/python/numpy_to_arrow.cc:
##########
@@ -365,14 +365,27 @@ Status CastBuffer(const std::shared_ptr<DataType>& 
in_type,
 }
 
 template <typename FromType, typename ToType>
-Status StaticCastBuffer(const Buffer& input, const int64_t length, MemoryPool* 
pool,
+Status StaticCastBuffer(const Buffer& input, int64_t length, MemoryPool* pool,
+                        const uint8_t* null_bitmap,
+                        const compute::CastOptions& cast_options,
                         std::shared_ptr<Buffer>* out) {
   ARROW_ASSIGN_OR_RAISE(auto result, AllocateBuffer(sizeof(ToType) * length, 
pool));
 
   auto in_values = reinterpret_cast<const FromType*>(input.data());
   auto out_values = reinterpret_cast<ToType*>(result->mutable_data());
+
+  constexpr FromType kMin = std::numeric_limits<ToType>::min();
+  constexpr FromType kMax = std::numeric_limits<ToType>::max();
+
   for (int64_t i = 0; i < length; ++i) {
-    *out_values++ = static_cast<ToType>(*in_values++);
+    FromType value = *in_values++;
+    // Skip overflow check for null values
+    bool is_null = (null_bitmap != nullptr) && !bit_util::GetBit(null_bitmap, 
i);
+    if (!is_null && !cast_options.allow_int_overflow && (value < kMin || value 
> kMax)) {
+      return Status::Invalid("Integer value ", value, " out of bounds for int",
+                             sizeof(ToType) * 8, " conversion at index ", i);
+    }

Review Comment:
   We can move the `cast_options.allow_int_overflow` check to skip calling 
`bit_util::GetBit(null_bitmap, i)` if true:
   
   ```suggestion
       // Skip overflow check when cast_options.allow_int_overflow is true and 
for null values
       bool check_overflow = !cast_options.allow_int_overflow && ( (null_bitmap 
== nullptr) || bit_util::GetBit(null_bitmap, i) );
       if (check_overflow && (value < kMin || value > kMax)) {
         return Status::Invalid("Integer value ", value, " out of bounds for 
int",
                                sizeof(ToType) * 8, " conversion at index ", i);
       }
   ```



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