Skip to content
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

Merged
merged 1 commit into from
Oct 13, 2020

Conversation

fgwang7w
Copy link
Member

@fgwang7w fgwang7w commented Jul 6, 2020

== RELEASE NOTES ==

General Changes

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 is 1234.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.

decimals = NUMBER_LENGTH;
}
}
return floatToRawIntBits(new BigDecimal(Float.toString(numBitsToFloats)).setScale((int) decimals, BigDecimal.ROUND_DOWN).floatValue());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Member Author

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.

Copy link
Contributor

@tdcmeehan tdcmeehan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kaikalur
Copy link
Contributor

The one with -1 as the second param are very confusing! Is it similar to any other system like Oracle or Mysql?

@fgwang7w
Copy link
Member Author

We'll also want to add documentation for it here.

sure will add spec doc accordingly

@fgwang7w
Copy link
Member Author

The one with -1 as the second param are very confusing! Is it similar to any other system like Oracle or Mysql?

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

@fgwang7w
Copy link
Member Author

We'll also want to add documentation for it here.

@tdcmeehan Thanks for the comment, I have added the doc accordingly for this function. Please help give another review. many thanks!

@fgwang7w fgwang7w requested review from tdcmeehan and removed request for kaikalur and rongrong July 21, 2020 06:59
Copy link
Contributor

@tdcmeehan tdcmeehan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. @kaikalur @rongrong do you want to look?

@fgwang7w
Copy link
Member Author

LGTM. @kaikalur @rongrong do you want to look?

Thanks @tdcmeehan !
@kaikalur @rongrong could you please help review this commit? Thanks!

@fgwang7w fgwang7w requested review from kaikalur and rongrong and removed request for shixuan-fan and aweisberg July 29, 2020 06:33
@@ -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")
Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Member Author

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

Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

@kaikalur kaikalur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fgwang7w fgwang7w requested a review from rongrong August 12, 2020 06:22
@fgwang7w
Copy link
Member Author

can someone please help to merge the code or does it need another review?

@fgwang7w
Copy link
Member Author

@rongrong assign this pr to you since you left the last comment, if all looks good please help coordinate to merge. many thanks!

@fgwang7w fgwang7w force-pushed the 14693 branch 3 times, most recently from 4685a6c to 900b104 Compare August 20, 2020 02:47
@rongrong rongrong merged commit f2965f6 into prestodb:master Oct 13, 2020
@caithagoras caithagoras mentioned this pull request Oct 19, 2020
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants