From ba5e85b75f02f2ab1cc226e87a45dbcb9774eaa7 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Wed, 14 Dec 2022 12:06:51 +0800 Subject: [PATCH 1/3] *: add dependabot (#39229) --- .github/dependabot.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000000000..bde24b721461e --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,14 @@ +version: 2 +updates: + - package-ecosystem: "gomod" + directory: "/" + schedule: + interval: "weekly" + day: "friday" + time: "18:00" + timezone: "Asia/Shanghai" + allow: + - dependency-name: "golang.org/*" + - dependency-name: "github.com/golangci/golangci-lint" + open-pull-requests-limit: 2 + From b41be060b96867999a9fca83853f196766bc4ef9 Mon Sep 17 00:00:00 2001 From: xiongjiwei Date: Wed, 14 Dec 2022 12:48:52 +0800 Subject: [PATCH 2/3] expression: support `json_overlaps` function (#39867) ref pingcap/tidb#39866 --- executor/showtest/show_test.go | 2 +- expression/builtin.go | 1 + expression/builtin_json.go | 58 +++++++++++++++++++++++++++++ expression/builtin_json_test.go | 65 +++++++++++++++++++++++++++++++++ expression/builtin_json_vec.go | 45 +++++++++++++++++++++++ expression/integration_test.go | 7 ++++ parser/ast/functions.go | 1 + types/json_binary_functions.go | 42 +++++++++++++++++++++ 8 files changed, 220 insertions(+), 1 deletion(-) diff --git a/executor/showtest/show_test.go b/executor/showtest/show_test.go index 38f6f7ada6410..bcbd7c0016f85 100644 --- a/executor/showtest/show_test.go +++ b/executor/showtest/show_test.go @@ -1515,7 +1515,7 @@ func TestShowBuiltin(t *testing.T) { res := tk.MustQuery("show builtins;") require.NotNil(t, res) rows := res.Rows() - const builtinFuncNum = 283 + const builtinFuncNum = 284 require.Equal(t, builtinFuncNum, len(rows)) require.Equal(t, rows[0][0].(string), "abs") require.Equal(t, rows[builtinFuncNum-1][0].(string), "yearweek") diff --git a/expression/builtin.go b/expression/builtin.go index f10cf9aa3dfa9..18e78ba17bdb8 100644 --- a/expression/builtin.go +++ b/expression/builtin.go @@ -874,6 +874,7 @@ var funcs = map[string]functionClass{ ast.JSONObject: &jsonObjectFunctionClass{baseFunctionClass{ast.JSONObject, 0, -1}}, ast.JSONArray: &jsonArrayFunctionClass{baseFunctionClass{ast.JSONArray, 0, -1}}, ast.JSONContains: &jsonContainsFunctionClass{baseFunctionClass{ast.JSONContains, 2, 3}}, + ast.JSONOverlaps: &jsonOverlapsFunctionClass{baseFunctionClass{ast.JSONOverlaps, 2, 2}}, ast.JSONContainsPath: &jsonContainsPathFunctionClass{baseFunctionClass{ast.JSONContainsPath, 3, -1}}, ast.JSONValid: &jsonValidFunctionClass{baseFunctionClass{ast.JSONValid, 1, 1}}, ast.JSONArrayAppend: &jsonArrayAppendFunctionClass{baseFunctionClass{ast.JSONArrayAppend, 3, -1}}, diff --git a/expression/builtin_json.go b/expression/builtin_json.go index e317fa88e952a..eeabef6fe2880 100644 --- a/expression/builtin_json.go +++ b/expression/builtin_json.go @@ -44,6 +44,7 @@ var ( _ functionClass = &jsonObjectFunctionClass{} _ functionClass = &jsonArrayFunctionClass{} _ functionClass = &jsonContainsFunctionClass{} + _ functionClass = &jsonOverlapsFunctionClass{} _ functionClass = &jsonContainsPathFunctionClass{} _ functionClass = &jsonValidFunctionClass{} _ functionClass = &jsonArrayAppendFunctionClass{} @@ -72,6 +73,7 @@ var ( _ builtinFunc = &builtinJSONRemoveSig{} _ builtinFunc = &builtinJSONMergeSig{} _ builtinFunc = &builtinJSONContainsSig{} + _ builtinFunc = &builtinJSONOverlapsSig{} _ builtinFunc = &builtinJSONStorageSizeSig{} _ builtinFunc = &builtinJSONDepthSig{} _ builtinFunc = &builtinJSONSearchSig{} @@ -820,6 +822,62 @@ func (b *builtinJSONContainsSig) evalInt(row chunk.Row) (res int64, isNull bool, return 0, false, nil } +type jsonOverlapsFunctionClass struct { + baseFunctionClass +} + +type builtinJSONOverlapsSig struct { + baseBuiltinFunc +} + +func (b *builtinJSONOverlapsSig) Clone() builtinFunc { + newSig := &builtinJSONOverlapsSig{} + newSig.cloneFrom(&b.baseBuiltinFunc) + return newSig +} + +func (c *jsonOverlapsFunctionClass) verifyArgs(args []Expression) error { + if err := c.baseFunctionClass.verifyArgs(args); err != nil { + return err + } + if evalType := args[0].GetType().EvalType(); evalType != types.ETJson && evalType != types.ETString { + return types.ErrInvalidJSONData.GenWithStackByArgs(1, "json_overlaps") + } + if evalType := args[1].GetType().EvalType(); evalType != types.ETJson && evalType != types.ETString { + return types.ErrInvalidJSONData.GenWithStackByArgs(2, "json_overlaps") + } + return nil +} + +func (c *jsonOverlapsFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) { + if err := c.verifyArgs(args); err != nil { + return nil, err + } + + argTps := []types.EvalType{types.ETJson, types.ETJson} + bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, argTps...) + if err != nil { + return nil, err + } + sig := &builtinJSONOverlapsSig{bf} + return sig, nil +} + +func (b *builtinJSONOverlapsSig) evalInt(row chunk.Row) (res int64, isNull bool, err error) { + obj, isNull, err := b.args[0].EvalJSON(b.ctx, row) + if isNull || err != nil { + return res, isNull, err + } + target, isNull, err := b.args[1].EvalJSON(b.ctx, row) + if isNull || err != nil { + return res, isNull, err + } + if types.OverlapsBinaryJSON(obj, target) { + return 1, false, nil + } + return 0, false, nil +} + type jsonValidFunctionClass struct { baseFunctionClass } diff --git a/expression/builtin_json_test.go b/expression/builtin_json_test.go index 3e142860fdd27..72e3c725594c4 100644 --- a/expression/builtin_json_test.go +++ b/expression/builtin_json_test.go @@ -466,6 +466,71 @@ func TestJSONContains(t *testing.T) { } } +func TestJSONOverlaps(t *testing.T) { + ctx := createContext(t) + fc := funcs[ast.JSONOverlaps] + tbl := []struct { + input []any + expected any + err error + }{ + {[]any{`[1,2,[1,3]]`, `a:1`}, 1, types.ErrInvalidJSONText}, + {[]any{`a:1`, `1`}, 1, types.ErrInvalidJSONText}, + {[]any{nil, `1`}, nil, nil}, + {[]any{`1`, nil}, nil, nil}, + + {[]any{`[1, 2]`, `[2,3]`}, 1, nil}, + {[]any{`[1, 2]`, `[2]`}, 1, nil}, + {[]any{`[1, 2]`, `2`}, 1, nil}, + {[]any{`[{"a":1}]`, `{"a":1}`}, 1, nil}, + {[]any{`[{"a":1}]`, `{"a":1,"b":2}`}, 0, nil}, + {[]any{`[{"a":1}]`, `{"a":2}`}, 0, nil}, + {[]any{`{"a":[1,2]}`, `{"a":[1]}`}, 0, nil}, + {[]any{`{"a":[1,2]}`, `{"a":[2,1]}`}, 0, nil}, + {[]any{`[1,1,1]`, `1`}, 1, nil}, + {[]any{`1`, `1`}, 1, nil}, + {[]any{`0`, `1`}, 0, nil}, + {[]any{`[[1,2], 3]`, `[1,[2,3]]`}, 0, nil}, + {[]any{`[[1,2], 3]`, `[1,3]`}, 1, nil}, + {[]any{`{"a":1,"b":10,"d":10}`, `{"a":5,"e":10,"f":1,"d":20}`}, 0, nil}, + {[]any{`[4,5,"6",7]`, `6`}, 0, nil}, + {[]any{`[4,5,6,7]`, `"6"`}, 0, nil}, + + {[]any{`[2,3]`, `[1, 2]`}, 1, nil}, + {[]any{`[2]`, `[1, 2]`}, 1, nil}, + {[]any{`2`, `[1, 2]`}, 1, nil}, + {[]any{`{"a":1}`, `[{"a":1}]`}, 1, nil}, + {[]any{`{"a":1,"b":2}`, `[{"a":1}]`}, 0, nil}, + {[]any{`{"a":2}`, `[{"a":1}]`}, 0, nil}, + {[]any{`{"a":[1]}`, `{"a":[1,2]}`}, 0, nil}, + {[]any{`{"a":[2,1]}`, `{"a":[1,2]}`}, 0, nil}, + {[]any{`1`, `[1,1,1]`}, 1, nil}, + {[]any{`1`, `1`}, 1, nil}, + {[]any{`1`, `0`}, 0, nil}, + {[]any{`[1,[2,3]]`, `[[1,2], 3]`}, 0, nil}, + {[]any{`[1,3]`, `[[1,2], 3]`}, 1, nil}, + {[]any{`{"a":5,"e":10,"f":1,"d":20}`, `{"a":1,"b":10,"d":10}`}, 0, nil}, + {[]any{`6`, `[4,5,"6",7]`}, 0, nil}, + {[]any{`"6"`, `[4,5,6,7]`}, 0, nil}, + } + for _, tt := range tbl { + args := types.MakeDatums(tt.input...) + f, err := fc.getFunction(ctx, datumsToConstants(args)) + require.NoError(t, err, tt.input) + d, err := evalBuiltinFunc(f, chunk.Row{}) + if tt.err == nil { + require.NoError(t, err, tt.input) + if tt.expected == nil { + require.True(t, d.IsNull(), tt.input) + } else { + require.Equal(t, int64(tt.expected.(int)), d.GetInt64(), tt.input) + } + } else { + require.True(t, tt.err.(*terror.Error).Equal(err), tt.input) + } + } +} + func TestJSONContainsPath(t *testing.T) { ctx := createContext(t) fc := funcs[ast.JSONContainsPath] diff --git a/expression/builtin_json_vec.go b/expression/builtin_json_vec.go index fb24808ff2c73..45cca97232d2c 100644 --- a/expression/builtin_json_vec.go +++ b/expression/builtin_json_vec.go @@ -359,6 +359,51 @@ func (b *builtinJSONContainsSig) vecEvalInt(input *chunk.Chunk, result *chunk.Co return nil } +func (b *builtinJSONOverlapsSig) vectorized() bool { + return true +} + +func (b *builtinJSONOverlapsSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column) error { + nr := input.NumRows() + + objCol, err := b.bufAllocator.get() + if err != nil { + return err + } + defer b.bufAllocator.put(objCol) + + if err := b.args[0].VecEvalJSON(b.ctx, input, objCol); err != nil { + return err + } + + targetCol, err := b.bufAllocator.get() + if err != nil { + return err + } + defer b.bufAllocator.put(targetCol) + + if err := b.args[1].VecEvalJSON(b.ctx, input, targetCol); err != nil { + return err + } + + result.ResizeInt64(nr, false) + resI64s := result.Int64s() + + result.MergeNulls(objCol, targetCol) + for i := 0; i < nr; i++ { + if result.IsNull(i) { + continue + } + if types.OverlapsBinaryJSON(objCol.GetJSON(i), targetCol.GetJSON(i)) { + resI64s[i] = 1 + } else { + resI64s[i] = 0 + } + } + + return nil +} + func (b *builtinJSONQuoteSig) vectorized() bool { return true } diff --git a/expression/integration_test.go b/expression/integration_test.go index 55c8f389a5df3..5a4061c011bff 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -2721,6 +2721,13 @@ func TestFuncJSON(t *testing.T) { // #16267 tk.MustQuery(`select json_array(922337203685477580) = json_array(922337203685477581);`).Check(testkit.Rows("0")) + tk.MustQuery("select json_overlaps('[[1,2], 3]', '[1, 3]');").Check(testkit.Rows("1")) + tk.MustQuery("select json_overlaps('[{\"a\":1}]', '{\"a\":1}');").Check(testkit.Rows("1")) + tk.MustQuery("select json_overlaps('{\"a\":1}', '[{\"a\":1}]');").Check(testkit.Rows("1")) + tk.MustQuery("select json_overlaps('[1,[2,3]]', '[[1,2], 3]');").Check(testkit.Rows("0")) + tk.MustQuery("select json_overlaps('{\"a\":[1,2]}', '{\"a\":[2,1]}');").Check(testkit.Rows("0")) + tk.MustQuery("select json_overlaps('{\"a\":[1,2]}', '{\"a\":[2,1]}');").Check(testkit.Rows("0")) + // #10461 tk.MustExec("drop table if exists tx1") tk.MustExec("create table tx1(id int key, a double, b double, c double, d double)") diff --git a/parser/ast/functions.go b/parser/ast/functions.go index d33550fc67626..fdedf53b701cf 100644 --- a/parser/ast/functions.go +++ b/parser/ast/functions.go @@ -331,6 +331,7 @@ const ( JSONInsert = "json_insert" JSONReplace = "json_replace" JSONRemove = "json_remove" + JSONOverlaps = "json_overlaps" JSONContains = "json_contains" JSONMemberOf = "json_memberof" JSONContainsPath = "json_contains_path" diff --git a/types/json_binary_functions.go b/types/json_binary_functions.go index 2b02d5a0f65e7..84bd86445aa94 100644 --- a/types/json_binary_functions.go +++ b/types/json_binary_functions.go @@ -1106,6 +1106,48 @@ func ContainsBinaryJSON(obj, target BinaryJSON) bool { } } +// OverlapsBinaryJSON is similar with ContainsBinaryJSON, but it checks the `OR` relationship. +func OverlapsBinaryJSON(obj, target BinaryJSON) bool { + if obj.TypeCode != JSONTypeCodeArray && target.TypeCode == JSONTypeCodeArray { + obj, target = target, obj + } + switch obj.TypeCode { + case JSONTypeCodeObject: + if target.TypeCode == JSONTypeCodeObject { + elemCount := target.GetElemCount() + for i := 0; i < elemCount; i++ { + key := target.objectGetKey(i) + val := target.objectGetVal(i) + if exp, exists := obj.objectSearchKey(key); exists && CompareBinaryJSON(exp, val) == 0 { + return true + } + } + } + return false + case JSONTypeCodeArray: + if target.TypeCode == JSONTypeCodeArray { + for i := 0; i < obj.GetElemCount(); i++ { + o := obj.arrayGetElem(i) + for j := 0; j < target.GetElemCount(); j++ { + if CompareBinaryJSON(o, target.arrayGetElem(j)) == 0 { + return true + } + } + } + return false + } + elemCount := obj.GetElemCount() + for i := 0; i < elemCount; i++ { + if CompareBinaryJSON(obj.arrayGetElem(i), target) == 0 { + return true + } + } + return false + default: + return CompareBinaryJSON(obj, target) == 0 + } +} + // GetElemDepth for JSON_DEPTH // Returns the maximum depth of a JSON document // rules referenced by MySQL JSON_DEPTH function From 1e7c552944ec9233ba0a70680cfee29bfa98e1ef Mon Sep 17 00:00:00 2001 From: Zhou Kunqin <25057648+time-and-fate@users.noreply.github.com> Date: Wed, 14 Dec 2022 13:36:51 +0800 Subject: [PATCH 3/3] executor, planner: `InitLogger` before `CreateMockStore` in tests to avoid data race (#39900) close pingcap/tidb#39892 --- executor/slow_query_sql_test.go | 34 +++++++++++----------- planner/core/binary_plan_test.go | 48 ++++++++++++++------------------ 2 files changed, 39 insertions(+), 43 deletions(-) diff --git a/executor/slow_query_sql_test.go b/executor/slow_query_sql_test.go index a77e32e3bfb16..f7029454023fa 100644 --- a/executor/slow_query_sql_test.go +++ b/executor/slow_query_sql_test.go @@ -46,8 +46,6 @@ func TestSlowQueryWithoutSlowLog(t *testing.T) { } func TestSlowQuerySensitiveQuery(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) originCfg := config.GetGlobalConfig() newCfg := *originCfg @@ -57,11 +55,15 @@ func TestSlowQuerySensitiveQuery(t *testing.T) { newCfg.Log.SlowQueryFile = f.Name() config.StoreGlobalConfig(&newCfg) defer func() { - tk.MustExec("set tidb_slow_log_threshold=300;") config.StoreGlobalConfig(originCfg) require.NoError(t, os.Remove(newCfg.Log.SlowQueryFile)) }() require.NoError(t, logutil.InitLogger(newCfg.Log.ToLogConfig())) + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + defer func() { + tk.MustExec("set tidb_slow_log_threshold=300;") + }() tk.MustExec(fmt.Sprintf("set @@tidb_slow_query_file='%v'", f.Name())) tk.MustExec("set tidb_slow_log_threshold=0;") @@ -80,8 +82,6 @@ func TestSlowQuerySensitiveQuery(t *testing.T) { } func TestSlowQueryPrepared(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) originCfg := config.GetGlobalConfig() newCfg := *originCfg @@ -91,12 +91,16 @@ func TestSlowQueryPrepared(t *testing.T) { newCfg.Log.SlowQueryFile = f.Name() config.StoreGlobalConfig(&newCfg) defer func() { - tk.MustExec("set tidb_slow_log_threshold=300;") - tk.MustExec("set tidb_redact_log=0;") config.StoreGlobalConfig(originCfg) require.NoError(t, os.Remove(newCfg.Log.SlowQueryFile)) }() require.NoError(t, logutil.InitLogger(newCfg.Log.ToLogConfig())) + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + defer func() { + tk.MustExec("set tidb_slow_log_threshold=300;") + tk.MustExec("set tidb_redact_log=0;") + }() tk.MustExec(fmt.Sprintf("set @@tidb_slow_query_file='%v'", f.Name())) tk.MustExec("set tidb_slow_log_threshold=0;") @@ -116,8 +120,6 @@ func TestSlowQueryPrepared(t *testing.T) { } func TestLogSlowLogIndex(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) f, err := os.CreateTemp("", "tidb-slow-*.log") require.NoError(t, err) require.NoError(t, f.Close()) @@ -127,6 +129,8 @@ func TestLogSlowLogIndex(t *testing.T) { conf.Log.SlowQueryFile = f.Name() }) require.NoError(t, logutil.InitLogger(config.GetGlobalConfig().Log.ToLogConfig())) + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) tk.MustExec(fmt.Sprintf("set @@tidb_slow_query_file='%v'", f.Name())) tk.MustExec("use test") @@ -140,9 +144,6 @@ func TestLogSlowLogIndex(t *testing.T) { } func TestSlowQuery(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - f, err := os.CreateTemp("", "tidb-slow-*.log") require.NoError(t, err) _, err = f.WriteString(` @@ -197,6 +198,8 @@ SELECT original_sql, bind_sql, default_db, status, create_time, update_time, cha require.NoError(t, os.Remove(newCfg.Log.SlowQueryFile)) }() require.NoError(t, logutil.InitLogger(newCfg.Log.ToLogConfig())) + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) tk.MustExec(fmt.Sprintf("set @@tidb_slow_query_file='%v'", f.Name())) tk.MustQuery("select count(*) from `information_schema`.`slow_query` where time > '2020-10-16 20:08:13' and time < '2020-10-16 21:08:13'").Check(testkit.Rows("1")) @@ -208,10 +211,6 @@ SELECT original_sql, bind_sql, default_db, status, create_time, update_time, cha } func TestIssue37066(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil)) - originCfg := config.GetGlobalConfig() newCfg := *originCfg f, err := os.CreateTemp("", "tidb-slow-*.log") @@ -224,6 +223,9 @@ func TestIssue37066(t *testing.T) { require.NoError(t, os.Remove(newCfg.Log.SlowQueryFile)) }() require.NoError(t, logutil.InitLogger(newCfg.Log.ToLogConfig())) + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil)) tk.MustExec(fmt.Sprintf("set @@tidb_slow_query_file='%v'", f.Name())) tk.MustExec("set tidb_slow_log_threshold=0;") defer func() { diff --git a/planner/core/binary_plan_test.go b/planner/core/binary_plan_test.go index be8bf75378dc7..95c67990b09be 100644 --- a/planner/core/binary_plan_test.go +++ b/planner/core/binary_plan_test.go @@ -77,13 +77,6 @@ func simplifyAndCheckBinaryOperator(t *testing.T, pb *tipb.ExplainOperator, with } func TestBinaryPlanInExplainAndSlowLog(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("set tidb_cost_model_version=2") - // If we don't set this, it will be false sometimes and the cost in the result will be different. - tk.MustExec("set @@tidb_enable_chunk_rpc=true") - // Prepare the slow log originCfg := config.GetGlobalConfig() newCfg := *originCfg @@ -97,6 +90,12 @@ func TestBinaryPlanInExplainAndSlowLog(t *testing.T) { require.NoError(t, os.Remove(newCfg.Log.SlowQueryFile)) }() require.NoError(t, logutil.InitLogger(newCfg.Log.ToLogConfig())) + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("set tidb_cost_model_version=2") + // If we don't set this, it will be false sometimes and the cost in the result will be different. + tk.MustExec("set @@tidb_enable_chunk_rpc=true") tk.MustExec(fmt.Sprintf("set @@tidb_slow_query_file='%v'", f.Name())) tk.MustExec("set tidb_slow_log_threshold=0;") defer func() { @@ -149,10 +148,6 @@ func TestBinaryPlanInExplainAndSlowLog(t *testing.T) { } func TestBinaryPlanSwitch(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil)) - originCfg := config.GetGlobalConfig() newCfg := *originCfg f, err := os.CreateTemp("", "tidb-slow-*.log") @@ -165,6 +160,9 @@ func TestBinaryPlanSwitch(t *testing.T) { require.NoError(t, os.Remove(newCfg.Log.SlowQueryFile)) }() require.NoError(t, logutil.InitLogger(newCfg.Log.ToLogConfig())) + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil)) tk.MustExec(fmt.Sprintf("set @@tidb_slow_query_file='%v'", f.Name())) tk.MustExec("use test") @@ -219,10 +217,6 @@ func TestBinaryPlanSwitch(t *testing.T) { // TestTooLongBinaryPlan asserts that if the binary plan is larger than 1024*1024 bytes, it should be output to slow query but not to stmt summary. func TestTooLongBinaryPlan(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil)) - originCfg := config.GetGlobalConfig() newCfg := *originCfg f, err := os.CreateTemp("", "tidb-slow-*.log") @@ -235,6 +229,9 @@ func TestTooLongBinaryPlan(t *testing.T) { require.NoError(t, os.Remove(newCfg.Log.SlowQueryFile)) }() require.NoError(t, logutil.InitLogger(newCfg.Log.ToLogConfig())) + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil)) tk.MustExec(fmt.Sprintf("set @@tidb_slow_query_file='%v'", f.Name())) tk.MustExec("use test") @@ -336,10 +333,6 @@ func TestLongBinaryPlan(t *testing.T) { } func TestBinaryPlanOfPreparedStmt(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil)) - originCfg := config.GetGlobalConfig() newCfg := *originCfg f, err := os.CreateTemp("", "tidb-slow-*.log") @@ -352,6 +345,9 @@ func TestBinaryPlanOfPreparedStmt(t *testing.T) { require.NoError(t, os.Remove(newCfg.Log.SlowQueryFile)) }() require.NoError(t, logutil.InitLogger(newCfg.Log.ToLogConfig())) + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil)) tk.MustExec(fmt.Sprintf("set @@tidb_slow_query_file='%v'", f.Name())) tk.MustExec("use test") @@ -389,10 +385,6 @@ func TestBinaryPlanOfPreparedStmt(t *testing.T) { // TestDecodeBinaryPlan asserts that the result of EXPLAIN ANALYZE FORMAT = 'verbose' is the same as tidb_decode_binary_plan(). func TestDecodeBinaryPlan(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - // Prepare the slow log originCfg := config.GetGlobalConfig() newCfg := *originCfg @@ -406,6 +398,9 @@ func TestDecodeBinaryPlan(t *testing.T) { require.NoError(t, os.Remove(newCfg.Log.SlowQueryFile)) }() require.NoError(t, logutil.InitLogger(newCfg.Log.ToLogConfig())) + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") tk.MustExec(fmt.Sprintf("set @@tidb_slow_query_file='%v'", f.Name())) tk.MustExec("set tidb_slow_log_threshold=0;") defer func() { @@ -499,10 +494,6 @@ func TestInvalidDecodeBinaryPlan(t *testing.T) { } func TestUnnecessaryBinaryPlanInSlowLog(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil)) - originCfg := config.GetGlobalConfig() newCfg := *originCfg f, err := os.CreateTemp("", "tidb-slow-*.log") @@ -515,6 +506,9 @@ func TestUnnecessaryBinaryPlanInSlowLog(t *testing.T) { require.NoError(t, os.Remove(newCfg.Log.SlowQueryFile)) }() require.NoError(t, logutil.InitLogger(newCfg.Log.ToLogConfig())) + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil)) tk.MustExec(fmt.Sprintf("set @@tidb_slow_query_file='%v'", f.Name())) tk.MustExec("use test")