Skip to content

Commit

Permalink
infoschema: fix compatibility of auto_increment column value o… (#10207)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored and zz-jason committed Jul 23, 2019
1 parent 435a912 commit 88db7ff
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 35 deletions.
9 changes: 0 additions & 9 deletions ddl/ddl_api.go
Expand Up @@ -1727,15 +1727,6 @@ func handleTableOptions(options []*ast.TableOption, tbInfo *model.TableInfo) err
return nil
}

func hasAutoIncrementColumn(tbInfo *model.TableInfo) (bool, string) {
for _, col := range tbInfo.Columns {
if mysql.HasAutoIncrementFlag(col.Flag) {
return true, col.Name.L
}
}
return false, ""
}

// isIgnorableSpec checks if the spec type is ignorable.
// Some specs are parsed by ignored. This is for compatibility.
func isIgnorableSpec(tp ast.AlterTableType) bool {
Expand Down
3 changes: 2 additions & 1 deletion ddl/generated_column.go
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/model"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/table"
)

Expand Down Expand Up @@ -238,7 +239,7 @@ func checkIndexOrStored(tbl table.Table, oldCol, newCol *table.Column) error {
// checkAutoIncrementRef checks if an generated column depends on an auto-increment column and raises an error if so.
// See https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html for details.
func checkAutoIncrementRef(name string, dependencies map[string]struct{}, tbInfo *model.TableInfo) error {
exists, autoIncrementColumn := hasAutoIncrementColumn(tbInfo)
exists, autoIncrementColumn := infoschema.HasAutoIncrementColumn(tbInfo)
if exists {
if _, found := dependencies[autoIncrementColumn]; found {
return ErrGeneratedColumnRefAutoInc.GenWithStackByArgs(name)
Expand Down
4 changes: 2 additions & 2 deletions executor/show_test.go
Expand Up @@ -267,7 +267,7 @@ func (s *testSuite2) TestShow2(c *C) {
tk.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: "192.168.0.1", AuthUsername: "root", AuthHostname: "%"}, nil, []byte("012345678901234567890"))

r := tk.MustQuery("show table status from test like 't'")
r.Check(testkit.Rows(fmt.Sprintf("t InnoDB 10 Compact 0 0 0 0 0 0 0 %s <nil> <nil> utf8mb4_bin 注释", createTime)))
r.Check(testkit.Rows(fmt.Sprintf("t InnoDB 10 Compact 0 0 0 0 0 0 <nil> %s <nil> <nil> utf8mb4_bin 注释", createTime)))

tk.MustQuery("show databases like 'test'").Check(testkit.Rows("test"))

Expand Down Expand Up @@ -346,7 +346,7 @@ func (s *testSuite2) TestUnprivilegedShow(c *C) {
c.Assert(err, IsNil)
createTime := model.TSConvert2Time(tblInfo.Meta().UpdateTS).Format("2006-01-02 15:04:05")

tk.MustQuery("show table status from testshow").Check(testkit.Rows(fmt.Sprintf("t1 InnoDB 10 Compact 0 0 0 0 0 0 0 %s <nil> <nil> utf8mb4_bin ", createTime)))
tk.MustQuery("show table status from testshow").Check(testkit.Rows(fmt.Sprintf("t1 InnoDB 10 Compact 0 0 0 0 0 0 <nil> %s <nil> <nil> utf8mb4_bin ", createTime)))

}

Expand Down
10 changes: 10 additions & 0 deletions infoschema/infoschema.go
Expand Up @@ -411,3 +411,13 @@ func IsMemoryDB(dbName string) bool {
}
return false
}

// HasAutoIncrementColumn checks whether the table has auto_increment columns, if so, return true and the column name.
func HasAutoIncrementColumn(tbInfo *model.TableInfo) (bool, string) {
for _, col := range tbInfo.Columns {
if mysql.HasAutoIncrementFlag(col.Flag) {
return true, col.Name.L
}
}
return false, ""
}
35 changes: 13 additions & 22 deletions infoschema/tables.go
Expand Up @@ -1082,26 +1082,12 @@ func (c *statsCache) get(ctx sessionctx.Context) (map[int64]uint64, map[tableHis
}

func getAutoIncrementID(ctx sessionctx.Context, schema *model.DBInfo, tblInfo *model.TableInfo) (int64, error) {
hasAutoIncID := false
for _, col := range tblInfo.Cols() {
if mysql.HasAutoIncrementFlag(col.Flag) {
hasAutoIncID = true
break
}
}
autoIncID := tblInfo.AutoIncID
if hasAutoIncID {
is := ctx.GetSessionVars().TxnCtx.InfoSchema.(InfoSchema)
tbl, err := is.TableByName(schema.Name, tblInfo.Name)
if err != nil {
return 0, err
}
autoIncID, err = tbl.Allocator(ctx).NextGlobalAutoID(tblInfo.ID)
if err != nil {
return 0, err
}
is := ctx.GetSessionVars().TxnCtx.InfoSchema.(InfoSchema)
tbl, err := is.TableByName(schema.Name, tblInfo.Name)
if err != nil {
return 0, err
}
return autoIncID, nil
return tbl.Allocator(ctx).Base() + 1, nil
}

func dataForViews(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.Datum, error) {
Expand Down Expand Up @@ -1172,10 +1158,15 @@ func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.D
if table.GetPartitionInfo() != nil {
createOptions = "partitioned"
}
autoIncID, err := getAutoIncrementID(ctx, schema, table)
if err != nil {
return nil, err
var autoIncID interface{}
hasAutoIncID, _ := HasAutoIncrementColumn(table)
if hasAutoIncID {
autoIncID, err = getAutoIncrementID(ctx, schema, table)
if err != nil {
return nil, err
}
}

rowCount := tableRowsMap[table.ID]
dataLength, indexLength := getDataAndIndexLength(table, rowCount, colLengthMap)
avgRowLength := uint64(0)
Expand Down
16 changes: 15 additions & 1 deletion infoschema/tables_test.go
Expand Up @@ -84,7 +84,21 @@ func (s *testTableSuite) TestInfoschemaFieldValue(c *C) {
testkit.Rows("1"))
tk.MustExec("insert into t(c, d) values(1, 1)")
tk.MustQuery("select auto_increment from information_schema.tables where table_name='t'").Check(
testkit.Rows("30002"))
testkit.Rows("2"))

tk.MustQuery("show create table t").Check(
testkit.Rows("" +
"t CREATE TABLE `t` (\n" +
" `c` int(11) NOT NULL AUTO_INCREMENT,\n" +
" `d` int(11) DEFAULT NULL,\n" +
" PRIMARY KEY (`c`)\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=30002"))

// Test auto_increment for table without auto_increment column
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (d int)")
tk.MustQuery("select auto_increment from information_schema.tables where table_name='t'").Check(
testkit.Rows("<nil>"))

tk.MustExec("create user xxx")
tk.MustExec("flush privileges")
Expand Down

0 comments on commit 88db7ff

Please sign in to comment.