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

ddl: fix corrupted default value for bit type column #18036

Merged
merged 31 commits into from
Oct 4, 2020

Conversation

tangenta
Copy link
Contributor

@tangenta tangenta commented Jun 15, 2020

What problem does this PR solve?

Issue Number: close #17641 close #17642

Problem Summary:

The ColumnInfo.OriginDefaultValue is corrupted when the column type is BIT. BIT type column values are stored as strings by JSON marshaling and read it by unmarshaling later. However, the data in a string can be changed in this case:

originBytes := []byte{25, 185}
originStr := string(originBytes)

mr, _ := json.Marshal(originStr)
var jsoned string
_ = json.Unmarshal(mr, &jsoned)

fmt.Printf("%v\n", []byte(originStr))
fmt.Printf("%v\n", []byte(jsoned))
[25 185]
[25 239 191 189]

If we store it as []byte, the problem disappeared. This is how DefaultValueBit works.

type ColumnInfo struct {
	// ...
	OriginDefaultValue    interface{} `json:"origin_default"`
	DefaultValue          interface{} `json:"default"`
	DefaultValueBit       []byte      `json:"default_bit"`
	// ...
}

The same problem exists in OriginDefaultValue. PR #12168 tried to restore the OriginDefaultValue from DefaultValueBit in the getter, but OriginDefaultValue is different from DefaultValue: OriginDefaultValue is used to skip the backfilling phase of AddColumn(). If a column's default value is modified by ALTER TABLE MODIFY COLUMN DEFAULT, they are not equal anymore(see the example in #17641).

What is changed and how it works?

This PR adds a field OriginDefaultValueBit to ColumnInfo, which borrows from DefaultValueBit to solve the problem completely.

Proposal: xxx

What's Changed:

How it Works:
If the column type is BIT, set/get the content with OriginDefaultValueBit.

Related changes

  • Need to cherry-pick to the release branch

Check List

Tests

  • Unit test
  • Integration test

Side effects

Release note

  • fix corrupted default value for bit type column.

@tangenta tangenta requested a review from a team as a code owner June 18, 2020 08:48
@tangenta tangenta requested review from XuHuaiyu and removed request for a team June 18, 2020 08:48
Copy link
Member

@wjhuang2016 wjhuang2016 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@zimulala zimulala left a comment

Choose a reason for hiding this comment

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

LGTM

@zimulala zimulala added status/LGT2 Indicates that a PR has LGTM 2. type/bugfix This PR fixes a bug. labels Jun 22, 2020
@zimulala
Copy link
Contributor

Do we need cherry-pick this PR to v4.0, v3.1, and v3.0?

@tangenta
Copy link
Contributor Author

Do we need cherry-pick this PR to v4.0, v3.1, and v3.0?

Sure. Let's pick manually since there will be lots of conflicts(the parser also needs to be picked).

@tangenta tangenta requested a review from djshow832 June 23, 2020 11:54
@codecov
Copy link

codecov bot commented Jun 24, 2020

Codecov Report

Merging #18036 into master will increase coverage by 0.1610%.
The diff coverage is n/a.

@@               Coverage Diff                @@
##             master     #18036        +/-   ##
================================================
+ Coverage   79.3606%   79.5217%   +0.1610%     
================================================
  Files           546        546                
  Lines        150465     149710       -755     
================================================
- Hits         119410     119052       -358     
+ Misses        21524      21141       -383     
+ Partials       9531       9517        -14     

djshow832
djshow832 previously approved these changes Jun 24, 2020
Copy link
Contributor

@djshow832 djshow832 left a comment

Choose a reason for hiding this comment

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

LGTM

@djshow832
Copy link
Contributor

/merge

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

Your auto merge job has been accepted, waiting for:

  • 18180

@ti-srebot
Copy link
Contributor

/run-all-tests

@ti-srebot
Copy link
Contributor

/run-all-tests

@zz-jason
Copy link
Member

zz-jason commented Sep 9, 2020

/merge

@ti-srebot
Copy link
Contributor

Your auto merge job has been accepted, waiting for:

  • 19675
  • 19775
  • 19455

@ti-srebot
Copy link
Contributor

/run-all-tests

@AndreMouche
Copy link
Contributor

LGTM

@ti-srebot
Copy link
Contributor

@AndreMouche,Thanks for your review. The bot only counts LGTMs from Reviewers and higher roles, but you're still welcome to leave your comments.See the corresponding SIG page for more information. Related SIG: ddl(slack).

@ti-srebot
Copy link
Contributor

@tangenta merge failed.

@ti-srebot
Copy link
Contributor

@tangenta, please update your pull request.

@XuHuaiyu XuHuaiyu removed their request for review September 17, 2020 03:18
@ti-srebot
Copy link
Contributor

@tangenta, please update your pull request.

@zz-jason
Copy link
Member

zz-jason commented Oct 4, 2020

/merge

@ti-srebot
Copy link
Contributor

Your auto merge job has been accepted, waiting for:

  • 19535

@ti-srebot
Copy link
Contributor

/run-all-tests

@ti-srebot ti-srebot merged commit 6342fa6 into pingcap:master Oct 4, 2020
ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Oct 4, 2020
@ti-srebot
Copy link
Contributor

cherry pick to release-3.0 in PR #20339

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Oct 4, 2020
@ti-srebot
Copy link
Contributor

cherry pick to release-4.0 in PR #20340

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/sql-infra SIG: SQL Infra 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.

Possible corrupted bit default value Incorrect default value for bit type column
7 participants