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

expression: fix cast json to decimal bug. #8030

Merged
merged 7 commits into from
Oct 29, 2018

Conversation

crazycs520
Copy link
Contributor

What problem does this PR solve?

sql

create table test ( a decimal(60,2) as (JSON_EXTRACT(b,'$.c')), b json );
insert into test (b) values 
('{"c": "1267.1"}'),
('{"c": "1267.01"}'),
('{"c": "1267.1234"}'),
('{"c": "1267.3456"}'),
('{"c": "1234567890123456789012345678901234567890123456789012345"}'),
('{"c": "1234567890123456789012345678901234567890123456789012345.12345"}');
select * from test\G

Original TiDB return

***************************[ 1. row ]***************************
a | 1267.1
b | {"c": "1267.1"}
***************************[ 2. row ]***************************
a | 1267.01
b | {"c": "1267.01"}
***************************[ 3. row ]***************************
a | 1267.1234
b | {"c": "1267.1234"}
***************************[ 4. row ]***************************
a | 1267.3456
b | {"c": "1267.3456"}
***************************[ 5. row ]***************************
a | 1234567890123456800000000000000000000000000000000000000
b | {"c": "1234567890123456789012345678901234567890123456789012345"}
***************************[ 6. row ]***************************
a | 1234567890123456800000000000000000000000000000000000000
b | {"c": "1234567890123456789012345678901234567890123456789012345.12345"}

Now:

***************************[ 1. row ]***************************
a | 1267.10
b | {"c": "1267.1"}
***************************[ 2. row ]***************************
a | 1267.01
b | {"c": "1267.01"}
***************************[ 3. row ]***************************
a | 1267.12
b | {"c": "1267.1234"}
***************************[ 4. row ]***************************
a | 1267.35
b | {"c": "1267.3456"}
***************************[ 5. row ]***************************
a | 1234567890123456789012345678901234567890123456789012345.00
b | {"c": "1234567890123456789012345678901234567890123456789012345"}
***************************[ 6. row ]***************************
a | 1234567890123456789012345678901234567890123456789012345.12
b | {"c": "1234567890123456789012345678901234567890123456789012345.12345"}

As you can see, original TiDB will lose precision and can‘t resolving big decimal number.

What is changed and how it works?

Add ConvertJSONToDecimal function, Use MyDecimal.FromStrin to parse json when json type is string.

Check List

Tests

  • Unit test

Code changes

  • Has exported function/method change

@ngaut ngaut changed the title expression: cast json to decimal bug. expression: fix cast json to decimal bug. Oct 24, 2018
@crazycs520 crazycs520 added the type/bugfix This PR fixes a bug. label Oct 24, 2018
@XuHuaiyu
Copy link
Contributor

Please add some integration test cases

return res, errors.Trace(err)
}
err = res.FromFloat64(f64)
return res, errors.Trace(err)
Copy link
Member

Choose a reason for hiding this comment

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

What about create two new function NewDecFromFloat and NewDecFromString and call it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

MyDecimal already have FromFloat64 and FromString methods. What is the difference between NewDecFromFloat and FromFloat64?

@crazycs520
Copy link
Contributor Author

@XuHuaiyu done. PTAL

@zz-jason
Copy link
Member

LGTM

@zz-jason zz-jason added the status/LGT1 Indicates that a PR has LGTM 1. label Oct 25, 2018
@zz-jason
Copy link
Member

@crazycs520 Should we cherrypick this commit to release-2.1 and release-2.0?

@crazycs520
Copy link
Contributor Author

@zz-jason OK, I will do cherrypick after this merge.

@crazycs520
Copy link
Contributor Author

/run-all-tests

@zz-jason
Copy link
Member

@winoros @lamxTyler PTAL

Copy link
Contributor

@alivxxx alivxxx left a comment

Choose a reason for hiding this comment

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

LGTM

@zz-jason zz-jason added status/LGT2 Indicates that a PR has LGTM 2. status/all tests passed and removed status/LGT1 Indicates that a PR has LGTM 1. labels Oct 29, 2018
@zhexuany zhexuany merged commit 3febc22 into pingcap:master Oct 29, 2018
crazycs520 added a commit to crazycs520/tidb that referenced this pull request Oct 30, 2018
crazycs520 added a commit to crazycs520/tidb that referenced this pull request Oct 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression status/LGT2 Indicates that a PR has LGTM 2. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants