Skip to content

Commit

Permalink
admin: fix admin check table compare bug (pingcap#7818)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 committed Oct 21, 2018
1 parent 60364fe commit 0a0d676
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 8 additions & 0 deletions executor/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,14 @@ func (s *testSuite) TestAdminCheckTable(c *C) {
tk.MustExec(`ALTER TABLE t1 ADD INDEX idx5 (c5)`)
tk.MustExec(`ALTER TABLE t1 ADD INDEX idx6 (c6)`)
tk.MustExec(`admin check table t1`)

// Test add index on decimal column.
tk.MustExec(`drop table if exists td1;`)
tk.MustExec(`CREATE TABLE td1 (c2 INT NULL DEFAULT '70');`)
tk.MustExec(`INSERT INTO td1 SET c2 = '5';`)
tk.MustExec(`ALTER TABLE td1 ADD COLUMN c4 DECIMAL(12,8) NULL DEFAULT '213.41598062';`)
tk.MustExec(`ALTER TABLE td1 ADD INDEX id2 (c4) ;`)
tk.MustExec(`ADMIN CHECK TABLE td1;`)
}

func (s *testSuite) TestAdminCheckPrimaryIndex(c *C) {
Expand Down
22 changes: 21 additions & 1 deletion util/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ func checkIndexAndRecord(sessCtx sessionctx.Context, txn kv.Transaction, t table
if err != nil {
return errors.Trace(err)
}
sc := new(stmtctx.StatementContext)
for {
vals1, h, err := it.Next()
if terror.ErrorEqual(err, io.EOF) {
Expand All @@ -375,7 +376,7 @@ func checkIndexAndRecord(sessCtx sessionctx.Context, txn kv.Transaction, t table
return errors.Trace(err)
}
adjustDatumKind(vals1, vals2)
if !reflect.DeepEqual(vals1, vals2) {
if !compareDatumSlice(sc, vals1, vals2) {
record1 := &RecordData{Handle: h, Values: vals1}
record2 := &RecordData{Handle: h, Values: vals2}
return ErrDataInConsistent.GenWithStack("index:%#v != record:%#v", record1, record2)
Expand All @@ -385,6 +386,25 @@ func checkIndexAndRecord(sessCtx sessionctx.Context, txn kv.Transaction, t table
return nil
}

func compareDatumSlice(sc *stmtctx.StatementContext, val1s, val2s []types.Datum) bool {
if len(val1s) != len(val2s) {
return false
}
for i, v := range val1s {
if v.Kind() == types.KindMysqlDecimal {
res, err := v.CompareDatum(sc, &val2s[i])
if err != nil || res != 0 {
return false
}
} else {
if !reflect.DeepEqual(v, val2s[i]) {
return false
}
}
}
return true
}

// CheckRecordAndIndex is exported for testing.
func CheckRecordAndIndex(sessCtx sessionctx.Context, txn kv.Transaction, t table.Table, idx table.Index, genExprs map[string]expression.Expression) error {
sc := sessCtx.GetSessionVars().StmtCtx
Expand Down

0 comments on commit 0a0d676

Please sign in to comment.