Skip to content

Commit eba5ec1

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

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

statistics/handle/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ go_test(
7272
embed = [":handle"],
7373
flaky = True,
7474
race = "on",
75+
<<<<<<< HEAD
7576
shard_count = 50,
77+
=======
78+
shard_count = 34,
79+
>>>>>>> db68a1222e9 (staistics: fix load stats from old version json (#42967))
7680
deps = [
7781
"//config",
7882
"//domain",

statistics/handle/dump.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,13 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J
421421
hist := statistics.HistogramFromProto(jsonIdx.Histogram)
422422
hist.ID, hist.NullCount, hist.LastUpdateVersion, hist.Correlation = idxInfo.ID, jsonIdx.NullCount, jsonIdx.LastUpdateVersion, jsonIdx.Correlation
423423
cm, topN := statistics.CMSketchAndTopNFromProto(jsonIdx.CMSketch)
424-
// If the statistics is loaded from a JSON without stats version,
425-
// we set it to 1.
426-
statsVer := int64(statistics.Version1)
424+
statsVer := int64(statistics.Version0)
427425
if jsonIdx.StatsVer != nil {
428426
statsVer = *jsonIdx.StatsVer
427+
} else if jsonIdx.Histogram.Ndv > 0 || jsonIdx.NullCount > 0 {
428+
// If the statistics are collected without setting stats version(which happens in v4.0 and earlier versions),
429+
// we set it to 1.
430+
statsVer = int64(statistics.Version1)
429431
}
430432
idx := &statistics.Index{
431433
Histogram: *hist,
@@ -465,11 +467,13 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J
465467
cm, topN := statistics.CMSketchAndTopNFromProto(jsonCol.CMSketch)
466468
fms := statistics.FMSketchFromProto(jsonCol.FMSketch)
467469
hist.ID, hist.NullCount, hist.LastUpdateVersion, hist.TotColSize, hist.Correlation = colInfo.ID, jsonCol.NullCount, jsonCol.LastUpdateVersion, jsonCol.TotColSize, jsonCol.Correlation
468-
// If the statistics is loaded from a JSON without stats version,
469-
// we set it to 1.
470-
statsVer := int64(statistics.Version1)
470+
statsVer := int64(statistics.Version0)
471471
if jsonCol.StatsVer != nil {
472472
statsVer = *jsonCol.StatsVer
473+
} else if jsonCol.Histogram.Ndv > 0 || jsonCol.NullCount > 0 {
474+
// If the statistics are collected without setting stats version(which happens in v4.0 and earlier versions),
475+
// we set it to 1.
476+
statsVer = int64(statistics.Version1)
473477
}
474478
col := &statistics.Column{
475479
PhysicalID: physicalID,

statistics/handle/dump_test.go

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

0 commit comments

Comments
 (0)