-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add truncate(x,n) Presto function #2892
Conversation
✅ Deploy Preview for meta-velox canceled.
|
This pull request was exported from Phabricator. Differential Revision: D40516665 |
velox/docs/functions/math.rst
Outdated
@@ -134,6 +134,10 @@ Mathematical Functions | |||
|
|||
Returns x rounded to integer by dropping digits after decimal point. | |||
|
|||
.. function:: truncate(x,n) -> double |
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.
nit: white-space between the arguments
@@ -487,8 +487,8 @@ struct EulerConstantFunction { | |||
template <typename T> | |||
struct TruncateFunction { | |||
template <typename TInput> | |||
FOLLY_ALWAYS_INLINE void call(TInput& result, TInput a) { | |||
result = std::trunc(a); | |||
FOLLY_ALWAYS_INLINE void call(TInput& result, TInput a, int32_t n = 0) { |
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.
I wonder if we should provide two overloads for these functions for performance, so we can remove the branch from the common case (which is probably a single parameter).
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.
@pedroerp Will that imply adding the overload to TruncateFunction class and adding a separate overload registration(unary) in the registrar?
if (std::isnan(number) || std::isinf(number) || decimals == 0) { | ||
return std::trunc(number); | ||
} | ||
const auto factor = std::pow(10, decimals); |
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.
is this how this is implemented in the java codebase?
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.
Not exactly. The java implementation relies on BigDecimal(setScale, valueOf etc.) and not very simple logic incapsulated in it. To my research we don't possess similar functionality in c++. Unless we want to implemet/grab BigDecimal like utilities from somewhere this seems to be the quickest way of having the function available(though with the risk of mismatches in some cases).
Side question, do we have a A/B engine tests setup to test between presto and velox operators?
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.
We support Decimal types in Velox (both Big and Small). The functionality should be similar but the API might not be exactly the same.
Side question, do we have a A/B engine tests setup to test between presto and velox operators?
There's a test suite in Prestissimo that validates that C++ and Java versions return the same results, but the queries need to be manually written there. We should make sure we add something covering truncate behavior as well.
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.
So we are interested in mimicking this line https://github.com/prestodb/presto/blob/ba9ec306d453f968f711e437ab2d60b91dc6b700/presto-main/src/main/java/com/facebook/presto/operator/scalar/MathFunctions.java#L380. Specifically:
- valueOf() - https://github.com/openjdk-mirror/jdk7u-jdk/blob/f4d80957e89a19a29bb9f9807d2a28351ed7f7df/src/share/classes/java/math/BigDecimal.java#L806
- setScale() - https://github.com/openjdk-mirror/jdk7u-jdk/blob/f4d80957e89a19a29bb9f9807d2a28351ed7f7df/src/share/classes/java/math/BigDecimal.java#L2378
- doubleValue() - https://github.com/openjdk-mirror/jdk7u-jdk/blob/f4d80957e89a19a29bb9f9807d2a28351ed7f7df/src/share/classes/java/math/BigDecimal.java#L3132
- And toString as an implementation detail
We have:
- UnscaledLongDecimal -
velox/velox/type/UnscaledLongDecimal.h
Line 55 in a19a652
struct UnscaledLongDecimal { - UnscaledShortDecimal -
velox/velox/type/UnscaledShortDecimal.h
Line 26 in a19a652
struct UnscaledShortDecimal { - DecimalUtils with rescale and toString -
velox/velox/type/DecimalUtil.h
Line 28 in a19a652
class DecimalUtil { - DECIMAL type -
Line 1105 in a19a652
TypePtr DECIMAL(uint8_t precision, uint8_t scale);
Looking into how to leverage those to mimick Java behavior.
This pull request was exported from Phabricator. Differential Revision: D40516665 |
fb66b98
to
4a1fa08
Compare
Summary: Pull Request resolved: facebookincubator#2892 Adding truncate(x,n) Presto function Differential Revision: D40516665 fbshipit-source-id: 75ac1ca44dfc354c9c809a082bfd3352d9c729dd
This pull request was exported from Phabricator. Differential Revision: D40516665 |
4a1fa08
to
831af7b
Compare
Summary: Pull Request resolved: facebookincubator#2892 Adding truncate(x,n) Presto function Differential Revision: D40516665 fbshipit-source-id: e076863863fb8b54d6960eb479d7f1447c708486
This pull request was exported from Phabricator. Differential Revision: D40516665 |
831af7b
to
b0f16b1
Compare
Summary: Pull Request resolved: facebookincubator#2892 Adding truncate(x,n) Presto function Differential Revision: D40516665 fbshipit-source-id: 1d3eeb2587f32a437cd093351d444911a8e5fcd3
Summary: Pull Request resolved: facebookincubator#2892 Adding truncate(x,n) Presto function Differential Revision: D40516665 fbshipit-source-id: 295451a2a6de62bb6f4c126a326db564f8947066
b0f16b1
to
c1e9493
Compare
This pull request was exported from Phabricator. Differential Revision: D40516665 |
Summary: Pull Request resolved: facebookincubator#2892 Adding truncate(x,n) Presto function Differential Revision: D40516665 fbshipit-source-id: 50fecc52801024b6b4c001c818b492aaa776b39a
This pull request was exported from Phabricator. Differential Revision: D40516665 |
c1e9493
to
7869c54
Compare
Summary: Pull Request resolved: facebookincubator#2892 Adding truncate(x,n) Presto function Differential Revision: D40516665 fbshipit-source-id: 49cd2c9dad87c63a940530c41aa192a2d3440f37
This pull request was exported from Phabricator. Differential Revision: D40516665 |
7869c54
to
dc8cfcf
Compare
Summary: Adding truncate(x,n) Presto function
Differential Revision: D40516665