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 wrong result of cast(float as decimal) when overflow happens #4380

Merged
merged 9 commits into from
Mar 23, 2022

Conversation

guo-shaoge
Copy link
Contributor

@guo-shaoge guo-shaoge commented Mar 22, 2022

What problem does this PR solve?

Issue Number: close #3998

Problem Summary:

What is changed and how it works?

When cast float32 to decimal, use Float64 as intermediate type to avoid overflow.

WATCHOUT: there is performance regression and compatibility issue. Explained in the following comments.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Fix wrong result of cast(float as decimal) when overflow happens

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Mar 22, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • fuzhe1989
  • windtalker

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added needs-cherry-pick-release-5.0 PR which needs to be cherry-picked to release-5.0 release-note Denotes a PR that will be considered when it comes time to generate release notes. needs-cherry-pick-release-5.1 PR which needs to be cherry-picked to release-5.1 needs-cherry-pick-release-5.2 PR which needs to be cherry-picked to release-5.2 needs-cherry-pick-release-5.3 Type: Need cherry pick to release-5.3 needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. needs-cherry-pick-release-6.0 Type: Need cherry pick to release-6.0 size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 22, 2022
@ti-chi-bot ti-chi-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 22, 2022
@guo-shaoge
Copy link
Contributor Author

guo-shaoge commented Mar 22, 2022

After change from Float32 to Float64. Result are different from older version.

drop table if exists t1;
create table t1(c1 float, c2 double);
insert into t1 values(123.123456789123456789, 123.123456789123456789);
alter table t1 set tiflash replica 1;
select cast(c1 as decimal(50,20)) from t1;
select cast(c1 as decimal(50,20)) from t1;

TiDB:

mysql>  select cast(c1 as decimal(50,20)) from t1;
+----------------------------+
| cast(c1 as decimal(50,20)) |
+----------------------------+
|   123.12345886230469000000 |
+----------------------------+
1 row in set (0.00 sec)

mysql>  select cast(c2 as decimal(50,20)) from t1;
+----------------------------+
| cast(c2 as decimal(50,20)) |
+----------------------------+
|   123.12345678912345000000 |
+----------------------------+
1 row in set (0.00 sec)

TiFlash Before:

MySQL [test]> select cast(c1 as decimal(50, 20)) from t1;
+-----------------------------+
| cast(c1 as decimal(50, 20)) |
+-----------------------------+
|    123.12344859372018466817 |
+-----------------------------+
1 row in set (0.25 sec)

MySQL [test]> select cast(c2 as decimal(50, 20)) from t1;
+-----------------------------+
| cast(c2 as decimal(50, 20)) |
+-----------------------------+
|    123.12345678912344293376 |
+-----------------------------+
1 row in set (0.02 sec)

TiFlash After:

MySQL [test]> select * from t1;
+-----------+--------------------+
| c1        | c2                 |
+-----------+--------------------+
| 123.12346 | 123.12345678912345 |
+-----------+--------------------+
1 row in set (0.02 sec)
MySQL [test]> select cast(c1 as decimal(50,20)) from t1;
+----------------------------+
| cast(c1 as decimal(50,20)) |
+----------------------------+
|   123.12345886230470197248 |
+----------------------------+
1 row in set (0.21 sec)

MySQL [test]> select cast(c2 as decimal(50,20)) from t1;
+----------------------------+
| cast(c2 as decimal(50,20)) |
+----------------------------+
|   123.12345678912344293376 |
+----------------------------+
1 row in set (0.02 sec)

@guo-shaoge
Copy link
Contributor Author

/run-all-tests

@sre-bot
Copy link
Collaborator

sre-bot commented Mar 22, 2022

Coverage for changed files

Filename                            Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FunctionsTiDBConversion.h               856               432    49.53%          89                26    70.79%        1940               635    67.27%         496               321    35.28%
tests/gtest_tidb_conversion.cpp        4645              1044    77.52%          37                 0   100.00%        1453                 0   100.00%        1468               746    49.18%
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                  5501              1476    73.17%         126                26    79.37%        3393               635    81.28%        1964              1067    45.67%

Coverage summary

Functions  MissedFunctions  Executed  Lines   MissedLines  Cover
17040      9576             43.80%    191889  97050        49.42%

full coverage report (for internal network access only)

@guo-shaoge
Copy link
Contributor Author

Performance regression for cast(float32 to decimal):

Before:

CastToDecimalBench/float32_to_decimal_60_30/iterations:1000     572218 ns       572220 ns         1000

After:

CastToDecimalBench/float32_to_decimal_60_30/iterations:1000    1007571 ns      1007572 ns         1000

@guo-shaoge
Copy link
Contributor Author

/cc @fuzhe1989
/cc @windtalker

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Mar 22, 2022
Signed-off-by: guo-shaoge <[email protected]>
@ti-chi-bot
Copy link
Member

@guo-shaoge: It seems you want to merge this PR, I will help you trigger all the tests:

/run-all-tests

You only need to trigger /merge once, and if the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

If you have any questions about the PR merge process, please refer to pr process.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 330948c

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Mar 23, 2022
@sre-bot
Copy link
Collaborator

sre-bot commented Mar 23, 2022

Coverage for changed files

Filename                            Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FunctionsTiDBConversion.h               856               432    49.53%          89                26    70.79%        1940               635    67.27%         496               321    35.28%
tests/gtest_tidb_conversion.cpp        4645              1044    77.52%          37                 0   100.00%        1455                 0   100.00%        1468               746    49.18%
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                  5501              1476    73.17%         126                26    79.37%        3395               635    81.30%        1964              1067    45.67%

Coverage summary

Functions  MissedFunctions  Executed  Lines   MissedLines  Cover
17040      9576             43.80%    191891  97033        49.43%

full coverage report (for internal network access only)

@ti-chi-bot ti-chi-bot merged commit 68906ed into pingcap:master Mar 23, 2022
ti-chi-bot pushed a commit to ti-chi-bot/tiflash that referenced this pull request Mar 23, 2022
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created: #4386.

@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created: #4387.

ti-chi-bot pushed a commit to ti-chi-bot/tiflash that referenced this pull request Mar 23, 2022
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created: #4388.

@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created: #4389.

@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created: #4390.

ti-chi-bot pushed a commit to ti-chi-bot/tiflash that referenced this pull request Mar 23, 2022
ti-chi-bot pushed a commit to ti-chi-bot/tiflash that referenced this pull request Mar 23, 2022
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created: #4391.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-cherry-pick-release-5.0 PR which needs to be cherry-picked to release-5.0 needs-cherry-pick-release-5.1 PR which needs to be cherry-picked to release-5.1 needs-cherry-pick-release-5.2 PR which needs to be cherry-picked to release-5.2 needs-cherry-pick-release-5.3 Type: Need cherry pick to release-5.3 needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. needs-cherry-pick-release-6.0 Type: Need cherry pick to release-6.0 release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong result when cast float to decimal
5 participants