diff --git a/planner/core/physical_plan_test.go b/planner/core/physical_plan_test.go index 458fe62fba065..3d1bce80e281d 100644 --- a/planner/core/physical_plan_test.go +++ b/planner/core/physical_plan_test.go @@ -198,6 +198,10 @@ func (s *testPlanSuite) TestDAGPlanBuilderSimpleCase(c *C) { sql: "select * from ((SELECT 1 a,6 b) UNION (SELECT 2,5) UNION (SELECT 2, 4) ORDER BY 1) t order by 1, 2", best: "UnionAll{Dual->Projection->Dual->Projection->Dual->Projection}->HashAgg->Sort->Sort", }, + { + sql: "select * from (select *, NULL as xxx from t) t order by xxx", + best: "TableReader(Table(t))->Projection", + }, } for i, tt := range tests { comment := Commentf("case:%v sql:%s", i, tt.sql) diff --git a/planner/core/rule_column_pruning.go b/planner/core/rule_column_pruning.go index 51e99cf7c71f7..508797b27958f 100644 --- a/planner/core/rule_column_pruning.go +++ b/planner/core/rule_column_pruning.go @@ -125,6 +125,8 @@ func (ls *LogicalSort) PruneColumns(parentUsedCols []*expression.Column) { continue } ls.ByItems = append(ls.ByItems[:i], ls.ByItems[i+1:]...) + } else if ls.ByItems[i].Expr.GetType().Tp == mysql.TypeNull { + ls.ByItems = append(ls.ByItems[:i], ls.ByItems[i+1:]...) } else { parentUsedCols = append(parentUsedCols, cols...) }