Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: fix inconsistent schema between UnionAll and child operator #30231

Merged
merged 11 commits into from
Dec 1, 2021
22 changes: 22 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4713,6 +4713,12 @@ func (s *testIntegrationSerialSuite) TestPushDownGroupConcatToTiFlash(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) " +
Expand Down Expand Up @@ -4897,3 +4903,19 @@ func (s *testIntegrationSuite) TestIssue30094(c *C) {
" └─TableFullScan 10000.00 cop[tikv] table:t30094 keep order:false, stats:pseudo",
))
}

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"))
}
19 changes: 19 additions & 0 deletions planner/core/rule_column_pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,25 @@ 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.
time-and-fate marked this conversation as resolved.
Show resolved Hide resolved
// 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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p.schema is adjusted with child[0] . Will it happens that p.schema.Len() > child[1].Schema().Len() ?

// Copy and use the Union's schema as the Projection's Exprs and schema.
newCols := make([]expression.Column, len(p.schema.Columns))
exprs := make([]expression.Expression, len(p.schema.Columns))
for j, col := range p.schema.Columns {
newCols[j] = *col
time-and-fate marked this conversation as resolved.
Show resolved Hide resolved
exprs[j] = &newCols[j]
}
proj := LogicalProjection{Exprs: exprs, AvoidColumnEvaluator: true}.Init(p.ctx, p.blockOffset)
proj.SetSchema(p.schema.Clone())

proj.SetChildren(child)
p.children[i] = proj
}
}
}
return nil
}
Expand Down