-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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 alter table auto_increment #5472
Changes from all commits
84f2adb
20da919
7731152
2041e2e
c6c3135
0201816
1244fbf
115dbe6
06f0c3a
65d7b9c
fdd70e3
6c60f0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -222,6 +222,43 @@ func (d *ddl) onTruncateTable(t *meta.Meta, job *model.Job) (ver int64, _ error) | |
return ver, nil | ||
} | ||
|
||
func (d *ddl) onRebaseAutoID(t *meta.Meta, job *model.Job) (ver int64, _ error) { | ||
schemaID := job.SchemaID | ||
var newBase int64 | ||
err := job.DecodeArgs(&newBase) | ||
if err != nil { | ||
job.State = model.JobStateCancelled | ||
return ver, errors.Trace(err) | ||
} | ||
tblInfo, err := getTableInfo(t, job, schemaID) | ||
if err != nil { | ||
job.State = model.JobStateCancelled | ||
return ver, errors.Trace(err) | ||
} | ||
tblInfo.AutoIncID = newBase | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check that if the new auto-id is not less than current id? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, though it will be checked in storage, it should set a more properly info here. Fixed. |
||
tbl, err := d.getTable(schemaID, tblInfo) | ||
if err != nil { | ||
job.State = model.JobStateCancelled | ||
return ver, errors.Trace(err) | ||
} | ||
// The operation of the minus 1 to make sure that the current value doesn't be used, | ||
// the next Alloc operation will get this value. | ||
// Its behavior is consistent with MySQL. | ||
err = tbl.RebaseAutoID(nil, tblInfo.AutoIncID-1, false) | ||
if err != nil { | ||
job.State = model.JobStateCancelled | ||
return ver, errors.Trace(err) | ||
} | ||
job.State = model.JobStateDone | ||
job.BinlogInfo.AddTableInfo(ver, tblInfo) | ||
ver, err = updateTableInfo(t, job, tblInfo, tblInfo.State) | ||
if err != nil { | ||
job.State = model.JobStateCancelled | ||
return ver, errors.Trace(err) | ||
} | ||
return ver, nil | ||
} | ||
|
||
func (d *ddl) onRenameTable(t *meta.Meta, job *model.Job) (ver int64, _ error) { | ||
var oldSchemaID int64 | ||
var tableName model.CIStr | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,9 +75,8 @@ func (s *testValidatorSuite) TestValidator(c *C) { | |
errors.New("[schema:1068]Multiple primary key defined")}, | ||
{"create table t(c1 int not null, c2 int not null, primary key(c1), primary key(c2))", true, | ||
errors.New("[schema:1068]Multiple primary key defined")}, | ||
{"alter table t auto_increment=1", true, errors.New("[autoid:3]No support for setting auto_increment using alter_table")}, | ||
{"alter table t add column c int auto_increment key, auto_increment=10", true, | ||
errors.New("[autoid:3]No support for setting auto_increment using alter_table")}, | ||
{"alter table t auto_increment=1", true, nil}, | ||
{"alter table t add column c int auto_increment key, auto_increment=10", true, nil}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This SQL is different with SQL in 78. Should we add tests in ddl_db_test.go? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! PTAL @zimulala |
||
{"alter table t add column c int auto_increment key", true, nil}, | ||
{"alter table t add column char4294967295 char(255)", true, nil}, | ||
{"create table t (c float(53))", true, nil}, | ||
|
@@ -184,6 +183,6 @@ func (s *testValidatorSuite) TestValidator(c *C) { | |
c.Assert(stmts, HasLen, 1) | ||
stmt := stmts[0] | ||
err = plan.Preprocess(ctx, stmt, is, tt.inPrepare) | ||
c.Assert(terror.ErrorEqual(err, tt.err), IsTrue) | ||
c.Assert(terror.ErrorEqual(err, tt.err), IsTrue, Commentf("sql: %s", tt.sql)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'd can put this logical to
onRebaseAutoID
。Because nowautoIncID
is n,its value may change when you rebase the global auto ID inonRebaseAutoID
。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is another check in storage node to guarantee the value be set properly. The comparison here is trying to set a more reasonable value in table info.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems that the global auto-id is not the current max id, for example, if the current max id is 1, the new base is 1000, then the next id inserted will be 5001. Is it desirable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is our expected result. I'll add a test for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. PTAL @lamxTyler