rok commented on code in PR #55460:
URL: https://github.com/apache/arrow/pull/55460#discussion_r4631254788


##########
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:
   `static_cast<double>` is only exact up to 2^53, so larger durations lose 
their low bits even when multiplied by 1.0:
   
   ```
   multiply(duration[ns] [9007199254740993], 1.0) -> [9007199254740992]
   ```
   
   2^53 ns is about 104 days, so this is easy to hit with `duration[ns]`. numpy 
behaves the same way, so I don't think we need to do better here, but it should 
be mentioned in the docs.
   



##########
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:
   Truncation matches numpy, but it makes the round trip from the issue lossy, 
because `n / unit * unit` often lands just below `n` in float64:
   
   ```
   d = [1, 2, 3, 100] (duration[s]), unit = 49s
   multiply(divide(d, unit), unit) -> [0, 1, 3, 100]
   multiply(100s, 0.29)            -> [28]
   ```
   
   numpy also gives 0 for `1 / 49 * 49`. Rounding to nearest would fix the 
round trip, but then the example from the issue would no longer match numpy 
(`3s * 0.5` would give 2 instead of 1).
   
   I'm fine with keeping truncation for numpy compatibility, but please 
document this caveat. @randolf-scholz does truncation work for your use case?
   



##########
python/pyarrow/tests/test_compute.py:
##########
@@ -1941,6 +1941,16 @@ def test_arithmetic_multiply():
     assert result.equals(expected)
 
 
+def test_arithmetic_multiply_divide_duration_by_float():
+    # Example from GH-48003. Results are truncated toward zero, like numpy.
+    x = pa.array([0, 1, 2, 3, 4, 5], type=pa.duration("s"))
+    half = pa.array([0, 0, 1, 1, 2, 2], type=pa.duration("s"))
+    assert pc.multiply(x, 0.5).equals(half)
+    assert pc.divide(x, 2.0).equals(half)
+    assert pc.multiply(x, 1.5).equals(
+        pa.array([0, 1, 3, 4, 6, 7], type=pa.duration("s")))

Review Comment:
   Could you also add the round trip from Randolf's second example in the 
issue? That is the actual use case:
   
   ```python
   deltas = pa.array([1, 2, 3], type=pa.duration("s"))
   unit = pa.scalar(1, type=pa.duration("s"))
   assert pc.multiply(pc.divide(deltas, unit), unit).equals(deltas)
   ```
   
   And one case with the float on the left, e.g. `pc.multiply(0.5, x)`.
   



##########
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:
   A few things for the docs:
   
   - The function docstrings (`mul_doc` and `div_doc` in 
`scalar_arithmetic.cc`) are what Python users see. They still say that 
`multiply` wraps on overflow and that floating-point division by zero returns 
an infinite. Neither holds for duration by float64, so please mention the 
exception there too.
   - "like numpy" only applies to the truncation; numpy returns NaT where we 
raise.
   - Please mention that durations beyond 2^53 lose precision, and that only 
float64 is accepted (unless we promote float32).
   



##########
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:
   The unchecked variants raise here, which differs from their neighbours: 
unchecked `multiply(duration, int64)` wraps on overflow and `divide(1.0, 0.0)` 
returns `inf`.
   
   It also differs from numpy, which returns `NaT` for `td * nan`, `td * inf` 
and `td / 0.0`. So "like numpy" only holds for the truncation.
   
   Raising seems reasonable to me, since an out of range `static_cast` is 
undefined behaviour and we have no NaT. Did you consider emitting null for the 
unchecked variants instead? Either way, please make the comment and the docs 
explicit about this.
   



##########
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:
   Only an exact `float64` matches. `multiply(duration[s], float32)` (and 
`halffloat`) still raises:
   
   ```
   NotImplemented: Function 'multiply' has no kernel matching input types 
(duration[s], float)
   ```
   
   All integer widths work because `PromoteIntegerForDurationArithmetic` casts 
them to int64 in `DispatchBest`. Could we promote floats to float64 there as 
well, and add a `CheckDispatchBest` case? If you would rather leave that for a 
follow-up, please add a test that pins the current behaviour and open an issue.
   



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