diff --git a/executor/prepared_test.go b/executor/prepared_test.go index 0a919a904980e..64c74a90ad7e8 100644 --- a/executor/prepared_test.go +++ b/executor/prepared_test.go @@ -263,4 +263,7 @@ func (s *testSuite) TestPreparedNameResolver(c *C) { tk.MustExec("prepare stmt from 'select * from t limit ? offset ?'") _, err := tk.Exec("prepare stmt from 'select b from t'") c.Assert(err.Error(), Equals, "[planner:1054]Unknown column 'b' in 'field list'") + + _, err = tk.Exec("prepare stmt from '(select * FROM t) union all (select * FROM t) order by a limit ?'") + c.Assert(err.Error(), Equals, "[planner:1054]Unknown column 'a' in 'order clause'") } diff --git a/plan/logical_plan_builder.go b/plan/logical_plan_builder.go index dc10ceb4f278e..36723e7d7dfb7 100644 --- a/plan/logical_plan_builder.go +++ b/plan/logical_plan_builder.go @@ -701,9 +701,17 @@ func (b *planBuilder) buildUnion(union *ast.UnionStmt) LogicalPlan { if union.OrderBy != nil { unionPlan = b.buildSort(unionPlan, union.OrderBy.Items, nil) + if b.err != nil { + b.err = errors.Trace(b.err) + return nil + } } if union.Limit != nil { unionPlan = b.buildLimit(unionPlan, union.Limit) + if b.err != nil { + b.err = errors.Trace(b.err) + return nil + } } return unionPlan }