Skip to content

Commit ee032ea

Browse files
xuyifangreeneyesti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#42967
Signed-off-by: ti-chi-bot <[email protected]>
1 parent 113e2cc commit ee032ea

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

statistics/handle/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ go_test(
7171
],
7272
embed = [":handle"],
7373
flaky = True,
74+
<<<<<<< HEAD
7475
shard_count = 50,
76+
=======
77+
race = "on",
78+
shard_count = 34,
79+
>>>>>>> db68a1222e9 (staistics: fix load stats from old version json (#42967))
7580
deps = [
7681
"//config",
7782
"//domain",

statistics/handle/dump.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,13 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J
276276
hist := statistics.HistogramFromProto(jsonIdx.Histogram)
277277
hist.ID, hist.NullCount, hist.LastUpdateVersion, hist.Correlation = idxInfo.ID, jsonIdx.NullCount, jsonIdx.LastUpdateVersion, jsonIdx.Correlation
278278
cm, topN := statistics.CMSketchAndTopNFromProto(jsonIdx.CMSketch)
279-
// If the statistics is loaded from a JSON without stats version,
280-
// we set it to 1.
281-
statsVer := int64(statistics.Version1)
279+
statsVer := int64(statistics.Version0)
282280
if jsonIdx.StatsVer != nil {
283281
statsVer = *jsonIdx.StatsVer
282+
} else if jsonIdx.Histogram.Ndv > 0 || jsonIdx.NullCount > 0 {
283+
// If the statistics are collected without setting stats version(which happens in v4.0 and earlier versions),
284+
// we set it to 1.
285+
statsVer = int64(statistics.Version1)
284286
}
285287
idx := &statistics.Index{
286288
Histogram: *hist,
@@ -320,11 +322,13 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J
320322
cm, topN := statistics.CMSketchAndTopNFromProto(jsonCol.CMSketch)
321323
fms := statistics.FMSketchFromProto(jsonCol.FMSketch)
322324
hist.ID, hist.NullCount, hist.LastUpdateVersion, hist.TotColSize, hist.Correlation = colInfo.ID, jsonCol.NullCount, jsonCol.LastUpdateVersion, jsonCol.TotColSize, jsonCol.Correlation
323-
// If the statistics is loaded from a JSON without stats version,
324-
// we set it to 1.
325-
statsVer := int64(statistics.Version1)
325+
statsVer := int64(statistics.Version0)
326326
if jsonCol.StatsVer != nil {
327327
statsVer = *jsonCol.StatsVer
328+
} else if jsonCol.Histogram.Ndv > 0 || jsonCol.NullCount > 0 {
329+
// If the statistics are collected without setting stats version(which happens in v4.0 and earlier versions),
330+
// we set it to 1.
331+
statsVer = int64(statistics.Version1)
328332
}
329333
col := &statistics.Column{
330334
PhysicalID: physicalID,

statistics/handle/dump_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,69 @@ func TestJSONTableToBlocks(t *testing.T) {
468468
require.NoError(t, err)
469469
require.JSONEq(t, string(jsOrigin), string(jsonStr))
470470
}
471+
472+
func TestLoadStatsFromOldVersion(t *testing.T) {
473+
store, dom := testkit.CreateMockStoreAndDomain(t)
474+
tk := testkit.NewTestKit(t, store)
475+
tk.MustExec("use test")
476+
tk.MustExec("drop table if exists t")
477+
tk.MustExec("create table t(a int, b int, index idx(b))")
478+
h := dom.StatsHandle()
479+
is := dom.InfoSchema()
480+
require.NoError(t, h.HandleDDLEvent(<-h.DDLEventCh()))
481+
require.NoError(t, h.Update(is))
482+
483+
statsJSONFromOldVersion := `{
484+
"database_name": "test",
485+
"table_name": "t",
486+
"columns": {
487+
"a": {
488+
"histogram": {
489+
"ndv": 0
490+
},
491+
"cm_sketch": null,
492+
"null_count": 0,
493+
"tot_col_size": 256,
494+
"last_update_version": 440735055846047747,
495+
"correlation": 0
496+
},
497+
"b": {
498+
"histogram": {
499+
"ndv": 0
500+
},
501+
"cm_sketch": null,
502+
"null_count": 0,
503+
"tot_col_size": 256,
504+
"last_update_version": 440735055846047747,
505+
"correlation": 0
506+
}
507+
},
508+
"indices": {
509+
"idx": {
510+
"histogram": {
511+
"ndv": 0
512+
},
513+
"cm_sketch": null,
514+
"null_count": 0,
515+
"tot_col_size": 0,
516+
"last_update_version": 440735055846047747,
517+
"correlation": 0
518+
}
519+
},
520+
"count": 256,
521+
"modify_count": 256,
522+
"partitions": null
523+
}`
524+
jsonTbl := &handle.JSONTable{}
525+
require.NoError(t, json.Unmarshal([]byte(statsJSONFromOldVersion), jsonTbl))
526+
require.NoError(t, h.LoadStatsFromJSON(is, jsonTbl))
527+
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
528+
require.NoError(t, err)
529+
statsTbl := h.GetTableStats(tbl.Meta())
530+
for _, col := range statsTbl.Columns {
531+
require.False(t, col.IsStatsInitialized())
532+
}
533+
for _, idx := range statsTbl.Indices {
534+
require.False(t, idx.IsStatsInitialized())
535+
}
536+
}

0 commit comments

Comments
 (0)