diff --git a/pkg/planner/core/collect_column_stats_usage.go b/pkg/planner/core/collect_column_stats_usage.go index 159f1556ceac5..2e30cc7496feb 100644 --- a/pkg/planner/core/collect_column_stats_usage.go +++ b/pkg/planner/core/collect_column_stats_usage.go @@ -205,18 +205,17 @@ func (c *columnStatsUsageCollector) addHistNeededColumns(ds *DataSource) { colIDSet.Insert(int(col.ID)) c.histNeededCols[tblColID] = true } - for _, col := range ds.Columns { + for _, column := range ds.tableInfo.Columns { // If the column is plan-generated one, Skip it. // TODO: we may need to consider the ExtraHandle. - if col.ID < 0 { + if column.ID < 0 { continue } - if !colIDSet.Has(int(col.ID)) && !col.Hidden { - tblColID := model.TableItemID{TableID: ds.physicalTableID, ID: col.ID, IsIndex: false} - if _, ok := c.histNeededCols[tblColID]; ok { - continue + if !column.Hidden { + tblColID := model.TableItemID{TableID: ds.physicalTableID, ID: column.ID, IsIndex: false} + if _, ok := c.histNeededCols[tblColID]; !ok { + c.histNeededCols[tblColID] = false } - c.histNeededCols[tblColID] = false } } } diff --git a/pkg/planner/core/collect_column_stats_usage_test.go b/pkg/planner/core/collect_column_stats_usage_test.go index 0ba631dfe583f..d9fb4234c874f 100644 --- a/pkg/planner/core/collect_column_stats_usage_test.go +++ b/pkg/planner/core/collect_column_stats_usage_test.go @@ -316,7 +316,7 @@ func TestCollectHistNeededColumns(t *testing.T) { }, { sql: "select b, count(a) from t where b > 1 group by b having count(a) > 2", - res: []string{"t.a meta", "t.b full"}, + res: []string{"t.a meta", "t.b full", "t.c meta", "t.c_str meta", "t.d meta", "t.d_str meta", "t.e meta", "t.e_str meta", "t.f meta", "t.g meta", "t.h meta", "t.i_date meta"}, }, { sql: "select * from t as x join t2 as y on x.b + y.b > 2 and x.c > 1 and y.a < 1", @@ -324,15 +324,15 @@ func TestCollectHistNeededColumns(t *testing.T) { }, { sql: "select * from t2 where t2.b > all(select b from t where t.c > 2)", - res: []string{"t.b meta", "t.c full", "t2.a meta", "t2.b meta", "t2.c meta"}, + res: []string{"t.a meta", "t.b meta", "t.c full", "t.c_str meta", "t.d meta", "t.d_str meta", "t.e meta", "t.e_str meta", "t.f meta", "t.g meta", "t.h meta", "t.i_date meta", "t2.a meta", "t2.b meta", "t2.c meta"}, }, { sql: "select * from t2 where t2.b > any(select b from t where t.c > 2)", - res: []string{"t.b meta", "t.c full", "t2.a meta", "t2.b meta", "t2.c meta"}, + res: []string{"t.a meta", "t.b meta", "t.c full", "t.c_str meta", "t.d meta", "t.d_str meta", "t.e meta", "t.e_str meta", "t.f meta", "t.g meta", "t.h meta", "t.i_date meta", "t2.a meta", "t2.b meta", "t2.c meta"}, }, { sql: "select * from t2 where t2.b in (select b from t where t.c > 2)", - res: []string{"t.b meta", "t.c full", "t2.a meta", "t2.b meta", "t2.c meta"}, + res: []string{"t.a meta", "t.b meta", "t.c full", "t.c_str meta", "t.d meta", "t.d_str meta", "t.e meta", "t.e_str meta", "t.f meta", "t.g meta", "t.h meta", "t.i_date meta", "t2.a meta", "t2.b meta", "t2.c meta"}, }, { pruneMode: "static",