Skip to content

GH-48003: [C++][Compute] Multiply and divide durations by float64 - #55460

Open
JonSnow1807 wants to merge 5 commits into
apache:mainfrom
JonSnow1807:GH-48003-duration-float-multiply-min
Open

JonSnow1807 wants to merge 5 commits into
apache:mainfrom
JonSnow1807:GH-48003-duration-float-multiply-min

Conversation

@JonSnow1807

@JonSnow1807 JonSnow1807 commented Nov 9, 2026 •

Copy link
Copy Markdown

Rationale for this change

There is no kernel for multiply(duration, float64) or divide(duration, float64), so these calls raise NotImplemented (see #48003).

What changes are included in this PR?

Kernels for multiply(duration, float64), multiply(float64, duration) and divide(duration, float64), registered for multiply, multiply_checked, divide and divide_checked. The result keeps the duration’s unit and is truncated toward zero, like numpy and divide(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:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

Code drafted by both human and AI, and reviewed by human.

@JonSnow1807
JonSnow1807 requested a review from pitrou as a code owner November 9, 2026 22:18
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

⚠️ GitHub issue #48003 has been automatically assigned in GitHub to PR creator.

@rok

rok commented Nov 9, 2026

Copy link
Copy Markdown
Member

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.

@rok

rok commented Nov 9, 2026

Copy link
Copy Markdown
Member

Also - add a small Python test that will use example proposed by Randolf in #48003.

@JonSnow1807

Copy link
Copy Markdown
Author

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, divide(duration, int64)). From that, the kernel now uses the physical Int64Type like its neighbours, the test also covers the lower int64 bound, and the docs say the computation is done in float64. I switched to truncation toward zero, so Randolf’s example now gives exactly numpy’s output, and I added it as a Python test. I’ve gone through every line myself. Also, to correct my earlier comment: float32 and float16 are not cast in this PR.

@rok

rok commented Nov 10, 2026

Copy link
Copy Markdown
Member

Thank you. I'll try to take a look tomorrow.
cc @randolf-scholz

@rok rok left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread docs/source/cpp/compute.rst Outdated
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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added Randolf's round trip and pc.multiply(0.5, x), plus a float32 case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants