From 0bae080092fee5a97433eae81b81ecf7a9ead5bd Mon Sep 17 00:00:00 2001 From: fzzf678 <108643977+fzzf678@users.noreply.github.com> Date: Fri, 16 Sep 2022 15:21:01 +0800 Subject: [PATCH 1/3] ut --- planner/core/physical_plan_test.go | 30 ++++---- planner/core/testdata/plan_suite_out.json | 84 ++++++++++++++++++++++- 2 files changed, 95 insertions(+), 19 deletions(-) diff --git a/planner/core/physical_plan_test.go b/planner/core/physical_plan_test.go index 792966ff6cebf..6f6c1a2304297 100644 --- a/planner/core/physical_plan_test.go +++ b/planner/core/physical_plan_test.go @@ -366,33 +366,27 @@ func TestDAGPlanBuilderUnionScan(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int, b int, c int)") var input []string var output []struct { SQL string - Best string + Best []string } - p := parser.New() - is := infoschema.MockInfoSchema([]*model.TableInfo{core.MockSignedTable(), core.MockUnsignedTable()}) - for i, tt := range input { - comment := fmt.Sprintf("input: %s", tt) - stmt, err := p.ParseOneStmt(tt, "", "") - require.NoError(t, err, comment) - require.NoError(t, sessiontxn.NewTxn(context.Background(), tk.Session())) + planSuiteData := core.GetPlanSuiteData() + planSuiteData.LoadTestCases(t, &input, &output) - // Make txn not read only. - txn, err := tk.Session().Txn(true) - require.NoError(t, err) - err = txn.Set(kv.Key("AAA"), []byte("BBB")) - require.NoError(t, err) - tk.Session().StmtCommit() - p, _, err := planner.Optimize(context.TODO(), tk.Session(), stmt, is) - require.NoError(t, err) + for i, tt := range input { + tk.MustExec("begin") + tk.MustExec("insert into t values(1,1,1)") testdata.OnRecord(func() { output[i].SQL = tt - output[i].Best = core.ToString(p) + output[i].Best = testdata.ConvertRowsToStrings(tk.MustQuery(tt).Rows()) }) - require.Equal(t, output[i].Best, core.ToString(p), fmt.Sprintf("input: %s", tt)) + require.Equal(t, tt, output[i].SQL) + tk.MustQuery("explain " + tt).Check(testkit.Rows(output[i].Best...)) + tk.MustExec("rollback") } } diff --git a/planner/core/testdata/plan_suite_out.json b/planner/core/testdata/plan_suite_out.json index e54e0e4e31656..8ecd93ad642c8 100644 --- a/planner/core/testdata/plan_suite_out.json +++ b/planner/core/testdata/plan_suite_out.json @@ -831,7 +831,89 @@ }, { "Name": "TestDAGPlanBuilderUnionScan", - "Cases": null + "Cases": [ + { + "SQL": "select * from t", + "Best": [ + "Projection_4 10000.00 root test.t.a, test.t.b, test.t.c", + "└─UnionScan_5 10000.00 root ", + " └─TableReader_7 10000.00 root data:TableFullScan_6", + " └─TableFullScan_6 10000.00 cop[tikv] table:t keep order:false, stats:pseudo" + ] + }, + { + "SQL": "select * from t where b = 1", + "Best": [ + "Projection_5 10.00 root test.t.a, test.t.b, test.t.c", + "└─UnionScan_6 10.00 root eq(test.t.b, 1)", + " └─TableReader_9 10.00 root data:Selection_8", + " └─Selection_8 10.00 cop[tikv] eq(test.t.b, 1)", + " └─TableFullScan_7 10000.00 cop[tikv] table:t keep order:false, stats:pseudo" + ] + }, + { + "SQL": "select * from t where a = 1", + "Best": [ + "Projection_5 10.00 root test.t.a, test.t.b, test.t.c", + "└─UnionScan_6 10.00 root eq(test.t.a, 1)", + " └─TableReader_9 10.00 root data:Selection_8", + " └─Selection_8 10.00 cop[tikv] eq(test.t.a, 1)", + " └─TableFullScan_7 10000.00 cop[tikv] table:t keep order:false, stats:pseudo" + ] + }, + { + "SQL": "select * from t where a = 1 order by a", + "Best": [ + "Sort_6 10.00 root test.t.a", + "└─Projection_8 10.00 root test.t.a, test.t.b, test.t.c", + " └─UnionScan_9 10.00 root eq(test.t.a, 1)", + " └─TableReader_12 10.00 root data:Selection_11", + " └─Selection_11 10.00 cop[tikv] eq(test.t.a, 1)", + " └─TableFullScan_10 10000.00 cop[tikv] table:t keep order:false, stats:pseudo" + ] + }, + { + "SQL": "select * from t where a = 1 order by b", + "Best": [ + "Sort_6 10.00 root test.t.b", + "└─Projection_8 10.00 root test.t.a, test.t.b, test.t.c", + " └─UnionScan_9 10.00 root eq(test.t.a, 1)", + " └─TableReader_12 10.00 root data:Selection_11", + " └─Selection_11 10.00 cop[tikv] eq(test.t.a, 1)", + " └─TableFullScan_10 10000.00 cop[tikv] table:t keep order:false, stats:pseudo" + ] + }, + { + "SQL": "select * from t where a = 1 limit 1", + "Best": [ + "Limit_11 1.00 root offset:0, count:1", + "└─UnionScan_12 1.00 root eq(test.t.a, 1)", + " └─TableReader_15 1.00 root data:Selection_14", + " └─Selection_14 1.00 cop[tikv] eq(test.t.a, 1)", + " └─TableFullScan_13 1000.00 cop[tikv] table:t keep order:false, stats:pseudo" + ] + }, + { + "SQL": "select * from t where c = 1", + "Best": [ + "Projection_5 10.00 root test.t.a, test.t.b, test.t.c", + "└─UnionScan_6 10.00 root eq(test.t.c, 1)", + " └─TableReader_9 10.00 root data:Selection_8", + " └─Selection_8 10.00 cop[tikv] eq(test.t.c, 1)", + " └─TableFullScan_7 10000.00 cop[tikv] table:t keep order:false, stats:pseudo" + ] + }, + { + "SQL": "select c from t where c = 1", + "Best": [ + "Projection_5 10.00 root test.t.c", + "└─UnionScan_6 10.00 root eq(test.t.c, 1)", + " └─TableReader_9 10.00 root data:Selection_8", + " └─Selection_8 10.00 cop[tikv] eq(test.t.c, 1)", + " └─TableFullScan_7 10000.00 cop[tikv] table:t keep order:false, stats:pseudo" + ] + } + ] }, { "Name": "TestDAGPlanBuilderAgg", From d5c65b23bf91faad54ce37319933eefdbfff49f3 Mon Sep 17 00:00:00 2001 From: fzzf678 <108643977+fzzf678@users.noreply.github.com> Date: Fri, 16 Sep 2022 15:29:40 +0800 Subject: [PATCH 2/3] Update nogo_config.json --- build/nogo_config.json | 1 - 1 file changed, 1 deletion(-) diff --git a/build/nogo_config.json b/build/nogo_config.json index 68aff712dcefc..3223c91645d54 100644 --- a/build/nogo_config.json +++ b/build/nogo_config.json @@ -313,7 +313,6 @@ "nilness": { "exclude_files": { "/external/": "no need to vet third party code", - "planner/core/physical_plan_test.go": "please fix it", ".*_generated\\.go$": "ignore generated code", "/cgo/": "ignore cgo" } From 26828b1a1b9b1b1954587a4e132ef8ff77f57d7e Mon Sep 17 00:00:00 2001 From: fzzf678 <108643977+fzzf678@users.noreply.github.com> Date: Wed, 21 Sep 2022 11:50:44 +0800 Subject: [PATCH 3/3] function method --- planner/core/physical_plan_test.go | 23 +++++--- planner/core/testdata/plan_suite_out.json | 65 +++-------------------- 2 files changed, 23 insertions(+), 65 deletions(-) diff --git a/planner/core/physical_plan_test.go b/planner/core/physical_plan_test.go index 9377157ba1cee..6847cff976e5a 100644 --- a/planner/core/physical_plan_test.go +++ b/planner/core/physical_plan_test.go @@ -363,7 +363,6 @@ func TestDAGPlanBuilderUnion(t *testing.T) { func TestDAGPlanBuilderUnionScan(t *testing.T) { store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) tk.MustExec("use test") tk.MustExec("drop table if exists t") @@ -372,21 +371,29 @@ func TestDAGPlanBuilderUnionScan(t *testing.T) { var input []string var output []struct { SQL string - Best []string + Best string } planSuiteData := core.GetPlanSuiteData() planSuiteData.LoadTestCases(t, &input, &output) + p := parser.New() for i, tt := range input { - tk.MustExec("begin") - tk.MustExec("insert into t values(1,1,1)") + tk.MustExec("begin;") + tk.MustExec("insert into t values(2, 2, 2);") + + comment := fmt.Sprintf("input: %s", tt) + stmt, err := p.ParseOneStmt(tt, "", "") + require.NoError(t, err, comment) + dom := domain.GetDomain(tk.Session()) + require.NoError(t, dom.Reload()) + plan, _, err := planner.Optimize(context.TODO(), tk.Session(), stmt, dom.InfoSchema()) + require.NoError(t, err) testdata.OnRecord(func() { output[i].SQL = tt - output[i].Best = testdata.ConvertRowsToStrings(tk.MustQuery(tt).Rows()) + output[i].Best = core.ToString(plan) }) - require.Equal(t, tt, output[i].SQL) - tk.MustQuery("explain " + tt).Check(testkit.Rows(output[i].Best...)) - tk.MustExec("rollback") + require.Equal(t, output[i].Best, core.ToString(plan), fmt.Sprintf("input: %s", tt)) + tk.MustExec("rollback;") } } diff --git a/planner/core/testdata/plan_suite_out.json b/planner/core/testdata/plan_suite_out.json index a96b453d2c312..b15f224757bc7 100644 --- a/planner/core/testdata/plan_suite_out.json +++ b/planner/core/testdata/plan_suite_out.json @@ -834,84 +834,35 @@ "Cases": [ { "SQL": "select * from t", - "Best": [ - "Projection_4 10000.00 root test.t.a, test.t.b, test.t.c", - "└─UnionScan_5 10000.00 root ", - " └─TableReader_7 10000.00 root data:TableFullScan_6", - " └─TableFullScan_6 10000.00 cop[tikv] table:t keep order:false, stats:pseudo" - ] + "Best": "TableReader(Table(t))->UnionScan([])->Projection" }, { "SQL": "select * from t where b = 1", - "Best": [ - "Projection_5 10.00 root test.t.a, test.t.b, test.t.c", - "└─UnionScan_6 10.00 root eq(test.t.b, 1)", - " └─TableReader_9 10.00 root data:Selection_8", - " └─Selection_8 10.00 cop[tikv] eq(test.t.b, 1)", - " └─TableFullScan_7 10000.00 cop[tikv] table:t keep order:false, stats:pseudo" - ] + "Best": "TableReader(Table(t)->Sel([eq(test.t.b, 1)]))->UnionScan([eq(test.t.b, 1)])->Projection" }, { "SQL": "select * from t where a = 1", - "Best": [ - "Projection_5 10.00 root test.t.a, test.t.b, test.t.c", - "└─UnionScan_6 10.00 root eq(test.t.a, 1)", - " └─TableReader_9 10.00 root data:Selection_8", - " └─Selection_8 10.00 cop[tikv] eq(test.t.a, 1)", - " └─TableFullScan_7 10000.00 cop[tikv] table:t keep order:false, stats:pseudo" - ] + "Best": "TableReader(Table(t)->Sel([eq(test.t.a, 1)]))->UnionScan([eq(test.t.a, 1)])->Projection" }, { "SQL": "select * from t where a = 1 order by a", - "Best": [ - "Sort_6 10.00 root test.t.a", - "└─Projection_8 10.00 root test.t.a, test.t.b, test.t.c", - " └─UnionScan_9 10.00 root eq(test.t.a, 1)", - " └─TableReader_12 10.00 root data:Selection_11", - " └─Selection_11 10.00 cop[tikv] eq(test.t.a, 1)", - " └─TableFullScan_10 10000.00 cop[tikv] table:t keep order:false, stats:pseudo" - ] + "Best": "TableReader(Table(t)->Sel([eq(test.t.a, 1)]))->UnionScan([eq(test.t.a, 1)])->Projection->Sort" }, { "SQL": "select * from t where a = 1 order by b", - "Best": [ - "Sort_6 10.00 root test.t.b", - "└─Projection_8 10.00 root test.t.a, test.t.b, test.t.c", - " └─UnionScan_9 10.00 root eq(test.t.a, 1)", - " └─TableReader_12 10.00 root data:Selection_11", - " └─Selection_11 10.00 cop[tikv] eq(test.t.a, 1)", - " └─TableFullScan_10 10000.00 cop[tikv] table:t keep order:false, stats:pseudo" - ] + "Best": "TableReader(Table(t)->Sel([eq(test.t.a, 1)]))->UnionScan([eq(test.t.a, 1)])->Projection->Sort" }, { "SQL": "select * from t where a = 1 limit 1", - "Best": [ - "Limit_11 1.00 root offset:0, count:1", - "└─UnionScan_12 1.00 root eq(test.t.a, 1)", - " └─TableReader_15 1.00 root data:Selection_14", - " └─Selection_14 1.00 cop[tikv] eq(test.t.a, 1)", - " └─TableFullScan_13 1000.00 cop[tikv] table:t keep order:false, stats:pseudo" - ] + "Best": "TableReader(Table(t)->Sel([eq(test.t.a, 1)]))->UnionScan([eq(test.t.a, 1)])->Limit" }, { "SQL": "select * from t where c = 1", - "Best": [ - "Projection_5 10.00 root test.t.a, test.t.b, test.t.c", - "└─UnionScan_6 10.00 root eq(test.t.c, 1)", - " └─TableReader_9 10.00 root data:Selection_8", - " └─Selection_8 10.00 cop[tikv] eq(test.t.c, 1)", - " └─TableFullScan_7 10000.00 cop[tikv] table:t keep order:false, stats:pseudo" - ] + "Best": "TableReader(Table(t)->Sel([eq(test.t.c, 1)]))->UnionScan([eq(test.t.c, 1)])->Projection" }, { "SQL": "select c from t where c = 1", - "Best": [ - "Projection_5 10.00 root test.t.c", - "└─UnionScan_6 10.00 root eq(test.t.c, 1)", - " └─TableReader_9 10.00 root data:Selection_8", - " └─Selection_8 10.00 cop[tikv] eq(test.t.c, 1)", - " └─TableFullScan_7 10000.00 cop[tikv] table:t keep order:false, stats:pseudo" - ] + "Best": "TableReader(Table(t)->Sel([eq(test.t.c, 1)]))->UnionScan([eq(test.t.c, 1)])->Projection" } ] },