Skip to content

Commit

Permalink
stats: fix zero column size update error (#7530)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx authored and ngaut committed Aug 29, 2018
1 parent afa7c86 commit eef448a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions statistics/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ func (h *Handle) dumpTableStatColSizeToKV(id int64, delta variable.TableDelta) e
}
values = append(values, fmt.Sprintf("(%d, 0, %d, 0, %d, %d)", id, histID, deltaColSize, version))
}
if len(values) == 0 {
_, err = h.ctx.(sqlexec.SQLExecutor).Execute(ctx, "rollback")
return errors.Trace(err)
}
sql := fmt.Sprintf("insert into mysql.stats_histograms (table_id, is_index, hist_id, distinct_count, tot_col_size, version) "+
"values %s on duplicate key update tot_col_size = tot_col_size + values(tot_col_size), version = values(version)", strings.Join(values, ","))
_, err = h.ctx.(sqlexec.SQLExecutor).Execute(ctx, sql)
Expand Down
12 changes: 12 additions & 0 deletions statistics/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,18 @@ func (s *testStatsUpdateSuite) TestAutoUpdate(c *C) {
c.Assert(hg.Len(), Equals, 3)
}

func (s *testStatsUpdateSuite) TestZeroColSizeUpdate(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
testKit.MustExec("create table t (a varchar(20))")
testKit.MustExec("analyze table t")
testKit.MustExec("insert into t values ('ss')")
testKit.MustExec("delete from t")
h := s.do.StatsHandle()
c.Assert(h.DumpStatsDeltaToKV(), IsNil)
}

func appendBucket(h *statistics.Histogram, l, r int64) {
lower, upper := types.NewIntDatum(l), types.NewIntDatum(r)
h.AppendBucket(&lower, &upper, 0, 0)
Expand Down

0 comments on commit eef448a

Please sign in to comment.