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

ddl: support batch create table #28763

Merged
merged 29 commits into from
Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
17a8270
ddl: add batch create table api
xhebox Oct 12, 2021
61077fa
ddl: add unit tests
xhebox Oct 12, 2021
30e7143
ddl: fix fmt
xhebox Oct 12, 2021
b2fa840
ddl: typo
xhebox Oct 18, 2021
3ca6acd
ddl: fix tests
xhebox Oct 19, 2021
833d3b7
ddl: rename to BatchCreateTableWithInfo
xhebox Oct 20, 2021
ee0446f
ddl: trace the error
xhebox Oct 20, 2021
5651062
ddl: comments
xhebox Oct 20, 2021
b8d3d2c
ddl: cancle the job right
xhebox Oct 20, 2021
2bb609c
ddl: cancel the job right 2
xhebox Oct 25, 2021
fdb11a2
ddl: report error if entry too large
xhebox Oct 26, 2021
a0dc4ad
ddl: report error when table is duplicated
xhebox Oct 27, 2021
b912d73
ddl: go fmt
xhebox Oct 27, 2021
aada00e
ddl: retain ID
xhebox Oct 29, 2021
9c91408
ddl: reduce log frequency
xhebox Dec 15, 2021
444e65c
ddl: fix tests
xhebox Dec 15, 2021
543aa18
ddl: remove retainID from the interface
xhebox Dec 22, 2021
0083958
ddl: fix tests
xhebox Dec 22, 2021
87c5a3c
executor: fix rebasing problem
xhebox Dec 27, 2021
0a039ef
ddl: address comments
xhebox Dec 31, 2021
319fbda
parser: remove useless struct
xhebox Dec 31, 2021
ed97b5b
Merge branch 'master' into batch_1
ti-chi-bot Dec 31, 2021
5c69dd2
Merge branch 'master' into batch_1
ti-chi-bot Dec 31, 2021
9bb0f9b
Merge branch 'master' into batch_1
ti-chi-bot Dec 31, 2021
97287c5
Merge branch 'master' into batch_1
ti-chi-bot Dec 31, 2021
599b287
Merge branch 'master' into batch_1
ti-chi-bot Dec 31, 2021
27ccc34
Merge branch 'master' into batch_1
ti-chi-bot Dec 31, 2021
75559f9
Merge branch 'master' into batch_1
ti-chi-bot Dec 31, 2021
de56dc3
Merge branch 'master' into batch_1
ti-chi-bot Dec 31, 2021
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
4 changes: 2 additions & 2 deletions br/pkg/gluetidb/glue.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (gs *tidbSession) CreateDatabase(ctx context.Context, schema *model.DBInfo)
if len(schema.Charset) == 0 {
schema.Charset = mysql.DefaultCharset
}
return d.CreateSchemaWithInfo(gs.se, schema, ddl.OnExistIgnore, true)
return d.CreateSchemaWithInfo(gs.se, schema, ddl.OnExistIgnore)
}

// CreateTable implements glue.Session.
Expand All @@ -143,7 +143,7 @@ func (gs *tidbSession) CreateTable(ctx context.Context, dbName model.CIStr, tabl
newPartition.Definitions = append([]model.PartitionDefinition{}, table.Partition.Definitions...)
table.Partition = &newPartition
}
return d.CreateTableWithInfo(gs.se, dbName, table, ddl.OnExistIgnore, true)
return d.CreateTableWithInfo(gs.se, dbName, table, ddl.OnExistIgnore)
}

// Close implements glue.Session.
Expand Down
38 changes: 38 additions & 0 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7641,3 +7641,41 @@ func (s *testDBSuite8) TestCreateTextAdjustLen(c *C) {
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
tk.MustExec("drop table if exists t")
}

func (s *testDBSuite2) TestCreateTables(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists tables_1")
tk.MustExec("drop table if exists tables_2")
tk.MustExec("drop table if exists tables_3")

d := s.dom.DDL()
infos := []*model.TableInfo{}
infos = append(infos, &model.TableInfo{
Name: model.NewCIStr("tables_1"),
})
infos = append(infos, &model.TableInfo{
Name: model.NewCIStr("tables_2"),
})
infos = append(infos, &model.TableInfo{
Name: model.NewCIStr("tables_3"),
})

// correct name
err := d.BatchCreateTableWithInfo(tk.Se, model.NewCIStr("test"), infos, ddl.OnExistError)
c.Check(err, IsNil)

tk.MustQuery("show tables like '%tables_%'").Check(testkit.Rows("tables_1", "tables_2", "tables_3"))
job := tk.MustQuery("admin show ddl jobs").Rows()[0]
Copy link
Member

Choose a reason for hiding this comment

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

ADMIN SHOW DDL QUERIES?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This API does not set query since it is not from parser. One must set ctx.Value(sessionctx.QueryString) manually for displaying admin show ddl queries.

c.Assert(job[1], Equals, "test")
c.Assert(job[2], Equals, "tables_1,tables_2,tables_3")
c.Assert(job[3], Equals, "create tables")
c.Assert(job[4], Equals, "public")
// FIXME: we must change column type to give multiple id
// c.Assert(job[6], Matches, "[^,]+,[^,]+,[^,]+")

// duplicated name
infos[1].Name = model.NewCIStr("tables_1")
err = d.BatchCreateTableWithInfo(tk.Se, model.NewCIStr("test"), infos, ddl.OnExistError)
c.Check(terror.ErrorEqual(err, infoschema.ErrTableExists), IsTrue)
}
22 changes: 9 additions & 13 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,32 +123,28 @@ type DDL interface {

// CreateSchemaWithInfo creates a database (schema) given its database info.
//
// If `tryRetainID` is true, this method will try to keep the database ID specified in
// the `info` rather than generating new ones. This is just a hint though, if the ID collides
// with an existing database a new ID will always be used.
//
// WARNING: the DDL owns the `info` after calling this function, and will modify its fields
// in-place. If you want to keep using `info`, please call Clone() first.
CreateSchemaWithInfo(
ctx sessionctx.Context,
info *model.DBInfo,
onExist OnExist,
tryRetainID bool) error
onExist OnExist) error

// CreateTableWithInfo creates a table, view or sequence given its table info.
//
// If `tryRetainID` is true, this method will try to keep the table ID specified in the `info`
// rather than generating new ones. This is just a hint though, if the ID collides with an
// existing table a new ID will always be used.
//
// WARNING: the DDL owns the `info` after calling this function, and will modify its fields
// in-place. If you want to keep using `info`, please call Clone() first.
CreateTableWithInfo(
ctx sessionctx.Context,
schema model.CIStr,
info *model.TableInfo,
onExist OnExist,
tryRetainID bool) error
onExist OnExist) error

// BatchCreateTableWithInfo is like CreateTableWithInfo, but can handle multiple tables.
BatchCreateTableWithInfo(ctx sessionctx.Context,
schema model.CIStr,
info []*model.TableInfo,
onExist OnExist) error

// Start campaigns the owner and starts workers.
// ctxPool is used for the worker's delRangeManager and creates sessions.
Expand Down Expand Up @@ -253,10 +249,10 @@ func asyncNotifyEvent(d *ddlCtx, e *util.Event) {
case d.ddlEventCh <- e:
return
default:
logutil.BgLogger().Warn("[ddl] fail to notify DDL event", zap.String("event", e.String()))
time.Sleep(time.Microsecond * 10)
}
}
logutil.BgLogger().Warn("[ddl] fail to notify DDL event", zap.String("event", e.String()))
}
}

Expand Down
Loading