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: try to fix some unstable test cases about partition table statistics #25026

Merged
merged 26 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
484f9ad
try to fix some unstable tests
qw4990 Jun 1, 2021
bb3e397
make linter happy
qw4990 Jun 1, 2021
88a64fc
Merge branch 'master' into gstats-ci
qw4990 Jun 1, 2021
10d89de
Merge branch 'master' into gstats-ci
qw4990 Jun 2, 2021
b4582fa
Merge branch 'master' into gstats-ci
ti-chi-bot Jun 2, 2021
71e4b23
Merge branch 'master' into gstats-ci
qw4990 Jun 2, 2021
a99bb3a
Merge branch 'master' into gstats-ci
qw4990 Jun 2, 2021
0bf4b90
Merge branch 'master' into gstats-ci
ti-chi-bot Jun 2, 2021
0c5d4f2
Merge branch 'master' into gstats-ci
qw4990 Jun 2, 2021
78c96ab
Merge branch 'master' into gstats-ci
qw4990 Jun 2, 2021
04d5e01
Merge branch 'master' into gstats-ci
qw4990 Jun 2, 2021
c0d9ffc
Merge branch 'master' into gstats-ci
ti-chi-bot Jun 2, 2021
b91d355
Merge branch 'master' into gstats-ci
qw4990 Jun 2, 2021
be94c3c
Merge branch 'master' into gstats-ci
ti-chi-bot Jun 2, 2021
d216570
Merge branch 'master' into gstats-ci
ti-chi-bot Jun 2, 2021
87fd5a7
Merge branch 'master' into gstats-ci
qw4990 Jun 2, 2021
07a9bcd
Merge branch 'master' into gstats-ci
ti-chi-bot Jun 2, 2021
445d2c5
Merge branch 'master' into gstats-ci
ti-chi-bot Jun 2, 2021
7846a3b
skip them under RACE TEST
qw4990 Jun 3, 2021
e72c7fb
fixup
qw4990 Jun 3, 2021
b440886
Merge remote-tracking branch 'upstream/master' into gstats-ci
qw4990 Jun 3, 2021
80a2664
refmt
qw4990 Jun 3, 2021
0ef2858
Merge branch 'master' into gstats-ci
ti-chi-bot Jun 3, 2021
53083f0
fixup
qw4990 Jun 3, 2021
2cd9fe9
Merge branch 'gstats-ci' of https://github.com/qw4990/tidb into gstat…
qw4990 Jun 3, 2021
779abe5
Merge branch 'master' into gstats-ci
qw4990 Jun 3, 2021
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
84 changes: 47 additions & 37 deletions executor/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/israce"
"github.com/pingcap/tidb/util/testkit"
)

Expand Down Expand Up @@ -237,7 +238,9 @@ func (s *testSuite1) TestAnalyzeTooLongColumns(c *C) {
}

