diff --git a/executor/builder.go b/executor/builder.go index aed3694fdde3a..15437ac3bf5c0 100644 --- a/executor/builder.go +++ b/executor/builder.go @@ -1228,7 +1228,7 @@ func (b *executorBuilder) buildUpdate(v *plan.Update) Executor { b.err = errors.Trace(b.err) return nil } - columns2Handle := buildColumns2Handle(v.Schema(), tblID2table) + columns2Handle := buildColumns2Handle(v.SelectPlan.Schema(), tblID2table) updateExec := &UpdateExec{ baseExecutor: newBaseExecutor(b.ctx, nil, v.ExplainID(), selExec), SelectExec: selExec, diff --git a/executor/executor_test.go b/executor/executor_test.go index 926a3e3f40afc..039bb2cdb53d0 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -3024,7 +3024,7 @@ func (s *testSuite) TestUnionAutoSignedCast(c *C) { func (s *testSuite) TestUpdateJoin(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") - tk.MustExec("drop table if exists t1, t2, t3, t4, t5") + tk.MustExec("drop table if exists t1, t2, t3, t4, t5, t6, t7") tk.MustExec("create table t1(k int, v int)") tk.MustExec("create table t2(k int, v int)") tk.MustExec("create table t3(id int auto_increment, k int, v int, primary key(id))") @@ -3032,6 +3032,8 @@ func (s *testSuite) TestUpdateJoin(c *C) { tk.MustExec("create table t5(v int, k int, primary key(k))") tk.MustExec("insert into t1 values (1, 1)") tk.MustExec("insert into t4 values (3, 3)") + tk.MustExec("create table t6 (id int, v longtext)") + tk.MustExec("create table t7 (x int, id int, v longtext, primary key(id))") // test the normal case that update one row for a single table. tk.MustExec("update t1 set v = 0 where k = 1") @@ -3086,6 +3088,10 @@ func (s *testSuite) TestUpdateJoin(c *C) { tk.MustQuery("select k, v from t1").Check(testkit.Rows(" 2")) tk.MustQuery("select k, v from t5").Check(testkit.Rows("0 0")) + tk.MustExec("insert into t6 values (1, NULL)") + tk.MustExec("insert into t7 values (5, 1, 'a')") + tk.MustExec("update t6, t7 set t6.v = t7.v where t6.id = t7.id and t7.x = 5") + tk.MustQuery("select v from t6").Check(testkit.Rows("a")) } func (s *testSuite) TestMaxOneRow(c *C) {