Skip to content

Commit

Permalink
statistics: fix wrong behavior for primary key' non-lite init stats (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Aug 29, 2024
1 parent d140d7b commit 020802f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
7 changes: 2 additions & 5 deletions statistics/handle/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,8 @@ func (h *Handle) InitStats(is infoschema.InfoSchema) (err error) {
for _, table := range cache.Values() {
for _, col := range table.Columns {
if col.StatsAvailable() {
if mysql.HasPriKeyFlag(col.Info.GetFlag()) {
col.StatsLoadedStatus = statistics.NewStatsFullLoadStatus()
} else {
col.StatsLoadedStatus = statistics.NewStatsAllEvictedStatus()
}
// primary key column has no stats info, because primary key's is_index is false. so it cannot load the topn
col.StatsLoadedStatus = statistics.NewStatsAllEvictedStatus()
}
}
}
Expand Down
1 change: 1 addition & 0 deletions statistics/handle/handletest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_test(
"//config",
"//domain",
"//parser/model",
"//parser/mysql",
"//session",
"//sessionctx/variable",
"//statistics",
Expand Down
34 changes: 34 additions & 0 deletions statistics/handle/handletest/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/statistics"
Expand Down Expand Up @@ -457,6 +458,39 @@ func TestInitStats(t *testing.T) {
h.SetLease(0)
}

func TestInitStats51358(t *testing.T) {
originValue := config.GetGlobalConfig().Performance.LiteInitStats
defer func() {
config.GetGlobalConfig().Performance.LiteInitStats = originValue
}()
config.GetGlobalConfig().Performance.LiteInitStats = false
store, dom := testkit.CreateMockStoreAndDomain(t)
testKit := testkit.NewTestKit(t, store)
testKit.MustExec("use test")
testKit.MustExec("set @@session.tidb_analyze_version = 1")
testKit.MustExec("create table t(a int, b int, c int, primary key(a), key idx(b))")
testKit.MustExec("insert into t values (1,1,1),(2,2,2),(3,3,3),(4,4,4),(5,5,5),(6,7,8)")
testKit.MustExec("analyze table t")
h := dom.StatsHandle()
is := dom.InfoSchema()
// `Update` will not use load by need strategy when `Lease` is 0, and `InitStats` is only called when
// `Lease` is not 0, so here we just change it.
h.SetLease(time.Millisecond)

h.Clear()
require.NoError(t, h.InitStats(is))
tbl, err := dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
require.NoError(t, err)
stats := h.GetTableStats(tbl.Meta())
for _, column := range stats.Columns {
if mysql.HasPriKeyFlag(column.Info.GetFlag()) {
// primary key column has no stats info, because primary key's is_index is false. so it cannot load the topn
require.Nil(t, column.TopN)
}
require.False(t, column.IsFullLoad())
}
}

func TestInitStatsVer2(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
Expand Down

0 comments on commit 020802f

Please sign in to comment.