Skip to content

Commit

Permalink
ddl: add more tests for expression index (#27037)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhuang2016 authored Aug 18, 2021
1 parent c5689a1 commit 6501c70
Showing 1 changed file with 157 additions and 15 deletions.
172 changes: 157 additions & 15 deletions ddl/db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,10 @@ func (s *serialTestStateChangeSuite) TestCreateExpressionIndex(c *C) {
tk1 := testkit.NewTestKit(c, s.store)
tk1.MustExec("use test_db_state")

stateDeleteOnlySQLs := []string{"insert into t values (5, 5)", "begin pessimistic;", "insert into t select * from t", "rollback", "insert into t set b = 6", "update t set b = 7 where a = 1", "delete from t where b = 4"}
stateWriteOnlySQLs := []string{"insert into t values (8, 8)", "begin pessimistic;", "insert into t select * from t", "rollback", "insert into t set b = 9", "update t set b = 7 where a = 2", "delete from t where b = 3"}
stateWriteReorganizationSQLs := []string{"insert into t values (10, 10)", "begin pessimistic;", "insert into t select * from t", "rollback", "insert into t set b = 11", "update t set b = 7 where a = 5", "delete from t where b = 6"}

var checkErr error
d := s.dom.DDL()
originalCallback := d.GetHook()
Expand All @@ -1827,25 +1831,88 @@ func (s *serialTestStateChangeSuite) TestCreateExpressionIndex(c *C) {
c.Assert(err, IsNil)
switch job.SchemaState {
case model.StateDeleteOnly:
_, checkErr = tk1.Exec("insert into t values (5, 5)")
if checkErr != nil {
return
for _, sql := range stateDeleteOnlySQLs {
_, checkErr = tk1.Exec(sql)
if checkErr != nil {
return
}
}
// (1, 7), (2, 2), (3, 3), (5, 5), (0, 6)
case model.StateWriteOnly:
for _, sql := range stateWriteOnlySQLs {
_, checkErr = tk1.Exec(sql)
if checkErr != nil {
return
}
}
_, checkErr = tk1.Exec("insert into t set b = 6")
// (1, 7), (2, 7), (5, 5), (0, 6), (8, 8), (0, 9)
case model.StateWriteReorganization:
for _, sql := range stateWriteReorganizationSQLs {
_, checkErr = tk1.Exec(sql)
if checkErr != nil {
return
}
}
// (1, 7), (2, 7), (5, 7), (8, 8), (0, 9), (10, 10), (10, 10), (0, 11), (0, 11)
}
}

d.(ddl.DDLForTest).SetHook(callback)
tk.MustExec("alter table t add index idx((b+1))")
c.Assert(checkErr, IsNil)
tk.MustExec("admin check table t")
tk.MustQuery("select * from t order by a, b").Check(testkit.Rows("0 9", "0 11", "0 11", "1 7", "2 7", "5 7", "8 8", "10 10", "10 10"))
}

func (s *serialTestStateChangeSuite) TestCreateUniqueExpressionIndex(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test_db_state")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int default 0, b int default 0)")
defer func() {
tk.MustExec("drop table t")
}()
tk.MustExec("insert into t values (1, 1), (2, 2), (3, 3), (4, 4)")

tk1 := testkit.NewTestKit(c, s.store)
tk1.MustExec("use test_db_state")

stateDeleteOnlySQLs := []string{"insert into t values (5, 5)", "begin pessimistic;", "insert into t select * from t", "rollback", "insert into t set b = 6", "update t set b = 7 where a = 1", "delete from t where b = 4"}

var checkErr error
d := s.dom.DDL()
originalCallback := d.GetHook()
callback := &ddl.TestDDLCallback{}
callback.OnJobUpdatedExported = func(job *model.Job) {
if checkErr != nil {
return
}
err := originalCallback.OnChanged(nil)
c.Assert(err, IsNil)
switch job.SchemaState {
case model.StateDeleteOnly:
for _, sql := range stateDeleteOnlySQLs {
_, checkErr = tk1.Exec(sql)
if checkErr != nil {
return
}
}
// (1, 7), (2, 2), (3, 3), (5, 5), (0, 6)
case model.StateWriteOnly:
_, checkErr = tk1.Exec("insert into t values (8, 8)")
if checkErr != nil {
return
}
_, checkErr = tk1.Exec("update t set b = 7 where a = 1")
_, checkErr = tk1.Exec("begin pessimistic;")
if checkErr != nil {
return
}
_, checkErr = tk1.Exec("delete from t where b = 4")
if checkErr != nil {
_, tmpErr := tk1.Exec("insert into t select * from t")
if tmpErr == nil {
checkErr = errors.New("should not be nil")
return
}
// (1, 7), (2, 2), (3, 3), (5, 5), (0, 6)
case model.StateWriteOnly:
_, checkErr = tk1.Exec("insert into t values (8, 8)")
_, checkErr = tk1.Exec("rollback")
if checkErr != nil {
return
}
Expand All @@ -1863,11 +1930,24 @@ func (s *serialTestStateChangeSuite) TestCreateExpressionIndex(c *C) {
}
// (1, 7), (2, 7), (5, 5), (0, 6), (8, 8), (0, 9)
case model.StateWriteReorganization:
_, checkErr = tk1.Exec("insert into t values (10, 10)")
_, checkErr = tk1.Exec("insert into t values (10, 10) on duplicate key update a = 11")
if checkErr != nil {
return
}
_, checkErr = tk1.Exec("insert into t set b = 11")
_, checkErr = tk1.Exec("begin pessimistic;")
if checkErr != nil {
return
}
_, tmpErr := tk1.Exec("insert into t select * from t")
if tmpErr == nil {
checkErr = errors.New("should not be nil")
return
}
_, checkErr = tk1.Exec("rollback")
if checkErr != nil {
return
}
_, checkErr = tk1.Exec("insert into t set b = 11 on duplicate key update a = 13")
if checkErr != nil {
return
}
Expand All @@ -1879,14 +1959,76 @@ func (s *serialTestStateChangeSuite) TestCreateExpressionIndex(c *C) {
if checkErr != nil {
return
}
// (1, 7), (2, 7), (5, 7), (8, 8), (0, 9), (10, 10), (10, 10), (0, 11), (0, 11)
// (1, 7), (2, 7), (5, 7), (8, 8), (13, 9), (11, 10), (0, 11)
}
}

d.(ddl.DDLForTest).SetHook(callback)
tk.MustExec("alter table t add index idx((b+1))")
tk.MustExec("alter table t add unique index idx((a*b+1))")
c.Assert(checkErr, IsNil)
tk.MustExec("admin check table t")
tk.MustQuery("select * from t order by a, b").Check(testkit.Rows("0 9", "0 11", "0 11", "1 7", "2 7", "5 7", "8 8", "10 10", "10 10"))
tk.MustQuery("select * from t order by a, b").Check(testkit.Rows("0 11", "1 7", "2 7", "5 7", "8 8", "11 10", "13 9"))
}

func (s *serialTestStateChangeSuite) TestDropExpressionIndex(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test_db_state")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int default 0, b int default 0, key idx((b+1)))")
defer func() {
tk.MustExec("drop table t")
}()
tk.MustExec("insert into t values (1, 1), (2, 2), (3, 3), (4, 4)")

tk1 := testkit.NewTestKit(c, s.store)
tk1.MustExec("use test_db_state")
stateDeleteOnlySQLs := []string{"insert into t values (5, 5)", "begin pessimistic;", "insert into t select * from t", "rollback", "insert into t set b = 6", "update t set b = 7 where a = 1", "delete from t where b = 4"}
stateWriteOnlySQLs := []string{"insert into t values (8, 8)", "begin pessimistic;", "insert into t select * from t", "rollback", "insert into t set b = 9", "update t set b = 7 where a = 2", "delete from t where b = 3"}
stateWriteReorganizationSQLs := []string{"insert into t values (10, 10)", "begin pessimistic;", "insert into t select * from t", "rollback", "insert into t set b = 11", "update t set b = 7 where a = 5", "delete from t where b = 6"}

var checkErr error
d := s.dom.DDL()
originalCallback := d.GetHook()
callback := &ddl.TestDDLCallback{}
callback.OnJobUpdatedExported = func(job *model.Job) {
if checkErr != nil {
return
}
err := originalCallback.OnChanged(nil)
c.Assert(err, IsNil)
switch job.SchemaState {
case model.StateDeleteOnly:
for _, sql := range stateDeleteOnlySQLs {
_, checkErr = tk1.Exec(sql)
if checkErr != nil {
return
}
}
// (1, 7), (2, 7), (5, 5), (8, 8), (0, 9), (0, 6)
case model.StateWriteOnly:
for _, sql := range stateWriteOnlySQLs {
_, checkErr = tk1.Exec(sql)
if checkErr != nil {
return
}
}
// (1, 1), (2, 7), (4, 4), (8, 8), (0, 9)
case model.StateDeleteReorganization:
for _, sql := range stateWriteReorganizationSQLs {
_, checkErr = tk1.Exec(sql)
if checkErr != nil {
return
}
}
// (1, 7), (2, 7), (5, 7), (8, 8), (0, 9), (10, 10), (0, 11)
}
}

d.(ddl.DDLForTest).SetHook(callback)
tk.MustExec("alter table t drop index idx")
c.Assert(checkErr, IsNil)
tk.MustExec("admin check table t")
tk.MustQuery("select * from t order by a, b").Check(testkit.Rows("0 9", "0 11", "1 7", "2 7", "5 7", "8 8", "10 10"))
}

func (s *testStateChangeSuite) TestExpressionIndexDDLError(c *C) {
Expand Down

0 comments on commit 6501c70

Please sign in to comment.