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

Optimize overflow check when cast to decimal #3912

Merged
merged 62 commits into from
Mar 11, 2022

Conversation

guo-shaoge
Copy link
Contributor

@guo-shaoge guo-shaoge commented Jan 20, 2022

What problem does this PR solve?

Issue Number: close #3721

Problem Summary:

  1. optimize cast(enum/int/decimal as decimal) to eliminate overflow check if possible
  2. optimize the internal type when do casting, e.g. use appropriate internal type when do int_val * scale_mul

What is changed and how it works?

design doc

benchmark result

tpch100 result:(2s -> 0.7s)

4j4NT5Lq4A

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 the wrong result that occurs when casting `FLOAT` to `DECIMAL` when overflow happens

Signed-off-by: guo-shaoge <shaoge1994@163.com>
Signed-off-by: guo-shaoge <shaoge1994@163.com>
Signed-off-by: guo-shaoge <shaoge1994@163.com>
Signed-off-by: guo-shaoge <shaoge1994@163.com>
@ti-chi-bot
Copy link
Member

ti-chi-bot commented Jan 20, 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 release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jan 20, 2022
@guo-shaoge
Copy link
Contributor Author

/run-integration-test
/run-unit-test

dbms/src/Functions/FunctionsTiDBConversion.h Outdated Show resolved Hide resolved
dbms/src/Functions/FunctionsTiDBConversion.h Outdated Show resolved Hide resolved
dbms/src/Functions/FunctionsTiDBConversion.h Outdated Show resolved Hide resolved
@guo-shaoge
Copy link
Contributor Author

make bench_dbms -j6 && ./dbms/bench_dbms --benchmark_filter='CastIntToDecimalBench.*'

2022-01-20T17:09:50+08:00
Running ./dbms/bench_dbms
Run on (8 X 2200 MHz CPU s)
CPU Caches:
  L1 Data 32 KiB (x8)
  L1 Instruction 32 KiB (x8)
  L2 Unified 4096 KiB (x8)
  L3 Unified 16384 KiB (x2)
Load Average: 0.59, 0.32, 0.19
---------------------------------------------------------------------------------------------------
Benchmark                                                         Time             CPU   Iterations
---------------------------------------------------------------------------------------------------
CastIntToDecimalBench/int8_to_decimal32/iterations:1000       67103 ns        67099 ns         1000
CastIntToDecimalBench/uint8_to_decimal32/iterations:1000      62730 ns        62730 ns         1000
CastIntToDecimalBench/int32_to_decimal32/iterations:1000      77848 ns        77823 ns         1000

int8_to_decimal32 and uint8_to_decimal32 no need to check overflow, because max_prec of uint8/int8 is 3, and decimal32 is prec is 9 and scale is 0.

int32_to_decimal32 needs check overflow, because max_prec of int32 is 10.

This micro benchmark shows we have a 13% performance optimization.

dbms/src/Functions/FunctionsTiDBConversion.h Show resolved Hide resolved
dbms/src/Functions/FunctionsTiDBConversion.h Outdated Show resolved Hide resolved
dbms/src/Functions/FunctionsTiDBConversion.h Outdated Show resolved Hide resolved
dbms/src/Functions/FunctionsTiDBConversion.h Outdated Show resolved Hide resolved
dbms/src/Functions/FunctionsTiDBConversion.h Outdated Show resolved Hide resolved
dbms/src/Functions/FunctionsTiDBConversion.h Outdated Show resolved Hide resolved
@sre-bot
Copy link
Collaborator

sre-bot commented Jan 20, 2022

Coverage detail: https://ci-internal.pingcap.net/job/tics_ghpr_unit_test/1084/cobertura/
(Coverage detail url is limited office network access)

lines: 44.7% (51583 out of 115323)
branches: 7.0% (89938 out of 1281787)

dbms/CMakeLists.txt Outdated Show resolved Hide resolved
dbms/CMakeLists.txt Outdated Show resolved Hide resolved
@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 26, 2022
…verflow

