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
Potential issue when using ROUND
#3982
Comments
This is the culprit and it is plain Java code: long scaled = BigDecimal.valueOf(original).setScale(scale, roundingMode).longValue(); |
Since you want to create a BigDecimal with 10,000 digits I guess you have to deal with the cost. Everything seems to be correct from the code's point of view: public static void main(String[] args) {
long t1 = System.nanoTime();
Long result = BigDecimal.valueOf(1L).setScale(-999999, RoundingMode.HALF_EVEN).longValue();
System.out.println((System.nanoTime() - t1) * 1E-6);
t1 = System.nanoTime();
result = BigDecimal.valueOf(1L).setScale(999999, RoundingMode.HALF_EVEN).longValue();
System.out.println((System.nanoTime() - t1) * 1E-6);
}
I think we can close this issue as "Explained, nothing to do"? |
I think these functions should reject out of range arguments. |
Or maybe use optimized code paths for them if we really have a reason to support them. |
I can craft a PR if you like (I though about this at first), but why exactly? Your call please. |
Values larger than They can throw something like this (for simplicity): throw DbException.get(ErrorCode.INVALID_VALUE_SCALE, Integer.toString(scale), "-" + MAXIMUM_SCALE, "" + MAXIMUM_SCALE); The |
Actually everything depends on data type of the first argument, so this range also isn't that good. |
Hi! Thanks for all the detailed explanations. I am curious to know if there has been any update regarding this issue. Is it considered as an expected behavior or so? I'm currently developing an automated testing tool and if such cases would be regarded as expected, I'll try to block these stuffs. |
Greetings. @katzyn: In my opinion, everything is correct here since the time is only consumed by creating the BigDecimal with many digits (positive and negative). Can we close this? |
No, there is a bigger problem with these functions, both implementations and documentation need to be improved. |
Consider the following test cases, the queries execute slowly.
I understand that the negative value for the second argument of
ROUND
is somewhat illegal. However, in SQLite, MySQL and PostgreSQL, the queries take less than 1 second.I originally find this by building the latest source version cd09d93. I could also reproduce this in 2.2.224.
Here's how I start H2 Shell:
The text was updated successfully, but these errors were encountered: