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: forbid truncating sequence object (#16981) #17037

Merged
merged 3 commits into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4248,12 +4248,19 @@ func (s *testDBSuite2) TestLockTables(c *C) {
_, err = tk.Exec("create view v1 as select * from t1;")
c.Assert(terror.ErrorEqual(err, infoschema.ErrTableNotLocked), IsTrue)

// Test for lock view was not supported.
// Test for locking view was not supported.
tk.MustExec("unlock tables")
tk.MustExec("create view v1 as select * from t1;")
_, err = tk.Exec("lock tables v1 read")
c.Assert(terror.ErrorEqual(err, table.ErrUnsupportedOp), IsTrue)

// Test for locking sequence was not supported.
tk.MustExec("unlock tables")
tk.MustExec("create sequence seq")
_, err = tk.Exec("lock tables seq read")
c.Assert(terror.ErrorEqual(err, table.ErrUnsupportedOp), IsTrue)
tk.MustExec("drop sequence seq")

// Test for create/drop/alter database when session is holding the table locks.
tk.MustExec("unlock tables")
tk.MustExec("lock table t1 write")
Expand Down
7 changes: 5 additions & 2 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3565,6 +3565,9 @@ func (d *ddl) TruncateTable(ctx sessionctx.Context, ti ast.Ident) error {
if err != nil {
return errors.Trace(err)
}
if tb.Meta().IsView() || tb.Meta().IsSequence() {
return infoschema.ErrTableNotExists.GenWithStackByArgs(schema.Name.O, tb.Meta().Name.O)
}
genIDs, err := d.genGlobalIDs(1)
if err != nil {
return errors.Trace(err)
Expand Down Expand Up @@ -4260,7 +4263,7 @@ func (d *ddl) LockTables(ctx sessionctx.Context, stmt *ast.LockTablesStmt) error
if err != nil {
return errors.Trace(err)
}
if t.Meta().IsView() {
if t.Meta().IsView() || t.Meta().IsSequence() {
return table.ErrUnsupportedOp.GenWithStackByArgs()
}
err = checkTableLocked(t.Meta(), tl.Type, sessionInfo)
Expand Down Expand Up @@ -4350,7 +4353,7 @@ func (d *ddl) CleanupTableLock(ctx sessionctx.Context, tables []*ast.TableName)
if err != nil {
return errors.Trace(err)
}
if t.Meta().IsView() {
if t.Meta().IsView() || t.Meta().IsSequence() {
return table.ErrUnsupportedOp
}
// Maybe the table t was not locked, but still try to unlock this table.
Expand Down
2 changes: 1 addition & 1 deletion ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func onTruncateTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ erro
if err != nil {
return ver, errors.Trace(err)
}
if tblInfo.IsView() {
if tblInfo.IsView() || tblInfo.IsSequence() {
job.State = model.JobStateCancelled
return ver, infoschema.ErrTableNotExists.GenWithStackByArgs(job.SchemaName, tblInfo.Name.O)
}
Expand Down
13 changes: 13 additions & 0 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,19 @@ func (s *testSuite6) TestIssue16250(c *C) {
c.Assert(err.Error(), Equals, "[schema:1146]Table 'test.view_issue16250' doesn't exist")
}

func (s testSuite6) TestTruncateSequence(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create sequence if not exists seq")
_, err := tk.Exec("truncate table seq")
c.Assert(err.Error(), Equals, "[schema:1146]Table 'test.seq' doesn't exist")
tk.MustExec("create sequence if not exists seq1 start 10 increment 2 maxvalue 10000 cycle")
_, err = tk.Exec("truncate table seq1")
c.Assert(err.Error(), Equals, "[schema:1146]Table 'test.seq1' doesn't exist")
tk.MustExec("drop sequence if exists seq")
tk.MustExec("drop sequence if exists seq1")
}

func (s *testSuite6) TestCreateViewWithOverlongColName(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down