From 0fefa18f3017e60c724957f57776541979e113d8 Mon Sep 17 00:00:00 2001 From: Haibin Xie Date: Mon, 26 Nov 2018 21:08:02 +0800 Subject: [PATCH] stats: fix panic when dumping stats (#8448) --- statistics/dump_test.go | 18 ++++++++++++++++++ statistics/histogram.go | 4 ++-- statistics/table.go | 9 +-------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/statistics/dump_test.go b/statistics/dump_test.go index c3824d38b88f3..5283855dc0a8d 100644 --- a/statistics/dump_test.go +++ b/statistics/dump_test.go @@ -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) +} diff --git a/statistics/histogram.go b/statistics/histogram.go index 54ddbf0a1c97b..67ef41640031d 100644 --- a/statistics/histogram.go +++ b/statistics/histogram.go @@ -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) @@ -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) { diff --git a/statistics/table.go b/statistics/table.go index 078b4b9dbd5e3..4d65063143ddc 100644 --- a/statistics/table.go +++ b/statistics/table.go @@ -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,