Signed-off-by: guo-shaoge <shaoge1994@163.com>

Conflicts:
	dbms/src/Functions/FunctionsTiDBConversion.h
Signed-off-by: guo-shaoge <shaoge1994@163.com>
…nto optimize_cast_overflow

Signed-off-by: guo-shaoge <shaoge1994@163.com>

Conflicts:
	dbms/src/Functions/FunctionsTiDBConversion.h
@ti-chi-bot ti-chi-bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jan 27, 2022
Signed-off-by: guo-shaoge <shaoge1994@163.com>
Signed-off-by: guo-shaoge <shaoge1994@163.com>
Signed-off-by: guo-shaoge <shaoge1994@163.com>
@guo-shaoge
Copy link
Contributor Author

/run-all-tests

@sre-bot
Copy link
Collaborator

sre-bot commented Feb 8, 2022

Coverage for changed files

Filename                                      Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Common/Decimal.h                                   81                29    64.20%          42                 8    80.95%         180                62    65.56%          32                18    43.75%
Functions/FunctionsTiDBConversion.h               807               425    47.34%          77                25    67.53%        1778               615    65.41%         482               318    34.02%
Functions/tests/gtest_tidb_conversion.cpp        3790               871    77.02%          31                 0   100.00%        1320                 0   100.00%        1186               605    48.99%
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                            4678              1325    71.68%         150                33    78.00%        3278               677    79.35%        1700               941    44.65%

Coverage summary

Functions  MissedFunctions  Executed  Lines   MissedLines  Cover
16352      9712             40.61%    179261  98420        45.10%

full coverage report (for internal network access only)

Signed-off-by: guo-shaoge <shaoge1994@163.com>
@guo-shaoge
Copy link
Contributor Author

/run-all-tests

Copy link
Contributor

@windtalker windtalker left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Mar 9, 2022
dbms/src/Common/Decimal.h Show resolved Hide resolved
dbms/src/Functions/FunctionsTiDBConversion.h Show resolved Hide resolved
dbms/src/Functions/FunctionsTiDBConversion.h Show resolved Hide resolved
dbms/src/Functions/FunctionsTiDBConversion.h Show resolved Hide resolved
dbms/src/Functions/FunctionsTiDBConversion.h Show resolved Hide resolved
dbms/src/Functions/FunctionsTiDBConversion.h Show resolved Hide resolved
dbms/src/Functions/FunctionsTiDBConversion.h Show resolved Hide resolved
dbms/src/Functions/FunctionsTiDBConversion.h Outdated Show resolved Hide resolved
dbms/src/Functions/FunctionsTiDBConversion.h Outdated Show resolved Hide resolved
Signed-off-by: guo-shaoge <shaoge1994@163.com>
@guo-shaoge
Copy link
Contributor Author

/run-all-tests

@guo-shaoge
Copy link
Contributor Author

/run-unit-test

@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Mar 9, 2022
@guo-shaoge
Copy link
Contributor Author

/run-all-tests

@sre-bot
Copy link
Collaborator

sre-bot commented Mar 9, 2022

Coverage for changed files

Filename                                           Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Columns/ColumnDecimal.h                                 52                36    30.77%          36                20    44.44%          62                46    25.81%           8                 8     0.00%
Common/Decimal.h                                        82                23    71.95%          43                 7    83.72%         184                54    70.65%          32                15    53.12%
Functions/FunctionsTiDBConversion.h                    856               432    49.53%          89                26    70.79%        1940               635    67.27%         496               321    35.28%
Functions/tests/gtest_arithmetic_functions.cpp        2619               418    84.04%          12                 3    75.00%         852                 3    99.65%        1026               351    65.79%
Functions/tests/gtest_tidb_conversion.cpp             4645              1044    77.52%          37                 0   100.00%        1451                 0   100.00%        1468               746    49.18%
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                                 8254              1953    76.34%         217                56    74.19%        4489               738    83.56%        3030              1441    52.44%

Coverage summary

