Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: stabilize tests in binary_plan_test.go #41356

Merged
merged 2 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion planner/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ go_library(

go_test(
name = "core_test",
timeout = "moderate",
timeout = "short",
srcs = [
"binary_plan_test.go",
"cbo_test.go",
Expand Down
28 changes: 24 additions & 4 deletions planner/core/binary_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ func TestBinaryPlanSwitch(t *testing.T) {
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()))

origin := tk.MustQuery("SELECT @@global.tidb_generate_binary_plan")
originStr := origin.Rows()[0][0].(string)
defer func() {
tk.MustExec("set @@global.tidb_generate_binary_plan = '" + originStr + "'")
}()

tk.MustExec("use test")
// 1. assert binary plan is generated if the variable is turned on
tk.MustExec("set global tidb_generate_binary_plan = 1")
Expand Down Expand Up @@ -234,6 +240,16 @@ func TestTooLongBinaryPlan(t *testing.T) {
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()))

origin := tk.MustQuery("SELECT @@global.tidb_enable_stmt_summary")
originStr := origin.Rows()[0][0].(string)
defer func() {
tk.MustExec("set @@global.tidb_enable_stmt_summary = '" + originStr + "'")
}()

// Trigger clear the stmt summary in memory to prevent this case from being affected by other cases.
tk.MustExec("set global tidb_enable_stmt_summary = 0")
tk.MustExec("set global tidb_enable_stmt_summary = 1")

tk.MustExec("use test")
tk.MustExec("drop table if exists th")
tk.MustExec("set @@session.tidb_enable_table_partition = 1")
Expand Down Expand Up @@ -276,7 +292,6 @@ func TestTooLongBinaryPlan(t *testing.T) {
}

// TestLongBinaryPlan asserts that if the binary plan is smaller than 1024*1024 bytes, it should be output to both slow query and stmt summary.
// The size of the binary plan in this test case is designed to be larger than 1024*1024*0.85 bytes but smaller than 1024*1024 bytes.
func TestLongBinaryPlan(t *testing.T) {
originCfg := config.GetGlobalConfig()
newCfg := *originCfg
Expand All @@ -301,18 +316,17 @@ func TestLongBinaryPlan(t *testing.T) {
tk.MustExec("drop table if exists th")
tk.MustExec("set @@session.tidb_enable_table_partition = 1")
tk.MustExec(`set @@tidb_partition_prune_mode='` + string(variable.Static) + `'`)
tk.MustExec("create table th (i int, a int,b int, c int, index (a)) partition by hash (a) partitions 1700;")
tk.MustExec("create table th (i int, a int,b int, c int, index (a)) partition by hash (a) partitions 1000;")
tk.MustQuery("select count(*) from th t1 join th t2 join th t3 join th t4 join th t5 join th t6 where t1.i=t2.a and t1.i=t3.i and t3.i=t4.i and t4.i=t5.i and t5.i=t6.i")

result := testdata.ConvertRowsToStrings(tk.MustQuery("select binary_plan from information_schema.slow_query " +
`where query like "%th t1 join th t2 join th t3%" and query not like "%like%" ` +
"limit 1;").Rows())
require.Len(t, result, 1)
s1 := result[0]
// The binary plan in this test case is expected to be slightly smaller than MaxEncodedPlanSizeInBytes.
// The binary plan in this test case is expected to be smaller than MaxEncodedPlanSizeInBytes.
// If the size of the binary plan changed and this case failed in the future, you can adjust the partition numbers in the CREATE TABLE statement above.
require.Less(t, len(s1), stmtsummary.MaxEncodedPlanSizeInBytes)
require.Greater(t, len(s1), int(float64(stmtsummary.MaxEncodedPlanSizeInBytes)*0.85))
b, err := base64.StdEncoding.DecodeString(s1)
require.NoError(t, err)
b, err = snappy.Decode(nil, b)
Expand Down Expand Up @@ -511,6 +525,12 @@ func TestUnnecessaryBinaryPlanInSlowLog(t *testing.T) {
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()))

origin := tk.MustQuery("SELECT @@global.tidb_slow_log_threshold")
originStr := origin.Rows()[0][0].(string)
defer func() {
tk.MustExec("set @@global.tidb_slow_log_threshold = '" + originStr + "'")
}()

tk.MustExec("use test")
tk.MustExec("drop table if exists th")
tk.MustExec("set global tidb_slow_log_threshold = 1;")
Expand Down