Skip to content

Commit

Permalink
stats: fix panic when dumping stats (pingcap#8448)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx committed Nov 27, 2018
1 parent 8b0dfa4 commit 0fefa18
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
18 changes: 18 additions & 0 deletions statistics/dump_test.go
Expand Up @@ -121,3 +121,21 @@ PARTITION BY RANGE ( a ) (
assertTableEqual(c, originTables[i], t)
}
}

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)")
tk.MustExec("analyze table t")
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
Expand Up @@ -411,7 +411,7 @@ func (hg *Histogram) greaterAndEqRowCount(value types.Datum) float64 {
// lessRowCount estimates the row count where the column less than value.
func (hg *Histogram) lessRowCountWithBktIdx(value types.Datum) (float64, int) {
// all the values is null
if hg.Bounds == nil {
if hg.Bounds.NumRows() == 0 {
return 0, 0
}
index, match := hg.Bounds.LowerBound(0, &value)
Expand Down Expand Up @@ -735,7 +735,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
9 changes: 1 addition & 8 deletions statistics/table.go
Expand Up @@ -177,14 +177,7 @@ func (h *Handle) columnStatsFromStorage(row chunk.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,
},
Histogram: *NewHistogram(histID, distinct, nullCount, histVer, &colInfo.FieldType, 0, totColSize),
Info: colInfo,
Count: count + nullCount,
ErrorRate: errorRate,
Expand Down

0 comments on commit 0fefa18

Please sign in to comment.