Skip to content

Commit

Permalink
tests: Use tk.MustHavePlan and tk.MustNotHavePlan instead of `tk.…
Browse files Browse the repository at this point in the history
…HasPlan` (#46947)

close #46945
  • Loading branch information
Defined2014 authored Sep 15, 2023
1 parent 4a86bb4 commit 4c1f28b
Show file tree
Hide file tree
Showing 23 changed files with 267 additions and 281 deletions.
22 changes: 11 additions & 11 deletions bindinfo/session_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,45 +43,45 @@ func TestGlobalAndSessionBindingBothExist(t *testing.T) {
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t1(id int)")
tk.MustExec("create table t2(id int)")
require.True(t, tk.HasPlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin"))
require.True(t, tk.HasPlan("SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id", "MergeJoin"))
tk.MustHavePlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin")
tk.MustHavePlan("SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id", "MergeJoin")

tk.MustExec("create global binding for SELECT * from t1,t2 where t1.id = t2.id using SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id")

// Test bindingUsage, which indicates how many times the binding is used.
metrics.BindUsageCounter.Reset()
require.True(t, tk.HasPlan("SELECT * from t1,t2 where t1.id = t2.id", "MergeJoin"))
tk.MustHavePlan("SELECT * from t1,t2 where t1.id = t2.id", "MergeJoin")
pb := &dto.Metric{}
err := metrics.BindUsageCounter.WithLabelValues(metrics.ScopeGlobal).Write(pb)
require.NoError(t, err)
require.Equal(t, float64(1), pb.GetCounter().GetValue())

// Test 'tidb_use_plan_baselines'
tk.MustExec("set @@tidb_use_plan_baselines = 0")
require.True(t, tk.HasPlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin"))
tk.MustHavePlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin")
tk.MustExec("set @@tidb_use_plan_baselines = 1")

// Test 'drop global binding'
require.True(t, tk.HasPlan("SELECT * from t1,t2 where t1.id = t2.id", "MergeJoin"))
tk.MustHavePlan("SELECT * from t1,t2 where t1.id = t2.id", "MergeJoin")
tk.MustExec("drop global binding for SELECT * from t1,t2 where t1.id = t2.id")
require.True(t, tk.HasPlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin"))
tk.MustHavePlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin")

// Test the case when global and session binding both exist
// PART1 : session binding should totally cover global binding
// use merge join as session binding here since the optimizer will choose hash join for this stmt in default
tk.MustExec("create global binding for SELECT * from t1,t2 where t1.id = t2.id using SELECT /*+ TIDB_HJ(t1, t2) */ * from t1,t2 where t1.id = t2.id")
require.True(t, tk.HasPlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin"))
tk.MustHavePlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin")
tk.MustExec("create binding for SELECT * from t1,t2 where t1.id = t2.id using SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id")
require.True(t, tk.HasPlan("SELECT * from t1,t2 where t1.id = t2.id", "MergeJoin"))
tk.MustHavePlan("SELECT * from t1,t2 where t1.id = t2.id", "MergeJoin")
tk.MustExec("drop global binding for SELECT * from t1,t2 where t1.id = t2.id")
require.True(t, tk.HasPlan("SELECT * from t1,t2 where t1.id = t2.id", "MergeJoin"))
tk.MustHavePlan("SELECT * from t1,t2 where t1.id = t2.id", "MergeJoin")

// PART2 : the dropped session binding should continue to block the effect of global binding
tk.MustExec("create global binding for SELECT * from t1,t2 where t1.id = t2.id using SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id")
tk.MustExec("drop binding for SELECT * from t1,t2 where t1.id = t2.id")
require.True(t, tk.HasPlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin"))
tk.MustHavePlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin")
tk.MustExec("drop global binding for SELECT * from t1,t2 where t1.id = t2.id")
require.True(t, tk.HasPlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin"))
tk.MustHavePlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin")
}

func TestSessionBinding(t *testing.T) {
Expand Down
38 changes: 19 additions & 19 deletions bindinfo/tests/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,23 +383,23 @@ func TestExplain(t *testing.T) {
tk.MustExec("create table t1(id int)")
tk.MustExec("create table t2(id int)")

require.True(t, tk.HasPlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin"))
require.True(t, tk.HasPlan("SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id", "MergeJoin"))
tk.MustHavePlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin")
tk.MustHavePlan("SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id", "MergeJoin")

tk.MustExec("create global binding for SELECT * from t1,t2 where t1.id = t2.id using SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id")

require.True(t, tk.HasPlan("SELECT * from t1,t2 where t1.id = t2.id", "MergeJoin"))
tk.MustHavePlan("SELECT * from t1,t2 where t1.id = t2.id", "MergeJoin")

tk.MustExec("drop global binding for SELECT * from t1,t2 where t1.id = t2.id")

// Add test for SetOprStmt
tk.MustExec("create index index_id on t1(id)")
require.True(t, tk.HasPlan("SELECT * from t1 union SELECT * from t1", "IndexReader"))
require.True(t, tk.HasPlan("SELECT * from t1 use index(index_id) union SELECT * from t1", "IndexReader"))
tk.MustHavePlan("SELECT * from t1 union SELECT * from t1", "IndexReader")
tk.MustHavePlan("SELECT * from t1 use index(index_id) union SELECT * from t1", "IndexReader")

tk.MustExec("create global binding for SELECT * from t1 union SELECT * from t1 using SELECT * from t1 use index(index_id) union SELECT * from t1")

require.True(t, tk.HasPlan("SELECT * from t1 union SELECT * from t1", "IndexReader"))
tk.MustHavePlan("SELECT * from t1 union SELECT * from t1", "IndexReader")

tk.MustExec("drop global binding for SELECT * from t1 union SELECT * from t1")
}
Expand Down Expand Up @@ -433,16 +433,16 @@ func TestBindCTEMerge(t *testing.T) {
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1(id int)")
require.True(t, tk.HasPlan("with cte as (select * from t1) select * from cte a, cte b", "CTEFullScan"))
require.False(t, tk.HasPlan("with cte as (select /*+ MERGE() */ * from t1) select * from cte a, cte b", "CTEFullScan"))
tk.MustHavePlan("with cte as (select * from t1) select * from cte a, cte b", "CTEFullScan")
tk.MustNotHavePlan("with cte as (select /*+ MERGE() */ * from t1) select * from cte a, cte b", "CTEFullScan")
tk.MustExec(`
create global binding for
with cte as (select * from t1) select * from cte
using
with cte as (select /*+ MERGE() */ * from t1) select * from cte
`)

require.False(t, tk.HasPlan("with cte as (select * from t1) select * from cte", "CTEFullScan"))
tk.MustNotHavePlan("with cte as (select * from t1) select * from cte", "CTEFullScan")
}

func TestBindNoDecorrelate(t *testing.T) {
Expand All @@ -454,8 +454,8 @@ func TestBindNoDecorrelate(t *testing.T) {
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t1(a int, b int)")
tk.MustExec("create table t2(a int, b int)")
require.False(t, tk.HasPlan("select exists (select t2.b from t2 where t2.a = t1.b limit 2) from t1", "Apply"))
require.True(t, tk.HasPlan("select exists (select /*+ no_decorrelate() */ t2.b from t2 where t2.a = t1.b limit 2) from t1", "Apply"))
tk.MustNotHavePlan("select exists (select t2.b from t2 where t2.a = t1.b limit 2) from t1", "Apply")
tk.MustHavePlan("select exists (select /*+ no_decorrelate() */ t2.b from t2 where t2.a = t1.b limit 2) from t1", "Apply")

tk.MustExec(`
create global binding for
Expand All @@ -464,7 +464,7 @@ using
select exists (select /*+ no_decorrelate() */ t2.b from t2 where t2.a = t1.b limit 2) from t1
`)

require.True(t, tk.HasPlan("select exists (select t2.b from t2 where t2.a = t1.b limit 2) from t1", "Apply"))
tk.MustHavePlan("select exists (select t2.b from t2 where t2.a = t1.b limit 2) from t1", "Apply")
}

// TestBindingSymbolList tests sql with "?, ?, ?, ?", fixes #13871
Expand Down Expand Up @@ -566,9 +566,9 @@ func TestDMLSQLBind(t *testing.T) {
require.Equal(t, "t1:idx_c", tk.Session().GetSessionVars().StmtCtx.IndexNames[0])
require.True(t, tk.MustUseIndex("delete from t1 where b = 1 and c > 1", "idx_c(c)"))

require.True(t, tk.HasPlan("delete t1, t2 from t1 inner join t2 on t1.b = t2.b", "HashJoin"))
tk.MustHavePlan("delete t1, t2 from t1 inner join t2 on t1.b = t2.b", "HashJoin")
tk.MustExec("create global binding for delete t1, t2 from t1 inner join t2 on t1.b = t2.b using delete /*+ inl_join(t1) */ t1, t2 from t1 inner join t2 on t1.b = t2.b")
require.True(t, tk.HasPlan("delete t1, t2 from t1 inner join t2 on t1.b = t2.b", "IndexJoin"))
tk.MustHavePlan("delete t1, t2 from t1 inner join t2 on t1.b = t2.b", "IndexJoin")

tk.MustExec("update t1 set a = 1 where b = 1 and c > 1")
require.Equal(t, "t1:idx_b", tk.Session().GetSessionVars().StmtCtx.IndexNames[0])
Expand All @@ -578,9 +578,9 @@ func TestDMLSQLBind(t *testing.T) {
require.Equal(t, "t1:idx_c", tk.Session().GetSessionVars().StmtCtx.IndexNames[0])
require.True(t, tk.MustUseIndex("update t1 set a = 1 where b = 1 and c > 1", "idx_c(c)"))

require.True(t, tk.HasPlan("update t1, t2 set t1.a = 1 where t1.b = t2.b", "HashJoin"))
tk.MustHavePlan("update t1, t2 set t1.a = 1 where t1.b = t2.b", "HashJoin")
tk.MustExec("create global binding for update t1, t2 set t1.a = 1 where t1.b = t2.b using update /*+ inl_join(t1) */ t1, t2 set t1.a = 1 where t1.b = t2.b")
require.True(t, tk.HasPlan("update t1, t2 set t1.a = 1 where t1.b = t2.b", "IndexJoin"))
tk.MustHavePlan("update t1, t2 set t1.a = 1 where t1.b = t2.b", "IndexJoin")

tk.MustExec("insert into t1 select * from t2 where t2.b = 2 and t2.c > 2")
require.Equal(t, "t2:idx_b", tk.Session().GetSessionVars().StmtCtx.IndexNames[0])
Expand Down Expand Up @@ -1154,14 +1154,14 @@ func TestSPMHitInfo(t *testing.T) {
tk.MustExec("create table t1(id int)")
tk.MustExec("create table t2(id int)")

require.True(t, tk.HasPlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin"))
require.True(t, tk.HasPlan("SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id", "MergeJoin"))
tk.MustHavePlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin")
tk.MustHavePlan("SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id", "MergeJoin")

tk.MustExec("SELECT * from t1,t2 where t1.id = t2.id")
tk.MustQuery(`select @@last_plan_from_binding;`).Check(testkit.Rows("0"))
tk.MustExec("create global binding for SELECT * from t1,t2 where t1.id = t2.id using SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id")

require.True(t, tk.HasPlan("SELECT * from t1,t2 where t1.id = t2.id", "MergeJoin"))
tk.MustHavePlan("SELECT * from t1,t2 where t1.id = t2.id", "MergeJoin")
tk.MustExec("SELECT * from t1,t2 where t1.id = t2.id")
tk.MustQuery(`select @@last_plan_from_binding;`).Check(testkit.Rows("1"))
tk.MustExec("set binding disabled for SELECT * from t1,t2 where t1.id = t2.id")
Expand Down
8 changes: 4 additions & 4 deletions executor/batch_point_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,10 @@ func TestBatchPointGetIssue46779(t *testing.T) {
tk.MustExec("CREATE TABLE t1 (id int, c varchar(128), primary key (id)) PARTITION BY HASH (id) PARTITIONS 3;")
tk.MustExec(`insert into t1 values (1, "a"), (11, "b"), (21, "c")`)
query := "select * from t1 where id in (1, 1, 11)"
require.True(t, tk.HasPlan(query, "Batch_Point_Get")) // check if BatchPointGet is used
tk.MustHavePlan(query, "Batch_Point_Get") // check if BatchPointGet is used
tk.MustQuery(query).Sort().Check(testkit.Rows("1 a", "11 b"))
query = "select * from t1 where id in (1, 11, 11, 21)"
require.True(t, tk.HasPlan(query, "Batch_Point_Get")) // check if BatchPointGet is used
tk.MustHavePlan(query, "Batch_Point_Get") // check if BatchPointGet is used
tk.MustQuery(query).Sort().Check(testkit.Rows("1 a", "11 b", "21 c"))

tk.MustExec("drop table if exists t2")
Expand All @@ -393,9 +393,9 @@ func TestBatchPointGetIssue46779(t *testing.T) {
partition p2 values less than (30));`)
tk.MustExec(`insert into t2 values (1, "a"), (11, "b"), (21, "c")`)
query = "select * from t2 where id in (1, 1, 11)"
require.True(t, tk.HasPlan(query, "Batch_Point_Get")) // check if BatchPointGet is used
tk.MustHavePlan(query, "Batch_Point_Get") // check if BatchPointGet is used
tk.MustQuery(query).Sort().Check(testkit.Rows("1 a", "11 b"))
require.True(t, tk.HasPlan(query, "Batch_Point_Get")) // check if BatchPointGet is used
tk.MustHavePlan(query, "Batch_Point_Get") // check if BatchPointGet is used
query = "select * from t2 where id in (1, 11, 11, 21)"
tk.MustQuery(query).Sort().Check(testkit.Rows("1 a", "11 b", "21 c"))
}
6 changes: 3 additions & 3 deletions executor/distsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,11 +690,11 @@ func TestIndexLookUpWithSelectForUpdateOnPartitionTable(t *testing.T) {
tk.MustExec("use test")
tk.MustExec("create table t(a int, b int, index k(b)) PARTITION BY HASH(a) partitions 4")
tk.MustExec("insert into t(a, b) values (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8)")
tk.HasPlan("select b from t use index(k) where b > 2 order by b limit 1 for update", "UnionScan")
tk.HasPlan("select b from t use index(k) where b > 2 order by b limit 1 for update", "IndexLookUp")
tk.MustHavePlan("select b from t use index(k) where b > 2 order by b limit 1 for update", "PartitionUnion")
tk.MustHavePlan("select b from t use index(k) where b > 2 order by b limit 1 for update", "IndexLookUp")
tk.MustQuery("select b from t use index(k) where b > 2 order by b limit 1 for update").Check(testkit.Rows("3"))

tk.MustExec("analyze table t")
tk.HasPlan("select b from t use index(k) where b > 2 order by b limit 1 for update", "IndexLookUp")
tk.MustHavePlan("select b from t use index(k) where b > 2 order by b limit 1 for update", "IndexLookUp")
tk.MustQuery("select b from t use index(k) where b > 2 order by b limit 1 for update").Check(testkit.Rows("3"))
}
2 changes: 1 addition & 1 deletion executor/index_lookup_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func TestInapplicableIndexJoinHint(t *testing.T) {

query := `select /*+ tidb_inlj(bb) */ aa.* from (select * from t1) as aa left join
(select t2.a, t2.a*2 as a2 from t2) as bb on aa.a=bb.a;`
tk.HasPlan(query, "IndexJoin")
tk.MustHavePlan(query, "IndexJoin")
}

func TestIndexJoinOverflow(t *testing.T) {
Expand Down
Loading

0 comments on commit 4c1f28b

Please sign in to comment.