Skip to content

Commit

Permalink
statistics: fix wrong behavior for primary key' non-lite init stats (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkingrei authored May 15, 2024
1 parent b9b330a commit 687f39c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
7 changes: 2 additions & 5 deletions pkg/statistics/handle/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,11 +751,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 pkg/statistics/handle/handletest/statstest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_test(
deps = [
"//pkg/config",
"//pkg/parser/model",
"//pkg/parser/mysql",
"//pkg/statistics/handle/internal",
"//pkg/testkit",
"//pkg/testkit/testsetup",
Expand Down
11 changes: 11 additions & 0 deletions pkg/statistics/handle/handletest/statstest/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/pkg/config"
"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/pingcap/tidb/pkg/statistics/handle/internal"
"github.com/pingcap/tidb/pkg/testkit"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -314,6 +315,16 @@ func TestInitStats51358(t *testing.T) {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/statistics/handle/cache/StatsCacheGetNil"))
}()
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) {
Expand Down

0 comments on commit 687f39c

Please sign in to comment.