Skip to content

Commit

Permalink
planner/core: force first column used if no columns are required in c…
Browse files Browse the repository at this point in the history
…olumn pruning (pingcap#9125)
  • Loading branch information
jarvys committed Mar 16, 2019
1 parent 20463d6 commit ae92e5b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
18 changes: 18 additions & 0 deletions planner/core/cbo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
))
}
7 changes: 7 additions & 0 deletions planner/core/rule_column_pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]...)
Expand Down

0 comments on commit ae92e5b

Please sign in to comment.