Skip to content

Commit ea844ff

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 9360d4a commit ea844ff

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
@@ -272,11 +272,13 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J
272272
hist := statistics.HistogramFromProto(jsonIdx.Histogram)
273273
hist.ID, hist.NullCount, hist.LastUpdateVersion, hist.Correlation = idxInfo.ID, jsonIdx.NullCount, jsonIdx.LastUpdateVersion, jsonIdx.Correlation
274274
cm, topN := statistics.CMSketchAndTopNFromProto(jsonIdx.CMSketch)
275-
// If the statistics is loaded from a JSON without stats version,
276-
// we set it to 1.
277-
statsVer := int64(statistics.Version1)
275+
statsVer := int64(statistics.Version0)
278276
if jsonIdx.StatsVer != nil {
279277
statsVer = *jsonIdx.StatsVer
278+
} else if jsonIdx.Histogram.Ndv > 0 || jsonIdx.NullCount > 0 {
279+
// If the statistics are collected without setting stats version(which happens in v4.0 and earlier versions),
280+
// we set it to 1.
281+
statsVer = int64(statistics.Version1)
280282
}
281283
idx := &statistics.Index{
282284
Histogram: *hist,
@@ -303,11 +305,13 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J
303305
cm, topN := statistics.CMSketchAndTopNFromProto(jsonCol.CMSketch)
304306
fms := statistics.FMSketchFromProto(jsonCol.FMSketch)
305307
hist.ID, hist.NullCount, hist.LastUpdateVersion, hist.TotColSize, hist.Correlation = colInfo.ID, jsonCol.NullCount, jsonCol.LastUpdateVersion, jsonCol.TotColSize, jsonCol.Correlation
306-
// If the statistics is loaded from a JSON without stats version,
307-
// we set it to 1.
308-
statsVer := int64(statistics.Version1)
308+
statsVer := int64(statistics.Version0)
309309
if jsonCol.StatsVer != nil {
310310
statsVer = *jsonCol.StatsVer
311+
} else if jsonCol.Histogram.Ndv > 0 || jsonCol.NullCount > 0 {
312+
// If the statistics are collected without setting stats version(which happens in v4.0 and earlier versions),
313+
// we set it to 1.
314+
statsVer = int64(statistics.Version1)
311315
}
312316
col := &statistics.Column{
313317
PhysicalID: physicalID,

statistics/handle/dump_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,69 @@ func (s *testStatsSuite) TestDumpVer2Stats(c *C) {
338338
// the statistics.Table in the stats cache is the same as the unmarshalled statistics.Table
339339
assertTableEqual(c, statsCacheTbl, loadTbl)
340340
}
341+
342+
func TestLoadStatsFromOldVersion(t *testing.T) {
343+
store, dom := testkit.CreateMockStoreAndDomain(t)
344+
tk := testkit.NewTestKit(t, store)
345+
tk.MustExec("use test")
346+
tk.MustExec("drop table if exists t")
347+
tk.MustExec("create table t(a int, b int, index idx(b))")
348+
h := dom.StatsHandle()
349+
is := dom.InfoSchema()
350+
require.NoError(t, h.HandleDDLEvent(<-h.DDLEventCh()))
351+
require.NoError(t, h.Update(is))
352+
353+
statsJSONFromOldVersion := `{
354+
"database_name": "test",
355+
"table_name": "t",
356+
"columns": {
357+
"a": {
358+
"histogram": {
359+
"ndv": 0
360+
},
361+
"cm_sketch": null,
362+
"null_count": 0,
363+
"tot_col_size": 256,
364+
"last_update_version": 440735055846047747,
365+
"correlation": 0
366+
},
367+
"b": {
368+
"histogram": {
369+
"ndv": 0
370+
},
371+
"cm_sketch": null,
372+
"null_count": 0,
373+
"tot_col_size": 256,
374+
"last_update_version": 440735055846047747,
375+
"correlation": 0
376+
}
377+
},
378+
"indices": {
379+
"idx": {
380+
"histogram": {
381+
"ndv": 0
382+
},
383+
"cm_sketch": null,
384+
"null_count": 0,
385+
"tot_col_size": 0,
386+
"last_update_version": 440735055846047747,
387+
"correlation": 0
388+
}
389+
},
390+
"count": 256,
391+
"modify_count": 256,
392+
"partitions": null
393+
}`
394+
jsonTbl := &handle.JSONTable{}
395+
require.NoError(t, json.Unmarshal([]byte(statsJSONFromOldVersion), jsonTbl))
396+
require.NoError(t, h.LoadStatsFromJSON(is, jsonTbl))
397+
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
398+
require.NoError(t, err)
399+
statsTbl := h.GetTableStats(tbl.Meta())
400+
for _, col := range statsTbl.Columns {
401+
require.False(t, col.IsStatsInitialized())
402+
}
403+
for _, idx := range statsTbl.Indices {
404+
require.False(t, idx.IsStatsInitialized())
405+
}
406+
}

0 commit comments

Comments
 (0)