Skip to content

Commit 7b4816a

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 36a9810 commit 7b4816a

File tree

3 files changed

+172
-6
lines changed

3 files changed

+172
-6
lines changed

statistics/handle/BUILD.bazel

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
2+
3+
go_library(
4+
name = "handle",
5+
srcs = [
6+
"bootstrap.go",
7+
"ddl.go",
8+
"dump.go",
9+
"gc.go",
10+
"handle.go",
11+
"handle_hist.go",
12+
"historical_stats_handler.go",
13+
"lru_cache.go",
14+
"statscache.go",
15+
"update.go",
16+
],
17+
importpath = "github.com/pingcap/tidb/statistics/handle",
18+
visibility = ["//visibility:public"],
19+
deps = [
20+
"//config",
21+
"//ddl/util",
22+
"//infoschema",
23+
"//kv",
24+
"//metrics",
25+
"//parser/ast",
26+
"//parser/model",
27+
"//parser/mysql",
28+
"//parser/terror",
29+
"//sessionctx",
30+
"//sessionctx/stmtctx",
31+
"//sessionctx/variable",
32+
"//sessiontxn",
33+
"//statistics",
34+
"//statistics/handle/metrics",
35+
"//table",
36+
"//types",
37+
"//util",
38+
"//util/chunk",
39+
"//util/codec",
40+
"//util/collate",
41+
"//util/logutil",
42+
"//util/mathutil",
43+
"//util/memory",
44+
"//util/ranger",
45+
"//util/sqlexec",
46+
"//util/syncutil",
47+
"//util/timeutil",
48+
"@com_github_ngaut_pools//:pools",
49+
"@com_github_pingcap_errors//:errors",
50+
"@com_github_pingcap_failpoint//:failpoint",
51+
"@com_github_pingcap_log//:log",
52+
"@com_github_pingcap_tipb//go-tipb",
53+
"@com_github_tikv_client_go_v2//oracle",
54+
"@org_golang_x_exp//slices",
55+
"@org_uber_go_atomic//:atomic",
56+
"@org_uber_go_zap//:zap",
57+
],
58+
)
59+
60+
go_test(
61+
name = "handle_test",
62+
timeout = "short",
63+
srcs = [
64+
"ddl_test.go",
65+
"dump_test.go",
66+
"gc_test.go",
67+
"handle_hist_test.go",
68+
"lru_cache_test.go",
69+
"main_test.go",
70+
"update_list_test.go",
71+
],
72+
embed = [":handle"],
73+
flaky = True,
74+
race = "on",
75+
shard_count = 34,
76+
deps = [
77+
"//config",
78+
"//domain",
79+
"//parser/model",
80+
"//parser/mysql",
81+
"//sessionctx/stmtctx",
82+
"//sessionctx/variable",
83+
"//statistics",
84+
"//statistics/handle/internal",
85+
"//testkit",
86+
"//testkit/testsetup",
87+
"//types",
88+
"//util",
89+
"//util/mathutil",
90+
"//util/mock",
91+
"//util/sqlexec",
92+
"@com_github_pingcap_failpoint//:failpoint",
93+
"@com_github_stretchr_testify//require",
94+
"@org_uber_go_goleak//:goleak",
95+
],
96+
)

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,
@@ -315,11 +317,13 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J
315317
cm, topN := statistics.CMSketchAndTopNFromProto(jsonCol.CMSketch)
316318
fms := statistics.FMSketchFromProto(jsonCol.FMSketch)
317319
hist.ID, hist.NullCount, hist.LastUpdateVersion, hist.TotColSize, hist.Correlation = colInfo.ID, jsonCol.NullCount, jsonCol.LastUpdateVersion, jsonCol.TotColSize, jsonCol.Correlation
318-
// If the statistics is loaded from a JSON without stats version,
319-
// we set it to 1.
320-
statsVer := int64(statistics.Version1)
320+
statsVer := int64(statistics.Version0)
321321
if jsonCol.StatsVer != nil {
322322
statsVer = *jsonCol.StatsVer
323+
} else if jsonCol.Histogram.Ndv > 0 || jsonCol.NullCount > 0 {
324+
// If the statistics are collected without setting stats version(which happens in v4.0 and earlier versions),
325+
// we set it to 1.
326+
statsVer = int64(statistics.Version1)
323327
}
324328
col := &statistics.Column{
325329
PhysicalID: physicalID,

statistics/handle/dump_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,69 @@ func TestJSONTableToBlocks(t *testing.T) {
479479
require.NoError(t, err)
480480
require.JSONEq(t, string(jsOrigin), string(jsonStr))
481481
}
482+
483+
func TestLoadStatsFromOldVersion(t *testing.T) {
484+
store, dom := testkit.CreateMockStoreAndDomain(t)
485+
tk := testkit.NewTestKit(t, store)
486+
tk.MustExec("use test")
487+
tk.MustExec("drop table if exists t")
488+
tk.MustExec("create table t(a int, b int, index idx(b))")
489+
h := dom.StatsHandle()
490+
is := dom.InfoSchema()
491+
require.NoError(t, h.HandleDDLEvent(<-h.DDLEventCh()))
492+
require.NoError(t, h.Update(is))
493+
494+
statsJSONFromOldVersion := `{
495+
"database_name": "test",
496+
"table_name": "t",
497+
"columns": {
498+
"a": {
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+
"b": {
509+
"histogram": {
510+
"ndv": 0
511+
},
512+
"cm_sketch": null,
513+
"null_count": 0,
514+
"tot_col_size": 256,
515+
"last_update_version": 440735055846047747,
516+
"correlation": 0
517+
}
518+
},
519+
"indices": {
520+
"idx": {
521+
"histogram": {
522+
"ndv": 0
523+
},
524+
"cm_sketch": null,
525+
"null_count": 0,
526+
"tot_col_size": 0,
527+
"last_update_version": 440735055846047747,
528+
"correlation": 0
529+
}
530+
},
531+
"count": 256,
532+
"modify_count": 256,
533+
"partitions": null
534+
}`
535+
jsonTbl := &handle.JSONTable{}
536+
require.NoError(t, json.Unmarshal([]byte(statsJSONFromOldVersion), jsonTbl))
537+
require.NoError(t, h.LoadStatsFromJSON(is, jsonTbl))
538+
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
539+
require.NoError(t, err)
540+
statsTbl := h.GetTableStats(tbl.Meta())
541+
for _, col := range statsTbl.Columns {
542+
require.False(t, col.IsStatsInitialized())
543+
}
544+
for _, idx := range statsTbl.Indices {
545+
require.False(t, idx.IsStatsInitialized())
546+
}
547+
}

0 commit comments

Comments
 (0)