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

types: fix wrong hash key for decimal #19131

Merged

Conversation

lzmhhh123
Copy link
Contributor

What problem does this PR solve?

Issue Number: close #19112

What is changed and how it works?

What's Changed: MyDecimal.ToHashKey should consider the length of int and Frac.

Related changes

  • PR to update pingcap/docs/pingcap/docs-cn:
  • Need to cherry-pick to the release branch

Check List

Tests

  • Unit test
  • Integration test

Side effects

  • Performance regression
    • Consumes more CPU
    • Consumes more MEM

Release note

  • fix wrong hash key for decimal

@lzmhhh123 lzmhhh123 requested a review from a team as a code owner August 11, 2020 08:09
@lzmhhh123 lzmhhh123 requested review from XuHuaiyu and removed request for a team August 11, 2020 08:09
@lzmhhh123 lzmhhh123 added component/expression needs-cherry-pick-3.0 type/bugfix This PR fixes a bug. priority/release-blocker This issue blocks a release. Please solve it ASAP. labels Aug 11, 2020
@lzmhhh123 lzmhhh123 requested a review from qw4990 August 11, 2020 08:11
Copy link
Contributor

@qw4990 qw4990 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-srebot ti-srebot added the status/LGT1 Indicates that a PR has LGTM 1. label Aug 11, 2020
@lzmhhh123
Copy link
Contributor Author

/run-all-tests

types/mydecimal.go Outdated Show resolved Hide resolved
@breezewish
Copy link
Member

This test case shows that current implementation is still incorrect:

func TestDec(t *testing.T) {
	dec1 := &MyDecimal{}
	if err := dec1.FromString([]byte(".1is column 'ommand den6ser '%-956465")); err != nil {
		panic("dec1")
	}

	dec2 := &MyDecimal{}
	if err := dec2.FromString([]byte("0.10s e8s'@'%-.664000000000000000000000000000")); err != nil {
		panic("dec2")
	}

	if dec1.Compare(dec2) != 0 {
		panic("dec not equal")
	}

	h1, err := dec1.ToHashKey()
	if err != nil {
		panic(err)
	}

	h2, err := dec2.ToHashKey()
	if err != nil {
		panic(err)
	}

	if bytes.Compare(h1, h2) != 0 {
		panic("dec1 eq dec2 but dec1 hash not eq dec2 hash")
	}
}
panic: dec1 eq dec2 but dec1 hash not eq dec2 hash

@lzmhhh123
Copy link
Contributor Author

This test case shows that current implementation is still incorrect:

func TestDec(t *testing.T) {
	dec1 := &MyDecimal{}
	if err := dec1.FromString([]byte(".1is column 'ommand den6ser '%-956465")); err != nil {
		panic("dec1")
	}

	dec2 := &MyDecimal{}
	if err := dec2.FromString([]byte("0.10s e8s'@'%-.664000000000000000000000000000")); err != nil {
		panic("dec2")
	}

	if dec1.Compare(dec2) != 0 {
		panic("dec not equal")
	}

	h1, err := dec1.ToHashKey()
	if err != nil {
		panic(err)
	}

	h2, err := dec2.ToHashKey()
	if err != nil {
		panic(err)
	}

	if bytes.Compare(h1, h2) != 0 {
		panic("dec1 eq dec2 but dec1 hash not eq dec2 hash")
	}
}
panic: dec1 eq dec2 but dec1 hash not eq dec2 hash

0.1 and 0.10 have different hash key. It's another bug.

types/mydecimal.go Outdated Show resolved Hide resolved
@lzmhhh123
Copy link
Contributor Author

@breeswish @SunRunAway PTAL. Already fixed.

@breezewish
Copy link
Member

breezewish commented Aug 12, 2020

Here is a case that value eq but hash not eq using the latest code base:

dec1 = 0000000000000000000000000000000000000000000.0000000000005987000000000000000/000000000000000000000000000000000000
dec2 = 00000000000000000000000000000.000000000000598700

@breezewish
Copy link
Member

I found some new bugs. "ToHashKey" function will return errors in some cases. I'm not sure whether this case should be handled:

dec1 = 4e-41
dec2 = 4e-41

@lzmhhh123
Copy link
Contributor Author

I found some new bugs. "ToHashKey" function will return errors in some cases. I'm not sure whether this case should be handled:

dec1 = 4e-41
dec2 = 4e-41

TiDB's MaxDecimalScale is 30.

breezewish
breezewish previously approved these changes Aug 12, 2020
Copy link
Member

@breezewish breezewish left a comment

Choose a reason for hiding this comment

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

Approve since fuzz test doesn't show anything wrong now.

@codecov
Copy link

codecov bot commented Aug 12, 2020

Codecov Report

Merging #19131 into master will decrease coverage by 0.1248%.
The diff coverage is n/a.

@@               Coverage Diff                @@
##             master     #19131        +/-   ##
================================================
- Coverage   79.3723%   79.2474%   -0.1249%     
================================================
  Files           549        550         +1     
  Lines        150647     150391       -256     
================================================
- Hits         119572     119181       -391     
- Misses        21601      21680        +79     
- Partials       9474       9530        +56     

types/mydecimal.go Outdated Show resolved Hide resolved
types/mydecimal.go Show resolved Hide resolved
@ichn-hu
Copy link
Contributor

ichn-hu commented Aug 13, 2020

This PR could close #17216

Would you please add a test case for this issue? It has a minial test case here #17216 (comment)

Copy link
Contributor

@SunRunAway SunRunAway 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-srebot ti-srebot removed the status/LGT1 Indicates that a PR has LGTM 1. label Aug 13, 2020
@ti-srebot ti-srebot added the status/LGT2 Indicates that a PR has LGTM 2. label Aug 13, 2020
@SunRunAway
Copy link
Contributor

/merge

@ti-srebot ti-srebot added the status/can-merge Indicates a PR has been approved by a committer. label Aug 13, 2020
@ti-srebot
Copy link
Contributor

/run-all-tests

@ti-srebot ti-srebot merged commit 0448a54 into pingcap:master Aug 13, 2020
ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Aug 13, 2020
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-3.0 in PR #19185

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Aug 13, 2020
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-3.1 in PR #19187

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Aug 13, 2020
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-4.0 in PR #19188

@lzmhhh123 lzmhhh123 deleted the bug-fix/fix_wrong_hash_key_for_decimal branch August 14, 2020 04:52
ti-srebot added a commit that referenced this pull request Aug 14, 2020
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
jebter added a commit that referenced this pull request Aug 18, 2020
* cherry pick #19131 to release-4.0

Signed-off-by: ti-srebot <ti-srebot@pingcap.com>

* fix conflicts

Co-authored-by: Zhuomin(Charming) Liu <lzmhhh123@gmail.com>
Co-authored-by: jebter <jebter@126.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression priority/release-blocker This issue blocks a release. Please solve it ASAP. 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
6 participants