From f80fcd533039a8c3b42b370a3027e3c18fb4c885 Mon Sep 17 00:00:00 2001 From: unconsolable Date: Wed, 11 Aug 2021 14:36:34 +0800 Subject: [PATCH 01/11] testkit: introduce func ExecToErr Signed-off-by: unconsolable --- testkit/testkit.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/testkit/testkit.go b/testkit/testkit.go index acad311033517..6ec4cbc729aab 100644 --- a/testkit/testkit.go +++ b/testkit/testkit.go @@ -83,7 +83,7 @@ func (tk *TestKit) QueryToErr(sql string, args ...interface{}) error { tk.require.NoError(err, comment) tk.require.NotNil(res, comment) _, resErr := session.GetRows4Test(context.Background(), tk.session, res) - tk.require.Nil(res.Close()) + tk.require.NoError(res.Close()) return resErr } @@ -148,6 +148,15 @@ func (tk *TestKit) Exec(sql string, args ...interface{}) (sqlexec.RecordSet, err return rs, nil } +// ExecToErr executes a sql statement and discard results. +func (tk *TestKit) ExecToErr(sql string, args ...interface{}) error { + res, err := tk.Exec(sql, args...) + if res != nil { + tk.require.NoError(res.Close()) + } + return err +} + func newSession(t *testing.T, store kv.Storage) session.Session { se, err := session.CreateSession4Test(store) require.Nil(t, err) From 756d1cd66c6ea1f626a8c5025ec7e2a2c2c77ba2 Mon Sep 17 00:00:00 2001 From: unconsolable Date: Wed, 11 Aug 2021 14:37:21 +0800 Subject: [PATCH 02/11] executor/cte_test.go: migrate test-infra to testify Signed-off-by: unconsolable --- executor/cte_test.go | 117 +++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 54 deletions(-) diff --git a/executor/cte_test.go b/executor/cte_test.go index d5ec86a03bb05..ea0d30ca50b93 100644 --- a/executor/cte_test.go +++ b/executor/cte_test.go @@ -18,64 +18,72 @@ import ( "fmt" "math/rand" "sort" - - "github.com/pingcap/check" + "testing" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/sessionctx" + "github.com/stretchr/testify/require" "github.com/pingcap/failpoint" "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/store/mockstore" + "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/util/mock" - "github.com/pingcap/tidb/util/testkit" ) -var _ = check.Suite(&CTETestSuite{&baseCTETestSuite{}}) -var _ = check.SerialSuites(&CTESerialTestSuite{&baseCTETestSuite{}}) - -type baseCTETestSuite struct { +type CTETestSuite struct { store kv.Storage dom *domain.Domain sessionCtx sessionctx.Context session session.Session ctx context.Context + close func() } -type CTETestSuite struct { - *baseCTETestSuite -} - -type CTESerialTestSuite struct { - *baseCTETestSuite -} +var cteTestSuite *CTETestSuite -func (test *baseCTETestSuite) SetUpSuite(c *check.C) { +func SetUpSuite(t *testing.T) *CTETestSuite { var err error + test := new(CTETestSuite) + test.store, err = mockstore.NewMockStore() - c.Assert(err, check.IsNil) + require.NoError(t, err) test.dom, err = session.BootstrapSession(test.store) - c.Assert(err, check.IsNil) + require.NoError(t, err) test.sessionCtx = mock.NewContext() test.session, err = session.CreateSession4Test(test.store) - c.Assert(err, check.IsNil) + require.NoError(t, err) test.session.SetConnectionID(0) test.ctx = context.Background() + + test.close = func() { + test.dom.Close() + test.store.Close() + } + + return test } -func (test *baseCTETestSuite) TearDownSuite(c *check.C) { - test.dom.Close() - test.store.Close() +func TestCTESuite(t *testing.T) { + t.Parallel() + + cteTestSuite = SetUpSuite(t) + defer cteTestSuite.close() + + t.Run("TestBasicCTE", BasicCTE) + t.Run("TestUnionDistinct", UnionDistinct) + t.Run("TestCTEMaxRecursionDepth", CTEMaxRecursionDepth) + t.Run("TestCTEWithLimit", CTEWithLimit) } -func (test *CTETestSuite) TestBasicCTE(c *check.C) { - tk := testkit.NewTestKit(c, test.store) +func BasicCTE(t *testing.T) { + tk := testkit.NewTestKit(t, cteTestSuite.store) tk.MustExec("use test") rows := tk.MustQuery("with recursive cte1 as (" + @@ -120,23 +128,24 @@ func (test *CTETestSuite) TestBasicCTE(c *check.C) { rows.Check(testkit.Rows("1 1", "2 1", "3 1", "4 1", "5 1")) } -func (test *CTESerialTestSuite) TestSpillToDisk(c *check.C) { +func TestSpillToDisk(t *testing.T) { defer config.RestoreFunc()() config.UpdateGlobal(func(conf *config.Config) { conf.OOMUseTmpStorage = true }) - tk := testkit.NewTestKit(c, test.store) + test := SetUpSuite(t) + tk := testkit.NewTestKit(t, test.store) tk.MustExec("use test;") - c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/testCTEStorageSpill", "return(true)"), check.IsNil) + require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/testCTEStorageSpill", "return(true)")) defer func() { - c.Assert(failpoint.Disable("github.com/pingcap/tidb/executor/testCTEStorageSpill"), check.IsNil) + require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/testCTEStorageSpill")) tk.MustExec("set tidb_mem_quota_query = 1073741824;") }() - c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/testSortedRowContainerSpill", "return(true)"), check.IsNil) + require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/testSortedRowContainerSpill", "return(true)")) defer func() { - c.Assert(failpoint.Disable("github.com/pingcap/tidb/executor/testSortedRowContainerSpill"), check.IsNil) + require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/testSortedRowContainerSpill")) }() // Use duplicated rows to test UNION DISTINCT. @@ -162,10 +171,10 @@ func (test *CTESerialTestSuite) TestSpillToDisk(c *check.C) { "select c1 from cte1 order by c1;", rowNum) rows := tk.MustQuery(sql) - memTracker := tk.Se.GetSessionVars().StmtCtx.MemTracker - diskTracker := tk.Se.GetSessionVars().StmtCtx.DiskTracker - c.Assert(memTracker.MaxConsumed(), check.Greater, int64(0)) - c.Assert(diskTracker.MaxConsumed(), check.Greater, int64(0)) + memTracker := tk.Session().GetSessionVars().StmtCtx.MemTracker + diskTracker := tk.Session().GetSessionVars().StmtCtx.DiskTracker + require.Greater(t, memTracker.MaxConsumed(), int64(0)) + require.Greater(t, diskTracker.MaxConsumed(), int64(0)) sort.Ints(vals) resRows := make([]string, 0, rowNum) @@ -175,8 +184,8 @@ func (test *CTESerialTestSuite) TestSpillToDisk(c *check.C) { rows.Check(testkit.Rows(resRows...)) } -func (test *CTETestSuite) TestUnionDistinct(c *check.C) { - tk := testkit.NewTestKit(c, test.store) +func UnionDistinct(t *testing.T) { + tk := testkit.NewTestKit(t, cteTestSuite.store) tk.MustExec("use test;") // Basic test. UNION/UNION ALL intersects. @@ -199,14 +208,14 @@ func (test *CTETestSuite) TestUnionDistinct(c *check.C) { rows.Check(testkit.Rows("1", "2", "3", "4")) } -func (test *CTETestSuite) TestCTEMaxRecursionDepth(c *check.C) { - tk := testkit.NewTestKit(c, test.store) +func CTEMaxRecursionDepth(t *testing.T) { + tk := testkit.NewTestKit(t, cteTestSuite.store) tk.MustExec("use test;") tk.MustExec("set @@cte_max_recursion_depth = -1;") err := tk.QueryToErr("with recursive cte1(c1) as (select 1 union select c1 + 1 c1 from cte1 where c1 < 100) select * from cte1;") - c.Assert(err, check.NotNil) - c.Assert(err.Error(), check.Equals, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value") + require.Error(t, err) + require.Equal(t, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error()) // If there is no recursive part, query runs ok. rows := tk.MustQuery("with recursive cte1(c1) as (select 1 union select 2) select * from cte1 order by c1;") rows.Check(testkit.Rows("1", "2")) @@ -215,11 +224,11 @@ func (test *CTETestSuite) TestCTEMaxRecursionDepth(c *check.C) { tk.MustExec("set @@cte_max_recursion_depth = 0;") err = tk.QueryToErr("with recursive cte1(c1) as (select 1 union select c1 + 1 c1 from cte1 where c1 < 0) select * from cte1;") - c.Assert(err, check.NotNil) - c.Assert(err.Error(), check.Equals, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value") + require.Error(t, err) + require.Equal(t, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error()) err = tk.QueryToErr("with recursive cte1(c1) as (select 1 union select c1 + 1 c1 from cte1 where c1 < 1) select * from cte1;") - c.Assert(err, check.NotNil) - c.Assert(err.Error(), check.Equals, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value") + require.Error(t, err) + require.Equal(t, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error()) // If there is no recursive part, query runs ok. rows = tk.MustQuery("with recursive cte1(c1) as (select 1 union select 2) select * from cte1 order by c1;") rows.Check(testkit.Rows("1", "2")) @@ -232,8 +241,8 @@ func (test *CTETestSuite) TestCTEMaxRecursionDepth(c *check.C) { rows = tk.MustQuery("with recursive cte1(c1) as (select 1 union select c1 + 1 c1 from cte1 where c1 < 1) select * from cte1;") rows.Check(testkit.Rows("1")) err = tk.QueryToErr("with recursive cte1(c1) as (select 1 union select c1 + 1 c1 from cte1 where c1 < 2) select * from cte1;") - c.Assert(err, check.NotNil) - c.Assert(err.Error(), check.Equals, "[executor:3636]Recursive query aborted after 2 iterations. Try increasing @@cte_max_recursion_depth to a larger value") + require.Error(t, err) + require.Equal(t, "[executor:3636]Recursive query aborted after 2 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error()) // If there is no recursive part, query runs ok. rows = tk.MustQuery("with recursive cte1(c1) as (select 1 union select 2) select * from cte1 order by c1;") rows.Check(testkit.Rows("1", "2")) @@ -241,8 +250,8 @@ func (test *CTETestSuite) TestCTEMaxRecursionDepth(c *check.C) { rows.Check(testkit.Rows("1", "2")) } -func (test *CTETestSuite) TestCTEWithLimit(c *check.C) { - tk := testkit.NewTestKit(c, test.store) +func CTEWithLimit(t *testing.T) { + tk := testkit.NewTestKit(t, cteTestSuite.store) tk.MustExec("use test;") // Basic recursive tests. @@ -267,16 +276,16 @@ func (test *CTETestSuite) TestCTEWithLimit(c *check.C) { rows.Check(testkit.Rows("2")) err := tk.QueryToErr("with recursive cte1(c1) as (select 0 union select c1 + 1 from cte1 limit 1 offset 3) select * from cte1;") - c.Assert(err, check.NotNil) - c.Assert(err.Error(), check.Equals, "[executor:3636]Recursive query aborted after 3 iterations. Try increasing @@cte_max_recursion_depth to a larger value") + require.Error(t, err) + require.Equal(t, "[executor:3636]Recursive query aborted after 3 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error()) tk.MustExec("set cte_max_recursion_depth=1000;") rows = tk.MustQuery("with recursive cte1(c1) as (select 0 union select c1 + 1 from cte1 limit 5 offset 996) select * from cte1;") rows.Check(testkit.Rows("996", "997", "998", "999", "1000")) err = tk.QueryToErr("with recursive cte1(c1) as (select 0 union select c1 + 1 from cte1 limit 5 offset 997) select * from cte1;") - c.Assert(err, check.NotNil) - c.Assert(err.Error(), check.Equals, "[executor:3636]Recursive query aborted after 1001 iterations. Try increasing @@cte_max_recursion_depth to a larger value") + require.Error(t, err) + require.Equal(t, "[executor:3636]Recursive query aborted after 1001 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error()) rows = tk.MustQuery("with recursive cte1(c1) as (select 1 union select c1 + 1 from cte1 limit 0 offset 1) select * from cte1") rows.Check(testkit.Rows()) @@ -311,7 +320,7 @@ func (test *CTETestSuite) TestCTEWithLimit(c *check.C) { // Error: ERROR 1221 (HY000): Incorrect usage of UNION and LIMIT. // Limit can only be at the end of SQL stmt. err = tk.ExecToErr("with recursive cte1(c1) as (select c1 from t1 limit 1 offset 1 union select c1 + 1 from cte1 limit 0 offset 1) select * from cte1") - c.Assert(err.Error(), check.Equals, "[planner:1221]Incorrect usage of UNION and LIMIT") + require.Equal(t, "[planner:1221]Incorrect usage of UNION and LIMIT", err.Error()) // Basic non-recusive tests. rows = tk.MustQuery("with recursive cte1(c1) as (select 1 union select 2 order by 1 limit 1 offset 1) select * from cte1") @@ -374,8 +383,8 @@ func (test *CTETestSuite) TestCTEWithLimit(c *check.C) { rows.Check(testkit.Rows()) // MySQL err: ERROR 1365 (22012): Division by 0. Because it gives error when computing 1/c1. err = tk.QueryToErr("with recursive cte1 as (select 1/c1 c1 from t1 union select c1 + 1 c1 from cte1 where c1 < 2 limit 1) select * from cte1;") - c.Assert(err, check.NotNil) - c.Assert(err.Error(), check.Equals, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value") + require.Error(t, err) + require.Equal(t, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error()) tk.MustExec("set cte_max_recursion_depth = 1000;") tk.MustExec("drop table if exists t1;") From dae42e595486e5c32ee7cf599297b6a77c097b4a Mon Sep 17 00:00:00 2001 From: unconsolable Date: Wed, 11 Aug 2021 14:50:39 +0800 Subject: [PATCH 03/11] executor/cte_test.go: defer close in TestSpillToDisk Signed-off-by: unconsolable --- executor/cte_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/executor/cte_test.go b/executor/cte_test.go index ea0d30ca50b93..b88ea06dc8027 100644 --- a/executor/cte_test.go +++ b/executor/cte_test.go @@ -135,6 +135,8 @@ func TestSpillToDisk(t *testing.T) { }) test := SetUpSuite(t) + defer test.close() + tk := testkit.NewTestKit(t, test.store) tk.MustExec("use test;") From 9eb3b38a84ef500750c531c220514a806409e1ad Mon Sep 17 00:00:00 2001 From: unconsolable Date: Wed, 11 Aug 2021 16:35:00 +0800 Subject: [PATCH 04/11] executor/cte_test.go: make SetUpSuite before NewTestKit in TestSpillToDisk Signed-off-by: unconsolable --- executor/cte_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/executor/cte_test.go b/executor/cte_test.go index b88ea06dc8027..1596926894692 100644 --- a/executor/cte_test.go +++ b/executor/cte_test.go @@ -129,14 +129,14 @@ func BasicCTE(t *testing.T) { } func TestSpillToDisk(t *testing.T) { + test := SetUpSuite(t) + defer test.close() + defer config.RestoreFunc()() config.UpdateGlobal(func(conf *config.Config) { conf.OOMUseTmpStorage = true }) - test := SetUpSuite(t) - defer test.close() - tk := testkit.NewTestKit(t, test.store) tk.MustExec("use test;") From 9cb1dd939b75b1c3239b378192a7eae60cd6fb7f Mon Sep 17 00:00:00 2001 From: unconsolable Date: Wed, 11 Aug 2021 16:56:13 +0800 Subject: [PATCH 05/11] executor/cte_test.go: try to remove t.Parallel() to eliminate TLE Signed-off-by: unconsolable --- executor/cte_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/executor/cte_test.go b/executor/cte_test.go index 1596926894692..f05f9617d048e 100644 --- a/executor/cte_test.go +++ b/executor/cte_test.go @@ -71,7 +71,9 @@ func SetUpSuite(t *testing.T) *CTETestSuite { } func TestCTESuite(t *testing.T) { - t.Parallel() + // Before migration, this suite can be parallel + // with other suite. However, if t.Parallel() is introduced, + // tidbTestSerialSuite.TestTLS will run out of time easily. cteTestSuite = SetUpSuite(t) defer cteTestSuite.close() From 74709653188d6540da93f4daa12dc93066f8f49c Mon Sep 17 00:00:00 2001 From: unconsolable Date: Sat, 14 Aug 2021 18:38:28 +0800 Subject: [PATCH 06/11] executor/cte_test.go: try to restore t.Parallel() --- executor/cte_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/executor/cte_test.go b/executor/cte_test.go index f05f9617d048e..1596926894692 100644 --- a/executor/cte_test.go +++ b/executor/cte_test.go @@ -71,9 +71,7 @@ func SetUpSuite(t *testing.T) *CTETestSuite { } func TestCTESuite(t *testing.T) { - // Before migration, this suite can be parallel - // with other suite. However, if t.Parallel() is introduced, - // tidbTestSerialSuite.TestTLS will run out of time easily. + t.Parallel() cteTestSuite = SetUpSuite(t) defer cteTestSuite.close() From 3738658922565a5890de5040bb61b49a20005ad5 Mon Sep 17 00:00:00 2001 From: unconsolable Date: Mon, 16 Aug 2021 19:18:16 +0800 Subject: [PATCH 07/11] executor/cte_test.go: remove t.Parallel() in TestCTESuite Signed-off-by: unconsolable --- executor/cte_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/executor/cte_test.go b/executor/cte_test.go index 211c060a2cd97..484e3c59f6745 100644 --- a/executor/cte_test.go +++ b/executor/cte_test.go @@ -72,8 +72,6 @@ func SetUpSuite(t *testing.T) *CTETestSuite { } func TestCTESuite(t *testing.T) { - t.Parallel() - cteTestSuite = SetUpSuite(t) defer cteTestSuite.close() From 93e5d74d2e8c5774b212a7519fa4cf9e617773b8 Mon Sep 17 00:00:00 2001 From: unconsolable Date: Thu, 19 Aug 2021 11:32:05 +0800 Subject: [PATCH 08/11] executor/cte_test.go: format import, remove global variable, try parallel Signed-off-by: unconsolable --- executor/cte_test.go | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/executor/cte_test.go b/executor/cte_test.go index 484e3c59f6745..88d726fe03010 100644 --- a/executor/cte_test.go +++ b/executor/cte_test.go @@ -21,17 +21,16 @@ import ( "sort" "testing" + "github.com/pingcap/failpoint" + "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/sessionctx" - "github.com/stretchr/testify/require" - - "github.com/pingcap/failpoint" - "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/util/mock" + "github.com/stretchr/testify/require" ) type CTETestSuite struct { @@ -43,8 +42,6 @@ type CTETestSuite struct { close func() } -var cteTestSuite *CTETestSuite - func SetUpSuite(t *testing.T) *CTETestSuite { var err error test := new(CTETestSuite) @@ -72,17 +69,19 @@ func SetUpSuite(t *testing.T) *CTETestSuite { } func TestCTESuite(t *testing.T) { - cteTestSuite = SetUpSuite(t) + t.Parallel() + + cteTestSuite := SetUpSuite(t) defer cteTestSuite.close() - t.Run("TestBasicCTE", BasicCTE) - t.Run("TestUnionDistinct", UnionDistinct) - t.Run("TestCTEMaxRecursionDepth", CTEMaxRecursionDepth) - t.Run("TestCTEWithLimit", CTEWithLimit) + BasicCTE(t, cteTestSuite) + UnionDistinct(t, cteTestSuite) + CTEMaxRecursionDepth(t, cteTestSuite) + CTEWithLimit(t, cteTestSuite) } -func BasicCTE(t *testing.T) { - tk := testkit.NewTestKit(t, cteTestSuite.store) +func BasicCTE(t *testing.T, suite *CTETestSuite) { + tk := testkit.NewTestKit(t, suite.store) tk.MustExec("use test") rows := tk.MustQuery("with recursive cte1 as (" + @@ -185,8 +184,8 @@ func TestSpillToDisk(t *testing.T) { rows.Check(testkit.Rows(resRows...)) } -func UnionDistinct(t *testing.T) { - tk := testkit.NewTestKit(t, cteTestSuite.store) +func UnionDistinct(t *testing.T, suite *CTETestSuite) { + tk := testkit.NewTestKit(t, suite.store) tk.MustExec("use test;") // Basic test. UNION/UNION ALL intersects. @@ -209,8 +208,8 @@ func UnionDistinct(t *testing.T) { rows.Check(testkit.Rows("1", "2", "3", "4")) } -func CTEMaxRecursionDepth(t *testing.T) { - tk := testkit.NewTestKit(t, cteTestSuite.store) +func CTEMaxRecursionDepth(t *testing.T, suite *CTETestSuite) { + tk := testkit.NewTestKit(t, suite.store) tk.MustExec("use test;") tk.MustExec("set @@cte_max_recursion_depth = -1;") @@ -251,8 +250,8 @@ func CTEMaxRecursionDepth(t *testing.T) { rows.Check(testkit.Rows("1", "2")) } -func CTEWithLimit(t *testing.T) { - tk := testkit.NewTestKit(t, cteTestSuite.store) +func CTEWithLimit(t *testing.T, suite *CTETestSuite) { + tk := testkit.NewTestKit(t, suite.store) tk.MustExec("use test;") // Basic recursive tests. From d686ecbc065d7682ce712ab30ba4d0622bfab754 Mon Sep 17 00:00:00 2001 From: unconsolable Date: Thu, 19 Aug 2021 11:41:59 +0800 Subject: [PATCH 09/11] executor/cte_test.go: remove parallel --- executor/cte_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/executor/cte_test.go b/executor/cte_test.go index 88d726fe03010..d994925550d24 100644 --- a/executor/cte_test.go +++ b/executor/cte_test.go @@ -69,8 +69,6 @@ func SetUpSuite(t *testing.T) *CTETestSuite { } func TestCTESuite(t *testing.T) { - t.Parallel() - cteTestSuite := SetUpSuite(t) defer cteTestSuite.close() From c3d8789f47ec11ac332dcb36f2cddc0bce36c837 Mon Sep 17 00:00:00 2001 From: unconsolable Date: Thu, 19 Aug 2021 16:31:13 +0800 Subject: [PATCH 10/11] executor/cte_test.go: remove suite, increase parallelism Signed-off-by: unconsolable --- executor/cte_serial_test.go | 85 +++++++++++++++++++++ executor/cte_test.go | 142 ++++++------------------------------ 2 files changed, 109 insertions(+), 118 deletions(-) create mode 100644 executor/cte_serial_test.go diff --git a/executor/cte_serial_test.go b/executor/cte_serial_test.go new file mode 100644 index 0000000000000..b8b04551b0c6e --- /dev/null +++ b/executor/cte_serial_test.go @@ -0,0 +1,85 @@ +// Copyright 2021 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package executor_test + +import ( + "fmt" + "math/rand" + "sort" + "testing" + + "github.com/pingcap/failpoint" + "github.com/pingcap/tidb/config" + "github.com/pingcap/tidb/testkit" + "github.com/stretchr/testify/require" +) + +func TestSpillToDisk(t *testing.T) { + defer config.RestoreFunc()() + config.UpdateGlobal(func(conf *config.Config) { + conf.OOMUseTmpStorage = true + }) + + store, close := testkit.CreateMockStore(t) + defer close() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test;") + + require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/testCTEStorageSpill", "return(true)")) + defer func() { + require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/testCTEStorageSpill")) + tk.MustExec("set tidb_mem_quota_query = 1073741824;") + }() + require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/testSortedRowContainerSpill", "return(true)")) + defer func() { + require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/testSortedRowContainerSpill")) + }() + + // Use duplicated rows to test UNION DISTINCT. + tk.MustExec("set tidb_mem_quota_query = 1073741824;") + insertStr := "insert into t1 values(0)" + rowNum := 1000 + vals := make([]int, rowNum) + vals[0] = 0 + for i := 1; i < rowNum; i++ { + v := rand.Intn(100) + vals[i] = v + insertStr += fmt.Sprintf(", (%d)", v) + } + tk.MustExec("drop table if exists t1;") + tk.MustExec("create table t1(c1 int);") + tk.MustExec(insertStr) + tk.MustExec("set tidb_mem_quota_query = 40000;") + tk.MustExec("set cte_max_recursion_depth = 500000;") + sql := fmt.Sprintf("with recursive cte1 as ( "+ + "select c1 from t1 "+ + "union "+ + "select c1 + 1 c1 from cte1 where c1 < %d) "+ + "select c1 from cte1 order by c1;", rowNum) + rows := tk.MustQuery(sql) + + memTracker := tk.Session().GetSessionVars().StmtCtx.MemTracker + diskTracker := tk.Session().GetSessionVars().StmtCtx.DiskTracker + require.Greater(t, memTracker.MaxConsumed(), int64(0)) + require.Greater(t, diskTracker.MaxConsumed(), int64(0)) + + sort.Ints(vals) + resRows := make([]string, 0, rowNum) + for i := vals[0]; i <= rowNum; i++ { + resRows = append(resRows, fmt.Sprintf("%d", i)) + } + rows.Check(testkit.Rows(resRows...)) +} diff --git a/executor/cte_test.go b/executor/cte_test.go index d994925550d24..3da5286e99cf5 100644 --- a/executor/cte_test.go +++ b/executor/cte_test.go @@ -15,71 +15,20 @@ package executor_test import ( - "context" "fmt" - "math/rand" - "sort" "testing" - "github.com/pingcap/failpoint" - "github.com/pingcap/tidb/config" - "github.com/pingcap/tidb/domain" - "github.com/pingcap/tidb/kv" - "github.com/pingcap/tidb/session" - "github.com/pingcap/tidb/sessionctx" - "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/testkit" - "github.com/pingcap/tidb/util/mock" "github.com/stretchr/testify/require" ) -type CTETestSuite struct { - store kv.Storage - dom *domain.Domain - sessionCtx sessionctx.Context - session session.Session - ctx context.Context - close func() -} - -func SetUpSuite(t *testing.T) *CTETestSuite { - var err error - test := new(CTETestSuite) - - test.store, err = mockstore.NewMockStore() - require.NoError(t, err) - - test.dom, err = session.BootstrapSession(test.store) - require.NoError(t, err) - - test.sessionCtx = mock.NewContext() - - test.session, err = session.CreateSession4Test(test.store) - require.NoError(t, err) - test.session.SetConnectionID(0) - - test.ctx = context.Background() +func TestBasicCTE(t *testing.T) { + t.Parallel() - test.close = func() { - test.dom.Close() - test.store.Close() - } - - return test -} - -func TestCTESuite(t *testing.T) { - cteTestSuite := SetUpSuite(t) - defer cteTestSuite.close() + store, close := testkit.CreateMockStore(t) + defer close() - BasicCTE(t, cteTestSuite) - UnionDistinct(t, cteTestSuite) - CTEMaxRecursionDepth(t, cteTestSuite) - CTEWithLimit(t, cteTestSuite) -} - -func BasicCTE(t *testing.T, suite *CTETestSuite) { - tk := testkit.NewTestKit(t, suite.store) + tk := testkit.NewTestKit(t, store) tk.MustExec("use test") rows := tk.MustQuery("with recursive cte1 as (" + @@ -124,66 +73,13 @@ func BasicCTE(t *testing.T, suite *CTETestSuite) { rows.Check(testkit.Rows("1 1", "2 1", "3 1", "4 1", "5 1")) } -func TestSpillToDisk(t *testing.T) { - test := SetUpSuite(t) - defer test.close() +func TestUnionDistinct(t *testing.T) { + t.Parallel() - defer config.RestoreFunc()() - config.UpdateGlobal(func(conf *config.Config) { - conf.OOMUseTmpStorage = true - }) + store, close := testkit.CreateMockStore(t) + defer close() - tk := testkit.NewTestKit(t, test.store) - tk.MustExec("use test;") - - require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/testCTEStorageSpill", "return(true)")) - defer func() { - require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/testCTEStorageSpill")) - tk.MustExec("set tidb_mem_quota_query = 1073741824;") - }() - require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/testSortedRowContainerSpill", "return(true)")) - defer func() { - require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/testSortedRowContainerSpill")) - }() - - // Use duplicated rows to test UNION DISTINCT. - tk.MustExec("set tidb_mem_quota_query = 1073741824;") - insertStr := "insert into t1 values(0)" - rowNum := 1000 - vals := make([]int, rowNum) - vals[0] = 0 - for i := 1; i < rowNum; i++ { - v := rand.Intn(100) - vals[i] = v - insertStr += fmt.Sprintf(", (%d)", v) - } - tk.MustExec("drop table if exists t1;") - tk.MustExec("create table t1(c1 int);") - tk.MustExec(insertStr) - tk.MustExec("set tidb_mem_quota_query = 40000;") - tk.MustExec("set cte_max_recursion_depth = 500000;") - sql := fmt.Sprintf("with recursive cte1 as ( "+ - "select c1 from t1 "+ - "union "+ - "select c1 + 1 c1 from cte1 where c1 < %d) "+ - "select c1 from cte1 order by c1;", rowNum) - rows := tk.MustQuery(sql) - - memTracker := tk.Session().GetSessionVars().StmtCtx.MemTracker - diskTracker := tk.Session().GetSessionVars().StmtCtx.DiskTracker - require.Greater(t, memTracker.MaxConsumed(), int64(0)) - require.Greater(t, diskTracker.MaxConsumed(), int64(0)) - - sort.Ints(vals) - resRows := make([]string, 0, rowNum) - for i := vals[0]; i <= rowNum; i++ { - resRows = append(resRows, fmt.Sprintf("%d", i)) - } - rows.Check(testkit.Rows(resRows...)) -} - -func UnionDistinct(t *testing.T, suite *CTETestSuite) { - tk := testkit.NewTestKit(t, suite.store) + tk := testkit.NewTestKit(t, store) tk.MustExec("use test;") // Basic test. UNION/UNION ALL intersects. @@ -206,8 +102,13 @@ func UnionDistinct(t *testing.T, suite *CTETestSuite) { rows.Check(testkit.Rows("1", "2", "3", "4")) } -func CTEMaxRecursionDepth(t *testing.T, suite *CTETestSuite) { - tk := testkit.NewTestKit(t, suite.store) +func TestCTEMaxRecursionDepth(t *testing.T) { + t.Parallel() + + store, close := testkit.CreateMockStore(t) + defer close() + + tk := testkit.NewTestKit(t, store) tk.MustExec("use test;") tk.MustExec("set @@cte_max_recursion_depth = -1;") @@ -248,8 +149,13 @@ func CTEMaxRecursionDepth(t *testing.T, suite *CTETestSuite) { rows.Check(testkit.Rows("1", "2")) } -func CTEWithLimit(t *testing.T, suite *CTETestSuite) { - tk := testkit.NewTestKit(t, suite.store) +func TestCTEWithLimit(t *testing.T) { + t.Parallel() + + store, close := testkit.CreateMockStore(t) + defer close() + + tk := testkit.NewTestKit(t, store) tk.MustExec("use test;") // Basic recursive tests. From a9e298f7a6a00798bdf3b6d7679b18df6e83a1de Mon Sep 17 00:00:00 2001 From: unconsolable Date: Thu, 19 Aug 2021 16:49:27 +0800 Subject: [PATCH 11/11] executor/cte_test.go: replace error check to EqualError Signed-off-by: unconsolable --- executor/cte_test.go | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/executor/cte_test.go b/executor/cte_test.go index 3da5286e99cf5..3a9c3a5a7987f 100644 --- a/executor/cte_test.go +++ b/executor/cte_test.go @@ -113,8 +113,7 @@ func TestCTEMaxRecursionDepth(t *testing.T) { tk.MustExec("set @@cte_max_recursion_depth = -1;") err := tk.QueryToErr("with recursive cte1(c1) as (select 1 union select c1 + 1 c1 from cte1 where c1 < 100) select * from cte1;") - require.Error(t, err) - require.Equal(t, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error()) + require.EqualError(t, err, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value") // If there is no recursive part, query runs ok. rows := tk.MustQuery("with recursive cte1(c1) as (select 1 union select 2) select * from cte1 order by c1;") rows.Check(testkit.Rows("1", "2")) @@ -123,11 +122,9 @@ func TestCTEMaxRecursionDepth(t *testing.T) { tk.MustExec("set @@cte_max_recursion_depth = 0;") err = tk.QueryToErr("with recursive cte1(c1) as (select 1 union select c1 + 1 c1 from cte1 where c1 < 0) select * from cte1;") - require.Error(t, err) - require.Equal(t, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error()) + require.EqualError(t, err, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value") err = tk.QueryToErr("with recursive cte1(c1) as (select 1 union select c1 + 1 c1 from cte1 where c1 < 1) select * from cte1;") - require.Error(t, err) - require.Equal(t, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error()) + require.EqualError(t, err, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value") // If there is no recursive part, query runs ok. rows = tk.MustQuery("with recursive cte1(c1) as (select 1 union select 2) select * from cte1 order by c1;") rows.Check(testkit.Rows("1", "2")) @@ -140,8 +137,7 @@ func TestCTEMaxRecursionDepth(t *testing.T) { rows = tk.MustQuery("with recursive cte1(c1) as (select 1 union select c1 + 1 c1 from cte1 where c1 < 1) select * from cte1;") rows.Check(testkit.Rows("1")) err = tk.QueryToErr("with recursive cte1(c1) as (select 1 union select c1 + 1 c1 from cte1 where c1 < 2) select * from cte1;") - require.Error(t, err) - require.Equal(t, "[executor:3636]Recursive query aborted after 2 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error()) + require.EqualError(t, err, "[executor:3636]Recursive query aborted after 2 iterations. Try increasing @@cte_max_recursion_depth to a larger value") // If there is no recursive part, query runs ok. rows = tk.MustQuery("with recursive cte1(c1) as (select 1 union select 2) select * from cte1 order by c1;") rows.Check(testkit.Rows("1", "2")) @@ -180,16 +176,14 @@ func TestCTEWithLimit(t *testing.T) { rows.Check(testkit.Rows("2")) err := tk.QueryToErr("with recursive cte1(c1) as (select 0 union select c1 + 1 from cte1 limit 1 offset 3) select * from cte1;") - require.Error(t, err) - require.Equal(t, "[executor:3636]Recursive query aborted after 3 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error()) + require.EqualError(t, err, "[executor:3636]Recursive query aborted after 3 iterations. Try increasing @@cte_max_recursion_depth to a larger value") tk.MustExec("set cte_max_recursion_depth=1000;") rows = tk.MustQuery("with recursive cte1(c1) as (select 0 union select c1 + 1 from cte1 limit 5 offset 996) select * from cte1;") rows.Check(testkit.Rows("996", "997", "998", "999", "1000")) err = tk.QueryToErr("with recursive cte1(c1) as (select 0 union select c1 + 1 from cte1 limit 5 offset 997) select * from cte1;") - require.Error(t, err) - require.Equal(t, "[executor:3636]Recursive query aborted after 1001 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error()) + require.EqualError(t, err, "[executor:3636]Recursive query aborted after 1001 iterations. Try increasing @@cte_max_recursion_depth to a larger value") rows = tk.MustQuery("with recursive cte1(c1) as (select 1 union select c1 + 1 from cte1 limit 0 offset 1) select * from cte1") rows.Check(testkit.Rows()) @@ -224,7 +218,7 @@ func TestCTEWithLimit(t *testing.T) { // Error: ERROR 1221 (HY000): Incorrect usage of UNION and LIMIT. // Limit can only be at the end of SQL stmt. err = tk.ExecToErr("with recursive cte1(c1) as (select c1 from t1 limit 1 offset 1 union select c1 + 1 from cte1 limit 0 offset 1) select * from cte1") - require.Equal(t, "[planner:1221]Incorrect usage of UNION and LIMIT", err.Error()) + require.EqualError(t, err, "[planner:1221]Incorrect usage of UNION and LIMIT") // Basic non-recusive tests. rows = tk.MustQuery("with recursive cte1(c1) as (select 1 union select 2 order by 1 limit 1 offset 1) select * from cte1") @@ -287,8 +281,7 @@ func TestCTEWithLimit(t *testing.T) { rows.Check(testkit.Rows()) // MySQL err: ERROR 1365 (22012): Division by 0. Because it gives error when computing 1/c1. err = tk.QueryToErr("with recursive cte1 as (select 1/c1 c1 from t1 union select c1 + 1 c1 from cte1 where c1 < 2 limit 1) select * from cte1;") - require.Error(t, err) - require.Equal(t, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error()) + require.EqualError(t, err, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value") tk.MustExec("set cte_max_recursion_depth = 1000;") tk.MustExec("drop table if exists t1;")