提交 19a0bc41 编写于 作者: martianzhang's avatar martianzhang

fix test case, compatible with mysql 8.0.13

上级 05030a65
......@@ -104,7 +104,7 @@ func (db *Connector) Version() (int, error) {
version := 99999
// 从数据库中获取版本信息
res, err := db.Query("select @@version")
if err != nil{
if err != nil {
common.Log.Warn("(db *Connector) Version() Error: %v", err)
return version, err
}
......@@ -189,6 +189,10 @@ func (db *Connector) ColumnCardinality(tb, col string) float64 {
}
}
// 当table status元数据不准确时 rowTotal 可能远小于count(*),导致散粒度大于1
if colNum > float64(rowTotal) {
return 1
}
// 散粒度区间:[0,1]
return colNum / float64(rowTotal)
}
......
......@@ -77,8 +77,8 @@ func TestColumnCardinality(t *testing.T) {
orgDatabase := connTest.Database
connTest.Database = "sakila"
a := connTest.ColumnCardinality("actor", "first_name")
if a >= 1 || a <= 0 {
t.Error("sakila.actor.first_name cardinality should in (0, 1), now it's", a)
if a > 1 || a <= 0 {
t.Error("sakila.actor.first_name cardinality should in [0, 1], now it's", a)
}
connTest.Database = orgDatabase
}
......
......@@ -116,6 +116,7 @@ func (db *Connector) ShowTableStatus(tableName string) (*TableStatInfo, error) {
return tbStatus, err
}
// columns info
ts := tableStatusRow{}
statusFields := make([]interface{}, 0)
fields := map[string]interface{}{
......@@ -175,6 +176,7 @@ type TableIndexRow struct {
Comment string
IndexComment string
Visible string
Expression []byte
}
// NewTableIndexInfo 构造 TableIndexInfo
......@@ -195,23 +197,34 @@ func (db *Connector) ShowIndex(tableName string) (*TableIndexInfo, error) {
return nil, err
}
// columns info
ti := TableIndexRow{}
indexFields := make([]interface{}, 0)
fields := map[string]interface{}{
"Table": &ti.Table,
"Non_unique": &ti.NonUnique,
"Key_name": &ti.KeyName,
"Seq_in_index": &ti.SeqInIndex,
"Column_name": &ti.ColumnName,
"Collation": &ti.Collation,
"Cardinality": &ti.Cardinality,
"Sub_part": &ti.SubPart,
"Packed": &ti.Packed,
"Null": &ti.Null,
"Index_type": &ti.IndexType,
"Comment": &ti.Comment,
"Index_comment": &ti.IndexComment,
"Visible": &ti.Visible,
"Expression": &ti.Expression,
}
cols, err := res.Rows.Columns()
common.LogIfError(err, "")
for _, col := range cols {
indexFields = append(indexFields, fields[col])
}
// 获取值
for res.Rows.Next() {
var ti TableIndexRow
res.Rows.Scan(&ti.Table,
&ti.NonUnique,
&ti.KeyName,
&ti.SeqInIndex,
&ti.ColumnName,
&ti.Collation,
&ti.Cardinality,
&ti.SubPart,
&ti.Packed,
&ti.Null,
&ti.IndexType,
&ti.Comment,
&ti.IndexComment,
&ti.Visible)
res.Rows.Scan(indexFields...)
tbIndex.Rows = append(tbIndex.Rows, ti)
}
return tbIndex, err
......@@ -314,18 +327,29 @@ func (db *Connector) ShowColumns(tableName string) (*TableDesc, error) {
return nil, err
}
// columns info
tc := TableDescValue{}
columnFields := make([]interface{}, 0)
fields := map[string]interface{}{
"Field": &tc.Field,
"Type": &tc.Type,
"Collation": &tc.Collation,
"Null": &tc.Null,
"Key": &tc.Key,
"Default": &tc.Default,
"Extra": &tc.Extra,
"Privileges": &tc.Privileges,
"Comment": &tc.Comment,
}
cols, err := res.Rows.Columns()
common.LogIfError(err, "")
for _, col := range cols {
columnFields = append(columnFields, fields[col])
}
// 获取值
for res.Rows.Next() {
var tc TableDescValue
res.Rows.Scan(&tc.Field,
&tc.Type,
&tc.Collation,
&tc.Null,
&tc.Key,
&tc.Default,
&tc.Extra,
&tc.Privileges,
&tc.Comment)
res.Rows.Scan(columnFields...)
tbDesc.DescValues = append(tbDesc.DescValues, tc)
}
return tbDesc, err
......
......@@ -114,7 +114,7 @@ func TestShowIndex(t *testing.T) {
func TestShowColumns(t *testing.T) {
orgDatabase := connTest.Database
connTest.Database = "sakila"
ti, err := connTest.ShowColumns("film")
ti, err := connTest.ShowColumns("actor_info")
if err != nil {
t.Error("ShowColumns Error: ", err)
}
......
&database.TableDesc{
Name: "film",
Name: "actor_info",
DescValues: {
{
Field: "film_id",
Field: "actor_id",
Type: "smallint(5) unsigned",
Collation: nil,
Null: "NO",
Key: "PRI",
Default: nil,
Extra: "auto_increment",
Privileges: "select,insert,update,references",
Comment: "",
},
{
Field: "title",
Type: "varchar(255)",
Collation: {0x75, 0x74, 0x66, 0x38, 0x5f, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x5f, 0x63, 0x69},
Null: "NO",
Key: "MUL",
Default: nil,
Extra: "",
Privileges: "select,insert,update,references",
Comment: "",
},
{
Field: "description",
Type: "text",
Collation: {0x75, 0x74, 0x66, 0x38, 0x5f, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x5f, 0x63, 0x69},
Null: "YES",
Key: "",
Default: nil,
Extra: "",
Privileges: "select,insert,update,references",
Comment: "",
},
{
Field: "release_year",
Type: "year(4)",
Collation: nil,
Null: "YES",
Key: "",
Default: nil,
Extra: "",
Privileges: "select,insert,update,references",
Comment: "",
},
{
Field: "language_id",
Type: "tinyint(3) unsigned",
Collation: nil,
Null: "NO",
Key: "MUL",
Default: nil,
Extra: "",
Privileges: "select,insert,update,references",
Comment: "",
},
{
Field: "original_language_id",
Type: "tinyint(3) unsigned",
Collation: nil,
Null: "YES",
Key: "MUL",
Default: nil,
Extra: "",
Privileges: "select,insert,update,references",
Comment: "",
},
{
Field: "rental_duration",
Type: "tinyint(3) unsigned",
Collation: nil,
Null: "NO",
Key: "",
Default: {0x33},
Default: {0x30},
Extra: "",
Privileges: "select,insert,update,references",
Comment: "",
},
{
Field: "rental_rate",
Type: "decimal(4,2)",
Collation: nil,
Field: "first_name",
Type: "varchar(45)",
Collation: {0x75, 0x74, 0x66, 0x38, 0x5f, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x5f, 0x63, 0x69},
Null: "NO",
Key: "",
Default: {0x34, 0x2e, 0x39, 0x39},
Extra: "",
Privileges: "select,insert,update,references",
Comment: "",
},
{
Field: "length",
Type: "smallint(5) unsigned",
Collation: nil,
Null: "YES",
Key: "",
Default: nil,
Extra: "",
Privileges: "select,insert,update,references",
Comment: "",
},
{
Field: "replacement_cost",
Type: "decimal(5,2)",
Collation: nil,
Null: "NO",
Key: "",
Default: {0x31, 0x39, 0x2e, 0x39, 0x39},
Extra: "",
Privileges: "select,insert,update,references",
Comment: "",
},
{
Field: "rating",
Type: "enum('G','PG','PG-13','R','NC-17')",
Field: "last_name",
Type: "varchar(45)",
Collation: {0x75, 0x74, 0x66, 0x38, 0x5f, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x5f, 0x63, 0x69},
Null: "YES",
Null: "NO",
Key: "",
Default: {0x47},
Default: nil,
Extra: "",
Privileges: "select,insert,update,references",
Comment: "",
},
{
Field: "special_features",
Type: "set('Trailers','Commentaries','Deleted Scenes','Behind the Scenes')",
Field: "film_info",
Type: "text",
Collation: {0x75, 0x74, 0x66, 0x38, 0x5f, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x5f, 0x63, 0x69},
Null: "YES",
Key: "",
......@@ -133,16 +45,5 @@
Privileges: "select,insert,update,references",
Comment: "",
},
{
Field: "last_update",
Type: "timestamp",
Collation: nil,
Null: "NO",
Key: "",
Default: {0x43, 0x55, 0x52, 0x52, 0x45, 0x4e, 0x54, 0x5f, 0x54, 0x49, 0x4d, 0x45, 0x53, 0x54, 0x41, 0x4d, 0x50},
Extra: "on update CURRENT_TIMESTAMP",
Privileges: "select,insert,update,references",
Comment: "",
},
},
}
&database.TableIndexInfo{
TableName: "film",
Rows: {
{Table:"film", NonUnique:0, KeyName:"PRIMARY", SeqInIndex:1, ColumnName:"film_id", Collation:"A", Cardinality:1000, SubPart:0, Packed:0, Null:"", IndexType:"", Comment:"", IndexComment:"", Visible:""},
{Table:"film", NonUnique:1, KeyName:"idx_title", SeqInIndex:1, ColumnName:"title", Collation:"A", Cardinality:1000, SubPart:0, Packed:0, Null:"", IndexType:"", Comment:"", IndexComment:"", Visible:""},
{Table:"film", NonUnique:1, KeyName:"idx_fk_language_id", SeqInIndex:1, ColumnName:"language_id", Collation:"A", Cardinality:1, SubPart:0, Packed:0, Null:"", IndexType:"", Comment:"", IndexComment:"", Visible:""},
{Table:"film", NonUnique:1, KeyName:"idx_fk_original_language_id", SeqInIndex:1, ColumnName:"original_language_id", Collation:"A", Cardinality:1, SubPart:0, Packed:0, Null:"", IndexType:"", Comment:"", IndexComment:"", Visible:""},
{
Table: "film",
NonUnique: 0,
KeyName: "PRIMARY",
SeqInIndex: 1,
ColumnName: "film_id",
Collation: "A",
Cardinality: 1000,
SubPart: 0,
Packed: 0,
Null: "",
IndexType: "",
Comment: "",
IndexComment: "",
Visible: "",
Expression: nil,
},
{
Table: "film",
NonUnique: 1,
KeyName: "idx_title",
SeqInIndex: 1,
ColumnName: "title",
Collation: "A",
Cardinality: 1000,
SubPart: 0,
Packed: 0,
Null: "",
IndexType: "",
Comment: "",
IndexComment: "",
Visible: "",
Expression: nil,
},
{
Table: "film",
NonUnique: 1,
KeyName: "idx_fk_language_id",
SeqInIndex: 1,
ColumnName: "language_id",
Collation: "A",
Cardinality: 1,
SubPart: 0,
Packed: 0,
Null: "",
IndexType: "",
Comment: "",
IndexComment: "",
Visible: "",
Expression: nil,
},
{
Table: "film",
NonUnique: 1,
KeyName: "idx_fk_original_language_id",
SeqInIndex: 1,
ColumnName: "original_language_id",
Collation: "A",
Cardinality: 1,
SubPart: 0,
Packed: 0,
Null: "",
IndexType: "",
Comment: "",
IndexComment: "",
Visible: "",
Expression: nil,
},
},
}
[]database.TableIndexRow{
{Table:"film", NonUnique:1, KeyName:"idx_title", SeqInIndex:1, ColumnName:"title", Collation:"A", Cardinality:1000, SubPart:0, Packed:0, Null:"", IndexType:"", Comment:"", IndexComment:"", Visible:""},
{
Table: "film",
NonUnique: 1,
KeyName: "idx_title",
SeqInIndex: 1,
ColumnName: "title",
Collation: "A",
Cardinality: 1000,
SubPart: 0,
Packed: 0,
Null: "",
IndexType: "",
Comment: "",
IndexComment: "",
Visible: "",
Expression: nil,
},
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册