Skip to content

Commit 128052d

Browse files
eurekakazz-jason
authored andcommitted
executor: only show valid columns in stats_histogram (#9487) (#9503)
1 parent b92404e commit 128052d

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

executor/show_stats.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ func (e *ShowExec) fetchShowStatsHistogram() error {
5454
statsTbl := h.GetTableStats(tbl)
5555
if !statsTbl.Pseudo {
5656
for _, col := range statsTbl.Columns {
57+
// Pass a nil StatementContext to avoid column stats being marked as needed.
58+
if statsTbl.ColumnIsInvalid(nil, col.ID) {
59+
continue
60+
}
5761
e.histogramToRow(db.Name.O, tbl.Name.O, col.Info.Name.O, 0, col.Histogram, col.AvgColSize(statsTbl.Count))
5862
}
5963
for _, idx := range statsTbl.Indices {

executor/show_stats_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,26 @@ func (s *testSuite) TestShowStatsHistograms(c *C) {
4141
tk.MustExec("drop table if exists t")
4242
tk.MustExec("create table t (a int, b int)")
4343
tk.MustExec("analyze table t")
44-
result := tk.MustQuery("show stats_histograms").Sort()
44+
result := tk.MustQuery("show stats_histograms")
45+
c.Assert(len(result.Rows()), Equals, 0)
46+
tk.MustExec("insert into t values(1,1)")
47+
tk.MustExec("analyze table t")
48+
result = tk.MustQuery("show stats_histograms").Sort()
4549
c.Assert(len(result.Rows()), Equals, 2)
4650
c.Assert(result.Rows()[0][2], Equals, "a")
4751
c.Assert(result.Rows()[1][2], Equals, "b")
4852
result = tk.MustQuery("show stats_histograms where column_name = 'a'")
4953
c.Assert(len(result.Rows()), Equals, 1)
5054
c.Assert(result.Rows()[0][2], Equals, "a")
55+
56+
tk.MustExec("drop table t")
57+
tk.MustExec("create table t(a int, b int, c int, index idx_b(b), index idx_c_a(c, a))")
58+
tk.MustExec("insert into t values(1,null,1),(2,null,2),(3,3,3),(4,null,4),(null,null,null)")
59+
res := tk.MustQuery("show stats_histograms where table_name = 't'")
60+
c.Assert(len(res.Rows()), Equals, 0)
61+
tk.MustExec("analyze table t index idx_b")
62+
res = tk.MustQuery("show stats_histograms where table_name = 't' and column_name = 'idx_b'")
63+
c.Assert(len(res.Rows()), Equals, 1)
5164
}
5265

5366
func (s *testSuite) TestShowStatsBuckets(c *C) {

statistics/table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func (t *Table) ColumnIsInvalid(sc *stmtctx.StatementContext, colID int64) bool
267267
return true
268268
}
269269
col, ok := t.Columns[colID]
270-
if ok && col.NDV > 0 && col.Len() == 0 {
270+
if ok && col.NDV > 0 && col.Len() == 0 && sc != nil {
271271
sc.SetHistogramsNotLoad()
272272
histogramNeededColumns.insert(tableColumnID{tableID: t.TableID, columnID: colID})
273273
}

0 commit comments

Comments
 (0)