Skip to content

Commit

Permalink
Merge branch 'release-5.0' into release-5.0-4947cf1f596d
Browse files Browse the repository at this point in the history
  • Loading branch information
wshwsh12 authored Dec 20, 2021
2 parents 2fa499c + 49117ed commit 3117d02
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
15 changes: 12 additions & 3 deletions executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,10 @@ func (e *InsertValues) adjustAutoIncrementDatum(ctx context.Context, d types.Dat
if retryInfo.Retrying {
id, ok := retryInfo.GetCurrAutoIncrementID()
if ok {
d.SetAutoID(id, c.Flag)
err := setDatumAutoIDAndCast(e.ctx, &d, id, c)
if err != nil {
return types.Datum{}, err
}
return d, nil
}
}
Expand Down Expand Up @@ -851,7 +854,10 @@ func (e *InsertValues) adjustAutoRandomDatum(ctx context.Context, d types.Datum,
if retryInfo.Retrying {
autoRandomID, ok := retryInfo.GetCurrAutoRandomID()
if ok {
d.SetAutoID(autoRandomID, c.Flag)
err := setDatumAutoIDAndCast(e.ctx, &d, autoRandomID, c)
if err != nil {
return types.Datum{}, err
}
return d, nil
}
}
Expand All @@ -877,7 +883,10 @@ func (e *InsertValues) adjustAutoRandomDatum(ctx context.Context, d types.Datum,
return types.Datum{}, err
}
e.ctx.GetSessionVars().StmtCtx.InsertID = uint64(recordID)
d.SetAutoID(recordID, c.Flag)
err = setDatumAutoIDAndCast(e.ctx, &d, recordID, c)
if err != nil {
return types.Datum{}, err
}
retryInfo.AddAutoRandomID(recordID)
return d, nil
}
Expand Down
30 changes: 30 additions & 0 deletions executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1597,3 +1597,33 @@ func (s *testSuite10) TestBinaryLiteralInsertToSet(c *C) {
tk.MustExec("insert into bintest(h) values(0x61)")
tk.MustQuery("select * from bintest").Check(testkit.Rows("a"))
}

// TestInsertIssue29892 test the double type with auto_increment problem, just leverage the serial test suite.
func (s *testSerialSuite) TestInsertIssue29892(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test`)

tk.MustExec("set global tidb_txn_mode='optimistic';")
tk.MustExec("set global tidb_disable_txn_auto_retry=false;")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a double auto_increment key, b int)")
tk.MustExec("insert into t values (146576794, 1)")

tk1 := testkit.NewTestKit(c, s.store)
tk1.MustExec(`use test`)
tk1.MustExec("begin")
tk1.MustExec("insert into t(b) select 1")

tk2 := testkit.NewTestKit(c, s.store)
tk2.MustExec(`use test`)
tk2.MustExec("begin")
tk2.MustExec("insert into t values (146576795, 1)")
tk2.MustExec("insert into t values (146576796, 1)")
tk2.MustExec("commit")

// since the origin auto-id (146576795) is cached in retryInfo, it will be fetched again to do the retry again,
// which will duplicate with what has been inserted in tk1.
_, err := tk1.Exec("commit")
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), "Write conflict"), Equals, true)
}
6 changes: 4 additions & 2 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,8 @@ func collectVisitInfoFromRevokeStmt(sctx sessionctx.Context, vi []visitInfo, stm
// and you must have the privileges that you are granting.
dbName := stmt.Level.DBName
tableName := stmt.Level.TableName
if dbName == "" {
// do not add dbName to the visitInfo of a *.* grant.
if dbName == "" && stmt.Level.Level != ast.GrantLevelGlobal {
dbName = sctx.GetSessionVars().CurrentDB
}
vi = appendVisitInfo(vi, mysql.GrantPriv, dbName, tableName, "", nil)
Expand Down Expand Up @@ -2304,7 +2305,8 @@ func collectVisitInfoFromGrantStmt(sctx sessionctx.Context, vi []visitInfo, stmt
// and you must have the privileges that you are granting.
dbName := stmt.Level.DBName
tableName := stmt.Level.TableName
if dbName == "" {
// do not add dbName to the visitInfo of a *.* grant.
if dbName == "" && stmt.Level.Level != ast.GrantLevelGlobal {
dbName = sctx.GetSessionVars().CurrentDB
}
vi = appendVisitInfo(vi, mysql.GrantPriv, dbName, tableName, "", nil)
Expand Down
11 changes: 11 additions & 0 deletions privilege/privileges/privileges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,17 @@ func (s *testPrivilegeSuite) TestCheckAuthenticate(c *C) {
mustExec(c, se1, "drop user 'r3@example.com'@'localhost'")
}

func (s *testPrivilegeSuite) TestGlobalGrantInfoDB(c *C) {
se := newSession(c, s.store, "INFORMATION_SCHEMA")
//mustExec(c, se, "USE information_schema'")
mustExec(c, se, "CREATE USER 'global'")
// can be executed in information schema for *.*
_, e := se.Execute(context.Background(), "GRANT all privileges ON *.* TO 'global'")
c.Assert(e, IsNil)
_, e = se.Execute(context.Background(), "REVOKE all privileges on *.* FROM 'global'")
c.Assert(e, IsNil)
}

func (s *testPrivilegeSuite) TestUseDB(c *C) {

se := newSession(c, s.store, s.dbName)
Expand Down
1 change: 1 addition & 0 deletions types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ func (d *Datum) GetRaw() []byte {
}

// SetAutoID set the auto increment ID according to its int flag.
// Don't use it directly, useless wrapped with setDatumAutoIDAndCast.
func (d *Datum) SetAutoID(id int64, flag uint) {
if mysql.HasUnsignedFlag(flag) {
d.SetUint64(uint64(id))
Expand Down

0 comments on commit 3117d02

Please sign in to comment.