From 4afbf001eaa4faf93b6675e0eda84c045ebd0601 Mon Sep 17 00:00:00 2001 From: Haibin Xie Date: Tue, 12 Nov 2019 16:44:59 +0800 Subject: [PATCH] stats: fix merge cm sketch panic (#13391) --- executor/analyze_test.go | 2 ++ statistics/cmsketch.go | 3 +++ 2 files changed, 5 insertions(+) diff --git a/executor/analyze_test.go b/executor/analyze_test.go index addb97d5c1482..5ea14b6ac0c8e 100644 --- a/executor/analyze_test.go +++ b/executor/analyze_test.go @@ -310,6 +310,8 @@ func (s *testFastAnalyze) TestFastAnalyze(c *C) { // Test CM Sketch built from fast analyze. tk.MustExec("create table t1(a int, b int, index idx(a, b))") + // Should not panic. + tk.MustExec("analyze table t1") tk.MustExec("insert into t1 values (1,1),(1,1),(1,2),(1,2)") tk.MustExec("analyze table t1") tk.MustQuery("explain select a from t1 where a = 1").Check(testkit.Rows( diff --git a/statistics/cmsketch.go b/statistics/cmsketch.go index 554300635f185..f97e0860426fa 100644 --- a/statistics/cmsketch.go +++ b/statistics/cmsketch.go @@ -336,6 +336,9 @@ func (c *CMSketch) mergeTopN(lTopN map[uint64][]*TopNMeta, rTopN map[uint64][]*T // MergeCMSketch merges two CM Sketch. func (c *CMSketch) MergeCMSketch(rc *CMSketch, numTopN uint32) error { + if c == nil || rc == nil { + return nil + } if c.depth != rc.depth || c.width != rc.width { return errors.New("Dimensions of Count-Min Sketch should be the same") }