diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 890d1a721a29c..a19b17b992e7f 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1702,15 +1702,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 { diff --git a/ddl/generated_column.go b/ddl/generated_column.go index e9ee788ff033b..c0550a292efc3 100644 --- a/ddl/generated_column.go +++ b/ddl/generated_column.go @@ -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" ) @@ -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) diff --git a/executor/show_test.go b/executor/show_test.go index 675a2ccf1901e..c7306f63cd44d 100644 --- a/executor/show_test.go +++ b/executor/show_test.go @@ -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 utf8mb4_bin 注释", createTime))) + r.Check(testkit.Rows(fmt.Sprintf("t InnoDB 10 Compact 0 0 0 0 0 0 %s utf8mb4_bin 注释", createTime))) tk.MustQuery("show databases like 'test'").Check(testkit.Rows("test")) @@ -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 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 %s utf8mb4_bin ", createTime))) } diff --git a/infoschema/infoschema.go b/infoschema/infoschema.go index 7b0b5f612cae0..9da6e2ee72dd0 100644 --- a/infoschema/infoschema.go +++ b/infoschema/infoschema.go @@ -388,3 +388,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, "" +} diff --git a/infoschema/tables.go b/infoschema/tables.go index a2ac042066c63..20386786579ca 100644 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -1072,26 +1072,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) { @@ -1162,10 +1148,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) diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index ce102e67bc1a0..c8d4e20c43984 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -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("")) tk.MustExec("create user xxx") tk.MustExec("flush privileges")