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

meta,executor: support show auto id #5489

Merged
merged 1 commit into from
Dec 25, 2017
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
13 changes: 11 additions & 2 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,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() {
buf.WriteString(fmt.Sprintf(" `%s` %s", col.Name.O, col.GetTypeDesc()))
if col.IsGenerated() {
Expand All @@ -424,6 +425,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 @@ -544,8 +546,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 @@ -56,6 +58,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 @@ -154,6 +168,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