Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statistics: right deal with error for reading stats from storage #57914

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pkg/executor/test/tiflashtest/tiflash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ func TestIssue57149(t *testing.T) {
}

func TestTiFlashPartitionTableBroadcastJoin(t *testing.T) {
store := testkit.CreateMockStore(t, withMockTiFlash(2))
store, dom := testkit.CreateMockStoreAndDomain(t, withMockTiFlash(2))
tk := testkit.NewTestKit(t, store)
tk.MustExec("create database tiflash_partition_BCJ")
tk.MustExec("use tiflash_partition_BCJ")
Expand All @@ -1040,7 +1040,8 @@ func TestTiFlashPartitionTableBroadcastJoin(t *testing.T) {
partition p0 values in (` + listPartitions[0] + `), partition p1 values in (` + listPartitions[1] + `),
partition p2 values in (` + listPartitions[2] + `), partition p3 values in (` + listPartitions[3] + `))`)
tk.MustExec(`create table tnormal (a int, b int) partition by hash(a) partitions 4`)

h := dom.StatsHandle()
require.NoError(t, h.HandleDDLEvent(<-h.DDLEventCh()))
for _, tbl := range []string{`thash`, `trange`, `tlist`, `tnormal`} {
tk.MustExec("alter table " + tbl + " set tiflash replica 1")
tb := external.GetTableByName(t, tk, "tiflash_partition_BCJ", tbl)
Expand All @@ -1054,7 +1055,7 @@ func TestTiFlashPartitionTableBroadcastJoin(t *testing.T) {
}
for _, tbl := range []string{`thash`, `trange`, `tlist`, `tnormal`} {
tk.MustExec(fmt.Sprintf("insert into %v values %v", tbl, strings.Join(vals, ", ")))
tk.MustExec(fmt.Sprintf("analyze table %v", tbl))
tk.MustExec(fmt.Sprintf("analyze table %v all columns", tbl))
}
tk.MustExec("set @@session.tidb_isolation_read_engines='tiflash'")
tk.MustExec("set @@session.tidb_enforce_mpp=1")
Expand Down
4 changes: 3 additions & 1 deletion pkg/statistics/handle/globalstats/global_stats_async.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,9 @@ func (a *AsyncMergePartitionStats2GlobalStats) dealHistogramAndTopN(stmtCtx *stm
if err != nil {
return err
}

if len(allhg) == 0 && len(poppedTopN) == 0 {
return errors.New("empty hg cannot merge")
}
// Merge histogram.
globalHg := &(a.globalStats.Hg[item.idx])
*globalHg, err = statistics.MergePartitionHist2GlobalHist(stmtCtx, allhg, poppedTopN,
Expand Down
3 changes: 2 additions & 1 deletion pkg/statistics/handle/handletest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ go_test(
],
flaky = True,
race = "on",
shard_count = 33,
shard_count = 34,
deps = [
"//pkg/config",
"//pkg/domain",
"//pkg/parser/model",
"//pkg/planner/cardinality",
"//pkg/sessionctx/variable",
"//pkg/sessiontxn",
"//pkg/statistics",
"//pkg/statistics/handle",
"//pkg/statistics/handle/util",
Expand Down
21 changes: 21 additions & 0 deletions pkg/statistics/handle/handletest/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/planner/cardinality"
"github.com/pingcap/tidb/pkg/sessionctx/variable"
"github.com/pingcap/tidb/pkg/sessiontxn"
"github.com/pingcap/tidb/pkg/statistics"
"github.com/pingcap/tidb/pkg/statistics/handle"
"github.com/pingcap/tidb/pkg/statistics/handle/util"
Expand Down Expand Up @@ -1489,3 +1490,23 @@ func TestSkipMissingPartitionStats(t *testing.T) {
return false
})
}

func TestReadStatsWithoutAnalyzeAndHandleEvent(t *testing.T) {
// it means it only has stats_meta without stats_hist
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_partition_prune_mode = 'dynamic'")
tk.MustExec("set @@tidb_skip_missing_partition_stats = 1")
tk.MustExec("create table t (a int, b int, c int, index idx_b(b)) partition by range (a) (partition p0 values less than (100), partition p1 values less than (200), partition p2 values less than (300))")
tk.MustExec("insert into t values (1,1,1), (2,2,2), (101,101,101), (102,102,102), (201,201,201), (202,202,202)")
h := dom.StatsHandle()
require.NoError(t, h.DumpStatsDeltaToKV(true))
is := sessiontxn.GetTxnManager(tk.Session()).GetTxnInfoSchema()
table, err := is.TableByName(context.Background(), model.NewCIStr("test"), model.NewCIStr("t"))
require.NoError(t, err)
tableInfo := table.Meta()
tblStats, err := h.TableStatsFromStorage(tableInfo, tableInfo.ID, true, 0)
require.NoError(t, err)
require.NotNil(t, tblStats)
}
8 changes: 6 additions & 2 deletions pkg/statistics/handle/storage/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,13 @@ func TableStatsFromStorage(sctx sessionctx.Context, snapshot uint64, tableInfo *
table.RealtimeCount = realtimeCount

rows, _, err := util.ExecRows(sctx, "select table_id, is_index, hist_id, distinct_count, version, null_count, tot_col_size, stats_ver, flag, correlation, last_analyze_pos from mysql.stats_histograms where table_id = %?", tableID)
if err != nil {
return nil, err
}
// Check deleted table.
if err != nil || len(rows) == 0 {
return nil, nil
if len(rows) == 0 {
table.Pseudo = true
return table, nil
}
for _, row := range rows {
if err := sctx.GetSessionVars().SQLKiller.HandleSignal(); err != nil {
Expand Down