Skip to content

Commit

Permalink
stats: fix panic when dumping stats (#8448) (#8464)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx authored and zz-jason committed Nov 28, 2018
1 parent f5dd339 commit ced80e5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
27 changes: 27 additions & 0 deletions statistics/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/statistics"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testleak"
)
Expand Down Expand Up @@ -64,3 +65,29 @@ func (s *testDumpStatsSuite) TestConversion(c *C) {
tbl := h.GetTableStats(tableInfo.Meta())
assertTableEqual(c, loadTbl, tbl)
}

func (s *testDumpStatsSuite) TestDumpAlteredTable(c *C) {
defer cleanEnv(c, s.store, s.do)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
h := s.do.StatsHandle()
oriLease := h.Lease
h.Lease = 1
defer func() { h.Lease = oriLease }()
tk.MustExec("create table t(a int, b int)")
h.HandleDDLEvent(<-h.DDLEventCh())
tk.MustExec("analyze table t")
t := <-h.AnalyzeResultCh()
h.Update(s.do.InfoSchema())
for i, hg := range t.Hist {
err := statistics.SaveStatsToStorage(tk.Se, t.TableID, t.Count, t.IsIndex, hg, t.Cms[i])
c.Assert(err, IsNil)
}
h.Update(s.do.InfoSchema())
tk.MustExec("alter table t drop column a")
table, err := s.do.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
c.Assert(err, IsNil)
_, err = h.DumpStatsToJSON("test", table.Meta())
c.Assert(err, IsNil)
}
4 changes: 2 additions & 2 deletions statistics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func (hg *Histogram) greaterAndEqRowCount(value types.Datum) float64 {
// lessRowCount estimates the row count where the column less than value.
func (hg *Histogram) lessRowCount(value types.Datum) float64 {
// all the values is null
if hg.Bounds == nil {
if hg.Bounds.NumRows() == 0 {
return 0
}
index, match := hg.Bounds.LowerBound(0, &value)
Expand Down Expand Up @@ -642,7 +642,7 @@ func (c *Column) equalRowCount(sc *stmtctx.StatementContext, val types.Datum, mo
return float64(c.NullCount), nil
}
// all the values is null
if c.Histogram.Bounds == nil {
if c.Histogram.Bounds.NumRows() == 0 {
return 0.0, nil
}
if c.NDV > 0 && c.outOfRange(val) {
Expand Down
15 changes: 4 additions & 11 deletions statistics/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,10 @@ func (h *Handle) columnStatsFromStorage(row types.Row, table *Table, tableInfo *
return errors.Trace(err)
}
col = &Column{
Histogram: Histogram{
ID: histID,
NDV: distinct,
NullCount: nullCount,
tp: &colInfo.FieldType,
LastUpdateVersion: histVer,
TotColSize: totColSize,
},
Info: colInfo,
Count: count + nullCount,
isHandle: tableInfo.PKIsHandle && mysql.HasPriKeyFlag(colInfo.Flag),
Histogram: *NewHistogram(histID, distinct, nullCount, histVer, &colInfo.FieldType, 0, totColSize),
Info: colInfo,
Count: count + nullCount,
isHandle: tableInfo.PKIsHandle && mysql.HasPriKeyFlag(colInfo.Flag),
}
break
}
Expand Down

0 comments on commit ced80e5

Please sign in to comment.