-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Fix #14693: support TRUNCATE function for DOUBLE and REAL #14783
Conversation
presto-main/src/test/java/com/facebook/presto/operator/scalar/TestMathFunctions.java
Outdated
Show resolved
Hide resolved
decimals = NUMBER_LENGTH; | ||
} | ||
} | ||
return floatToRawIntBits(new BigDecimal(Float.toString(numBitsToFloats)).setScale((int) decimals, BigDecimal.ROUND_DOWN).floatValue()); |
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.
return floatToRawIntBits(new BigDecimal(Float.toString(numBitsToFloats)).setScale((int) decimals, BigDecimal.ROUND_DOWN).floatValue()); | |
return floatToRawIntBits(new BigDecimal(numBitsToFloats).setScale((int) decimals, BigDecimal.ROUND_DOWN).floatValue()); |
Any reason why this had to be converted to a string? The test cases pass with the above.
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.
BigDecimal takes type of DOUBLE not FLOAT so use String as a safe cast value to return.
presto-main/src/main/java/com/facebook/presto/operator/scalar/MathFunctions.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/operator/scalar/MathFunctions.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/operator/scalar/MathFunctions.java
Outdated
Show resolved
Hide resolved
presto-main/src/test/java/com/facebook/presto/operator/scalar/TestMathFunctions.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/operator/scalar/MathFunctions.java
Outdated
Show resolved
Hide resolved
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.
The one with -1 as the second param are very confusing! Is it similar to any other system like Oracle or Mysql? |
sure will add spec doc accordingly |
Yes this is commonly used in the industry, Oracle has a spec here using -1: https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions200.htm |
@tdcmeehan Thanks for the comment, I have added the doc accordingly for this function. Please help give another review. many thanks! |
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.
Thanks @tdcmeehan ! |
@@ -327,6 +331,51 @@ public static long truncate(@SqlType(StandardTypes.REAL) long num) | |||
return floatToRawIntBits((float) (Math.signum(numInFloat) * Math.floor(Math.abs(numInFloat)))); | |||
} | |||
|
|||
@Description("truncate to double by dropping digits after decimal point") |
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.
If there's a limit for decimal point, I think we should be explicit about it and mention in description and in documentation. We should probably also fail the query if an out-of-range number is given, rather than silently changing the behavior.
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.
Though if the limit is actually limitation of double then this behavior is fine. But in that case I think we also don't have to special handle the max?
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.
Thanks for the review. Are you suggesting to document the max number of precision for double type in alignment with MAX_DECIMAL_PLACES
value? Yes the limit only handles the double/real type behavior. If the max limit reaches/exceeded, the outcome should be either 0.0 or negative value. I have 2 tests covered that:
assertFunction("truncate(DOUBLE '" + Double.MAX_VALUE + "', -408)", DOUBLE, 0.0); assertFunction("truncate(DOUBLE '" + -Double.MAX_VALUE / 10 + "', 408)", DOUBLE, -Double.MAX_VALUE / 10);
Is this what you were thinking to cover? @rongrong
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 mean add this to the rst file. Maybe also add it to the function description. So people know the maximum value for decimals should be within 400. Also document the behavior if the provided decimal is larger than 400. Why do we choose 400 as the max digit though? What's the logic behind this?
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.
Actually I double-checked the oracle doc which indicates that the scale of the decimal points ranges from -84 to 127. I will change 400 to 127 to align with oracle's behavior and document it in rst file.
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.
LGTM
can someone please help to merge the code or does it need another review? |
@rongrong assign this pr to you since you left the last comment, if all looks good please help coordinate to merge. many thanks! |
4685a6c
to
900b104
Compare
== RELEASE NOTES ==
The solution is to fix issue #14693 by adding an enhancement to TRUNCATE function for allowing DOUBLE and REAL types.
Following examples show how user may use this feature:
truncate(DOUBLE '1234.56', 1)
-> The outcome of the evaluation is1234.5
truncate(REAL '-12.333', -1)
-> The outcome of the evaluation is-10.0
NOTE: Neither does this patch change any behaviors of current mathemtical functionalities, nor is the existing workload being impacted. In addition, there is zero impact to data/workload migration.