Skip to content

Commit

Permalink
meta,executor: support show auto id (#5489)
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala authored and winkyao committed Dec 25, 2017
1 parent c799664 commit ba17715
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
13 changes: 11 additions & 2 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ func (e *ShowExec) fetchShowCreateTable() error {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("CREATE TABLE `%s` (\n", tb.Meta().Name.O))
var pkCol *table.Column
var hasAutoIncID bool
for i, col := range tb.Cols() {
if col.State != model.StatePublic {
continue
Expand All @@ -455,6 +456,7 @@ func (e *ShowExec) fetchShowCreateTable() error {
}
}
if mysql.HasAutoIncrementFlag(col.Flag) {
hasAutoIncID = true
buf.WriteString(" NOT NULL AUTO_INCREMENT")
} else {
if mysql.HasNotNullFlag(col.Flag) {
Expand Down Expand Up @@ -582,8 +584,15 @@ func (e *ShowExec) fetchShowCreateTable() error {
// to make it work on MySQL server which has default collate utf8_general_ci.
buf.WriteString(fmt.Sprintf(" DEFAULT CHARSET=%s COLLATE=%s", charsetName, collate))

if tb.Meta().AutoIncID > 0 {
buf.WriteString(fmt.Sprintf(" AUTO_INCREMENT=%d", tb.Meta().AutoIncID))
if hasAutoIncID {
autoIncID, err := tb.Allocator().NextGlobalAutoID(tb.Meta().ID)
if err != nil {
return errors.Trace(err)
}
// It's campatible with MySQL.
if autoIncID > 1 {
buf.WriteString(fmt.Sprintf(" AUTO_INCREMENT=%d", autoIncID))
}
}

if len(tb.Meta().Comment) > 0 {
Expand Down
36 changes: 34 additions & 2 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
package executor_test

import (
"strconv"
"strings"

. "github.com/pingcap/check"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/plan"
"github.com/pingcap/tidb/privilege/privileges"
Expand Down Expand Up @@ -164,13 +166,43 @@ func (s *testSuite) TestShow(c *C) {
// Test show create table with AUTO_INCREMENT option
// for issue https://github.com/pingcap/tidb/issues/3747
tk.MustExec(`drop table if exists show_auto_increment`)
tk.MustExec(`create table show_auto_increment (id int) auto_increment=4`)
tk.MustExec(`create table show_auto_increment (id int key auto_increment) auto_increment=4`)
tk.MustQuery(`show create table show_auto_increment`).Check(testutil.RowsWithSep("|",
""+
"show_auto_increment CREATE TABLE `show_auto_increment` (\n"+
" `id` int(11) DEFAULT NULL\n"+
" `id` int(11) NOT NULL AUTO_INCREMENT,\n"+
" PRIMARY KEY (`id`)\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=4",
))
// for issue https://github.com/pingcap/tidb/issues/4678
autoIDStep := autoid.GetStep()
tk.MustExec("insert into show_auto_increment values(20)")
autoID := autoIDStep + 21
tk.MustQuery(`show create table show_auto_increment`).Check(testutil.RowsWithSep("|",
""+
"show_auto_increment CREATE TABLE `show_auto_increment` (\n"+
" `id` int(11) NOT NULL AUTO_INCREMENT,\n"+
" PRIMARY KEY (`id`)\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT="+strconv.Itoa(int(autoID)),
))
tk.MustExec(`drop table show_auto_increment`)
tk.MustExec(`create table show_auto_increment (id int primary key auto_increment)`)
tk.MustQuery(`show create table show_auto_increment`).Check(testutil.RowsWithSep("|",
""+
"show_auto_increment CREATE TABLE `show_auto_increment` (\n"+
" `id` int(11) NOT NULL AUTO_INCREMENT,\n"+
" PRIMARY KEY (`id`)\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin",
))
tk.MustExec("insert into show_auto_increment values(10)")
autoID = autoIDStep + 11
tk.MustQuery(`show create table show_auto_increment`).Check(testutil.RowsWithSep("|",
""+
"show_auto_increment CREATE TABLE `show_auto_increment` (\n"+
" `id` int(11) NOT NULL AUTO_INCREMENT,\n"+
" PRIMARY KEY (`id`)\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT="+strconv.Itoa(int(autoID)),
))

// Test show table with column's comment contain escape character
// for issue https://github.com/pingcap/tidb/issues/4411
Expand Down
21 changes: 21 additions & 0 deletions meta/autoid/autoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type Allocator interface {
// If allocIDs is true, it will allocate some IDs and save to the cache.
// If allocIDs is false, it will not allocate IDs.
Rebase(tableID, newBase int64, allocIDs bool) error
// NextGlobalAutoID returns the next global autoID.
NextGlobalAutoID(tableID int64) (int64, error)
}

type allocator struct {
Expand All @@ -57,6 +59,18 @@ func GetStep() int64 {
return step
}

// NextGlobalAutoID implements autoid.Allocator NextGlobalAutoID interface.
func (alloc *allocator) NextGlobalAutoID(tableID int64) (int64, error) {
var autoID int64
err := kv.RunInNewTxn(alloc.store, true, func(txn kv.Transaction) error {
var err1 error
m := meta.NewMeta(txn)
autoID, err1 = m.GetAutoTableID(alloc.dbID, tableID)
return errors.Trace(err1)
})
return autoID + 1, errors.Trace(err)
}

// Rebase implements autoid.Allocator Rebase interface.
// The requiredBase is the minimum base value after Rebase.
// The real base may be greater than the required base.
Expand Down Expand Up @@ -155,6 +169,13 @@ type memoryAllocator struct {
dbID int64
}

// NextGlobalAutoID implements autoid.Allocator NextGlobalAutoID interface.
func (alloc *memoryAllocator) NextGlobalAutoID(tableID int64) (int64, error) {
memIDLock.Lock()
defer memIDLock.Unlock()
return memID + 1, nil
}

// Rebase implements autoid.Allocator Rebase interface.
func (alloc *memoryAllocator) Rebase(tableID, newBase int64, allocIDs bool) error {
// TODO: implement it.
Expand Down
6 changes: 6 additions & 0 deletions meta/autoid/autoid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func (*testSuite) TestT(c *C) {
alloc := NewAllocator(store, 1)
c.Assert(alloc, NotNil)

globalAutoID, err := alloc.NextGlobalAutoID(1)
c.Assert(err, IsNil)
c.Assert(globalAutoID, Equals, int64(1))
id, err := alloc.Alloc(1)
c.Assert(err, IsNil)
c.Assert(id, Equals, int64(1))
Expand All @@ -69,6 +72,9 @@ func (*testSuite) TestT(c *C) {
c.Assert(id, Equals, int64(2))
id, err = alloc.Alloc(0)
c.Assert(err, NotNil)
globalAutoID, err = alloc.NextGlobalAutoID(1)
c.Assert(err, IsNil)
c.Assert(globalAutoID, Equals, int64(step+1))

// rebase
err = alloc.Rebase(1, int64(1), true)
Expand Down

0 comments on commit ba17715

Please sign in to comment.