Functions  MissedFunctions  Executed  Lines   MissedLines  Cover
16799      9448             43.76%    189230  95763        49.39%

full coverage report (for internal network access only)

@guo-shaoge
Copy link
Contributor Author

/merge

@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: 751660e

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

sre-bot commented Mar 9, 2022

Coverage for changed files

Filename                                           Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Columns/ColumnDecimal.h                                 52                36    30.77%          36                20    44.44%          62                46    25.81%           8                 8     0.00%
Common/Decimal.h                                        82                23    71.95%          43                 7    83.72%         184                54    70.65%          32                15    53.12%
Functions/FunctionsTiDBConversion.h                    856               432    49.53%          89                26    70.79%        1940               635    67.27%         496               321    35.28%
Functions/tests/gtest_arithmetic_functions.cpp        2619               418    84.04%          12                 3    75.00%         852                 3    99.65%        1026               351    65.79%
Functions/tests/gtest_tidb_conversion.cpp             4645              1044    77.52%          37                 0   100.00%        1451                 0   100.00%        1468               746    49.18%
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                                 8254              1953    76.34%         217                56    74.19%        4489               738    83.56%        3030              1441    52.44%

Coverage summary

Functions  MissedFunctions  Executed  Lines   MissedLines  Cover
16799      9448             43.76%    189230  95757        49.40%

full coverage report (for internal network access only)

@guo-shaoge
Copy link
Contributor Author

/run-integration-test

@sre-bot
Copy link
Collaborator

sre-bot commented Mar 11, 2022

Coverage for changed files

Filename                                                       Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Columns/ColumnDecimal.h                                             52                36    30.77%          36                20    44.44%          62                46    25.81%           8                 8     0.00%
Common/Decimal.h                                                    82                23    71.95%          43                 7    83.72%         184                54    70.65%          32                15    53.12%
Common/MPMCQueue.h                                                  76                 0   100.00%          29                 0   100.00%         166                 1    99.40%          44                 2    95.45%
Common/tests/gtest_mpmc_queue.cpp                                 1424               292    79.49%          45                 0   100.00%         514                12    97.67%         480               218    54.58%
Debug/DBGInvoker.cpp                                                30                29     3.33%          10                 9    10.00%         138                75    45.65%          14                14     0.00%
Debug/MockTiDB.cpp                                                 139               135     2.88%          34                32     5.88%         421               403     4.28%          92                91     1.09%
Debug/MockTiDB.h                                                     8                 6    25.00%           8                 6    25.00%          15                13    13.33%           0                 0         -
Debug/dbgFuncMockTiDBTable.cpp                                     109               109     0.00%          14                14     0.00%         219               219     0.00%          72                72     0.00%
Debug/dbgFuncSchema.cpp                                             44                44     0.00%           5                 5     0.00%          62                62     0.00%          28                28     0.00%
Flash/Coprocessor/DAGStorageInterpreter.cpp                        272               272     0.00%          15                15     0.00%         499               499     0.00%         170               170     0.00%
Functions/FunctionsTiDBConversion.h                                856               432    49.53%          89                26    70.79%        1940               635    67.27%         496               321    35.28%
Functions/tests/gtest_arithmetic_functions.cpp                    2619               418    84.04%          12                 3    75.00%         852                 3    99.65%        1026               351    65.79%
Functions/tests/gtest_tidb_conversion.cpp                         4645              1044    77.52%          37                 0   100.00%        1451                 0   100.00%        1468               746    49.18%
Storages/DeltaMerge/DMSegmentThreadInputStream.h                    62                 6    90.32%           5                 0   100.00%          73                 2    97.26%          28                 8    71.43%
Storages/DeltaMerge/DeltaMergeDefines.h                             26                 0   100.00%           8                 0   100.00%          28                 0   100.00%           2                 0   100.00%
Storages/DeltaMerge/DeltaMergeStore.cpp                           1317               467    64.54%          65                 5    92.31%        1885               426    77.40%         766               367    52.09%
Storages/DeltaMerge/DeltaMergeStore.h                               41                13    68.29%          19                 2    89.47%          85                30    64.71%          42                11    73.81%
Storages/DeltaMerge/tests/gtest_dm_storage_delta_merge.cpp        1489               285    80.86%          14                 0   100.00%         540                 8    98.52%         472               223    52.75%
Storages/ITableDeclaration.cpp                                      92                87     5.43%          18                15    16.67%         182               172     5.49%          68                67     1.47%
Storages/ITableDeclaration.h                                         3                 1    66.67%           3                 1    66.67%           5                 3    40.00%           0                 0         -
Storages/MutableSupport.h                                           17                17     0.00%          10                10     0.00%          37                37     0.00%           6                 6     0.00%
Storages/PrimaryKeyNotMatchException.h                               1                 1     0.00%           1                 1     0.00%           1                 1     0.00%           0                 0         -
Storages/StorageDeltaMerge.cpp                                     667               358    46.33%          56                29    48.21%        1290               762    40.93%         376               257    31.65%
Storages/StorageDeltaMerge.h                                        11                 7    36.36%          11                 7    36.36%          15                 9    40.00%           0                 0         -
Storages/Transaction/SchemaBuilder.cpp                             651               616     5.38%          46                42     8.70%        1020               948     7.06%         404               386     4.46%
Storages/Transaction/SchemaBuilder.h                                 1                 1     0.00%           1                 1     0.00%           1                 1     0.00%           0                 0         -
Storages/Transaction/SchemaGetter.h                                  1                 1     0.00%           1                 1     0.00%           1                 1     0.00%           0                 0         -
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                                            14735              4700    68.10%         635               251    60.47%       11686              4422    62.16%        6094              3361    44.85%

