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

infoschema: Support for showing "AUTO_INCREMENT" in "information_schema.tables" #7037

Merged
merged 3 commits into from
Jul 14, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 28 additions & 1 deletion infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,29 @@ func getRowCountAllTable(ctx sessionctx.Context) (map[int64]uint64, error) {
return rowCountMap, nil
}

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can break here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zimulala Please change here.

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, errors.Trace(err)
}
autoIncID, err = tbl.Allocator(ctx).NextGlobalAutoID(tblInfo.ID)
if err != nil {
return 0, errors.Trace(err)
}
}
return autoIncID, nil
}

func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.Datum, error) {
tableRowsMap, err := getRowCountAllTable(ctx)
if err != nil {
Expand All @@ -676,6 +699,10 @@ func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.D
continue
}

autoIncID, err := getAutoIncrementID(ctx, schema, table)
if err != nil {
return nil, errors.Trace(err)
}
record := types.MakeDatums(
catalogVal, // TABLE_CATALOG
schema.Name.O, // TABLE_SCHEMA
Expand All @@ -690,7 +717,7 @@ func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.D
uint64(0), // MAX_DATA_LENGTH
uint64(0), // INDEX_LENGTH
uint64(0), // DATA_FREE
table.AutoIncID, // AUTO_INCREMENT
autoIncID, // AUTO_INCREMENT
createTime, // CREATE_TIME
nil, // UPDATE_TIME
nil, // CHECK_TIME
Expand Down
9 changes: 9 additions & 0 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ func (s *testSuite) TestDataForTableRowsCountField(c *C) {
tk.MustQuery("select table_rows from information_schema.tables where table_name='t'").Check(
testkit.Rows("2"))

// Test for auto increment ID.
tk.MustExec("drop table t")
tk.MustExec("create table t (c int auto_increment primary key, d int)")
tk.MustQuery("select auto_increment from information_schema.tables where table_name='t'").Check(
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"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this return 30002?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zimulala Allocate a batch of 30000 AUTO_INCREMENT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@XuHuaiyu
Because the next global auto ID is 30002.
We can do some operations as follows:
TiDB 1

tidb> create table t(`id` int(11) NOT NULL AUTO_INCREMENT,`c` int(11) DEFAULT NULL, PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.13 sec)

tidb> insert into t values(1,1);
Query OK, 1 row affected (0.14 sec)

tidb> insert into t values(10000,1);
Query OK, 1 row affected (0.02 sec)

tidb> select * from t;
+-------+------+
| id    | c    |
+-------+------+
|     1 |    1 |
| 10000 |    1 |
+-------+------+
2 rows in set (0.01 sec)

TiDB 2
Then we do the operation as follows:

tidb> insert into t values();
Query OK, 1 row affected (0.13 sec)

tidb> select * from t;
+-------+------+
| id    | c    |
+-------+------+
|     1 |    1 |
| 10000 |    1 |
| 30002 | NULL |
+-------+------+
3 rows in set (0.01 sec)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ciscoxll
Yes.


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

Expand Down