From 008142c3bff6022ab5e858386ccce45a296ca712 Mon Sep 17 00:00:00 2001 From: jixiaodong Date: Tue, 27 Oct 2020 16:35:48 +0800 Subject: [PATCH 1/2] add unit test --- planner/core/physical_plan_test.go | 42 +++++++ planner/core/testdata/plan_suite_in.json | 6 + planner/core/testdata/plan_suite_out.json | 143 ++++++++++++++++++++++ 3 files changed, 191 insertions(+) diff --git a/planner/core/physical_plan_test.go b/planner/core/physical_plan_test.go index 570c89182b433..b2654e84670e1 100644 --- a/planner/core/physical_plan_test.go +++ b/planner/core/physical_plan_test.go @@ -1623,3 +1623,45 @@ 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) TestPreferRangeScan(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("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()) + output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(ts).Sort().Rows()) + }) + tk.MustQuery("explain " + ts).Check(testkit.Rows(output[i].Plan...)) + tk.MustQuery(ts).Check(testkit.Rows(output[i].Result...)) + } +} diff --git a/planner/core/testdata/plan_suite_in.json b/planner/core/testdata/plan_suite_in.json index 85d039dbedd7f..2bae5d4f7b8b4 100644 --- a/planner/core/testdata/plan_suite_in.json +++ b/planner/core/testdata/plan_suite_in.json @@ -662,5 +662,11 @@ "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": "TestPreferRangeScan", + "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 396c643699306..b3d6d7c653716 100644 --- a/planner/core/testdata/plan_suite_out.json +++ b/planner/core/testdata/plan_suite_out.json @@ -2219,5 +2219,148 @@ ] } ] + }, + { + "Name": "TestPreferRangeScan", + "Cases": [ + { + "SQL": "select * from test where age=5;", + "Plan": [ + "IndexLookUp_7 128.00 root ", + "├─IndexRangeScan_5(Build) 128.00 cop[tikv] table:test, index:idx_age(age) range:[5,5], keep order:false", + "└─TableRowIDScan_6(Probe) 128.00 cop[tikv] table:test keep order:false" + ], + "Result": [ + "1 tidb 5 The ocean of stars", + "2 tidb 5 The ocean of stars", + "3 tidb 5 The ocean of stars", + "4 tidb 5 The ocean of stars", + "5 tidb 5 The ocean of stars", + "6 tidb 5 The ocean of stars", + "7 tidb 5 The ocean of stars", + "8 tidb 5 The ocean of stars", + "9 tidb 5 The ocean of stars", + "10 tidb 5 The ocean of stars", + "11 tidb 5 The ocean of stars", + "12 tidb 5 The ocean of stars", + "13 tidb 5 The ocean of stars", + "14 tidb 5 The ocean of stars", + "15 tidb 5 The ocean of stars", + "16 tidb 5 The ocean of stars", + "17 tidb 5 The ocean of stars", + "18 tidb 5 The ocean of stars", + "19 tidb 5 The ocean of stars", + "20 tidb 5 The ocean of stars", + "21 tidb 5 The ocean of stars", + "22 tidb 5 The ocean of stars", + "23 tidb 5 The ocean of stars", + "24 tidb 5 The ocean of stars", + "25 tidb 5 The ocean of stars", + "26 tidb 5 The ocean of stars", + "27 tidb 5 The ocean of stars", + "28 tidb 5 The ocean of stars", + "29 tidb 5 The ocean of stars", + "30 tidb 5 The ocean of stars", + "31 tidb 5 The ocean of stars", + "32 tidb 5 The ocean of stars", + "33 tidb 5 The ocean of stars", + "34 tidb 5 The ocean of stars", + "35 tidb 5 The ocean of stars", + "36 tidb 5 The ocean of stars", + "37 tidb 5 The ocean of stars", + "38 tidb 5 The ocean of stars", + "39 tidb 5 The ocean of stars", + "40 tidb 5 The ocean of stars", + "41 tidb 5 The ocean of stars", + "42 tidb 5 The ocean of stars", + "43 tidb 5 The ocean of stars", + "44 tidb 5 The ocean of stars", + "45 tidb 5 The ocean of stars", + "46 tidb 5 The ocean of stars", + "47 tidb 5 The ocean of stars", + "48 tidb 5 The ocean of stars", + "49 tidb 5 The ocean of stars", + "50 tidb 5 The ocean of stars", + "51 tidb 5 The ocean of stars", + "52 tidb 5 The ocean of stars", + "53 tidb 5 The ocean of stars", + "54 tidb 5 The ocean of stars", + "55 tidb 5 The ocean of stars", + "56 tidb 5 The ocean of stars", + "57 tidb 5 The ocean of stars", + "58 tidb 5 The ocean of stars", + "59 tidb 5 The ocean of stars", + "60 tidb 5 The ocean of stars", + "61 tidb 5 The ocean of stars", + "62 tidb 5 The ocean of stars", + "63 tidb 5 The ocean of stars", + "64 tidb 5 The ocean of stars", + "65 tidb 5 The ocean of stars", + "66 tidb 5 The ocean of stars", + "67 tidb 5 The ocean of stars", + "68 tidb 5 The ocean of stars", + "69 tidb 5 The ocean of stars", + "70 tidb 5 The ocean of stars", + "71 tidb 5 The ocean of stars", + "72 tidb 5 The ocean of stars", + "73 tidb 5 The ocean of stars", + "74 tidb 5 The ocean of stars", + "75 tidb 5 The ocean of stars", + "76 tidb 5 The ocean of stars", + "77 tidb 5 The ocean of stars", + "78 tidb 5 The ocean of stars", + "79 tidb 5 The ocean of stars", + "80 tidb 5 The ocean of stars", + "81 tidb 5 The ocean of stars", + "82 tidb 5 The ocean of stars", + "83 tidb 5 The ocean of stars", + "84 tidb 5 The ocean of stars", + "85 tidb 5 The ocean of stars", + "86 tidb 5 The ocean of stars", + "87 tidb 5 The ocean of stars", + "88 tidb 5 The ocean of stars", + "89 tidb 5 The ocean of stars", + "90 tidb 5 The ocean of stars", + "91 tidb 5 The ocean of stars", + "92 tidb 5 The ocean of stars", + "93 tidb 5 The ocean of stars", + "94 tidb 5 The ocean of stars", + "95 tidb 5 The ocean of stars", + "96 tidb 5 The ocean of stars", + "97 tidb 5 The ocean of stars", + "98 tidb 5 The ocean of stars", + "99 tidb 5 The ocean of stars", + "100 tidb 5 The ocean of stars", + "101 tidb 5 The ocean of stars", + "102 tidb 5 The ocean of stars", + "103 tidb 5 The ocean of stars", + "104 tidb 5 The ocean of stars", + "105 tidb 5 The ocean of stars", + "106 tidb 5 The ocean of stars", + "107 tidb 5 The ocean of stars", + "108 tidb 5 The ocean of stars", + "109 tidb 5 The ocean of stars", + "110 tidb 5 The ocean of stars", + "111 tidb 5 The ocean of stars", + "112 tidb 5 The ocean of stars", + "113 tidb 5 The ocean of stars", + "114 tidb 5 The ocean of stars", + "115 tidb 5 The ocean of stars", + "116 tidb 5 The ocean of stars", + "117 tidb 5 The ocean of stars", + "118 tidb 5 The ocean of stars", + "119 tidb 5 The ocean of stars", + "120 tidb 5 The ocean of stars", + "121 tidb 5 The ocean of stars", + "122 tidb 5 The ocean of stars", + "123 tidb 5 The ocean of stars", + "124 tidb 5 The ocean of stars", + "125 tidb 5 The ocean of stars", + "126 tidb 5 The ocean of stars", + "127 tidb 5 The ocean of stars", + "128 tidb 5 The ocean of stars" + ] + } + ] } ] From 7044c40960878d651f18e297e1a5b78f000235f0 Mon Sep 17 00:00:00 2001 From: jixiaodong Date: Wed, 28 Oct 2020 14:58:44 +0800 Subject: [PATCH 2/2] modify unit test --- planner/core/physical_plan_test.go | 52 +++++++- planner/core/testdata/plan_suite_in.json | 8 +- planner/core/testdata/plan_suite_out.json | 151 +++------------------- 3 files changed, 73 insertions(+), 138 deletions(-) diff --git a/planner/core/physical_plan_test.go b/planner/core/physical_plan_test.go index b2654e84670e1..9aa323e342fd8 100644 --- a/planner/core/physical_plan_test.go +++ b/planner/core/physical_plan_test.go @@ -1624,7 +1624,7 @@ func (s *testPlanSuite) TestNthPlanHintWithExplain(c *C) { tk.MustQuery("explain select * from test.tt where a=1 and b=1").Check(testkit.Rows(output[1].Plan...)) } -func (s *testPlanSuite) TestPreferRangeScan(c *C) { +func (s *testPlanSuite) TestPreferRangeScanOff(c *C) { var ( input []string output []struct { @@ -1652,6 +1652,54 @@ func (s *testPlanSuite) TestPreferRangeScan(c *C) { 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)) @@ -1659,9 +1707,7 @@ func (s *testPlanSuite) TestPreferRangeScan(c *C) { s.testData.OnRecord(func() { output[i].SQL = ts output[i].Plan = s.testData.ConvertRowsToStrings(tk.MustQuery("explain " + ts).Rows()) - output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(ts).Sort().Rows()) }) tk.MustQuery("explain " + ts).Check(testkit.Rows(output[i].Plan...)) - tk.MustQuery(ts).Check(testkit.Rows(output[i].Result...)) } } diff --git a/planner/core/testdata/plan_suite_in.json b/planner/core/testdata/plan_suite_in.json index 2bae5d4f7b8b4..22e77d4f6e792 100644 --- a/planner/core/testdata/plan_suite_in.json +++ b/planner/core/testdata/plan_suite_in.json @@ -664,7 +664,13 @@ ] }, { - "name": "TestPreferRangeScan", + "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 b3d6d7c653716..6f4c64fa36c0d 100644 --- a/planner/core/testdata/plan_suite_out.json +++ b/planner/core/testdata/plan_suite_out.json @@ -2221,144 +2221,27 @@ ] }, { - "Name": "TestPreferRangeScan", + "Name": "TestPreferRangeScanOff", "Cases": [ { "SQL": "select * from test where age=5;", "Plan": [ - "IndexLookUp_7 128.00 root ", - "├─IndexRangeScan_5(Build) 128.00 cop[tikv] table:test, index:idx_age(age) range:[5,5], keep order:false", - "└─TableRowIDScan_6(Probe) 128.00 cop[tikv] table:test keep order:false" - ], - "Result": [ - "1 tidb 5 The ocean of stars", - "2 tidb 5 The ocean of stars", - "3 tidb 5 The ocean of stars", - "4 tidb 5 The ocean of stars", - "5 tidb 5 The ocean of stars", - "6 tidb 5 The ocean of stars", - "7 tidb 5 The ocean of stars", - "8 tidb 5 The ocean of stars", - "9 tidb 5 The ocean of stars", - "10 tidb 5 The ocean of stars", - "11 tidb 5 The ocean of stars", - "12 tidb 5 The ocean of stars", - "13 tidb 5 The ocean of stars", - "14 tidb 5 The ocean of stars", - "15 tidb 5 The ocean of stars", - "16 tidb 5 The ocean of stars", - "17 tidb 5 The ocean of stars", - "18 tidb 5 The ocean of stars", - "19 tidb 5 The ocean of stars", - "20 tidb 5 The ocean of stars", - "21 tidb 5 The ocean of stars", - "22 tidb 5 The ocean of stars", - "23 tidb 5 The ocean of stars", - "24 tidb 5 The ocean of stars", - "25 tidb 5 The ocean of stars", - "26 tidb 5 The ocean of stars", - "27 tidb 5 The ocean of stars", - "28 tidb 5 The ocean of stars", - "29 tidb 5 The ocean of stars", - "30 tidb 5 The ocean of stars", - "31 tidb 5 The ocean of stars", - "32 tidb 5 The ocean of stars", - "33 tidb 5 The ocean of stars", - "34 tidb 5 The ocean of stars", - "35 tidb 5 The ocean of stars", - "36 tidb 5 The ocean of stars", - "37 tidb 5 The ocean of stars", - "38 tidb 5 The ocean of stars", - "39 tidb 5 The ocean of stars", - "40 tidb 5 The ocean of stars", - "41 tidb 5 The ocean of stars", - "42 tidb 5 The ocean of stars", - "43 tidb 5 The ocean of stars", - "44 tidb 5 The ocean of stars", - "45 tidb 5 The ocean of stars", - "46 tidb 5 The ocean of stars", - "47 tidb 5 The ocean of stars", - "48 tidb 5 The ocean of stars", - "49 tidb 5 The ocean of stars", - "50 tidb 5 The ocean of stars", - "51 tidb 5 The ocean of stars", - "52 tidb 5 The ocean of stars", - "53 tidb 5 The ocean of stars", - "54 tidb 5 The ocean of stars", - "55 tidb 5 The ocean of stars", - "56 tidb 5 The ocean of stars", - "57 tidb 5 The ocean of stars", - "58 tidb 5 The ocean of stars", - "59 tidb 5 The ocean of stars", - "60 tidb 5 The ocean of stars", - "61 tidb 5 The ocean of stars", - "62 tidb 5 The ocean of stars", - "63 tidb 5 The ocean of stars", - "64 tidb 5 The ocean of stars", - "65 tidb 5 The ocean of stars", - "66 tidb 5 The ocean of stars", - "67 tidb 5 The ocean of stars", - "68 tidb 5 The ocean of stars", - "69 tidb 5 The ocean of stars", - "70 tidb 5 The ocean of stars", - "71 tidb 5 The ocean of stars", - "72 tidb 5 The ocean of stars", - "73 tidb 5 The ocean of stars", - "74 tidb 5 The ocean of stars", - "75 tidb 5 The ocean of stars", - "76 tidb 5 The ocean of stars", - "77 tidb 5 The ocean of stars", - "78 tidb 5 The ocean of stars", - "79 tidb 5 The ocean of stars", - "80 tidb 5 The ocean of stars", - "81 tidb 5 The ocean of stars", - "82 tidb 5 The ocean of stars", - "83 tidb 5 The ocean of stars", - "84 tidb 5 The ocean of stars", - "85 tidb 5 The ocean of stars", - "86 tidb 5 The ocean of stars", - "87 tidb 5 The ocean of stars", - "88 tidb 5 The ocean of stars", - "89 tidb 5 The ocean of stars", - "90 tidb 5 The ocean of stars", - "91 tidb 5 The ocean of stars", - "92 tidb 5 The ocean of stars", - "93 tidb 5 The ocean of stars", - "94 tidb 5 The ocean of stars", - "95 tidb 5 The ocean of stars", - "96 tidb 5 The ocean of stars", - "97 tidb 5 The ocean of stars", - "98 tidb 5 The ocean of stars", - "99 tidb 5 The ocean of stars", - "100 tidb 5 The ocean of stars", - "101 tidb 5 The ocean of stars", - "102 tidb 5 The ocean of stars", - "103 tidb 5 The ocean of stars", - "104 tidb 5 The ocean of stars", - "105 tidb 5 The ocean of stars", - "106 tidb 5 The ocean of stars", - "107 tidb 5 The ocean of stars", - "108 tidb 5 The ocean of stars", - "109 tidb 5 The ocean of stars", - "110 tidb 5 The ocean of stars", - "111 tidb 5 The ocean of stars", - "112 tidb 5 The ocean of stars", - "113 tidb 5 The ocean of stars", - "114 tidb 5 The ocean of stars", - "115 tidb 5 The ocean of stars", - "116 tidb 5 The ocean of stars", - "117 tidb 5 The ocean of stars", - "118 tidb 5 The ocean of stars", - "119 tidb 5 The ocean of stars", - "120 tidb 5 The ocean of stars", - "121 tidb 5 The ocean of stars", - "122 tidb 5 The ocean of stars", - "123 tidb 5 The ocean of stars", - "124 tidb 5 The ocean of stars", - "125 tidb 5 The ocean of stars", - "126 tidb 5 The ocean of stars", - "127 tidb 5 The ocean of stars", - "128 tidb 5 The ocean of stars" + "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" ] } ]