From 340ecb86146411d2e0c6b87b5968f01d24a91bce Mon Sep 17 00:00:00 2001 From: pingcap-github-bot Date: Wed, 1 Apr 2020 19:50:53 +0800 Subject: [PATCH] statistic: fix error when fast analyze on only indexes (#15889) (#15968) --- executor/analyze.go | 2 +- executor/analyze_test.go | 21 +++++++++++++++++++++ statistics/cmsketch.go | 4 ++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/executor/analyze.go b/executor/analyze.go index 001373445219f..e84633459fa5f 100755 --- a/executor/analyze.go +++ b/executor/analyze.go @@ -842,7 +842,7 @@ func (e *AnalyzeFastExec) updateCollectorSamples(sValue []byte, sKey kv.Key, sam for j, idxInfo := range e.idxsInfo { idxVals := make([]types.Datum, 0, len(idxInfo.Columns)) for _, idxCol := range idxInfo.Columns { - for _, colInfo := range e.colsInfo { + for _, colInfo := range e.tblInfo.Columns { if colInfo.Name == idxCol.Name { v, err := e.getValueByInfo(colInfo, values) if err != nil { diff --git a/executor/analyze_test.go b/executor/analyze_test.go index 136d5ff4e49a1..bcaf10e323ce8 100644 --- a/executor/analyze_test.go +++ b/executor/analyze_test.go @@ -308,6 +308,27 @@ func (s *testSuite1) TestFastAnalyze(c *C) { "test t2 a 0 1 2 1 18446744073709551615 18446744073709551615")) } +func (s *testSuite1) TestIssue15751(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t0") + tk.MustExec("CREATE TABLE t0(c0 INT, c1 INT, PRIMARY KEY(c0, c1))") + tk.MustExec("INSERT INTO t0 VALUES (0, 0)") + tk.MustExec("set @@tidb_enable_fast_analyze=1") + tk.MustExec("ANALYZE TABLE t0") +} + +func (s *testSuite1) TestIssue15752(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t0") + tk.MustExec("CREATE TABLE t0(c0 INT)") + tk.MustExec("INSERT INTO t0 VALUES (0)") + tk.MustExec("CREATE INDEX i0 ON t0(c0)") + tk.MustExec("set @@tidb_enable_fast_analyze=1") + tk.MustExec("ANALYZE TABLE t0 INDEX i0") +} + func (s *testSuite1) TestAnalyzeIncremental(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") diff --git a/statistics/cmsketch.go b/statistics/cmsketch.go index d5752ff8527b6..4a40328a28329 100644 --- a/statistics/cmsketch.go +++ b/statistics/cmsketch.go @@ -345,7 +345,7 @@ func (c *CMSketch) MergeCMSketch(rc *CMSketch, numTopN uint32) error { if c.depth != rc.depth || c.width != rc.width { return errors.New("Dimensions of Count-Min Sketch should be the same") } - if c.topN != nil || rc.topN != nil { + if len(c.topN) > 0 || len(rc.topN) > 0 { c.mergeTopN(c.topN, rc.topN, numTopN, false) } c.count += rc.count @@ -369,7 +369,7 @@ func (c *CMSketch) MergeCMSketch4IncrementalAnalyze(rc *CMSketch, numTopN uint32 if c.depth != rc.depth || c.width != rc.width { return errors.New("Dimensions of Count-Min Sketch should be the same") } - if c.topN != nil || rc.topN != nil { + if len(c.topN) > 0 || len(rc.topN) > 0 { c.mergeTopN(c.topN, rc.topN, numTopN, true) } for i := range c.table {