JonSnow1807 commented on code in PR #55460:
URL: https://github.com/apache/arrow/pull/55460#discussion_r4646224400
##########
cpp/src/arrow/compute/kernels/scalar_arithmetic.cc:
##########
@@ -1126,6 +1126,40 @@ void
AddArithmeticFunctionDurationTime(std::shared_ptr<ScalarFunction> func) {
std::move(exec_4)));
}
+// Apply Op in double precision and truncate toward zero, like numpy.
+// NaN and out of range results are errors.
+template <typename Op>
+struct DurationByFloat {
+ template <typename T, typename Arg0, typename Arg1>
+ static T Call(KernelContext* ctx, Arg0 left, Arg1 right, Status* st) {
+ double value = Op::template Call<double, double, double>(
+ ctx, static_cast<double>(left), static_cast<double>(right), st);
Review Comment:
Documented in the kernel comment and compute.rst: durations beyond 2^53 lose
precision when converted to float64, as in numpy.
##########
cpp/src/arrow/compute/kernels/scalar_arithmetic.cc:
##########
@@ -1126,6 +1126,40 @@ void
AddArithmeticFunctionDurationTime(std::shared_ptr<ScalarFunction> func) {
std::move(exec_4)));
}
+// Apply Op in double precision and truncate toward zero, like numpy.
+// NaN and out of range results are errors.
+template <typename Op>
+struct DurationByFloat {
+ template <typename T, typename Arg0, typename Arg1>
+ static T Call(KernelContext* ctx, Arg0 left, Arg1 right, Status* st) {
+ double value = Op::template Call<double, double, double>(
+ ctx, static_cast<double>(left), static_cast<double>(right), st);
+ double truncated = std::trunc(value);
Review Comment:
Kept truncation and documented the caveat in compute.rst, with 1s / 49s ×
49s coming back as 0s.
##########
cpp/src/arrow/compute/kernels/scalar_arithmetic.cc:
##########
@@ -1126,6 +1126,40 @@ void
AddArithmeticFunctionDurationTime(std::shared_ptr<ScalarFunction> func) {
std::move(exec_4)));
}
+// Apply Op in double precision and truncate toward zero, like numpy.
+// NaN and out of range results are errors.
+template <typename Op>
+struct DurationByFloat {
+ template <typename T, typename Arg0, typename Arg1>
+ static T Call(KernelContext* ctx, Arg0 left, Arg1 right, Status* st) {
+ double value = Op::template Call<double, double, double>(
+ ctx, static_cast<double>(left), static_cast<double>(right), st);
+ double truncated = std::trunc(value);
+ // The int64 range is [-2^63, 2^63); the comparison is false for NaN
+ if (ARROW_PREDICT_FALSE(!(truncated >= -0x1p63 && truncated < 0x1p63))) {
+ *st = Status::Invalid(std::isnan(truncated) ? "result is NaN" :
"overflow");
Review Comment:
I kept raising: null would make an invalid result look like a missing value,
and there is no NaT. The comment, the docstrings and compute.rst now say this
differs from the unchecked integer kernels and from numpy.
##########
cpp/src/arrow/compute/kernels/scalar_arithmetic.cc:
##########
@@ -1126,6 +1126,40 @@ void
AddArithmeticFunctionDurationTime(std::shared_ptr<ScalarFunction> func) {
std::move(exec_4)));
}
+// Apply Op in double precision and truncate toward zero, like numpy.
+// NaN and out of range results are errors.
+template <typename Op>
+struct DurationByFloat {
+ template <typename T, typename Arg0, typename Arg1>
+ static T Call(KernelContext* ctx, Arg0 left, Arg1 right, Status* st) {
+ double value = Op::template Call<double, double, double>(
+ ctx, static_cast<double>(left), static_cast<double>(right), st);
+ double truncated = std::trunc(value);
+ // The int64 range is [-2^63, 2^63); the comparison is false for NaN
+ if (ARROW_PREDICT_FALSE(!(truncated >= -0x1p63 && truncated < 0x1p63))) {
+ *st = Status::Invalid(std::isnan(truncated) ? "result is NaN" :
"overflow");
+ return 0;
+ }
+ return static_cast<T>(truncated);
+ }
+};
+
+// Add Op(duration, float64) -> duration, and Op(float64, duration) ->
duration for
+// Multiply. The same kernels are used for the checked and unchecked functions.
+template <typename Op>
+void AddDurationByFloatKernels(ScalarFunction* func) {
+ for (auto unit : TimeUnit::values()) {
+ auto exec =
+ ScalarBinaryNotNull<Int64Type, Int64Type, DoubleType,
DurationByFloat<Op>>::Exec;
+ DCHECK_OK(func->AddKernel({duration(unit), float64()}, duration(unit),
exec));
Review Comment:
float32 and halffloat are now cast to float64 in DispatchBest, like integers
are cast to int64 (the helper is renamed to
`PromoteNumericForDurationArithmetic`), with CheckDispatchBest and execution
tests.
##########
docs/source/cpp/compute.rst:
##########
@@ -493,6 +493,9 @@ appropriately. Mixed decimal and floating-point arguments
will cast all
arguments to floating-point, while mixed decimal and integer arguments will
cast all arguments to decimals.
Mixed time resolution temporal inputs will be cast to finest input resolution.
+A duration multiplied or divided by a float64 is computed in float64, then
+truncated toward zero (like numpy); NaN, overflow and division by zero are
errors
+in all variants.
Review Comment:
The multiply and divide docstrings now describe the duration-by-float
exception, and compute.rst limits 'like numpy' to the truncation.
--
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]