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

plan/validator: return an error when alter auto_incerment (#3041) #3072

Merged
merged 11 commits into from
Apr 19, 2017

Conversation

bobotu
Copy link
Contributor

@bobotu bobotu commented Apr 17, 2017

Refer to #3041

@CLAassistant
Copy link

CLAassistant commented Apr 17, 2017

CLA assistant check
All committers have signed the CLA.

@hanfei1991 hanfei1991 added the contribution This PR is from a community contributor. label Apr 17, 2017
@hanfei1991
Copy link
Member

Thanks. Please sign the CLA

@@ -290,3 +301,5 @@ func checkDuplicateColumnName(indexColNames []*ast.IndexColName) error {
}
return nil
}

const codeAlterAutoID terror.ErrCode = 2
Copy link
Member

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

Copy link
Contributor

@tiancaiamao tiancaiamao left a comment

Choose a reason for hiding this comment

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

PTAL @zimulala

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

Choose a reason for hiding this comment

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

why not if else ?

Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

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

okay

Copy link
Contributor

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.

Copy link
Contributor Author

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

@@ -45,6 +46,7 @@ const (
CodeAmbiguous terror.ErrCode = 1052
CodeUnknownColumn terror.ErrCode = 1054
CodeWrongArguments terror.ErrCode = 1210
CodeAlterAutoID terror.ErrCode = 2
Copy link
Member

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")
Copy link
Member

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?

Copy link
Contributor Author

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.

@zimulala
Copy link
Contributor

Please add some tests.

@bobotu
Copy link
Contributor Author

bobotu commented Apr 19, 2017

There is already a test in this RP which check the returned error message when user commit a alter table t auto_increment=1. I don't know is there have any other tests should I adde. Please tell me and I'll add it. @zimulala

@zimulala
Copy link
Contributor

zimulala commented Apr 19, 2017

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")},
Copy link
Contributor

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;.

Copy link
Contributor Author

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.

@zimulala
Copy link
Contributor

LGTM
PTAL @tiancaiamao @coocood

Copy link
Member

@hanfei1991 hanfei1991 left a comment

Choose a reason for hiding this comment

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

LGTM

@hanfei1991 hanfei1991 merged commit 7b381c0 into pingcap:master Apr 19, 2017
@bobotu bobotu deleted the iss3041 branch April 21, 2017 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution This PR is from a community contributor.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants