-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-12814: [C++][Gandiva] Implements ABS, FLOOR, PI, SQRT, SIGN, LSHIFT, RSHIFT, CEIL, TRUNC, LN and LOG2 functions #10350
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
anthonylouisbsb
wants to merge
2
commits into
apache:main
from
s1mbi0se:feature/add-new-math-functions
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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 |
|---|---|---|
|
|
@@ -21,11 +21,18 @@ | |
| #include <gmock/gmock.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <cfloat> | ||
| #include <cmath> | ||
|
|
||
| #include "arrow/util/logging.h" | ||
| #include "gandiva/execution_context.h" | ||
|
|
||
| namespace gandiva { | ||
|
|
||
| void VerifyAlmostEquals(double actual, double expected, double max_error = FLT_EPSILON) { | ||
| EXPECT_TRUE(fabs(actual - expected) < max_error) << actual << " != " << expected; | ||
| } | ||
|
|
||
| TEST(TestGdvFnStubs, TestCrc32) { | ||
| gandiva::ExecutionContext ctx; | ||
| auto ctx_ptr = reinterpret_cast<int64_t>(&ctx); | ||
|
|
@@ -949,4 +956,59 @@ TEST(TestGdvFnStubs, TestMaskLastN) { | |
| EXPECT_EQ(expected, std::string(result, out_len)); | ||
| } | ||
|
|
||
| TEST(TestGdvFnStubs, TestAbs) { | ||
| gandiva::ExecutionContext ctx; | ||
| uint64_t ctx_ptr = reinterpret_cast<int64_t>(&ctx); | ||
|
|
||
| // Abs functions | ||
| EXPECT_EQ(gdv_fn_abs_int32(ctx_ptr, 0), 0); | ||
| EXPECT_EQ(gdv_fn_abs_uint32(ctx_ptr, 0), 0); | ||
| EXPECT_EQ(gdv_fn_abs_int64(ctx_ptr, 0), 0L); | ||
| EXPECT_EQ(gdv_fn_abs_uint64(ctx_ptr, 0), 0L); | ||
| VerifyAlmostEquals(gdv_fn_abs_float32(ctx_ptr, 0.0f), abs(0.0f)); | ||
| VerifyAlmostEquals(gdv_fn_abs_float64(ctx_ptr, 0.0), abs(0.0)); | ||
|
|
||
| EXPECT_EQ(gdv_fn_abs_int32(ctx_ptr, (INT32_MIN + 1)), abs(INT32_MIN + 1)); | ||
| EXPECT_EQ(gdv_fn_abs_int64(ctx_ptr, (INT64_MIN + 1)), abs(INT64_MIN + 1)); | ||
| VerifyAlmostEquals(gdv_fn_abs_float32(ctx_ptr, static_cast<float>(INT32_MIN + 1)), | ||
| abs(static_cast<float>(INT32_MIN + 1))); | ||
| VerifyAlmostEquals(gdv_fn_abs_float64(ctx_ptr, static_cast<double>(INT32_MIN + 1)), | ||
| abs(static_cast<double>(INT32_MIN + 1))); | ||
|
|
||
| VerifyAlmostEquals(gdv_fn_abs_float32(ctx_ptr, std::numeric_limits<float>::max()), | ||
| abs(std::numeric_limits<float>::max())); | ||
| VerifyAlmostEquals(gdv_fn_abs_float32(ctx_ptr, std::numeric_limits<float>::min()), | ||
| abs(std::numeric_limits<float>::min())); | ||
| VerifyAlmostEquals(gdv_fn_abs_float64(ctx_ptr, std::numeric_limits<double>::max()), | ||
| abs(std::numeric_limits<double>::max())); | ||
| VerifyAlmostEquals(gdv_fn_abs_float64(ctx_ptr, std::numeric_limits<double>::min()), | ||
| abs(std::numeric_limits<double>::min())); | ||
|
|
||
| EXPECT_EQ(gdv_fn_abs_int64(ctx_ptr, (INT64_MIN + 1)), | ||
| abs(static_cast<double>(INT64_MIN + 1))); | ||
| VerifyAlmostEquals(gdv_fn_abs_float64(ctx_ptr, static_cast<double>(INT64_MIN + 1)), | ||
| abs(static_cast<double>(INT64_MIN + 1))); | ||
|
|
||
|
||
| VerifyAlmostEquals(gdv_fn_abs_float32(ctx_ptr, -3600.50f), abs(-3600.50f)); | ||
| VerifyAlmostEquals(gdv_fn_abs_float64(ctx_ptr, -3600.50), abs(-3600.50)); | ||
| } | ||
|
|
||
| TEST(TestGdvFnStubs, TestAbsOverflow) { | ||
| gandiva::ExecutionContext ctx; | ||
| uint64_t ctx_ptr = reinterpret_cast<int64_t>(&ctx); | ||
|
|
||
| int64_t value = gdv_fn_abs_int64(ctx_ptr, INT64_MIN); | ||
| EXPECT_TRUE(ctx.has_error()); | ||
| EXPECT_THAT(ctx.get_error(), ::testing::HasSubstr("Overflow in abs execution")); | ||
| EXPECT_EQ(value, 0); | ||
|
|
||
| ctx.Reset(); | ||
|
|
||
| int32_t value_int32 = gdv_fn_abs_int32(ctx_ptr, INT32_MIN); | ||
| EXPECT_TRUE(ctx.has_error()); | ||
| EXPECT_THAT(ctx.get_error(), ::testing::HasSubstr("Overflow in abs execution")); | ||
| EXPECT_EQ(value_int32, 0); | ||
|
|
||
| ctx.Reset(); | ||
| } | ||
| } // namespace gandiva | ||
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
Oops, something went wrong.
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.
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.
Does it mean that we will accept the loss of digits in INT64?
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.
@kiszk Thanks for your revision! Please, could you explain how would occur digit loss here? I did not understand what you said.
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.
Sorry, I thought the previous version of abs is to cast int64 to double long. But, now,
gdv_fn_abs_int64handles as int64. Correct?Uh oh!
There was an error while loading. Please reload this page.
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.
@kiszk Yes, this line has the changes I made in the function. I handle the number as int64 and I add a check similar to the compute kernel to avoid overflow when getting the absolute value for the
INT64_MINvalue.