Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: update generated column inside a transaction should return error (#8909) #8980

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func TestT(t *testing.T) {
var _ = Suite(&testSuite{})
var _ = Suite(&testContextOptionSuite{})
var _ = Suite(&testBypassSuite{})
var _ = Suite(&testUpdateSuite{})

type testSuite struct {
cluster *mocktikv.Cluster
Expand Down
93 changes: 93 additions & 0 deletions executor/update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package executor_test

import (
"flag"
"fmt"

. "github.com/pingcap/check"
"github.com/pingcap/parser"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/mockstore/mocktikv"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testleak"
)

type testUpdateSuite struct {
cluster *mocktikv.Cluster
mvccStore mocktikv.MVCCStore
store kv.Storage
domain *domain.Domain
*parser.Parser
ctx *mock.Context
}

func (s *testUpdateSuite) SetUpSuite(c *C) {
testleak.BeforeTest()
s.Parser = parser.New()
flag.Lookup("mockTikv")
useMockTikv := *mockTikv
if useMockTikv {
s.cluster = mocktikv.NewCluster()
mocktikv.BootstrapWithSingleStore(s.cluster)
s.mvccStore = mocktikv.MustNewMVCCStore()
store, err := mockstore.NewMockTikvStore(
mockstore.WithCluster(s.cluster),
mockstore.WithMVCCStore(s.mvccStore),
)
c.Assert(err, IsNil)
s.store = store
session.SetSchemaLease(0)
session.SetStatsLease(0)
}
d, err := session.BootstrapSession(s.store)
c.Assert(err, IsNil)
d.SetStatsUpdating(true)
s.domain = d
}

func (s *testUpdateSuite) TearDownSuite(c *C) {
s.domain.Close()
s.store.Close()
testleak.AfterTest(c)
}

func (s *testUpdateSuite) TearDownTest(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
r := tk.MustQuery("show tables")
for _, tb := range r.Rows() {
tableName := tb[0]
tk.MustExec(fmt.Sprintf("drop table %v", tableName))
}
}

func (s *testUpdateSuite) TestUpdateGenColInTxn(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec(`create table t(a bigint, b bigint as (a+1));`)
tk.MustExec(`begin;`)
tk.MustExec(`insert into t(a) values(1);`)
rs, err := tk.Exec(`update t set b=6 where b=2;`)
c.Assert(err.Error(), Equals, "[planner:3105]The value specified for generated column 'b' in table 't' is not allowed.")
c.Assert(rs, IsNil)
tk.MustExec(`commit;`)
tk.MustQuery(`select * from t;`).Check(testkit.Rows(
`1 2`))
}
28 changes: 20 additions & 8 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2214,15 +2214,27 @@ func extractTableAsNameForUpdate(p LogicalPlan, asNames map[*model.TableInfo][]*
asNames[x.tableInfo] = append(asNames[x.tableInfo], alias)
}
case *LogicalProjection:
if x.calculateGenCols {
ds := x.Children()[0].(*DataSource)
alias := extractTableAlias(x)
if alias != nil {
if _, ok := asNames[ds.tableInfo]; !ok {
asNames[ds.tableInfo] = make([]*model.CIStr, 0, 1)
}
asNames[ds.tableInfo] = append(asNames[ds.tableInfo], alias)
if !x.calculateGenCols {
return
}

ds, isDS := x.Children()[0].(*DataSource)
if !isDS {
// try to extract the DataSource below a LogicalUnionScan.
if us, isUS := x.Children()[0].(*LogicalUnionScan); isUS {
ds, isDS = us.Children()[0].(*DataSource)
}
}
if !isDS {
return
}

alias := extractTableAlias(x)
if alias != nil {
if _, ok := asNames[ds.tableInfo]; !ok {
asNames[ds.tableInfo] = make([]*model.CIStr, 0, 1)
}
asNames[ds.tableInfo] = append(asNames[ds.tableInfo], alias)
}
default:
for _, child := range p.Children() {
Expand Down