diff --git a/statistics/ddl_test.go b/statistics/ddl_test.go index a0f70babc5..4b4eb456b8 100644 --- a/statistics/ddl_test.go +++ b/statistics/ddl_test.go @@ -84,7 +84,7 @@ func (s *testStatsCacheSuite) TestDDLTable(c *C) { statsTbl = h.GetTableStats(tableInfo.ID) c.Assert(statsTbl.Pseudo, IsTrue) - testKit.MustExec("create table t1 (c1 int, c2 int)") + testKit.MustExec("create table t1 (c1 int, c2 int, index idx(c1))") is = do.InfoSchema() tbl, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t1")) c.Assert(err, IsNil) diff --git a/statistics/statistics_test.go b/statistics/statistics_test.go index b8940bfdd3..ea7561d978 100644 --- a/statistics/statistics_test.go +++ b/statistics/statistics_test.go @@ -14,6 +14,7 @@ package statistics import ( + "math" "testing" . "github.com/pingcap/check" @@ -180,6 +181,7 @@ func (s *testStatisticsSuite) TestBuild(c *C) { c.Check(err, IsNil) c.Check(int(count), Equals, 0) + s.pk.(*recordSet).cursor = 0 tblCount, col, err = BuildPK(ctx, bucketCount, 4, ast.RecordSet(s.pk)) c.Check(err, IsNil) c.Check(int(tblCount), Equals, 100000) @@ -266,6 +268,10 @@ func (s *testStatisticsSuite) TestColumnRange(c *C) { count, err = tbl.GetRowCountByColumnRanges(sc, 0, ran) c.Assert(err, IsNil) c.Assert(int(count), Equals, 2500) + ran[0].Low = ran[0].High + count, err = tbl.GetRowCountByColumnRanges(sc, 0, ran) + c.Assert(err, IsNil) + c.Assert(int(count), Equals, 100) tbl.Columns[0] = col ran[0].Low = types.Datum{} @@ -285,4 +291,68 @@ func (s *testStatisticsSuite) TestColumnRange(c *C) { count, err = tbl.GetRowCountByColumnRanges(sc, 0, ran) c.Assert(err, IsNil) c.Assert(int(count), Equals, 9965) + ran[0].Low = ran[0].High + count, err = tbl.GetRowCountByColumnRanges(sc, 0, ran) + c.Assert(err, IsNil) + c.Assert(int(count), Equals, 1) +} + +func (s *testStatisticsSuite) TestIntColumnRanges(c *C) { + bucketCount := int64(256) + ctx := mock.NewContext() + sc := ctx.GetSessionVars().StmtCtx + + s.pk.(*recordSet).cursor = 0 + rowCount, hg, err := build4SortedColumn(ctx, bucketCount, 0, s.pk, true) + c.Check(err, IsNil) + c.Check(rowCount, Equals, int64(100000)) + col := &Column{Histogram: *hg} + tbl := &Table{ + Count: int64(col.totalRowCount()), + Columns: make(map[int64]*Column), + } + ran := []types.IntColumnRange{{ + LowVal: math.MinInt64, + HighVal: math.MaxInt64, + }} + count, err := tbl.GetRowCountByIntColumnRanges(sc, 0, ran) + c.Assert(err, IsNil) + c.Assert(int(count), Equals, 100000) + ran[0].LowVal = 1000 + ran[0].HighVal = 2000 + count, err = tbl.GetRowCountByIntColumnRanges(sc, 0, ran) + c.Assert(err, IsNil) + c.Assert(int(count), Equals, 1000) + ran[0].LowVal = 1001 + ran[0].HighVal = 1999 + count, err = tbl.GetRowCountByIntColumnRanges(sc, 0, ran) + c.Assert(err, IsNil) + c.Assert(int(count), Equals, 998) + ran[0].LowVal = 1000 + ran[0].HighVal = 1000 + count, err = tbl.GetRowCountByIntColumnRanges(sc, 0, ran) + c.Assert(err, IsNil) + c.Assert(int(count), Equals, 100) + + tbl.Columns[0] = col + ran[0].LowVal = math.MinInt64 + ran[0].HighVal = math.MaxInt64 + count, err = tbl.GetRowCountByIntColumnRanges(sc, 0, ran) + c.Assert(err, IsNil) + c.Assert(int(count), Equals, 100000) + ran[0].LowVal = 1000 + ran[0].HighVal = 2000 + count, err = tbl.GetRowCountByIntColumnRanges(sc, 0, ran) + c.Assert(err, IsNil) + c.Assert(int(count), Equals, 1000) + ran[0].LowVal = 1001 + ran[0].HighVal = 1999 + count, err = tbl.GetRowCountByIntColumnRanges(sc, 0, ran) + c.Assert(err, IsNil) + c.Assert(int(count), Equals, 998) + ran[0].LowVal = 1000 + ran[0].HighVal = 1000 + count, err = tbl.GetRowCountByIntColumnRanges(sc, 0, ran) + c.Assert(err, IsNil) + c.Assert(int(count), Equals, 1) }