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 decimal decoding #401

Merged
merged 4 commits into from
Jul 23, 2018
Merged

Fix decimal decoding #401

merged 4 commits into from
Jul 23, 2018

Conversation

ilovesoup
Copy link
Contributor

@ilovesoup ilovesoup commented Jul 19, 2018

Old implementation was wrong dealing with unsigned / signed part of Java and some code path might break in specific values.


This change is Reviewable

@ilovesoup
Copy link
Contributor Author

/run-all-tests tikv=release-2.0 tidb=release-2.0 pd=release-2.0

@codecov-io
Copy link

codecov-io commented Jul 19, 2018

Codecov Report

❗ No coverage uploaded for pull request base (master@c948d12). Click here to learn what that means.
The diff coverage is 41.17%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #401   +/-   ##
=========================================
  Coverage          ?   55.14%           
=========================================
  Files             ?      127           
  Lines             ?     5812           
  Branches          ?      864           
=========================================
  Hits              ?     3205           
  Misses            ?     2313           
  Partials          ?      294
Impacted Files Coverage Δ
...rc/main/java/com/pingcap/tikv/codec/MyDecimal.java 65.93% <41.17%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c948d12...e0f20cd. Read the comment docs.

this.digitsInt = wordsInt * digitsPerWord + leadingDigits;
this.digitsFrac = wordsFrac * digitsPerWord + trailingDigits;
this.digitsInt = (byte)(wordsInt * digitsPerWord + leadingDigits);
this.digitsFrac = (byte)(wordsFrac * digitsPerWord + trailingDigits);
Copy link
Contributor

Choose a reason for hiding this comment

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

type conversion seems unnecessary. Digits of int and fraction can never go beyond 127. tidb does this simply because it is static type language. It has to do such conversion otherwise compiler will not be happy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To keep the same behavior as in TiDB which does a cap.

int x = 0;
switch (size) {
case 1:
x = b[start];
x = (byte)b[start];
Copy link
Contributor

Choose a reason for hiding this comment

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

same reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In java byte is signed. b[start] is unsigned in fact since we read as unsigned byte.
So actually convert to byte will cause it to be signed. It's not trivial.

break;
case 3:
int sign = b[start] & 128;
if (sign > 0) {
x = 255 << 24 | (b[start] & 0xFF) << 16 | (b[start + 1] & 0xFF) << 8 | (b[start + 2]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a reminder: b's type is byte[] which shares a range from -128 to 127 whereas golang's byte shares range from 0 to 255. We are better take and operation with 0xFF.

Copy link
Contributor

Choose a reason for hiding this comment

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

Additionally, 255 << 24 could be overflow. We need convert 255 to a uint and then convert back to int.

Copy link
Contributor

Choose a reason for hiding this comment

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

my bad, java does not have uint..

Copy link
Contributor

@zhexuany zhexuany Jul 23, 2018

Choose a reason for hiding this comment

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

~(0x7F << 24 | b[start] << 16 | b[start + 1] << 8 | (b[start + 2]) + 1);

Copy link
Contributor

Choose a reason for hiding this comment

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

you may use ~(((b[start] & 7FFF) << 16 | (b[start + 1] & 0xFF) << 8 | (b[start + 2] & 0xFF)) + 1);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. b's type is int not byte[].
  2. 255 as const literal is int type.

Copy link
Contributor

Choose a reason for hiding this comment

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

255 << 24 has correct binary form. Numeric overflow does not affect binary. ignore my comment.

@zhexuany
Copy link
Contributor

We are better to add some comments for precision() anb fraction(). It returns real precision and fraction.

Copy link
Contributor

@zhexuany zhexuany left a comment

Choose a reason for hiding this comment

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

If would be better if you could address comments.

@zhexuany zhexuany dismissed their stale review July 23, 2018 09:36

overthink

@ilovesoup
Copy link
Contributor Author

/run-all-tests tikv=release-2.0 tidb=release-2.0 pd=release-2.0

Copy link
Contributor

@zhexuany zhexuany left a comment

Choose a reason for hiding this comment

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

LGTM

@ilovesoup ilovesoup merged commit dfe8f3e into master Jul 23, 2018
@ilovesoup ilovesoup deleted the fix_decimal branch July 23, 2018 10:25
birdstorm pushed a commit that referenced this pull request Jul 26, 2018
birdstorm pushed a commit that referenced this pull request Aug 24, 2018
wfxxh pushed a commit to wanfangdata/tispark that referenced this pull request Jun 30, 2023
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.

4 participants