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 bug that UnionScan can't keep order caused wrong result (#33218) #33319

Merged
merged 2 commits into from
Mar 24, 2022

Conversation

ti-srebot
Copy link
Contributor

@ti-srebot ti-srebot commented Mar 22, 2022

cherry-pick #33218 to release-6.0
You can switch your code base to this Pull Request by using git-extras:

# In tidb repo:
git pr https://github.com/pingcap/tidb/pull/33319

After apply modifications, you can push your change to this PR via:

git push git@github.com:ti-srebot/tidb.git pr/33319:release-6.0-eb002463f5eb

What problem does this PR solve?

Issue Number: close #33175

Problem Summary:

What is changed and how it works?

Actually, there are two bugs that cause the UnionScan fail to keep order,
and since it can't keep order, the following plan return wrong result.
Limit1->UnionScan->XXX doesn't return the max(id)

mysql> explain select max(id) from t;
+------------------------------+---------+-----------+---------------+-------------------------------------+
| id                           | estRows | task      | access object | operator info                       |
+------------------------------+---------+-----------+---------------+-------------------------------------+
| StreamAgg_10                 | 1.00    | root      |               | funcs:max([test.t.id](http://test.t.id/))->Column#3      |
| └─Limit_17                   | 1.00    | root      |               | offset:0, count:1                   |
|   └─UnionScan_21             | 1.00    | root      |               |                                     |
|     └─TableReader_23         | 1.00    | root      |               | data:TableFullScan_22               |
|       └─TableFullScan_22     | 1.00    | cop[tikv] | table:t       | keep order:true, desc, stats:pseudo |
+------------------------------+---------+-----------+---------------+-------------------------------------+
5 rows in set (0.00 sec)

The first bug is the datum comparing, the data type is KindUint64, but the old code treat it as GetInt64(),
10353107668348738101 become a negative number and 10353107668348738101 < 33

The second bug is the handling of kv range, in TiKV, the range is singed int
Here the data type is uint, so correct range handling is:

handling range: 7480000000000000415f720000000000000000 7480000000000000415f728000000000000000
handling range: 7480000000000000415f728000000000000000 7480000000000000415f72ffffffffffffffff00
add Row is nil, get snapshot row... [KindUint64 10353107668348738101]
add Row is nil, get snapshot row... [KindUint64 9734095886065816707]
add Row is nil, get snapshot row... [KindUint64 0]

the wrong range handling is:

handling range: 7480000000000000415f728000000000000000 7480000000000000415f72ffffffffffffffff00
in iterator ... add row == [KindUint64 0]
handling range: 7480000000000000415f720000000000000000 7480000000000000415f728000000000000000
in iterator ... add row == [KindUint64 9734095886065816707]
in iterator ... add row == [KindUint64 10353107668348738101]

i.e. the kv range order should be [-max int64, 0] [0, max int64]

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 bug that 'select max(PK) ...' query return wrong result, the trigger condition is that UnionScan is used and the type of `PK` is uint64

Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-chi-bot
Copy link
Member

ti-chi-bot commented Mar 22, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • qw4990
  • winoros

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-srebot
Copy link
Contributor Author

/run-all-tests

@ti-chi-bot ti-chi-bot added do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Mar 22, 2022
@ti-srebot ti-srebot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. type/6.0-cherry-pick labels Mar 22, 2022
@ti-srebot
Copy link
Contributor Author

@tiancaiamao you're already a collaborator in bot's repo.

@VelocityLight VelocityLight added the cherry-pick-approved Cherry pick PR approved by release team. label Mar 23, 2022
@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Mar 23, 2022
@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 23, 2022
@VelocityLight
Copy link

/merge

@ti-chi-bot
Copy link
Member

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

Commit hash: 9e50c98

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

@ti-srebot: Your PR was out of date, I have automatically updated it for you.

At the same time I will also trigger all tests for you:

/run-all-tests

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.

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.

@hawkingrei
Copy link
Member

/run-unit-test

@sre-bot
Copy link
Contributor

sre-bot commented Mar 23, 2022

@ti-chi-bot ti-chi-bot merged commit 4938619 into pingcap:release-6.0 Mar 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherry-pick-approved Cherry pick PR approved by release team. 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. type/6.0-cherry-pick
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants