From c91290fd8ddc0ca02abe79a776b330c8502ceab1 Mon Sep 17 00:00:00 2001 From: Haibin Xie Date: Wed, 17 Oct 2018 16:47:23 +0800 Subject: [PATCH] stats: fix panic caused by empty histogram (#7912) (#7928) --- statistics/histogram.go | 4 ++-- statistics/selectivity_test.go | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/statistics/histogram.go b/statistics/histogram.go index f56072ffb29f1..ca52273b20e1a 100644 --- a/statistics/histogram.go +++ b/statistics/histogram.go @@ -679,7 +679,7 @@ func (hg *Histogram) AvgCountPerValue(totalCount int64) float64 { } func (hg *Histogram) outOfRange(val types.Datum) bool { - if hg.Bounds == nil { + if hg.Len() == 0 { return true } return chunk.Compare(hg.Bounds.GetRow(0), 0, &val) > 0 || @@ -858,7 +858,7 @@ func (idx *Index) getRowCount(sc *stmtctx.StatementContext, indexRanges []*range } func (idx *Index) outOfRange(val types.Datum) bool { - if idx.Bounds == nil { + if idx.Histogram.Len() == 0 { return true } withInLowBoundOrPrefixMatch := chunk.Compare(idx.Bounds.GetRow(0), 0, &val) <= 0 || diff --git a/statistics/selectivity_test.go b/statistics/selectivity_test.go index ed24d0b97f326..77225890c2fca 100644 --- a/statistics/selectivity_test.go +++ b/statistics/selectivity_test.go @@ -292,6 +292,18 @@ func (s *testSelectivitySuite) TestEstimationForUnknownValues(c *C) { count, err = statsTbl.GetRowCountByIndexRanges(sc, idxID, getRange(9, 30)) c.Assert(err, IsNil) c.Assert(count, Equals, 2.2) + + testKit.MustExec("truncate table t") + testKit.MustExec("insert into t values (null, null)") + testKit.MustExec("analyze table t") + table, err = s.dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t")) + c.Assert(err, IsNil) + statsTbl = h.GetTableStats(table.Meta()) + + colID = table.Meta().Columns[0].ID + count, err = statsTbl.GetRowCountByColumnRanges(sc, colID, getRange(1, 30)) + c.Assert(err, IsNil) + c.Assert(count, Equals, 0.0) } func BenchmarkSelectivity(b *testing.B) {