Skip to content

Commit

Permalink
planner: add unit test for tidb_opt_prefer_range_scan variable (#20672)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaodong-ji authored Nov 13, 2020
1 parent d1bc995 commit 5aada1f
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
88 changes: 88 additions & 0 deletions planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1643,3 +1643,91 @@ func (s *testPlanSuite) TestNthPlanHintWithExplain(c *C) {
// hold in the future, you may need to modify this.
tk.MustQuery("explain select * from test.tt where a=1 and b=1").Check(testkit.Rows(output[1].Plan...))
}

func (s *testPlanSuite) TestPreferRangeScanOff(c *C) {
var (
input []string
output []struct {
SQL string
Plan []string
Result []string
}
)
s.testData.GetTestCases(c, &input, &output)
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer func() {
dom.Close()
store.Close()
}()
tk := testkit.NewTestKit(c, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists test;")
tk.MustExec("create table test(`id` int(10) NOT NULL AUTO_INCREMENT,`name` varchar(50) NOT NULL DEFAULT 'tidb',`age` int(11) NOT NULL,`addr` varchar(50) DEFAULT 'The ocean of stars',PRIMARY KEY (`id`),KEY `idx_age` (`age`))")
tk.MustExec("insert into test(age) values(5);")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("analyze table test;")
tk.MustExec(fmt.Sprintf("set session tidb_opt_prefer_range_scan = %v", 0))

for i, ts := range input {
s.testData.OnRecord(func() {
output[i].SQL = ts
output[i].Plan = s.testData.ConvertRowsToStrings(tk.MustQuery("explain " + ts).Rows())
})
tk.MustQuery("explain " + ts).Check(testkit.Rows(output[i].Plan...))
}
}

func (s *testPlanSuite) TestPreferRangeScanOn(c *C) {
var (
input []string
output []struct {
SQL string
Plan []string
Result []string
}
)
s.testData.GetTestCases(c, &input, &output)
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer func() {
dom.Close()
store.Close()
}()
tk := testkit.NewTestKit(c, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists test;")
tk.MustExec("create table test(`id` int(10) NOT NULL AUTO_INCREMENT,`name` varchar(50) NOT NULL DEFAULT 'tidb',`age` int(11) NOT NULL,`addr` varchar(50) DEFAULT 'The ocean of stars',PRIMARY KEY (`id`),KEY `idx_age` (`age`))")
tk.MustExec("insert into test(age) values(5);")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("insert into test(name,age,addr) select name,age,addr from test;")
tk.MustExec("analyze table test;")
tk.MustExec(fmt.Sprintf("set session tidb_opt_prefer_range_scan = %v", 1))

for i, ts := range input {
s.testData.OnRecord(func() {
output[i].SQL = ts
output[i].Plan = s.testData.ConvertRowsToStrings(tk.MustQuery("explain " + ts).Rows())
})
tk.MustQuery("explain " + ts).Check(testkit.Rows(output[i].Plan...))
}
}
12 changes: 12 additions & 0 deletions planner/core/testdata/plan_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -662,5 +662,17 @@
"select a from t2 where t2.a < (select t1.a from t1 where t1.b = t2.b and t1.a is null);",
"select a from t2 where t2.a < (select t3.a from t3 where t3.a = t2.a);"
]
},
{
"name": "TestPreferRangeScanOff",
"cases": [
"select * from test where age=5;"
]
},
{
"name": "TestPreferRangeScanOn",
"cases": [
"select * from test where age=5;"
]
}
]
26 changes: 26 additions & 0 deletions planner/core/testdata/plan_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -2222,5 +2222,31 @@
"Result": null
}
]
},
{
"Name": "TestPreferRangeScanOff",
"Cases": [
{
"SQL": "select * from test where age=5;",
"Plan": [
"TableReader_7 2048.00 root data:Selection_6",
"└─Selection_6 2048.00 cop[tikv] eq(test.test.age, 5)",
" └─TableFullScan_5 2048.00 cop[tikv] table:test keep order:false"
]
}
]
},
{
"Name": "TestPreferRangeScanOn",
"Cases": [
{
"SQL": "select * from test where age=5;",
"Plan": [
"IndexLookUp_7 2048.00 root ",
"├─IndexRangeScan_5(Build) 2048.00 cop[tikv] table:test, index:idx_age(age) range:[5,5], keep order:false",
"└─TableRowIDScan_6(Probe) 2048.00 cop[tikv] table:test keep order:false"
]
}
]
}
]

0 comments on commit 5aada1f

Please sign in to comment.