Skip to content

Commit

Permalink
statistics: add a test case which builds global-stats on different ve…
Browse files Browse the repository at this point in the history
…rsions of partition-stats (#23231)
  • Loading branch information
qw4990 authored Mar 11, 2021
1 parent d129737 commit c1b8347
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion errno/errcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ const (
ErrInvalidTableSample = 8128
ErrJSONObjectKeyTooLong = 8129
ErrMultiStatementDisabled = 8130
ErrBuildGlobalLevelStatsFailed = 8131
ErrPartitionStatsMissing = 8131

// Error codes used by TiDB ddl package
ErrUnsupportedDDLOperation = 8200
Expand Down
4 changes: 2 additions & 2 deletions errno/errname.go
Original file line number Diff line number Diff line change
Expand Up @@ -1032,8 +1032,8 @@ var MySQLErrName = map[uint16]*mysql.ErrMessage{

ErrInvalidTableSample: mysql.Message("Invalid TABLESAMPLE: %s", nil),

ErrJSONObjectKeyTooLong: mysql.Message("TiDB does not yet support JSON objects with the key length >= 65536", nil),
ErrBuildGlobalLevelStatsFailed: mysql.Message("Build table: %s global-level stats failed due to missing partition-level stats", nil),
ErrJSONObjectKeyTooLong: mysql.Message("TiDB does not yet support JSON objects with the key length >= 65536", nil),
ErrPartitionStatsMissing: mysql.Message("Build table: %s global-level stats failed due to missing partition-level stats", nil),

ErrInvalidPlacementSpec: mysql.Message("Invalid placement policy '%s': %s", nil),
ErrPlacementPolicyCheck: mysql.Message("Placement policy didn't meet the constraint, reason: %s", nil),
Expand Down
2 changes: 1 addition & 1 deletion executor/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (e *AnalyzeExec) Next(ctx context.Context, req *chunk.Chunk) error {
for globalStatsID, info := range globalStatsMap {
globalStats, err := statsHandle.MergePartitionStats2GlobalStats(e.ctx, e.opts, infoschema.GetInfoSchema(e.ctx), globalStatsID.tableID, info.isIndex, info.idxID)
if err != nil {
if types.ErrBuildGlobalLevelStatsFailed.Equal(err) {
if types.ErrPartitionStatsMissing.Equal(err) {
// When we find some partition-level stats are missing, we need to report warning.
e.ctx.GetSessionVars().StmtCtx.AppendWarning(err)
continue
Expand Down
5 changes: 2 additions & 3 deletions statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,12 @@ func (h *Handle) MergePartitionStats2GlobalStats(sc sessionctx.Context, opts map
}
errMsg = fmt.Sprintf("`%s` index: `%s`", tableInfo.Name.L, indexName)
}
err = types.ErrBuildGlobalLevelStatsFailed.GenWithStackByArgs(errMsg)
err = types.ErrPartitionStatsMissing.GenWithStackByArgs(errMsg)
return
}
statistics.CheckAnalyzeVerOnTable(partitionStats, &statsVer)
if statsVer != statistics.Version2 { // global-stats only support stats-ver2
return nil, fmt.Errorf("[stats]: global statistics for partitioned tables only available in statistics version2, please set tidb_analyze_version to 2")

return nil, fmt.Errorf("[stats]: some partition level statistics are not in statistics version 2, please set tidb_analyze_version to 2 and analyze the this table")
}
for i := 0; i < globalStats.Num; i++ {
ID := tableInfo.Columns[i].ID
Expand Down
26 changes: 19 additions & 7 deletions statistics/handle/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1326,18 +1326,30 @@ partition by range (a) (
c.Assert(s.do.StatsHandle().DumpStatsDeltaToKV(handle.DumpAll), IsNil)

tk.MustExec("set @@tidb_partition_prune_mode='static'")
tk.MustExec("analyze table t")
c.Assert(len(tk.MustQuery("show stats_meta").Rows()), Equals, 2) // p0 + p1
tk.MustExec("set @@tidb_analyze_version=1")
tk.MustExec("analyze table t") // both p0 and p1 are in ver1
c.Assert(len(tk.MustQuery("show stats_meta").Rows()), Equals, 2)

tk.MustExec("set @@tidb_partition_prune_mode='dynamic'")
tk.MustExec("set @@tidb_analyze_version=1")
err := tk.ExecToErr("analyze table t")
err := tk.ExecToErr("analyze table t") // try to build global-stats on ver1
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[stats]: global statistics for partitioned tables only available in statistics version2, please set tidb_analyze_version to 2")
c.Assert(err.Error(), Equals, "[stats]: some partition level statistics are not in statistics version 2, please set tidb_analyze_version to 2 and analyze the this table")

tk.MustExec("set @@tidb_partition_prune_mode='dynamic'")
tk.MustExec("set @@tidb_analyze_version=2")
tk.MustExec("analyze table t")
c.Assert(len(tk.MustQuery("show stats_meta").Rows()), Equals, 3) // p0 + p1 + global
err = tk.ExecToErr("analyze table t partition p1") // only analyze p1 to let it in ver2 while p0 is in ver1
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[stats]: some partition level statistics are not in statistics version 2, please set tidb_analyze_version to 2 and analyze the this table")

tk.MustExec("analyze table t") // both p0 and p1 are in ver2
c.Assert(len(tk.MustQuery("show stats_meta").Rows()), Equals, 3)

tk.MustExec("alter table t add partition (partition p2 values less than (30))")
tk.MustExec("insert t values (13), (14)")
c.Assert(s.do.StatsHandle().DumpStatsDeltaToKV(handle.DumpAll), IsNil)
tk.MustExec("analyze table t partition p2") // it will success since p0 and p1 are both in ver2
c.Assert(len(tk.MustQuery("show stats_meta").Rows()), Equals, 4) // p0, p1, p2 and global
}

func (s *testStatsSuite) TestExtendedStatsDefaultSwitch(c *C) {
Expand Down Expand Up @@ -1585,7 +1597,7 @@ func (s *testStatsSuite) TestPartitionPruneModeSessionVariable(c *C) {
tk2.MustExec("set @@tidb_partition_prune_mode = '" + string(variable.Static) + "'")
tk2.MustExec(`set @@tidb_analyze_version=2`)

tk1.MustExec(`create table t (a int, key(a)) partition by range(a)
tk1.MustExec(`create table t (a int, key(a)) partition by range(a)
(partition p0 values less than (10),
partition p1 values less than (22))`)

Expand Down
4 changes: 2 additions & 2 deletions types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ var (
ErrWrongValue = dbterror.ClassTypes.NewStdErr(mysql.ErrTruncatedWrongValue, mysql.MySQLErrName[mysql.ErrWrongValue])
// ErrWrongValueForType is returned when the input value is in wrong format for function.
ErrWrongValueForType = dbterror.ClassTypes.NewStdErr(mysql.ErrWrongValueForType, mysql.MySQLErrName[mysql.ErrWrongValueForType])
// ErrBuildGlobalLevelStatsFailed is returned when the partition-level stats is missing and the build global-level stats fails.
// ErrPartitionStatsMissing is returned when the partition-level stats is missing and the build global-level stats fails.
// Put this error here is to prevent `import cycle not allowed`.
ErrBuildGlobalLevelStatsFailed = dbterror.ClassTypes.NewStd(mysql.ErrBuildGlobalLevelStatsFailed)
ErrPartitionStatsMissing = dbterror.ClassTypes.NewStd(mysql.ErrPartitionStatsMissing)
)

0 comments on commit c1b8347

Please sign in to comment.