Repository navigation
GH-48003: [C++][Compute] Multiply and divide durations by float64 #55460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
fc2644b
63195d8
0c84d36
9f92fc0
c1d1500
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -785,7 +785,7 @@ struct ArithmeticFunction : ScalarFunction { | |
|
|
||
| if (name_ == "multiply" || name_ == "multiply_checked" || name_ == "divide" || | ||
| name_ == "divide_checked") { | ||
| PromoteIntegerForDurationArithmetic(types); | ||
| PromoteNumericForDurationArithmetic(types); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1126,6 +1126,44 @@ void AddArithmeticFunctionDurationTime(std::shared_ptr<ScalarFunction> func) { | |
| std::move(exec_4))); | ||
| } | ||
|
|
||
| // Apply Op in double precision and truncate toward zero, like numpy. Durations | ||
| // beyond 2^53 lose precision in the conversion to double. | ||
| // NaN and out of range results are errors, also in the unchecked functions, where | ||
| // integer overflow wraps around instead. numpy returns NaT for these, but Arrow has | ||
| // no NaT, and casting an out of range double to int64 is undefined behaviour. | ||
| 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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 numpy also gives 0 for I'm fine with keeping truncation for numpy compatibility, but please document this caveat. @randolf-scholz does truncation work for your use case?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| // 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"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The unchecked variants raise here, which differs from their neighbours: unchecked It also differs from numpy, which returns Raising seems reasonable to me, since an out of range
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| 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. | ||
| // Other floating point types are cast to float64 in DispatchBest. | ||
| 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)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only an exact All integer widths work because
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| if constexpr (std::is_same_v<Op, Multiply>) { | ||
| exec = ScalarBinaryNotNull<Int64Type, DoubleType, Int64Type, | ||
| DurationByFloat<Op>>::Exec; | ||
| DCHECK_OK(func->AddKernel({float64(), duration(unit)}, duration(unit), exec)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const FunctionDoc absolute_value_doc{ | ||
| "Calculate the absolute value of the argument element-wise", | ||
| ("Results will wrap around on integer overflow.\n" | ||
|
|
@@ -1166,7 +1204,10 @@ const FunctionDoc sub_checked_doc{ | |
| const FunctionDoc mul_doc{"Multiply the arguments element-wise", | ||
| ("Results will wrap around on integer overflow.\n" | ||
| "Use function \"multiply_checked\" if you want overflow\n" | ||
| "to return an error."), | ||
| "to return an error.\n" | ||
| "A duration multiplied by a floating-point number is\n" | ||
| "computed in float64 and truncated toward zero, and\n" | ||
| "overflow and NaN always return an error."), | ||
| {"x", "y"}}; | ||
|
|
||
| const FunctionDoc mul_checked_doc{ | ||
|
|
@@ -1180,7 +1221,10 @@ const FunctionDoc div_doc{ | |
| ("Integer division by zero returns an error. However, integer overflow\n" | ||
| "wraps around, and floating-point division by zero returns an infinite.\n" | ||
| "Use function \"divide_checked\" if you want to get an error\n" | ||
| "in all the aforementioned cases."), | ||
| "in all the aforementioned cases.\n" | ||
| "A duration divided by a floating-point number is computed in float64\n" | ||
| "and truncated toward zero, and division by zero, overflow and NaN\n" | ||
| "always return an error."), | ||
| {"dividend", "divisor"}}; | ||
|
|
||
| const FunctionDoc div_checked_doc{ | ||
|
|
@@ -1711,6 +1755,7 @@ void RegisterScalarArithmetic(FunctionRegistry* registry) { | |
| DCHECK_OK(multiply->AddKernel({duration(unit), int64()}, duration(unit), exec)); | ||
| DCHECK_OK(multiply->AddKernel({int64(), duration(unit)}, duration(unit), exec)); | ||
| } | ||
| AddDurationByFloatKernels<Multiply>(multiply.get()); | ||
|
|
||
| DCHECK_OK(registry->AddFunction(std::move(multiply))); | ||
|
|
||
|
|
@@ -1727,6 +1772,7 @@ void RegisterScalarArithmetic(FunctionRegistry* registry) { | |
| DCHECK_OK( | ||
| multiply_checked->AddKernel({int64(), duration(unit)}, duration(unit), exec)); | ||
| } | ||
| AddDurationByFloatKernels<Multiply>(multiply_checked.get()); | ||
|
|
||
| DCHECK_OK(registry->AddFunction(std::move(multiply_checked))); | ||
|
|
||
|
|
@@ -1740,6 +1786,7 @@ void RegisterScalarArithmetic(FunctionRegistry* registry) { | |
| DCHECK_OK( | ||
| divide->AddKernel({duration(unit), int64()}, duration(unit), std::move(exec))); | ||
| } | ||
| AddDurationByFloatKernels<DivideChecked>(divide.get()); | ||
|
|
||
| // Add divide(duration, duration) -> float64 | ||
| for (auto unit : TimeUnit::values()) { | ||
|
|
@@ -1761,6 +1808,7 @@ void RegisterScalarArithmetic(FunctionRegistry* registry) { | |
| DCHECK_OK(divide_checked->AddKernel({duration(unit), int64()}, duration(unit), | ||
| std::move(exec))); | ||
| } | ||
| AddDurationByFloatKernels<DivideChecked>(divide_checked.get()); | ||
|
|
||
| // Add divide_checked(duration, duration) -> float64 | ||
| for (auto unit : TimeUnit::values()) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1941,6 +1941,23 @@ 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.multiply(0.5, x).equals(half) | ||
| assert pc.multiply(x, pa.scalar(0.5, pa.float32())).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"))) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added Randolf's round trip and |
||
|
|
||
| # Round trip through float64 from GH-48003 | ||
| 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) | ||
|
|
||
|
|
||
| def test_arithmetic_remainder_modulo(): | ||
| left = pa.array([7, -7, 7, -7, None]) | ||
| right = pa.array([3, 3, -3, -3, 3]) | ||
|
|
||
There was a problem hiding this comment.
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: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.
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.