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 alter table charset bug that change blob column type to text #10477

Merged
merged 10 commits into from
Jun 4, 2019
11 changes: 11 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testutil"
)

var _ = Suite(&testIntegrationSuite1{&testIntegrationSuite{}})
Expand Down Expand Up @@ -738,6 +739,16 @@ func (s *testIntegrationSuite10) TestChangingTableCharset(c *C) {
c.Assert(tbl.Meta().Columns[0].Collate, Equals, "")
tk.MustExec("alter table t convert to charset utf8mb4;")
checkCharset()

tk.MustExec("drop table t")
tk.MustExec("create table t (a blob) character set utf8;")
tk.MustExec("alter table t charset=utf8mb4 collate=utf8mb4_bin;")
tk.MustQuery("show create table t").Check(testutil.RowsWithSep("|",
"t CREATE TABLE `t` (\n"+
" `a` blob DEFAULT NULL\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))

}

func (s *testIntegrationSuite7) TestCaseInsensitiveCharsetAndCollate(c *C) {
Expand Down
3 changes: 2 additions & 1 deletion ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/parser/charset"
"github.com/pingcap/parser/model"
field_types "github.com/pingcap/parser/types"
"github.com/pingcap/tidb/ddl/util"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -744,7 +745,7 @@ func onModifyTableCharsetAndCollate(t *meta.Meta, job *model.Job) (ver int64, _
tblInfo.Collate = toCollate
// update column charset.
for _, col := range tblInfo.Columns {
if typesNeedCharset(col.Tp) {
if field_types.HasCharset(&col.FieldType) {
Copy link
Member

Choose a reason for hiding this comment

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

I guess typesNeedCharset can be replaced by HasCharset. Another code that calls this function is:

if typesNeedCharset(tp.Tp) {

Can you confirm it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No,

if typesNeedCharset(tp.Tp) {
here may be have not set the column Flag, use HasCharset here may be got some error.

Copy link
Contributor

Choose a reason for hiding this comment

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

so why you use HashCharset finally?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

HashCharset can be used here(ddl/table.go #739), but ddl/ddl_api.go #299 should use typesNeedCharset function.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I still don't quite understand, why is it correct to change to HasCharset?

col.Charset = toCharset
col.Collate = toCollate
} else {
Expand Down