diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index fd12090bc8b19..dab631c06e981 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -4721,6 +4721,14 @@ func (d *ddl) AlterTableSetTiFlashReplica(ctx sessionctx.Context, ident ast.Iden return ErrOptOnTemporaryTable.GenWithStackByArgs("set tiflash replica") } + // Ban setting replica count for tables which has charset not supported by TiFlash + for _, col := range tb.Cols() { + _, ok := charset.TiFlashSupportedCharsets[col.Charset] + if !ok { + return errAlterReplicaForUnsupportedCharsetTable.GenWithStackByArgs(col.Charset) + } + } + tbReplicaInfo := tb.Meta().TiFlashReplica if tbReplicaInfo != nil && tbReplicaInfo.Count == replicaInfo.Count && len(tbReplicaInfo.LocationLabels) == len(replicaInfo.Labels) { diff --git a/ddl/error.go b/ddl/error.go index b3eeb9655fc77..f95b5baf0e4a9 100644 --- a/ddl/error.go +++ b/ddl/error.go @@ -65,6 +65,8 @@ var ( errFkColumnCannotDrop = dbterror.ClassDDL.NewStd(mysql.ErrFkColumnCannotDrop) errFKIncompatibleColumns = dbterror.ClassDDL.NewStd(mysql.ErrFKIncompatibleColumns) + errAlterReplicaForUnsupportedCharsetTable = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message(fmt.Sprintf(mysql.MySQLErrName[mysql.ErrUnsupportedDDLOperation].Raw, "ALTER table replica for table contain %s charset"), nil)) + errOnlyOnRangeListPartition = dbterror.ClassDDL.NewStd(mysql.ErrOnlyOnRangeListPartition) // errWrongKeyColumn is for table column cannot be indexed. errWrongKeyColumn = dbterror.ClassDDL.NewStd(mysql.ErrWrongKeyColumn) diff --git a/executor/tiflash_test.go b/executor/tiflash_test.go index 5bc9503cc29ff..b394d36c6da5b 100644 --- a/executor/tiflash_test.go +++ b/executor/tiflash_test.go @@ -37,6 +37,7 @@ import ( "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/store/mockstore/unistore" + "github.com/pingcap/tidb/util/collate" "github.com/pingcap/tidb/util/israce" "github.com/pingcap/tidb/util/kvcache" "github.com/pingcap/tidb/util/testkit" @@ -84,6 +85,22 @@ func (s *tiflashTestSuite) TearDownSuite(c *C) { c.Assert(s.store.Close(), IsNil) } +func (s *tiflashTestSuite) TestNonsupportCharsetTable(c *C) { + collate.SetCharsetFeatEnabledForTest(true) + defer collate.SetCharsetFeatEnabledForTest(false) + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int, b char(10) charset gbk collate gbk_bin)") + err := tk.ExecToErr("alter table t set tiflash replica 1") + c.Assert(err, NotNil) + c.Assert(err.Error(), Equals, "[ddl:8200]Unsupported ALTER table replica for table contain gbk charset") + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a char(10) charset utf8)") + tk.MustExec("alter table t set tiflash replica 1") +} + func (s *tiflashTestSuite) TestReadPartitionTable(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") diff --git a/parser/charset/charset.go b/parser/charset/charset.go index e47a65d4c89df..091800dd63003 100644 --- a/parser/charset/charset.go +++ b/parser/charset/charset.go @@ -68,6 +68,15 @@ var supportedCollationNames = map[string]struct{}{ CollationBin: {}, } +// TiFlashSupportedCharsets is a map which contains TiFlash supports charsets. +var TiFlashSupportedCharsets = map[string]struct{}{ + CharsetUTF8: {}, + CharsetUTF8MB4: {}, + CharsetASCII: {}, + CharsetLatin1: {}, + CharsetBin: {}, +} + // GetSupportedCharsets gets descriptions for all charsets supported so far. func GetSupportedCharsets() []*Charset { charsets := make([]*Charset, 0, len(charsetInfos))