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

Merged
merged 5 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -85,6 +85,7 @@ var _ = Suite(&testSuite1{})
var _ = Suite(&testSuite2{})
var _ = Suite(&testSuite3{})
var _ = Suite(&testBypassSuite{})
var _ = Suite(&testUpdateSuite{})

type testSuite struct {
cluster *mocktikv.Cluster
Expand Down
92 changes: 92 additions & 0 deletions executor/update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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, TestLeakCheckCnt)()
}

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);`)
err := tk.ExecToErr(`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.")
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 @@ -2347,15 +2347,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