Skip to content

Commit

Permalink
UCP: improve UT coverage of package planner, part 1 (pingcap#16093)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsqlu authored May 6, 2020
1 parent 079939f commit b95d857
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
38 changes: 38 additions & 0 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,44 @@ func (s *testSuite) TestBindingSymbolList(c *C) {
c.Check(bind.UpdateTime, NotNil)
}

func (s *testSuite) TestBestPlanInBaselines(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int, INDEX ia (a), INDEX ib (b));")
tk.MustExec("insert into t value(1, 1);")

// before binding
tk.MustQuery("select a, b from t where a = 3 limit 1, 100")
c.Assert(tk.Se.GetSessionVars().StmtCtx.IndexNames[0], Equals, "t:ia")
c.Assert(tk.MustUseIndex("select a, b from t where a = 3 limit 1, 100", "ia(a)"), IsTrue)

tk.MustQuery("select a, b from t where b = 3 limit 1, 100")
c.Assert(tk.Se.GetSessionVars().StmtCtx.IndexNames[0], Equals, "t:ib")
c.Assert(tk.MustUseIndex("select a, b from t where b = 3 limit 1, 100", "ib(b)"), IsTrue)

tk.MustExec(`create global binding for select a, b from t where a = 1 limit 0, 1 using select /*+ use_index(@sel_1 test.t, ia) */ a, b from t where a = 1 limit 0, 1`)
tk.MustExec(`create global binding for select a, b from t where b = 1 limit 0, 1 using select /*+ use_index(@sel_1 test.t, ib) */ a, b from t where b = 1 limit 0, 1`)

sql, hash := parser.NormalizeDigest("select a, b from t where a = 1 limit 0, 1")
bindData := s.domain.BindHandle().GetBindRecord(hash, sql, "test")
c.Check(bindData, NotNil)
c.Check(bindData.OriginalSQL, Equals, "select a , b from t where a = ? limit ...")
bind := bindData.Bindings[0]
c.Check(bind.BindSQL, Equals, "select /*+ use_index(@sel_1 test.t, ia) */ a, b from t where a = 1 limit 0, 1")
c.Check(bindData.Db, Equals, "test")
c.Check(bind.Status, Equals, "using")

tk.MustQuery("select a, b from t where a = 3 limit 1, 10")
c.Assert(tk.Se.GetSessionVars().StmtCtx.IndexNames[0], Equals, "t:ia")
c.Assert(tk.MustUseIndex("select a, b from t where a = 3 limit 1, 100", "ia(a)"), IsTrue)

tk.MustQuery("select a, b from t where b = 3 limit 1, 100")
c.Assert(tk.Se.GetSessionVars().StmtCtx.IndexNames[0], Equals, "t:ib")
c.Assert(tk.MustUseIndex("select a, b from t where b = 3 limit 1, 100", "ib(b)"), IsTrue)
}

func (s *testSuite) TestErrorBind(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
Expand Down
13 changes: 12 additions & 1 deletion session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2950,12 +2950,22 @@ func (s *testSessionSuite2) TestTxnGoString(c *C) {
}

func (s *testSessionSuite3) TestMaxExeucteTime(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
var err error
tk := testkit.NewTestKit(c, s.store)
tk.Se, err = session.CreateSession4Test(s.store)
c.Assert(err, IsNil)

tk.MustExec("use test")
tk.MustExec("create table MaxExecTime( id int,name varchar(128),age int);")
tk.MustExec("begin")
tk.MustExec("insert into MaxExecTime (id,name,age) values (1,'john',18),(2,'lary',19),(3,'lily',18);")

tk.MustQuery("select /*+ MAX_EXECUTION_TIME(1000) MAX_EXECUTION_TIME(500) */ * FROM MaxExecTime;")
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings(), HasLen, 1)
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings()[0].Err.Error(), Equals, "There are multiple MAX_EXECUTION_TIME hints, only the last one will take effect")
c.Assert(tk.Se.GetSessionVars().StmtCtx.HasMaxExecutionTime, Equals, true)
c.Assert(tk.Se.GetSessionVars().StmtCtx.MaxExecutionTime, Equals, uint64(500))

tk.MustQuery("select @@MAX_EXECUTION_TIME;").Check(testkit.Rows("0"))
tk.MustQuery("select @@global.MAX_EXECUTION_TIME;").Check(testkit.Rows("0"))
tk.MustQuery("select /*+ MAX_EXECUTION_TIME(1000) */ * FROM MaxExecTime;")
Expand Down Expand Up @@ -3075,6 +3085,7 @@ func (s *testSessionSuite2) TestStmtHints(c *C) {
val = int64(0)
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings(), HasLen, 1)
c.Assert(tk.Se.GetSessionVars().StmtCtx.MemTracker.CheckBytesLimit(val), IsTrue)
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings()[0].Err.Error(), Equals, "Setting the MEMORY_QUOTA to 0 means no memory limit")

// Test NO_INDEX_MERGE hint
tk.Se.GetSessionVars().SetEnableIndexMerge(true)
Expand Down

0 comments on commit b95d857

Please sign in to comment.