diff --git a/executor/executor_test.go b/executor/executor_test.go index 50cf1d048494f..90b38d5467497 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