diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index a0c31fd7d6769..c1ad241f45913 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -3786,6 +3786,22 @@ func (s *testIntegrationSuite) TestGroupBySetVar(c *C) { } } +func (s *testIntegrationSuite) TestIssue29705(c *C) { + tk := testkit.NewTestKit(c, s.store) + origin := tk.MustQuery("SELECT @@session.tidb_partition_prune_mode") + originStr := origin.Rows()[0][0].(string) + defer func() { + tk.MustExec("set @@session.tidb_partition_prune_mode = '" + originStr + "'") + }() + tk.MustExec("set @@session.tidb_partition_prune_mode = 'static'") + tk.MustExec("use test") + tk.MustExec("drop table if exists t;") + tk.MustExec("create table t(id int) partition by hash(id) partitions 4;") + tk.MustExec("insert into t values(1);") + result := tk.MustQuery("SELECT COUNT(1) FROM ( SELECT COUNT(1) FROM t b GROUP BY id) a;") + result.Check(testkit.Rows("1")) +} + func (s *testIntegrationSuite) TestIssues29711(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") @@ -3824,6 +3840,12 @@ func (s *testIntegrationSuite) TestIssues29711(c *C) { func (s *testIntegrationSuite) TestIssue27797(c *C) { tk := testkit.NewTestKit(c, s.store) + origin := tk.MustQuery("SELECT @@session.tidb_partition_prune_mode") + originStr := origin.Rows()[0][0].(string) + defer func() { + tk.MustExec("set @@session.tidb_partition_prune_mode = '" + originStr + "'") + }() + tk.MustExec("set @@session.tidb_partition_prune_mode = 'static'") tk.MustExec("use test") tk.MustExec("drop table if exists t27797") tk.MustExec("create table t27797(a int, b int, c int, d int) " + diff --git a/planner/core/rule_column_pruning.go b/planner/core/rule_column_pruning.go index 2ec6665396b7f..333d7e9c5f6ad 100644 --- a/planner/core/rule_column_pruning.go +++ b/planner/core/rule_column_pruning.go @@ -239,6 +239,23 @@ func (p *LogicalUnionAll) PruneColumns(parentUsedCols []*expression.Column) erro p.schema.Columns = append(p.schema.Columns[:i], p.schema.Columns[i+1:]...) } } + // It's possible that the child operator adds extra columns to the schema. + // Currently, (*LogicalAggregation).PruneColumns() might do this. + // But we don't need such columns, so we add an extra Projection to prune this column when this happened. + for i, child := range p.Children() { + if p.schema.Len() < child.Schema().Len() { + schema := p.schema.Clone() + exprs := make([]expression.Expression, len(p.schema.Columns)) + for j, col := range schema.Columns { + exprs[j] = col + } + proj := LogicalProjection{Exprs: exprs, AvoidColumnEvaluator: true}.Init(p.ctx, p.blockOffset) + proj.SetSchema(schema) + + proj.SetChildren(child) + p.children[i] = proj + } + } } return nil }