From 74817dc70e35eefc890f3612ac7cc2304a26e10e Mon Sep 17 00:00:00 2001 From: dongyan <34701401+dyzsr@users.noreply.github.com> Date: Thu, 3 Dec 2020 14:43:18 +0800 Subject: [PATCH] planner: report error when UPDATE set generated column with non-default value (#21460) --- planner/core/integration_test.go | 19 +++++++++++++++++++ planner/core/logical_plan_builder.go | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 72dfc8f47ec5c..03c10f82814c9 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -1918,6 +1918,25 @@ func (s *testIntegrationSuite) TestUpdateMultiUpdatePK(c *C) { tk.MustQuery("SELECT * FROM t").Check(testkit.Rows("2 12")) } +func (s *testIntegrationSuite) TestUpdateSetDefault(c *C) { + // #20598 + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("create table tt (x int, z int as (x+10) stored)") + tk.MustExec("insert into tt(x) values (1)") + tk.MustExec("update tt set x=2, z = default") + tk.MustQuery("select * from tt").Check(testkit.Rows("2 12")) + + tk.MustGetErrMsg("update tt set z = 123", + "[planner:3105]The value specified for generated column 'z' in table 'tt' is not allowed.") + tk.MustGetErrMsg("update tt as ss set z = 123", + "[planner:3105]The value specified for generated column 'z' in table 'tt' is not allowed.") + tk.MustGetErrMsg("update tt as ss set x = 3, z = 13", + "[planner:3105]The value specified for generated column 'z' in table 'tt' is not allowed.") + tk.MustGetErrMsg("update tt as s1, tt as s2 set s1.z = default, s2.z = 456", + "[planner:3105]The value specified for generated column 'z' in table 'tt' is not allowed.") +} + func (s *testIntegrationSerialSuite) TestPreferRangeScan(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 520191f19efb8..e02e46dee1dab 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -3895,7 +3895,7 @@ func (b *PlanBuilder) buildUpdateLists( if !colInfo.IsGenerated() { continue } - columnFullName := fmt.Sprintf("%s.%s.%s", tn.Schema.L, tn.Name.L, colInfo.Name.L) + columnFullName := fmt.Sprintf("%s.%s.%s", tn.DBInfo.Name.L, tn.Name.L, colInfo.Name.L) isDefault, ok := modifyColumns[columnFullName] if ok && colInfo.Hidden { return nil, nil, false, ErrUnknownColumn.GenWithStackByArgs(colInfo.Name, clauseMsg[fieldList])