diff --git a/planner/core/cbo_test.go b/planner/core/cbo_test.go index a29fb980a07d7..4b8ceefd1e3df 100644 --- a/planner/core/cbo_test.go +++ b/planner/core/cbo_test.go @@ -924,3 +924,21 @@ func (s *testAnalyzeSuite) TestIssue9562(c *C) { " └─TableScan_13 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo", )) } + +func (s *testAnalyzeSuite) TestIssue9125(c *C) { + defer testleak.AfterTest(c)() + store, dom, err := newStoreWithBootstrap() + c.Assert(err, IsNil) + tk := testkit.NewTestKit(c, store) + defer func() { + dom.Close() + store.Close() + }() + + tk.MustExec("use test") + tk.MustExec("create table t1(name varchar(100) DEFAULT NULL)") + tk.MustExec("insert into t1(name) values('test')") + tk.MustQuery("select count(1) from (select count(1) from (select * from t1 where name='test') t) t2").Check(testkit.Rows( + "1", + )) +} diff --git a/planner/core/rule_column_pruning.go b/planner/core/rule_column_pruning.go index 34f74d1eca006..f3ae618fa41e0 100644 --- a/planner/core/rule_column_pruning.go +++ b/planner/core/rule_column_pruning.go @@ -105,6 +105,13 @@ func (la *LogicalAggregation) PruneColumns(parentUsedCols []*expression.Column) return err } + if len(parentUsedCols) == 0 && len(used) > 0 { + // For sql like `select count(1) from (select count(1) from t)`, no column used. + // tikv's response will be empty if the inner aggr is pushed down. + // We should force some column to be used. + used[0] = true + } + for i := len(used) - 1; i >= 0; i-- { if !used[i] { la.schema.Columns = append(la.schema.Columns[:i], la.schema.Columns[i+1:]...)