From 019ee551a63bfed0be2eae1fb738b05a4c188f0b Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Wed, 19 Dec 2018 21:54:50 +0800 Subject: [PATCH] try to fix Rows.Scan with NULL able column --- advisor/index.go | 3 +-- database/show.go | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/advisor/index.go b/advisor/index.go index 9a2ffc4..9758468 100644 --- a/advisor/index.go +++ b/advisor/index.go @@ -785,7 +785,6 @@ func CompleteColumnsInfo(stmt sqlparser.Statement, cols []*common.Column, env *e if find { break } - } // 如果不依赖env环境,利用ast中包含的信息推理列的库表信息 @@ -904,7 +903,7 @@ func (idxAdv *IndexAdvisor) calcCardinality(cols []*common.Column) []*common.Col // 如果是不存在的表就会报错,报错的可能性有三个: // 1.数据库错误 2.表不存在 3.临时表 // 而这三种错误都是不需要在这一层关注的,直接跳过 - common.Log.Debug("calcCardinality error: %v", err) + common.Log.Warn("calcCardinality error: %v", err) continue } diff --git a/database/show.go b/database/show.go index d5c2dfc..db46043 100644 --- a/database/show.go +++ b/database/show.go @@ -18,10 +18,11 @@ package database import ( "fmt" - "github.com/XiaoMi/soar/common" "regexp" "strconv" "strings" + + "github.com/XiaoMi/soar/common" ) // SHOW TABLE STATUS Syntax @@ -191,6 +192,10 @@ func NewTableIndexInfo(tableName string) *TableIndexInfo { func (db *Connector) ShowIndex(tableName string) (*TableIndexInfo, error) { tbIndex := NewTableIndexInfo(tableName) + if db.Database == "" || tableName == "" { + return nil, fmt.Errorf("database('%s') or table('%s') name should not empty", db.Database, tableName) + } + // 执行 show create table res, err := db.Query(fmt.Sprintf("show index from `%s`.`%s`", db.Database, tableName)) if err != nil { @@ -456,12 +461,14 @@ func (db *Connector) FindColumn(name, dbName string, tables ...string) ([]*commo var col common.Column for res.Rows.Next() { + var character, collation string res.Rows.Scan(&col.Table, &col.DB, &col.DataType, - &col.Character, - &col.Collation) - + &character, + &collation) + col.Character = character + col.Collation = collation // 填充字符集和排序规则 if col.Character == "" { // 当从`INFORMATION_SCHEMA`.`COLUMNS`表中查询不到相关列的character和collation的信息时 @@ -479,13 +486,13 @@ func (db *Connector) FindColumn(name, dbName string, tables ...string) ([]*commo return columns, err } - var tbCollation string + var tbCollation []byte if newRes.Rows.Next() { newRes.Rows.Scan(&tbCollation) } - if tbCollation != "" { - col.Character = strings.Split(tbCollation, "_")[0] - col.Collation = tbCollation + if string(tbCollation) != "" { + col.Character = strings.Split(string(tbCollation), "_")[0] + col.Collation = string(tbCollation) } } columns = append(columns, &col) -- GitLab