Skip to content

Commit

Permalink
fix(dbm-services): 补充tendbcluster create table 没有指定shardkey情况下分语法检查 #…
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq authored and zhangzhw8 committed Sep 14, 2024
1 parent ab6dc0f commit cd42c1a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion dbm-services/common/go-pubpkg/bkrepo/bkrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (b *BkRepoClient) QueryFileNodeInfo(filepath, filename string) (realData Fi
return
}

// UploadRespData TODO
// UploadRespData upload respone data
type UploadRespData struct {
Sha256 string `json:"sha256"`
Md5 string `json:"md5"`
Expand Down
10 changes: 8 additions & 2 deletions dbm-services/mysql/db-simulation/app/syntax/create_table_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ func (c CreateTableResult) GetTableCharset() (engine string) {

// GetAllColCharsets get columns define charset
func (c CreateTableResult) GetAllColCharsets() (charsets []string) {
haveDefaultCharseTypes := []string{
"timestamp", "time", "datetime", "date",
"tinyblob", "blob", "mediumblob", "longblob",
"string", "varchar", "json",
}
for _, colDef := range c.CreateDefinitions.ColDefs {
if lo.IsNotEmpty(colDef.CharacterSet) {
// Mysql会对MYSQL_TYPE_TIME,MYSQL_TYPE_TIMESTAMP,MYSQL_TYPE_DATETIME这三种类型的字段默认设置字符集为latin1
if lo.IsNotEmpty(colDef.CharacterSet) && !lo.Contains(haveDefaultCharseTypes, colDef.DataType) {
charsets = append(charsets, colDef.CharacterSet)
}
}
Expand All @@ -105,7 +111,7 @@ func (c CreateTableResult) ColCharsetNotEqTbCharset() bool {
if lo.IsEmpty(tableDefineCharset) {
return false
}
if strings.Compare(strings.ToUpper(colCharsets[0]), tableDefineCharset) == 0 {
if strings.Compare(strings.ToLower(colCharsets[0]), strings.ToLower(tableDefineCharset)) == 0 {
return false
}
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,19 @@ func (c CreateTableResult) SpiderChecker(spiderVersion string) (r *CheckerResult
}

// shardKeyChecker 分片键检查
// nolint
func (c CreateTableResult) shardKeyChecker(r *CheckerResult) {
var hasShardKey bool
var commentSpecialShardKey bool
var shardKeyCol string
var err error
var pubCols []string

if len(c.CreateDefinitions.KeyDefs) == 0 {
r.Trigger(SR.SpiderCreateTableRule.NoIndexExists, "")
return
}
hasPk, uks, keys := c.findTablesIndex()
_, uks, keys := c.findTablesIndex()
// 如果存在多个唯一健(含主键),多个唯一键都没有包含相同的字段也是不允许的
var pubCols []string
logger.Info("uniqueKeys is %v,len is %d", uks, len(uks))
if len(uks) > 1 {
pubCols = findCommonColByKeys(uks)
Expand All @@ -77,19 +79,23 @@ func (c CreateTableResult) shardKeyChecker(r *CheckerResult) {
}
shardKeyCol, err = util.ParseGetShardKeyForSpider(tableComment)
if err != nil {
// Todo 错误处理
logger.Error("parse %s comment %s shard key failed %s", c.TableName, tableComment, err.Error())
return
}
hasShardKey = true
commentSpecialShardKey = true
}
// 如果table comment 为空,表示没有指定shard key,或table comnent 没有指定shardkey 由中控自主选择
if !hasShardKey {
if !commentSpecialShardKey {
switch {
case hasPk:
return
case len(uks) >= 1:
case len(uks) == 1:
return
case len(uks) > 1:
// 如果没有显示的指定shard key,多个唯一键必须要包含相同的字段,且是第一个字段
pubPreCols := findCommonPreColByKeys(uks)
if len(pubPreCols) != 1 {
r.Trigger(SR.SpiderCreateTableRule.NoPubColAtMultUniqueIndex, "")
return
}
case len(keys) > 1:
// 如果没有唯一索引,如果包含多个普通索引,则必须指定shard_key,否则需要报错
r.Trigger(SR.SpiderCreateTableRule.MustSpecialShardKeyOnlyHaveCommonIndex, "")
Expand Down Expand Up @@ -136,6 +142,7 @@ func (c CreateTableResult) findTablesIndex() (hasPk bool, uks []KeyDef, keys []K
switch {
case key.PrimaryKey:
hasPk = true
uks = append(uks, key)
case key.UniqueKey:
uks = append(uks, key)
default:
Expand All @@ -161,6 +168,16 @@ func findCommonColByKeys(keys []KeyDef) (cols []string) {
return cols
}

// findCommonPreColByKeys 寻找多个唯一键中的公共前缀列
func findCommonPreColByKeys(keys []KeyDef) (cols []string) {
for _, key := range keys {
if len(key.KeyParts) > 0 {
cols = append(cols, key.KeyParts[0].ColName)
}
}
return lo.Uniq(cols)
}

func (c CreateTableResult) validateSpiderComment(comment string) (legal bool, prompt string) {
ret := util.ParseGetSpiderUserComment(comment)
switch ret {
Expand Down

0 comments on commit cd42c1a

Please sign in to comment.