From 084e7190b8124f4b53ce83064547042a77ce3ae4 Mon Sep 17 00:00:00 2001 From: xiaodong-ji Date: Wed, 25 Nov 2020 19:56:16 +0800 Subject: [PATCH] planner: fix unit test for tidb_opt_prefer_range_scan variable (#21294) Co-authored-by: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Co-authored-by: Yiding Cui --- planner/core/integration_test.go | 31 ++++++++ planner/core/physical_plan_test.go | 88 ----------------------- planner/core/testdata/plan_suite_in.json | 12 ---- planner/core/testdata/plan_suite_out.json | 28 -------- 4 files changed, 31 insertions(+), 128 deletions(-) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 9fa689298e349..adb730fea896d 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -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")) +} diff --git a/planner/core/physical_plan_test.go b/planner/core/physical_plan_test.go index 07e9e6abc1396..bf499ddbb213a 100644 --- a/planner/core/physical_plan_test.go +++ b/planner/core/physical_plan_test.go @@ -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...)) - } -} diff --git a/planner/core/testdata/plan_suite_in.json b/planner/core/testdata/plan_suite_in.json index e2a62fb8112cb..c7ef95cc9402b 100644 --- a/planner/core/testdata/plan_suite_in.json +++ b/planner/core/testdata/plan_suite_in.json @@ -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;" - ] } ] diff --git a/planner/core/testdata/plan_suite_out.json b/planner/core/testdata/plan_suite_out.json index 30f825afe345d..825cbbfc774d4 100644 --- a/planner/core/testdata/plan_suite_out.json +++ b/planner/core/testdata/plan_suite_out.json @@ -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 - } - ] } ]