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

fix(dbm-services): 补充tendbcluster create table 没有指定shardkey情况下分语法检查 #6840 #6922

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading