提交 019ee551 编写于 作者: martianzhang's avatar martianzhang

try to fix Rows.Scan with NULL able column

上级 19a0bc41
...@@ -785,7 +785,6 @@ func CompleteColumnsInfo(stmt sqlparser.Statement, cols []*common.Column, env *e ...@@ -785,7 +785,6 @@ func CompleteColumnsInfo(stmt sqlparser.Statement, cols []*common.Column, env *e
if find { if find {
break break
} }
} }
// 如果不依赖env环境,利用ast中包含的信息推理列的库表信息 // 如果不依赖env环境,利用ast中包含的信息推理列的库表信息
...@@ -904,7 +903,7 @@ func (idxAdv *IndexAdvisor) calcCardinality(cols []*common.Column) []*common.Col ...@@ -904,7 +903,7 @@ func (idxAdv *IndexAdvisor) calcCardinality(cols []*common.Column) []*common.Col
// 如果是不存在的表就会报错,报错的可能性有三个: // 如果是不存在的表就会报错,报错的可能性有三个:
// 1.数据库错误 2.表不存在 3.临时表 // 1.数据库错误 2.表不存在 3.临时表
// 而这三种错误都是不需要在这一层关注的,直接跳过 // 而这三种错误都是不需要在这一层关注的,直接跳过
common.Log.Debug("calcCardinality error: %v", err) common.Log.Warn("calcCardinality error: %v", err)
continue continue
} }
......
...@@ -18,10 +18,11 @@ package database ...@@ -18,10 +18,11 @@ package database
import ( import (
"fmt" "fmt"
"github.com/XiaoMi/soar/common"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"github.com/XiaoMi/soar/common"
) )
// SHOW TABLE STATUS Syntax // SHOW TABLE STATUS Syntax
...@@ -191,6 +192,10 @@ func NewTableIndexInfo(tableName string) *TableIndexInfo { ...@@ -191,6 +192,10 @@ func NewTableIndexInfo(tableName string) *TableIndexInfo {
func (db *Connector) ShowIndex(tableName string) (*TableIndexInfo, error) { func (db *Connector) ShowIndex(tableName string) (*TableIndexInfo, error) {
tbIndex := NewTableIndexInfo(tableName) 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 // 执行 show create table
res, err := db.Query(fmt.Sprintf("show index from `%s`.`%s`", db.Database, tableName)) res, err := db.Query(fmt.Sprintf("show index from `%s`.`%s`", db.Database, tableName))
if err != nil { if err != nil {
...@@ -456,12 +461,14 @@ func (db *Connector) FindColumn(name, dbName string, tables ...string) ([]*commo ...@@ -456,12 +461,14 @@ func (db *Connector) FindColumn(name, dbName string, tables ...string) ([]*commo
var col common.Column var col common.Column
for res.Rows.Next() { for res.Rows.Next() {
var character, collation string
res.Rows.Scan(&col.Table, res.Rows.Scan(&col.Table,
&col.DB, &col.DB,
&col.DataType, &col.DataType,
&col.Character, &character,
&col.Collation) &collation)
col.Character = character
col.Collation = collation
// 填充字符集和排序规则 // 填充字符集和排序规则
if col.Character == "" { if col.Character == "" {
// 当从`INFORMATION_SCHEMA`.`COLUMNS`表中查询不到相关列的character和collation的信息时 // 当从`INFORMATION_SCHEMA`.`COLUMNS`表中查询不到相关列的character和collation的信息时
...@@ -479,13 +486,13 @@ func (db *Connector) FindColumn(name, dbName string, tables ...string) ([]*commo ...@@ -479,13 +486,13 @@ func (db *Connector) FindColumn(name, dbName string, tables ...string) ([]*commo
return columns, err return columns, err
} }
var tbCollation string var tbCollation []byte
if newRes.Rows.Next() { if newRes.Rows.Next() {
newRes.Rows.Scan(&tbCollation) newRes.Rows.Scan(&tbCollation)
} }
if tbCollation != "" { if string(tbCollation) != "" {
col.Character = strings.Split(tbCollation, "_")[0] col.Character = strings.Split(string(tbCollation), "_")[0]
col.Collation = tbCollation col.Collation = string(tbCollation)
} }
} }
columns = append(columns, &col) columns = append(columns, &col)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册