Repository navigation
GH-48003: [C++][Compute] Multiply and divide durations by float64 - #55460
JonSnow1807 wants to merge 5 commits into
Conversation
|
|
|
Please do adversarial review with your agents. Have them look at other kernels. Once you've done that please review this yourself such that you know what each line does, then ping me. |
|
Also - add a small Python test that will use example proposed by Randolf in #48003. |
|
Thanks for the guidance. I ran an adversarial review with two separate reviewers and compared the kernel against the existing ones (round, checked arithmetic, casts, |
|
Thank you. I'll try to take a look tomorrow. |
rok
left a comment
There was a problem hiding this comment.
Thanks for working on this @JonSnow1807. I posted some comments, please address them.
| 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); |
There was a problem hiding this 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.
There was a problem hiding this comment.
Documented in the kernel comment and compute.rst: durations beyond 2^53 lose precision when converted to float64, as in numpy.
| 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); |
There was a problem hiding this 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?
There was a problem hiding this comment.
Kept truncation and documented the caveat in compute.rst, with 1s / 49s × 49s coming back as 0s.
| 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"); |
There was a problem hiding this 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.
There was a problem hiding this 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.
| for (auto unit : TimeUnit::values()) { | ||
| auto exec = | ||
| ScalarBinaryNotNull<Int64Type, Int64Type, DoubleType, DurationByFloat<Op>>::Exec; | ||
| DCHECK_OK(func->AddKernel({duration(unit), float64()}, duration(unit), exec)); |
There was a problem hiding this 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.
There was a problem hiding this 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.
| 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. |
There was a problem hiding this comment.
A few things for the docs:
- The function docstrings (
mul_docanddiv_docinscalar_arithmetic.cc) are what Python users see. They still say thatmultiplywraps 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).
There was a problem hiding this comment.
The multiply and divide docstrings now describe the duration-by-float exception, and compute.rst limits 'like numpy' to the truncation.
| 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"))) |
There was a problem hiding this comment.
Could you also add the round trip from Randolf's second example in the issue? That is the actual use case:
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).
There was a problem hiding this comment.
Added Randolf's round trip and pc.multiply(0.5, x), plus a float32 case.
Rationale for this change
There is no kernel for
multiply(duration, float64)ordivide(duration, float64), so these calls raise NotImplemented (see #48003).What changes are included in this PR?
Kernels for
multiply(duration, float64),multiply(float64, duration)anddivide(duration, float64), registered formultiply,multiply_checked,divideanddivide_checked. The result keeps the duration’s unit and is truncated toward zero, like numpy anddivide(duration, int64). NaN, overflow and division by zero return an error. Only float64 is handled; float32 and float16 are not cast.Are these changes tested?
Yes, a new test in
scalar_temporal_test.cc(truncation, both argument orders, nulls, errors, all four time units) and a small Python test with the example from the issue.Are there any user-facing changes?
Yes, these calls now return a duration instead of raising NotImplemented.
Was AI used for this PR?
In accordance with the AI generation guidelines, please disclose below whether and how AI was used in this PR.
PR code and description written by:
Reviewed before submission by:
Code drafted by both human and AI, and reviewed by human.