Copilot commented on code in PR #51480:
URL: https://github.com/apache/arrow/pull/51480#discussion_r4090380551
##########
python/pyarrow/src/arrow/python/numpy_to_arrow.cc:
##########
@@ -466,6 +467,31 @@ inline Status
NumPyConverter::PrepareInputData(std::shared_ptr<Buffer>* data) {
*data = std::make_shared<NumPyBuffer>(reinterpret_cast<PyObject*>(arr_));
}
+ if (dtype_->type_num == NPY_DATETIME || dtype_->type_num == NPY_TIMEDELTA) {
+ auto metadata =
+
reinterpret_cast<PyArray_DatetimeDTypeMetaData*>(PyDataType_C_METADATA(dtype_));
+ const int64_t multiplier = metadata->meta.num;
+ if (multiplier != 1) {
+ ARROW_ASSIGN_OR_RAISE(auto scaled,
+ AllocateBuffer(sizeof(int64_t) * length_, pool_));
+ auto in_values = reinterpret_cast<const int64_t*>((*data)->data());
+ auto out_values = reinterpret_cast<int64_t*>(scaled->mutable_data());
+ for (int64_t i = 0; i < length_; ++i) {
+ if (null_bitmap_ && !bit_util::GetBit(null_bitmap_->data(), i)) {
+ out_values[i] = 0;
+ } else if (cast_options_.allow_time_overflow) {
+ out_values[i] =
+ static_cast<uint64_t>(in_values[i]) *
static_cast<uint64_t>(multiplier);
+ } else if (::arrow::internal::MultiplyWithOverflow(in_values[i],
multiplier,
Review Comment:
This check only validates the multiplication into the base unit. For inputs
such as `datetime64[10D]` converted to `date32` or `date64`, the subsequent
date conversion can still overflow: `StaticCastBuffer<int64_t, int32_t>` does
not check its narrowing conversion, and the `date64` path multiplies by
`kMillisecondsInDay` with signed arithmetic. Thus `safe=True` can still return
wrapped data or invoke signed-overflow UB, contrary to the stated overflow
behavior; please extend checked/defined overflow handling through those date
conversions as well.
--
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]