AlenkaF commented on code in PR #48458:
URL: https://github.com/apache/arrow/pull/48458#discussion_r2682379625
##########
python/pyarrow/src/arrow/python/numpy_to_arrow.cc:
##########
@@ -364,15 +364,33 @@ Status CastBuffer(const std::shared_ptr<DataType>&
in_type,
return Status::OK();
}
+// Downcast buffer from FromType to ToType with optional overflow checking.
+// This function only supports narrowing casts (FromType wider than ToType).
+// Do not use this function for widening casts (ToType wider than FromType).
template <typename FromType, typename ToType>
-Status StaticCastBuffer(const Buffer& input, const int64_t length, MemoryPool*
pool,
- std::shared_ptr<Buffer>* out) {
+Status StaticDowncastBuffer(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++;
+ // Check overflow only when cast_options.allow_int_overflow is false and
value is not
+ // null
+ bool check_overflow = !cast_options.allow_int_overflow &&
+ ((null_bitmap == nullptr) ||
bit_util::GetBit(null_bitmap, i));
+ if (check_overflow && (value < kMin || value > kMax)) {
Review Comment:
Could we also test conversions with different masks?
--
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]