diff --git a/statistics/handle/BUILD.bazel b/statistics/handle/BUILD.bazel index db9a1d64930ff..9cdb60688da47 100644 --- a/statistics/handle/BUILD.bazel +++ b/statistics/handle/BUILD.bazel @@ -71,7 +71,12 @@ go_test( ], embed = [":handle"], flaky = True, +<<<<<<< HEAD shard_count = 50, +======= + race = "on", + shard_count = 34, +>>>>>>> db68a1222e9 (staistics: fix load stats from old version json (#42967)) deps = [ "//config", "//domain", diff --git a/statistics/handle/dump.go b/statistics/handle/dump.go index 5216622dc9a35..aea89db264f92 100644 --- a/statistics/handle/dump.go +++ b/statistics/handle/dump.go @@ -276,11 +276,13 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J hist := statistics.HistogramFromProto(jsonIdx.Histogram) hist.ID, hist.NullCount, hist.LastUpdateVersion, hist.Correlation = idxInfo.ID, jsonIdx.NullCount, jsonIdx.LastUpdateVersion, jsonIdx.Correlation cm, topN := statistics.CMSketchAndTopNFromProto(jsonIdx.CMSketch) - // If the statistics is loaded from a JSON without stats version, - // we set it to 1. - statsVer := int64(statistics.Version1) + statsVer := int64(statistics.Version0) if jsonIdx.StatsVer != nil { statsVer = *jsonIdx.StatsVer + } else if jsonIdx.Histogram.Ndv > 0 || jsonIdx.NullCount > 0 { + // If the statistics are collected without setting stats version(which happens in v4.0 and earlier versions), + // we set it to 1. + statsVer = int64(statistics.Version1) } idx := &statistics.Index{ Histogram: *hist, @@ -320,11 +322,13 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J cm, topN := statistics.CMSketchAndTopNFromProto(jsonCol.CMSketch) fms := statistics.FMSketchFromProto(jsonCol.FMSketch) hist.ID, hist.NullCount, hist.LastUpdateVersion, hist.TotColSize, hist.Correlation = colInfo.ID, jsonCol.NullCount, jsonCol.LastUpdateVersion, jsonCol.TotColSize, jsonCol.Correlation - // If the statistics is loaded from a JSON without stats version, - // we set it to 1. - statsVer := int64(statistics.Version1) + statsVer := int64(statistics.Version0) if jsonCol.StatsVer != nil { statsVer = *jsonCol.StatsVer + } else if jsonCol.Histogram.Ndv > 0 || jsonCol.NullCount > 0 { + // If the statistics are collected without setting stats version(which happens in v4.0 and earlier versions), + // we set it to 1. + statsVer = int64(statistics.Version1) } col := &statistics.Column{ PhysicalID: physicalID, diff --git a/statistics/handle/dump_test.go b/statistics/handle/dump_test.go index 26335bd203785..9d85d8d6fa305 100644 --- a/statistics/handle/dump_test.go +++ b/statistics/handle/dump_test.go @@ -479,3 +479,69 @@ func TestJSONTableToBlocks(t *testing.T) { require.NoError(t, err) require.JSONEq(t, string(jsOrigin), string(jsonStr)) } + +func TestLoadStatsFromOldVersion(t *testing.T) { + store, dom := testkit.CreateMockStoreAndDomain(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int, b int, index idx(b))") + h := dom.StatsHandle() + is := dom.InfoSchema() + require.NoError(t, h.HandleDDLEvent(<-h.DDLEventCh())) + require.NoError(t, h.Update(is)) + + statsJSONFromOldVersion := `{ + "database_name": "test", + "table_name": "t", + "columns": { + "a": { + "histogram": { + "ndv": 0 + }, + "cm_sketch": null, + "null_count": 0, + "tot_col_size": 256, + "last_update_version": 440735055846047747, + "correlation": 0 + }, + "b": { + "histogram": { + "ndv": 0 + }, + "cm_sketch": null, + "null_count": 0, + "tot_col_size": 256, + "last_update_version": 440735055846047747, + "correlation": 0 + } + }, + "indices": { + "idx": { + "histogram": { + "ndv": 0 + }, + "cm_sketch": null, + "null_count": 0, + "tot_col_size": 0, + "last_update_version": 440735055846047747, + "correlation": 0 + } + }, + "count": 256, + "modify_count": 256, + "partitions": null +}` + jsonTbl := &handle.JSONTable{} + require.NoError(t, json.Unmarshal([]byte(statsJSONFromOldVersion), jsonTbl)) + require.NoError(t, h.LoadStatsFromJSON(is, jsonTbl)) + tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t")) + require.NoError(t, err) + statsTbl := h.GetTableStats(tbl.Meta()) + for _, col := range statsTbl.Columns { + require.False(t, col.IsStatsInitialized()) + } + for _, idx := range statsTbl.Indices { + require.False(t, idx.IsStatsInitialized()) + } +}