Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.3k
ARROW-12685: [C++][Compute] Add unary absolute value kernel#10274
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
Closed
edponce
wants to merge
3
commits into
apache:master
from
edponce:ARROW-12685-Compute-Add-unary-absolute-value-kernel
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 106 additions & 2 deletions
108 cpp/src/arrow/compute/kernels/scalar_arithmetic_test.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -933,7 +933,7 @@ TEST(TestBinaryArithmetic, AddWithImplicitCastsUint64EdgeCase) { | ||
| } | ||
| TEST(TestUnaryArithmetic, DispatchBest) { | ||
| for (std::string name : {"negate"}) { | ||
| for (std::string name : {"negate", "abs", "abs_checked"}) { | ||
| for (const auto& ty : {int8(), int16(), int32(), int64(), uint8(), uint16(), uint32(), | ||
| uint64(), float32(), float64()}) { | ||
| CheckDispatchBest(name, {ty}, {ty}); | ||
| @@ -948,7 +948,7 @@ TEST(TestUnaryArithmetic, DispatchBest) { | ||
| } | ||
| } | ||
| for (std::string name : {"negate", "negate_checked"}) { | ||
| for (std::string name : {"negate", "negate_checked", "abs", "abs_checked"}) { | ||
| CheckDispatchFails(name, {null()}); | ||
| } | ||
| } | ||
| @@ -1057,5 +1057,109 @@ TYPED_TEST(TestUnaryArithmeticFloating, Negate) { | ||
| } | ||
| } | ||
| TYPED_TEST(TestUnaryArithmeticSigned, AbsoluteValue) { | ||
| using CType = typename TestFixture::CType; | ||
| auto min = std::numeric_limits<CType>::min(); | ||
| auto max = std::numeric_limits<CType>::max(); | ||
| for (auto check_overflow : {false, true}) { | ||
| this->SetOverflowCheck(check_overflow); | ||
| // Empty array | ||
| this->AssertUnaryOp(AbsoluteValue, "[]", "[]"); | ||
| // Scalar/arrays with nulls | ||
| this->AssertUnaryOp(AbsoluteValue, "[null]", "[null]"); | ||
| this->AssertUnaryOp(AbsoluteValue, "[1, null, -10]", "[1, null, 10]"); | ||
| this->AssertUnaryOp(AbsoluteValue, this->MakeNullScalar(), this->MakeNullScalar()); | ||
| // Scalar/arrays with zeros | ||
| this->AssertUnaryOp(AbsoluteValue, "[0, -0]", "[0, 0]"); | ||
| this->AssertUnaryOp(AbsoluteValue, -0, 0); | ||
| this->AssertUnaryOp(AbsoluteValue, 0, 0); | ||
| // Ordinary scalar/arrays (positive inputs) | ||
| this->AssertUnaryOp(AbsoluteValue, "[1, 10, 127]", "[1, 10, 127]"); | ||
| this->AssertUnaryOp(AbsoluteValue, 1, 1); | ||
| this->AssertUnaryOp(AbsoluteValue, this->MakeScalar(1), this->MakeScalar(1)); | ||
| // Ordinary scalar/arrays (negative inputs) | ||
| this->AssertUnaryOp(AbsoluteValue, "[-1, -10, -127]", "[1, 10, 127]"); | ||
| this->AssertUnaryOp(AbsoluteValue, -1, 1); | ||
| this->AssertUnaryOp(AbsoluteValue, MakeArray(-1), "[1]"); | ||
| // Min/max | ||
| this->AssertUnaryOp(AbsoluteValue, max, max); | ||
| if (check_overflow) { | ||
| this->AssertUnaryOpRaises(AbsoluteValue, MakeArray(min), "overflow"); | ||
| } else { | ||
| this->AssertUnaryOp(AbsoluteValue, min, min); | ||
| } | ||
| } | ||
| // Overflow should not be checked on underlying value slots when output would be null | ||
| this->SetOverflowCheck(true); | ||
| auto arg = ArrayFromJSON(this->type_singleton(), MakeArray(-1, max, min)); | ||
| arg = TweakValidityBit(arg, 1, false); | ||
| arg = TweakValidityBit(arg, 2, false); | ||
| this->AssertUnaryOp(AbsoluteValue, arg, "[1, null, null]"); | ||
| } | ||
| TYPED_TEST(TestUnaryArithmeticUnsigned, AbsoluteValue) { | ||
| using CType = typename TestFixture::CType; | ||
| auto min = std::numeric_limits<CType>::min(); | ||
| auto max = std::numeric_limits<CType>::max(); | ||
| for (auto check_overflow : {false, true}) { | ||
| this->SetOverflowCheck(check_overflow); | ||
| // Empty arrays | ||
| this->AssertUnaryOp(AbsoluteValue, "[]", "[]"); | ||
| // Array with nulls | ||
| this->AssertUnaryOp(AbsoluteValue, "[null]", "[null]"); | ||
| this->AssertUnaryOp(AbsoluteValue, this->MakeNullScalar(), this->MakeNullScalar()); | ||
| // Ordinary arrays | ||
| this->AssertUnaryOp(AbsoluteValue, "[0, 1, 10, 127]", "[0, 1, 10, 127]"); | ||
| // Min/max | ||
| this->AssertUnaryOp(AbsoluteValue, min, min); | ||
| this->AssertUnaryOp(AbsoluteValue, max, max); | ||
| } | ||
| } | ||
| TYPED_TEST(TestUnaryArithmeticFloating, AbsoluteValue) { | ||
| using CType = typename TestFixture::CType; | ||
| auto min = std::numeric_limits<CType>::lowest(); | ||
| auto max = std::numeric_limits<CType>::max(); | ||
| for (auto check_overflow : {false, true}) { | ||
| this->SetOverflowCheck(check_overflow); | ||
| // Empty array | ||
| this->AssertUnaryOp(AbsoluteValue, "[]", "[]"); | ||
| // Scalar/arrays with nulls | ||
| this->AssertUnaryOp(AbsoluteValue, "[null]", "[null]"); | ||
| this->AssertUnaryOp(AbsoluteValue, "[1.3, null, -10.80]", "[1.3, null, 10.80]"); | ||
| this->AssertUnaryOp(AbsoluteValue, this->MakeNullScalar(), this->MakeNullScalar()); | ||
| // Scalars/arrays with zeros | ||
| this->AssertUnaryOp(AbsoluteValue, "[0.0, -0.0]", "[0.0, 0.0]"); | ||
| this->AssertUnaryOp(AbsoluteValue, -0.0F, 0.0F); | ||
cyb70289 marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| this->AssertUnaryOp(AbsoluteValue, 0.0F, 0.0F); | ||
| // Ordinary scalars/arrays (positive inputs) | ||
| this->AssertUnaryOp(AbsoluteValue, "[1.3, 10.80, 12748.001]", | ||
| "[1.3, 10.80, 12748.001]"); | ||
| this->AssertUnaryOp(AbsoluteValue, 1.3F, 1.3F); | ||
| this->AssertUnaryOp(AbsoluteValue, this->MakeScalar(1.3F), this->MakeScalar(1.3F)); | ||
| // Ordinary scalars/arrays (negative inputs) | ||
| this->AssertUnaryOp(AbsoluteValue, "[-1.3, -10.80, -12748.001]", | ||
| "[1.3, 10.80, 12748.001]"); | ||
| this->AssertUnaryOp(AbsoluteValue, -1.3F, 1.3F); | ||
| this->AssertUnaryOp(AbsoluteValue, MakeArray(-1.3F), "[1.3]"); | ||
| // Arrays with infinites | ||
| this->AssertUnaryOp(AbsoluteValue, "[Inf, -Inf]", "[Inf, Inf]"); | ||
| // Arrays with NaNs | ||
| this->SetNansEqual(true); | ||
| this->AssertUnaryOp(AbsoluteValue, "[NaN]", "[NaN]"); | ||
| this->AssertUnaryOp(AbsoluteValue, "[-NaN]", "[NaN]"); | ||
| // Min/max | ||
| this->AssertUnaryOp(AbsoluteValue, min, max); | ||
| this->AssertUnaryOp(AbsoluteValue, max, max); | ||
| } | ||
| } | ||
| } // namespace compute | ||
| } // namespace arrow | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -260,6 +260,10 @@ an ``Invalid`` :class:`Status` when overflow is detected. | ||
| +--------------------------+------------+--------------------+---------------------+ | ||
| | Function name | Arity | Input types | Output type | | ||
| +==========================+============+====================+=====================+ | ||
| | abs | Unary | Numeric | Numeric | | ||
edponce marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| +--------------------------+------------+--------------------+---------------------+ | ||
| | abs_checked | Unary | Numeric | Numeric | | ||
| +--------------------------+------------+--------------------+---------------------+ | ||
| | add | Binary | Numeric | Numeric | | ||
| +--------------------------+------------+--------------------+---------------------+ | ||
| | add_checked | Binary | Numeric | Numeric | | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.