Coverage summary

Functions  MissedFunctions  Executed  Lines   MissedLines  Cover
16807      9445             43.80%    189428  95769        49.44%

full coverage report (for internal network access only)

@sre-bot
Copy link
Collaborator

sre-bot commented Mar 11, 2022

Coverage for changed files

too many lines from llvm-cov, please refer to full report instead

Coverage summary

Functions  MissedFunctions  Executed  Lines   MissedLines  Cover
16812      9445             43.82%    189452  95783        49.44%

full coverage report (for internal network access only)

@sre-bot
Copy link
Collaborator

sre-bot commented Mar 11, 2022

Coverage for changed files

too many lines from llvm-cov, please refer to full report instead

Coverage summary

Functions  MissedFunctions  Executed  Lines   MissedLines  Cover
16842      9445             43.92%    189535  95790        49.46%

full coverage report (for internal network access only)

@ti-chi-bot ti-chi-bot merged commit 360d2bd into pingcap:master Mar 11, 2022
@JaySon-Huang JaySon-Huang added the type/bugfix This PR fixes a bug. label Apr 20, 2022
@sre-bot
Copy link
Collaborator

sre-bot commented Apr 20, 2022

Coverage for changed files

Filename                                           Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Columns/ColumnDecimal.h                                 52                36    30.77%          36                20    44.44%          62                46    25.81%           8                 8     0.00%
Common/Decimal.h                                        82                23    71.95%          43                 7    83.72%         184                54    70.65%          32                15    53.12%
Functions/FunctionsTiDBConversion.h                    856               432    49.53%          89                26    70.79%        1940               635    67.27%         496               321    35.28%
Functions/tests/gtest_arithmetic_functions.cpp        2619               418    84.04%          12                 3    75.00%         852                 3    99.65%        1026               351    65.79%
Functions/tests/gtest_tidb_conversion.cpp             4645              1044    77.52%          37                 0   100.00%        1451                 0   100.00%        1468               746    49.18%
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                                 8254              1953    76.34%         217                56    74.19%        4489               738    83.56%        3030              1441    52.44%

Coverage summary

Functions  MissedFunctions  Executed  Lines   MissedLines  Cover
16842      9445             43.92%    189535  95777        49.47%

full coverage report (for internal network access only)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ 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. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimize cast(xx as Decimal) to avoid redundancy overflow check
8 participants