From 5210b6ef0f38731a8805ee4621eba92105c26d01 Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Thu, 11 Jul 2019 20:26:03 +0800 Subject: [PATCH] planner: fix bug when pruning columns for TableDual (#11054) --- executor/executor_test.go | 3 +++ planner/core/rule_column_pruning.go | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/executor/executor_test.go b/executor/executor_test.go index cc3f17253a4e9..35ec54dd3ae90 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -1853,6 +1853,9 @@ func (s *testSuite) TestTableDual(c *C) { result.Check(testkit.Rows("1")) result = tk.MustQuery("Select 1 from dual where 1") result.Check(testkit.Rows("1")) + + tk.MustExec("create table t(a int primary key)") + tk.MustQuery("select t1.* from t t1, t t2 where t1.a=t2.a and 1=0").Check(testkit.Rows()) } func (s *testSuite) TestTableScan(c *C) { diff --git a/planner/core/rule_column_pruning.go b/planner/core/rule_column_pruning.go index 6a5a278921388..1f4873618203f 100644 --- a/planner/core/rule_column_pruning.go +++ b/planner/core/rule_column_pruning.go @@ -262,8 +262,15 @@ func (p *LogicalTableDual) PruneColumns(parentUsedCols []*expression.Column) err } } for k, cols := range p.schema.TblID2Handle { - if p.schema.ColumnIndex(cols[0]) == -1 { + for i := len(cols) - 1; i >= 0; i-- { + if p.schema.ColumnIndex(cols[i]) == -1 { + cols = append(cols[:i], cols[i+1:]...) + } + } + if len(cols) == 0 { delete(p.schema.TblID2Handle, k) + } else { + p.schema.TblID2Handle[k] = cols } } return nil