From 128052d507cfc3307bb2f7687fd84c37bcdb2111 Mon Sep 17 00:00:00 2001 From: Kenan Yao Date: Thu, 28 Feb 2019 14:27:53 +0800 Subject: [PATCH] executor: only show valid columns in `stats_histogram` (#9487) (#9503) --- executor/show_stats.go | 4 ++++ executor/show_stats_test.go | 15 ++++++++++++++- statistics/table.go | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/executor/show_stats.go b/executor/show_stats.go index 3cc82f941b5c0..4913fa2baec9f 100644 --- a/executor/show_stats.go +++ b/executor/show_stats.go @@ -54,6 +54,10 @@ func (e *ShowExec) fetchShowStatsHistogram() error { statsTbl := h.GetTableStats(tbl) if !statsTbl.Pseudo { for _, col := range statsTbl.Columns { + // Pass a nil StatementContext to avoid column stats being marked as needed. + if statsTbl.ColumnIsInvalid(nil, col.ID) { + continue + } e.histogramToRow(db.Name.O, tbl.Name.O, col.Info.Name.O, 0, col.Histogram, col.AvgColSize(statsTbl.Count)) } for _, idx := range statsTbl.Indices { diff --git a/executor/show_stats_test.go b/executor/show_stats_test.go index 324d818a90fda..37caf000d998e 100644 --- a/executor/show_stats_test.go +++ b/executor/show_stats_test.go @@ -41,13 +41,26 @@ func (s *testSuite) TestShowStatsHistograms(c *C) { tk.MustExec("drop table if exists t") tk.MustExec("create table t (a int, b int)") tk.MustExec("analyze table t") - result := tk.MustQuery("show stats_histograms").Sort() + result := tk.MustQuery("show stats_histograms") + c.Assert(len(result.Rows()), Equals, 0) + tk.MustExec("insert into t values(1,1)") + tk.MustExec("analyze table t") + result = tk.MustQuery("show stats_histograms").Sort() c.Assert(len(result.Rows()), Equals, 2) c.Assert(result.Rows()[0][2], Equals, "a") c.Assert(result.Rows()[1][2], Equals, "b") result = tk.MustQuery("show stats_histograms where column_name = 'a'") c.Assert(len(result.Rows()), Equals, 1) c.Assert(result.Rows()[0][2], Equals, "a") + + tk.MustExec("drop table t") + tk.MustExec("create table t(a int, b int, c int, index idx_b(b), index idx_c_a(c, a))") + tk.MustExec("insert into t values(1,null,1),(2,null,2),(3,3,3),(4,null,4),(null,null,null)") + res := tk.MustQuery("show stats_histograms where table_name = 't'") + c.Assert(len(res.Rows()), Equals, 0) + tk.MustExec("analyze table t index idx_b") + res = tk.MustQuery("show stats_histograms where table_name = 't' and column_name = 'idx_b'") + c.Assert(len(res.Rows()), Equals, 1) } func (s *testSuite) TestShowStatsBuckets(c *C) { diff --git a/statistics/table.go b/statistics/table.go index a575a3c66b582..14238aacfa35f 100644 --- a/statistics/table.go +++ b/statistics/table.go @@ -267,7 +267,7 @@ func (t *Table) ColumnIsInvalid(sc *stmtctx.StatementContext, colID int64) bool return true } col, ok := t.Columns[colID] - if ok && col.NDV > 0 && col.Len() == 0 { + if ok && col.NDV > 0 && col.Len() == 0 && sc != nil { sc.SetHistogramsNotLoad() histogramNeededColumns.insert(tableColumnID{tableID: t.TableID, columnID: colID}) }