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 column collate should use table's if it has when create table or alter table. #13119

Merged
merged 10 commits into from
Nov 6, 2019

Conversation

AilinKid
Copy link
Contributor

@AilinKid AilinKid commented Nov 4, 2019

What problem does this PR solve?

fix issue #13113
When creating a table, if we set a collate for this table, all columns in this table should use this collate instead of using default collates.
In MySQL:

mysql> show create table t;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t     | CREATE TABLE `t` (
  `id` int(11) NOT NULL,
  `c1` varchar(10) COLLATE utf8_slovak_ci DEFAULT NULL,
  `c2` varchar(20) COLLATE utf8_slovak_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_slovak_ci |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

In TiDB

mysql> create table t (id int(11) primary key ,c1 varchar(10) ,c2 varchar(20)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_slovak_ci;
Query OK, 0 rows affected (0.01 sec)

mysql> show create table t;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                     |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t     | CREATE TABLE `t` (
  `id` int(11) NOT NULL,
  `c1` varchar(10) COLLATE utf8_bin DEFAULT NULL,
  `c2` varchar(20) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_slovak_ci |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

What is changed and how it works?

Table's collate should be passed into buildColumnsAndConstraints when in create table or alter table.

Check List

Tests

  • Unit test
  • Integration test

Code changes

  • add parameter for buildColumnsAndConstraints function.

Related changes

  • Need to cherry-pick to the release branch

Release note

  • fix column collate should use table's if it has when create table or alter table.

@codecov
Copy link

codecov bot commented Nov 4, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@a28fc71). Click here to learn what that means.
The diff coverage is n/a.

@@             Coverage Diff             @@
##             master     #13119   +/-   ##
===========================================
  Coverage          ?   80.7511%           
===========================================
  Files             ?        468           
  Lines             ?     114417           
  Branches          ?          0           
===========================================
  Hits              ?      92393           
  Misses            ?      15134           
  Partials          ?       6890

@AilinKid
Copy link
Contributor Author

AilinKid commented Nov 4, 2019

/rebuild

@AilinKid
Copy link
Contributor Author

AilinKid commented Nov 4, 2019

/run-unit-test

@@ -106,7 +111,12 @@ func (s *testStateChangeSuite) TestShowCreateTable(c *C) {
currTestCaseOffset++
}
if job.SchemaState != model.StatePublic {
result := tk.MustQuery("show create table t")
var result *testkit.Result
if job.Query[12:14] == "t2" {
Copy link
Contributor

Choose a reason for hiding this comment

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

How about use t2 := testGetTableByName(c, tk.Se, "test", "t2") then compare the job.TableID == t2.Meta().ID

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice idea.

Copy link
Contributor

@crazycs520 crazycs520 left a comment

Choose a reason for hiding this comment

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

REST LGTM

@AilinKid AilinKid added the status/LGT1 Indicates that a PR has LGTM 1. label Nov 5, 2019
ddl/ddl_api.go Show resolved Hide resolved
@@ -106,7 +111,12 @@ func (s *testStateChangeSuite) TestShowCreateTable(c *C) {
currTestCaseOffset++
}
if job.SchemaState != model.StatePublic {
result := tk.MustQuery("show create table t")
var result *testkit.Result
if job.Query[12:14] == "t2" {

This comment was marked as resolved.

ddl/ddl_api.go Show resolved Hide resolved
}
}

return tableCharset
return tableCharset, tableCollate
Copy link
Contributor

Choose a reason for hiding this comment

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

I think using two loops is more expressive when there is little impact on the performance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there is one loop with two if judgment.

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

@tangenta tangenta left a comment

Choose a reason for hiding this comment

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

LGTM

@tangenta tangenta added status/LGT2 Indicates that a PR has LGTM 2. component/DDL-need-LGT3 and removed status/LGT1 Indicates that a PR has LGTM 1. labels Nov 5, 2019
Copy link
Contributor

@crazycs520 crazycs520 left a comment

Choose a reason for hiding this comment

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

LGTM

@crazycs520
Copy link
Contributor

/run-all-tests

@AilinKid
Copy link
Contributor Author

AilinKid commented Nov 6, 2019

this pr need fix mysql_test as well

@Deardrops Deardrops added sig/transaction SIG:Transaction status/LGT3 The PR has already had 3 LGTM. and removed status/LGT2 Indicates that a PR has LGTM 2. sig/transaction SIG:Transaction labels Nov 6, 2019
@AilinKid
Copy link
Contributor Author

AilinKid commented Nov 6, 2019

/run-common-test

@AilinKid
Copy link
Contributor Author

AilinKid commented Nov 6, 2019

/run-integration-common-test

@sre-bot
Copy link
Contributor

sre-bot commented Nov 6, 2019

cherry pick to release-3.0 in PR #13174

@sre-bot
Copy link
Contributor

sre-bot commented Nov 6, 2019

cherry pick to release-3.1 in PR #13175

@sre-bot
Copy link
Contributor

sre-bot commented Nov 6, 2019

cherry pick to release-2.1 failed

@@ -1759,18 +1768,30 @@ func resolveDefaultTableCharsetAndCollation(tbInfo *model.TableInfo, dbCharset s
return
}

func findTableOptionCharset(options []*ast.TableOption) string {
var tableCharset string
func findTableOptionCharsetAndCollate(options []*ast.TableOption) (tableCharset, tableCollate string) {
Copy link
Member

Choose a reason for hiding this comment

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

Please use this function instead:

func getCharsetAndCollateInTableOption(startIdx int, options []*ast.TableOption) (ca, co string, err error) {

I think we can remove findTableOptionCharsetAndCollate

AilinKid added a commit to AilinKid/tidb that referenced this pull request Nov 6, 2019
…le or alter table. (pingcap#13119)

fix column's collation should use table's collation
bb7133 pushed a commit that referenced this pull request Nov 6, 2019
bb7133 pushed a commit that referenced this pull request Nov 6, 2019
bb7133 pushed a commit that referenced this pull request Nov 6, 2019
XiaTianliang pushed a commit to XiaTianliang/tidb that referenced this pull request Dec 21, 2019
…le or alter table. (pingcap#13119)

fix column's collation should use table's collation
@you06 you06 added the sig/sql-infra SIG: SQL Infra label Mar 4, 2020
@sre-bot
Copy link
Contributor

sre-bot commented Apr 7, 2020

It seems that, not for sure, we failed to cherry-pick this commit to release-2.1 release-3.0. Please comment '/run-cherry-picker' to try to trigger the cherry-picker if we did fail to cherry-pick this commit before. @AilinKid PTAL.

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/LGT3 The PR has already had 3 LGTM. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants