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

Corrected scale representation for NUMERIC/DECIMAL #251

Closed
wants to merge 1 commit into from
Closed

Corrected scale representation for NUMERIC/DECIMAL #251

wants to merge 1 commit into from

Conversation

mcapitanio
Copy link

@gunnarmorling This PR is a proposal to fix the issue for the missing trailing zeros in the double representation of a NUMERIC/DECIMAL data type in PostgreSql.

The bug is reported in the Issue DBZ-287.

@gunnarmorling
Copy link
Member

gunnarmorling commented Jun 20, 2017 via email

@gunnarmorling gunnarmorling self-assigned this Jul 10, 2017
@fritzblue
Copy link

Any updates on merging this PR? This is a blocker for us.

@jpechane
Copy link
Contributor

I am sorry for the delay, Gunnar is away this week. I'll take look at it tomrrow. Is there any chance you could provide integration test for the functionality. Also just a reminder - having the issue merged does not mean that we will release the new version with the fix - the release date for 0.5.2 has not been set yet.

@mcapitanio
Copy link
Author

@user453584 @jpechane I am on vacation till 27 of July, anyway I'll try to provide integration tests in the next days.

@mcapitanio
Copy link
Author

@jpechane I have added integration test for the fix and verified that it correctly check the scenario fixed with this PR, so it fails with the actual master and passes with this branch.

Copy link
Member

@gunnarmorling gunnarmorling left a comment

Choose a reason for hiding this comment

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

Hey @mcapitanio, the change looks good overall, I've added some comments inline about some questions. Could you also squash all the commits into one and add the issue number to the commit message ("DBZ-287 Corrected scale..."). I can also help with the latter if needed. Thanks!

return newDecimal;
}
if (column.scale() > -1) {
String decimalWithTrailingZeros = String.format("%-" + column.scale() + "s", newDecimal.doubleValue())
Copy link
Member

Choose a reason for hiding this comment

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

Why is it that you create this string and then instantiate a new BigDecimal with it? Shouldn't it be enough to do newDecimal = newDecimal.setScale(column.scale())?

Copy link
Author

Choose a reason for hiding this comment

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

I found that simply changing the scale does not affect the string serialized value of the BigDecimal, so it does not recover the missing trailing zeros.
Anyway I found maybe a more elegant solution using the MathContext instead of the string:

MathContext mc = new MathContext((newDecimal.precision() - newDecimal.scale()) + column.scale());
newDecimal = new BigDecimal(newDecimal.doubleValue(), mc);

So in the case of the integration test, where I have a DECIMAL(3,2) and a database value of 1.1:

newDecimal.precision() assumes the value of 2
newDecimal.scale() assumes the wrong value of 1
newDecimal.precision() - newDecimal.scale() is the size of the integer part of the original newDecimal (so it would assume the value 1)
column.scale() is the correct scale for the newDecimal, so it would be 2

The MathContext would consider a precision of 3 and the value will be correctly represented as 1.10.

if (newDecimal == null) {
return newDecimal;
}
if (column.scale() > -1) {
Copy link
Member

Choose a reason for hiding this comment

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

How about checking for if (column.scale() > newDecimal.scale())? I.e. not re-scale if the value already has the right scale.

Copy link
Author

Choose a reason for hiding this comment

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

@gunnarmorling Totally agree! Thanks for the suggestion!

@gunnarmorling
Copy link
Member

In terms of a release, I would think that we can ship a 0.5.2 some time next week, including this change and hopefully a few others from the pending PRs. They warrant another bugfix release for sure.

@fritzblue
Copy link

🙌

@mcapitanio
Copy link
Author

@gunnarmorling I've squashed all my commits in just one commit, hope it is as you expected. I found a different solution for the fixing, not using the string but with the more elegant usage of MathContext: just changing the scale to the existing decimal value unfortunately does not solve the problem.

@gunnarmorling
Copy link
Member

changing the scale to the existing decimal value unfortunately does not solve the problem

Hum, did you re-assign the BigDecimal returned by setScale() (the method doesn't alter the given instance but returns a new object)? Anyways, your solution does the trick, too.

@gunnarmorling
Copy link
Member

Rebased and applied. Thanks a lot, @mcapitanio! Note that I changed the implementation to use setScale(), as I think it's better to avoid the double conversion of the previous approach. The tests pass (also changed them to use a larger scale), so it should be good. Thanks again!

@mcapitanio
Copy link
Author

@gunnarmorling Absolutely, the setScale is the best solution, I didn't realize it was necessary to make the re-assignement! Hope in helping Debezium again, Postgres connector is so crucial for us!

@gunnarmorling
Copy link
Member

gunnarmorling commented Jul 25, 2017 via email

@fritzblue
Copy link

fritzblue commented Jul 28, 2017

@gunnarmorling Thanks for helping this issue along (and @mcapitanio for the PR!). @gunnarmorling What is the status of the 0.5.2 release? This bug has been a blocker for us, so we're hoping to get the new release pulled down as soon as possible

@gunnarmorling
Copy link
Member

Hi, the 0.5.2 release is scheduled for next week. If you like, you can download the latest snaphot build from here: https://oss.sonatype.org/content/repositories/snapshots/io/debezium/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants