Skip to content

Commit 8de6d30

Browse files
authored
statistics: fix wrong result in the MergeGlobalStatsTopNByConcurrency (#48757)
ref #48713
1 parent 058a961 commit 8de6d30

File tree

6 files changed

+39
-20
lines changed

6 files changed

+39
-20
lines changed

pkg/statistics/handle/globalstats/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ go_test(
4444
],
4545
embed = [":globalstats"],
4646
flaky = True,
47-
shard_count = 21,
47+
shard_count = 23,
4848
deps = [
4949
"//pkg/config",
5050
"//pkg/parser/model",

pkg/statistics/handle/globalstats/globalstats_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,3 +1240,38 @@ func TestGlobalIndexStatistics(t *testing.T) {
12401240
"└─IndexRangeScan_11 4.00 cop[tikv] table:t, index:idx(b) range:[-inf,16), keep order:true"))
12411241
}
12421242
}
1243+
1244+
func TestIssues24349(t *testing.T) {
1245+
store := testkit.CreateMockStore(t)
1246+
testKit := testkit.NewTestKit(t, store)
1247+
testKit.MustExec("use test")
1248+
testKit.MustExec("set @@tidb_partition_prune_mode='dynamic'")
1249+
testKit.MustExec("set @@tidb_analyze_version=2")
1250+
defer testKit.MustExec("set @@tidb_analyze_version=1")
1251+
defer testKit.MustExec("set @@tidb_partition_prune_mode='static'")
1252+
testIssues24349(testKit)
1253+
}
1254+
1255+
func TestIssues24349WithConcurrency(t *testing.T) {
1256+
store := testkit.CreateMockStore(t)
1257+
testKit := testkit.NewTestKit(t, store)
1258+
testKit.MustExec("use test")
1259+
testKit.MustExec("set @@tidb_partition_prune_mode='dynamic'")
1260+
testKit.MustExec("set @@tidb_analyze_version=2")
1261+
testKit.MustExec("set global tidb_merge_partition_stats_concurrency=2")
1262+
defer testKit.MustExec("set @@tidb_analyze_version=1")
1263+
defer testKit.MustExec("set @@tidb_partition_prune_mode='static'")
1264+
defer testKit.MustExec("set global tidb_merge_partition_stats_concurrency=1")
1265+
testIssues24349(testKit)
1266+
}
1267+
1268+
func testIssues24349(testKit *testkit.TestKit) {
1269+
testKit.MustExec("create table t (a int, b int) partition by hash(a) partitions 3")
1270+
testKit.MustExec("insert into t values (0, 3), (0, 3), (0, 3), (0, 2), (1, 1), (1, 2), (1, 2), (1, 2), (1, 3), (1, 4), (2, 1), (2, 1)")
1271+
testKit.MustExec("analyze table t with 1 topn, 3 buckets")
1272+
testKit.MustQuery("show stats_buckets where partition_name='global'").Check(testkit.Rows(
1273+
"test t global a 0 0 2 2 0 2 0",
1274+
"test t global b 0 0 3 1 1 2 0",
1275+
"test t global b 0 1 10 1 4 4 0",
1276+
))
1277+
}

pkg/statistics/handle/globalstats/merge_worker.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func (worker *topnStatsMergeWorker) Run(timeZone *time.Location, isIndex bool,
107107
datumMap := statistics.NewDatumMapCache()
108108

109109
for i, topN := range checkTopNs {
110+
i = i + start
110111
if err := worker.killer.HandleSignal(); err != nil {
111112
resp.Err = err
112113
worker.respCh <- resp

pkg/statistics/handle/handletest/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ go_test(
99
],
1010
flaky = True,
1111
race = "on",
12-
shard_count = 37,
12+
shard_count = 36,
1313
deps = [
1414
"//pkg/config",
1515
"//pkg/domain",

pkg/statistics/handle/handletest/handle_test.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,24 +1361,6 @@ func TestStatsCacheUpdateSkip(t *testing.T) {
13611361
require.Equal(t, statsTbl2, statsTbl1)
13621362
}
13631363

1364-
func TestIssues24349(t *testing.T) {
1365-
store := testkit.CreateMockStore(t)
1366-
testKit := testkit.NewTestKit(t, store)
1367-
testKit.MustExec("use test")
1368-
testKit.MustExec("set @@tidb_partition_prune_mode='dynamic'")
1369-
testKit.MustExec("set @@tidb_analyze_version=2")
1370-
defer testKit.MustExec("set @@tidb_analyze_version=1")
1371-
defer testKit.MustExec("set @@tidb_partition_prune_mode='static'")
1372-
testKit.MustExec("create table t (a int, b int) partition by hash(a) partitions 3")
1373-
testKit.MustExec("insert into t values (0, 3), (0, 3), (0, 3), (0, 2), (1, 1), (1, 2), (1, 2), (1, 2), (1, 3), (1, 4), (2, 1), (2, 1)")
1374-
testKit.MustExec("analyze table t with 1 topn, 3 buckets")
1375-
testKit.MustQuery("show stats_buckets where partition_name='global'").Check(testkit.Rows(
1376-
"test t global a 0 0 2 2 0 2 0",
1377-
"test t global b 0 0 3 1 1 2 0",
1378-
"test t global b 0 1 10 1 4 4 0",
1379-
))
1380-
}
1381-
13821364
func testIncrementalModifyCountUpdateHelper(analyzeSnapshot bool) func(*testing.T) {
13831365
return func(t *testing.T) {
13841366
store, dom := testkit.CreateMockStoreAndDomain(t)

pkg/statistics/histogram.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,7 @@ func newbucket4MergingForRecycle() *bucket4Merging {
11881188
}
11891189

11901190
func releasebucket4MergingForRecycle(b *bucket4Merging) {
1191+
b.disjointNDV = 0
11911192
bucket4MergingPool.Put(b)
11921193
}
11931194

0 commit comments

Comments
 (0)