Investigate empty ColAndIdxExistenceMap in cache and impact on sync/async load/init stats
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
Please answer these questions before submitting your issue. Thanks!
### 1. Minimal reproduce step (Required)
Use the unit test below (from `pkg/statistics/handle/autoanalyze/refresher/refresher_test.go`, `TestAutoAnalyzeHandlesEmptyExistenceMapInCache`):
```go
func TestAutoAnalyzeHandlesEmptyExistenceMapInCache(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(a int, b int)")
tk.MustExec("insert into t values (1,1),(2,2)")
h := dom.StatsHandle()
// Make sure the count and modify count are updated.
require.NoError(t, h.DumpStatsDeltaToKV(true))
tbl, err := dom.InfoSchema().TableByName(context.Background(), ast.NewCIStr("test"), ast.NewCIStr("t"))
require.NoError(t, err)
tid := tbl.Meta().ID
// Ensure the stats object is created in memory and the existence map is initialized but empty (no cols/idxs).
// This happens because we haven't handled DDL events yet, so no rows exist in mysql.histograms.
require.NoError(t, h.Update(context.Background(), dom.InfoSchema()))
st, ok := h.Get(tid)
require.True(t, ok)
require.True(t, st.ColAndIdxExistenceMap.IsEmpty())
require.NotZero(t, st.ColAndIdxExistenceMap.ColNum())
}
```
### 2. What did you expect to see? (Required)
Sync load, async load, and init stats should safely handle an empty `ColAndIdxExistenceMap` by backfilling/loading correctly, without skipping columns/indexes or causing incorrect stats behavior.
### 3. What did you see instead (Required)
The existence map can be empty while a stats object exists in cache. It is unclear whether current sync/async load or init stats logic treats this as “no columns/indexes” and potentially skips loading or misbehaves. Needs investigation.
### 4. What is your TiDB version? (Required)
master branch, commit `00007d85784ab1702d95d09a03235d478f0c5261`
Contributor guide
Assessment
This issue has not been assessed yet.