-
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
plan/validator: return an error when alter auto_incerment (#3041) #3072
Conversation
Thanks. Please sign the CLA |
plan/validator.go
Outdated
@@ -290,3 +301,5 @@ func checkDuplicateColumnName(indexColNames []*ast.IndexColName) error { | |||
} | |||
return nil | |||
} | |||
|
|||
const codeAlterAutoID terror.ErrCode = 2 |
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.
add error code to planbuilder.go
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.
PTAL @zimulala
plan/validator.go
Outdated
@@ -271,6 +271,16 @@ func (v *validator) checkAlterTableGrammar(stmt *ast.AlterTableStmt) { | |||
default: | |||
// Nothing to do now. | |||
} | |||
case ast.AlterTableOption: | |||
for _, opt := range spec.Options { | |||
switch opt.Tp { |
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.
why not if else
?
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.
I don't know if there will be any code about other TableOption.Tp
in the future, so I use switch
instead of if-else
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.
okay
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.
I think it's enough to use if-else
here. We may remove it when we intend to support this type in the future. And if we need to handle other types in the future, then we can update it at that time.
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.
I'll change the code to use if-else
plan/planbuilder.go
Outdated
@@ -45,6 +46,7 @@ const ( | |||
CodeAmbiguous terror.ErrCode = 1052 | |||
CodeUnknownColumn terror.ErrCode = 1054 | |||
CodeWrongArguments terror.ErrCode = 1210 | |||
CodeAlterAutoID terror.ErrCode = 2 |
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.
move it to line 46 and set it to 3
@@ -36,6 +36,7 @@ var ( | |||
ErrUnknownColumn = terror.ClassOptimizerPlan.New(CodeUnknownColumn, "Unknown column '%s' in '%s'") | |||
ErrWrongArguments = terror.ClassOptimizerPlan.New(CodeWrongArguments, "Incorrect arguments to EXECUTE") | |||
ErrAmbiguous = terror.ClassOptimizerPlan.New(CodeAmbiguous, "Column '%s' in field list is ambiguous") | |||
ErrAlterAutoID = terror.ClassAutoid.New(CodeAlterAutoID, "No support for setting auto_increment using alter_table") |
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.
Is there a MySQL standard error code for this error?
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.
I have searched in MySQL 8.0 Server Error Codes and Messages and got these error mentioned AUTO_INCREMENT
or AUTOINC
. In my opinon there have no appropriate MySQL standard error code to describe this error. Because this problem caused by the implementation of autoid
module, so I put this error under ClassAutoid
.
Error: 1164 SQLSTATE: 42000 (ER_TABLE_CANT_HANDLE_AUTO_INCREMENT)
Message: The used table type doesn't support AUTO_INCREMENT columns
Error: 1467 SQLSTATE: HY000 (ER_AUTOINC_READ_FAILED)
Message: Failed to read auto-increment value from storage engine
Error: 1569 SQLSTATE: HY000 (ER_DUP_ENTRY_AUTOINCREMENT_CASE)
Message: ALTER TABLE causes auto_increment resequencing, resulting in duplicate entry '%s' for key '%s'
Error: 1671 SQLSTATE: HY000 (ER_BINLOG_UNSAFE_AUTOINC_COLUMNS)
Message: Statement is unsafe because it invokes a trigger or a stored function that inserts into an AUTO_INCREMENT column. Inserted values cannot be logged correctly.
Error: 1722 SQLSTATE: HY000 (ER_BINLOG_UNSAFE_WRITE_AUTOINC_SELECT)
Message: Statements writing to a table with an auto-increment column after selecting from another table are unsafe because the order in which rows are retrieved determines what (if any) rows will be written. This order cannot be predicted and may differ on master and the slave.
Error: 1723 SQLSTATE: HY000 (ER_BINLOG_UNSAFE_CREATE_SELECT_AUTOINC)
Message: CREATE TABLE... SELECT... on a table with an auto-increment column is unsafe because the order in which rows are retrieved by the SELECT determines which (if any) rows are inserted. This order cannot be predicted and may differ on master and the slave.
Error: 1727 SQLSTATE: HY000 (ER_BINLOG_UNSAFE_AUTOINC_NOT_FIRST)
Message: INSERT into autoincrement field which is not the first part in the composed primary key is unsafe.
Error: 1854 SQLSTATE: HY000 (ER_ALTER_OPERATION_NOT_SUPPORTED_REASON_AUTOINC)
Message: Adding an auto-increment column requires a lock
Error: 1869 SQLSTATE: HY000 (ER_AUTO_INCREMENT_CONFLICT)
Message: Auto-increment value in UPDATE conflicts with internally generated values
Error: 3109 SQLSTATE: HY000 (ER_GENERATED_COLUMN_REF_AUTO_INC)
Message: Generated column '%s' cannot refer to auto-increment column.
Please add some tests. |
There is already a test in this RP which check the returned error message when user commit a |
Sorry, I just saw it. @bobotu |
@@ -71,6 +71,7 @@ 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")}, |
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.
Add the test like alter table t add column c int auto_increment key, auto_increment=10;
.
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.
I have added two tests. I think validator should return a nil
in the validation of alter table t add column c int auto_increment key
, because there are existed guard to avoid user commit something like alter table t add column c int auto_increment key
or alter table t modify c int auto_increment key
in ddl
module.
LGTM |
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.
LGTM
Refer to #3041