Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: add unit test for tidb_opt_prefer_range_scan variable #20672

Merged
merged 3 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1623,3 +1623,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))
xiaodong-ji marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -2219,5 +2219,31 @@
]
}
]
},
{
"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"
]
}
]
}
]