Skip to content

Commit

Permalink
planner: fix unit test for tidb_opt_prefer_range_scan variable (#21294)
Browse files Browse the repository at this point in the history
Co-authored-by: ti-srebot <66930949+ti-srebot@users.noreply.github.com>
Co-authored-by: Yiding Cui <winoros@gmail.com>
  • Loading branch information
3 people authored Nov 25, 2020
1 parent af58658 commit 084e719
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 128 deletions.
31 changes: 31 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1906,3 +1906,34 @@ func (s *testIntegrationSuite) TestUpdateMultiUpdatePK(c *C) {
tk.MustExec(`UPDATE t m, t n SET m.a = m.a + 1, n.b = n.b + 10`)
tk.MustQuery("SELECT * FROM t").Check(testkit.Rows("2 12"))
}

func (s *testIntegrationSuite) TestPreferRangeScan(c *C) {
tk := testkit.NewTestKit(c, s.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("set session tidb_opt_prefer_range_scan=0")
tk.MustQuery("explain select * from test where age=5").Check(testkit.Rows(
"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"))

tk.MustExec("set session tidb_opt_prefer_range_scan=1")
tk.MustQuery("explain select * from test where age=5").Check(testkit.Rows(
"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"))
}
88 changes: 0 additions & 88 deletions planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1643,91 +1643,3 @@ 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: 0 additions & 12 deletions planner/core/testdata/plan_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -662,17 +662,5 @@
"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;"
]
}
]
28 changes: 0 additions & 28 deletions planner/core/testdata/plan_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -2222,33 +2222,5 @@
"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"
],
"Result": null
}
]
},
{
"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"
],
"Result": null
}
]
}
]

0 comments on commit 084e719

Please sign in to comment.