Skip to content

Commit

Permalink
*: forbid set TiFlash Replica for a table with unsupport charset (#30162
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Defined2014 authored Nov 30, 2021
1 parent 37e0dac commit a0f7643
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions ddl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions executor/tiflash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
9 changes: 9 additions & 0 deletions parser/charset/charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit a0f7643

Please sign in to comment.