func (s *testSuite1) TestAnalyzeIndexExtractTopN(c *C) {
c.Skip("unstable")
if israce.RaceEnabled {
c.Skip("unstable, skip race test")
}
store, err := mockstore.NewMockStore()
c.Assert(err, IsNil)
defer func() {
Expand All @@ -252,15 +255,16 @@ func (s *testSuite1) TestAnalyzeIndexExtractTopN(c *C) {
defer dom.Close()
tk := testkit.NewTestKit(c, store)

tk.MustExec("use test")
tk.MustExec("create database test_index_extract_topn")
tk.MustExec("use test_index_extract_topn")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int, index idx(a, b))")
tk.MustExec("insert into t values(1, 1), (1, 1), (1, 2), (1, 2)")
tk.MustExec("set @@session.tidb_analyze_version=2")
tk.MustExec("analyze table t with 10 cmsketch width")

is := tk.Se.(sessionctx.Context).GetInfoSchema().(infoschema.InfoSchema)
table, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
table, err := is.TableByName(model.NewCIStr("test_index_extract_topn"), model.NewCIStr("t"))
c.Assert(err, IsNil)
tableInfo := table.Meta()
tbl := dom.StatsHandle().GetTableStats(tableInfo)
Expand Down Expand Up @@ -511,18 +515,21 @@ func (s *testFastAnalyze) TestFastAnalyze(c *C) {
}

func (s *testSerialSuite2) TestFastAnalyze4GlobalStats(c *C) {
c.Skip("unstable")
if israce.RaceEnabled {
c.Skip("unstable, skip race test")
}
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec(`create database if not exists test_fast_gstats`)
tk.MustExec("use test_fast_gstats")
tk.MustExec("set @@session.tidb_enable_fast_analyze=1")
tk.MustExec("set @@session.tidb_build_stats_concurrency=1")
// test fast analyze in dynamic mode
tk.MustExec("set @@session.tidb_analyze_version = 2;")
tk.MustExec("set @@session.tidb_partition_prune_mode = 'dynamic';")
tk.MustExec("drop table if exists t4;")
tk.MustExec("create table t4(a int, b int) PARTITION BY HASH(a) PARTITIONS 2;")
tk.MustExec("insert into t4 values(1,1),(3,3),(4,4),(2,2),(5,5);")
err := tk.ExecToErr("analyze table t4;")
tk.MustExec("drop table if exists test_fast_gstats;")
tk.MustExec("create table test_fast_gstats(a int, b int) PARTITION BY HASH(a) PARTITIONS 2;")
tk.MustExec("insert into test_fast_gstats values(1,1),(3,3),(4,4),(2,2),(5,5);")
err := tk.ExecToErr("analyze table test_fast_gstats;")
c.Assert(err.Error(), Equals, "Fast analyze hasn't reached General Availability and only support analyze version 1 currently.")
}

Expand Down Expand Up @@ -747,21 +754,24 @@ func (s *testSuite10) TestFailedAnalyzeRequest(c *C) {
}

func (s *testSuite1) TestExtractTopN(c *C) {
c.Skip("unstable")
if israce.RaceEnabled {
c.Skip("unstable, skip race test")
}
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int primary key, b int, index index_b(b))")
tk.MustExec("create database if not exists test_extract_topn")
tk.MustExec("use test_extract_topn")
tk.MustExec("drop table if exists test_extract_topn")
tk.MustExec("create table test_extract_topn(a int primary key, b int, index index_b(b))")
tk.MustExec("set @@session.tidb_analyze_version=2")
for i := 0; i < 10; i++ {
tk.MustExec(fmt.Sprintf("insert into t values (%d, %d)", i, i))
tk.MustExec(fmt.Sprintf("insert into test_extract_topn values (%d, %d)", i, i))
}
for i := 0; i < 10; i++ {
tk.MustExec(fmt.Sprintf("insert into t values (%d, 0)", i+10))
tk.MustExec(fmt.Sprintf("insert into test_extract_topn values (%d, 0)", i+10))
}
tk.MustExec("analyze table t")
tk.MustExec("analyze table test_extract_topn")
is := s.dom.InfoSchema()
table, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
table, err := is.TableByName(model.NewCIStr("test_extract_topn"), model.NewCIStr("test_extract_topn"))
c.Assert(err, IsNil)
tblInfo := table.Meta()
tblStats := s.dom.StatsHandle().GetTableStats(tblInfo)
Expand All @@ -774,26 +784,26 @@ func (s *testSuite1) TestExtractTopN(c *C) {
idxItem := idxStats.TopN.TopN[0]
c.Assert(idxItem.Count, Equals, uint64(11))
// The columns are: DBName, table name, column name, is index, value, count.
tk.MustQuery("show stats_topn").Sort().Check(testkit.Rows("test t b 0 0 11",
"test t b 0 1 1",
"test t b 0 2 1",
"test t b 0 3 1",
"test t b 0 4 1",
"test t b 0 5 1",
"test t b 0 6 1",
"test t b 0 7 1",
"test t b 0 8 1",
"test t b 0 9 1",
"test t index_b 1 0 11",
"test t index_b 1 1 1",
"test t index_b 1 2 1",
"test t index_b 1 3 1",
"test t index_b 1 4 1",
"test t index_b 1 5 1",
"test t index_b 1 6 1",
"test t index_b 1 7 1",
"test t index_b 1 8 1",
"test t index_b 1 9 1",
tk.MustQuery("show stats_topn").Sort().Check(testkit.Rows("test_extract_topn test_extract_topn b 0 0 11",
"test_extract_topn test_extract_topn b 0 1 1",
"test_extract_topn test_extract_topn b 0 2 1",
"test_extract_topn test_extract_topn b 0 3 1",
"test_extract_topn test_extract_topn b 0 4 1",
"test_extract_topn test_extract_topn b 0 5 1",
"test_extract_topn test_extract_topn b 0 6 1",
"test_extract_topn test_extract_topn b 0 7 1",
"test_extract_topn test_extract_topn b 0 8 1",
"test_extract_topn test_extract_topn b 0 9 1",
"test_extract_topn test_extract_topn index_b 1 0 11",
"test_extract_topn test_extract_topn index_b 1 1 1",
"test_extract_topn test_extract_topn index_b 1 2 1",
"test_extract_topn test_extract_topn index_b 1 3 1",
"test_extract_topn test_extract_topn index_b 1 4 1",
"test_extract_topn test_extract_topn index_b 1 5 1",
"test_extract_topn test_extract_topn index_b 1 6 1",
"test_extract_topn test_extract_topn index_b 1 7 1",
"test_extract_topn test_extract_topn index_b 1 8 1",
"test_extract_topn test_extract_topn index_b 1 9 1",
))
}

Expand Down
28 changes: 16 additions & 12 deletions executor/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/pingcap/tidb/statistics/handle"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/israce"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testutil"
)
Expand Down Expand Up @@ -604,12 +605,15 @@ func (s *testFlushSuite) TestFlushPrivilegesPanic(c *C) {
}

func (s *testSerialSuite) TestDropPartitionStats(c *C) {
c.Skip("unstable")
if israce.RaceEnabled {
c.Skip("unstable, skip race test")
}
// Use the testSerialSuite to fix the unstable test
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec(`create table t (
tk.MustExec(`create database if not exists test_drop_gstats`)
tk.MustExec("use test_drop_gstats")
tk.MustExec("drop table if exists test_drop_gstats;")
tk.MustExec(`create table test_drop_gstats (
a int,
key(a)
)
Expand All @@ -620,7 +624,7 @@ partition by range (a) (
)`)
tk.MustExec("set @@tidb_analyze_version = 2")
tk.MustExec("set @@tidb_partition_prune_mode='dynamic'")
tk.MustExec("insert into t values (1), (5), (11), (15), (21), (25)")
tk.MustExec("insert into test_drop_gstats values (1), (5), (11), (15), (21), (25)")
c.Assert(s.domain.StatsHandle().DumpStatsDeltaToKV(handle.DumpAll), IsNil)

checkPartitionStats := func(names ...string) {
Expand All @@ -631,26 +635,26 @@ partition by range (a) (
}
}

tk.MustExec("analyze table t")
tk.MustExec("analyze table test_drop_gstats")
checkPartitionStats("global", "p0", "p1", "global")

tk.MustExec("drop stats t partition p0")
tk.MustExec("drop stats test_drop_gstats partition p0")
checkPartitionStats("global", "p1", "global")

err := tk.ExecToErr("drop stats t partition abcde")
err := tk.ExecToErr("drop stats test_drop_gstats partition abcde")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "can not found the specified partition name abcde in the table definition")

tk.MustExec("drop stats t partition global")
tk.MustExec("drop stats test_drop_gstats partition global")
checkPartitionStats("global", "p1")

tk.MustExec("drop stats t global")
tk.MustExec("drop stats test_drop_gstats global")
checkPartitionStats("p1")

tk.MustExec("analyze table t")
tk.MustExec("analyze table test_drop_gstats")
checkPartitionStats("global", "p0", "p1", "global")

tk.MustExec("drop stats t partition p0, p1, global")
tk.MustExec("drop stats test_drop_gstats partition p0, p1, global")
checkPartitionStats("global")
}

Expand Down
30 changes: 19 additions & 11 deletions statistics/handle/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/pingcap/tidb/store/tikv/oracle"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/israce"
"github.com/pingcap/tidb/util/testkit"
)

Expand Down Expand Up @@ -837,13 +838,14 @@ func (s *testStatsSuite) TestBuildGlobalLevelStats(c *C) {
c.Assert(len(result.Rows()), Equals, 20)
}

func (s *testStatsSuite) prepareForGlobalStatsWithOpts(c *C, tk *testkit.TestKit) {
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec(` create table t (a int, key(a)) partition by range (a) ` +
func (s *testStatsSuite) prepareForGlobalStatsWithOpts(c *C, tk *testkit.TestKit, tblName, dbName string) {
tk.MustExec("create database if not exists " + dbName)
tk.MustExec("use " + dbName)
tk.MustExec("drop table if exists " + tblName)
tk.MustExec(` create table ` + tblName + ` (a int, key(a)) partition by range (a) ` +
`(partition p0 values less than (100000), partition p1 values less than (200000))`)
buf1 := bytes.NewBufferString("insert into t values (0)")
buf2 := bytes.NewBufferString("insert into t values (100000)")
buf1 := bytes.NewBufferString("insert into " + tblName + " values (0)")
buf2 := bytes.NewBufferString("insert into " + tblName + " values (100000)")
for i := 0; i < 5000; i += 3 {
buf1.WriteString(fmt.Sprintf(", (%v)", i))
buf2.WriteString(fmt.Sprintf(", (%v)", 100000+i))
Expand Down Expand Up @@ -872,9 +874,12 @@ func (s *testStatsSuite) checkForGlobalStatsWithOpts(c *C, tk *testkit.TestKit,
}

func (s *testStatsSuite) TestAnalyzeGlobalStatsWithOpts(c *C) {
if israce.RaceEnabled {
c.Skip("unstable, skip race test")
}
defer cleanEnv(c, s.store, s.do)
tk := testkit.NewTestKit(c, s.store)
s.prepareForGlobalStatsWithOpts(c, tk)
s.prepareForGlobalStatsWithOpts(c, tk, "test_gstats_opt", "test_gstats_opt")

type opt struct {
topn int
Expand All @@ -893,7 +898,7 @@ func (s *testStatsSuite) TestAnalyzeGlobalStatsWithOpts(c *C) {
{77, 47000, true},
}
for _, ca := range cases {
sql := fmt.Sprintf("analyze table t with %v topn, %v buckets", ca.topn, ca.buckets)
sql := fmt.Sprintf("analyze table test_gstats_opt with %v topn, %v buckets", ca.topn, ca.buckets)
if !ca.err {
tk.MustExec(sql)
s.checkForGlobalStatsWithOpts(c, tk, "global", ca.topn, ca.buckets)
Expand All @@ -907,22 +912,25 @@ func (s *testStatsSuite) TestAnalyzeGlobalStatsWithOpts(c *C) {
}

func (s *testStatsSuite) TestAnalyzeGlobalStatsWithOpts2(c *C) {
if israce.RaceEnabled {
c.Skip("unstable, skip race test")
}
defer cleanEnv(c, s.store, s.do)
tk := testkit.NewTestKit(c, s.store)
s.prepareForGlobalStatsWithOpts(c, tk)
s.prepareForGlobalStatsWithOpts(c, tk, "test_gstats_opt2", "test_gstats_opt2")

tk.MustExec("analyze table t with 20 topn, 50 buckets, 1000 samples")
s.checkForGlobalStatsWithOpts(c, tk, "global", 2, 50)
s.checkForGlobalStatsWithOpts(c, tk, "p0", 1, 50)
s.checkForGlobalStatsWithOpts(c, tk, "p1", 1, 50)

// analyze a partition to let its options be different with others'
tk.MustExec("analyze table t partition p0 with 10 topn, 20 buckets")
tk.MustExec("analyze table test_gstats_opt2 partition p0 with 10 topn, 20 buckets")
s.checkForGlobalStatsWithOpts(c, tk, "global", 10, 20) // use new options
s.checkForGlobalStatsWithOpts(c, tk, "p0", 10, 20)
s.checkForGlobalStatsWithOpts(c, tk, "p1", 1, 50)

tk.MustExec("analyze table t partition p1 with 100 topn, 200 buckets")
tk.MustExec("analyze table test_gstats_opt2 partition p1 with 100 topn, 200 buckets")
s.checkForGlobalStatsWithOpts(c, tk, "global", 100, 200)
s.checkForGlobalStatsWithOpts(c, tk, "p0", 10, 20)
s.checkForGlobalStatsWithOpts(c, tk, "p1", 100, 200)
Expand Down