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: enhance validation of column names when creating table #6349

Merged
merged 9 commits into from
Apr 26, 2018

Conversation

ciscoxll
Copy link
Contributor

@ciscoxll ciscoxll commented Apr 23, 2018

Check the table name that are included in the field is it consistent with the create table name
respond issues #6330

mysql:
mysql> drop table if exists t; create table t(c1.c2 blob default null); insert into t values(); select * from t;
Query OK, 0 rows affected, 1 warning (0.00 sec)

ERROR 1103 (42000): Incorrect table name 'c1'
ERROR 1146 (42S02): Table 'test.t' doesn't exist
ERROR 1146 (42S02): Table 'test.t' doesn't exist
tidb:
tidb> drop table if exists t; create table t(c1.c2 blob default null); insert into t values(); select * from t;
Query OK, 0 rows affected (0.00 sec)

ERROR 1103 (42000): Incorrect table name 'c1'
ERROR 1146 (42S02): Table 'test.t' doesn't exist
ERROR 1146 (42S02): Table 'test.t' doesn't exist

@ciscoxll ciscoxll changed the title ddl:Missed extra validation for column with dot in create table state… ddl:missed extra validation for column with dot in create table state… Apr 23, 2018
@ciscoxll
Copy link
Contributor Author

ciscoxll commented Apr 23, 2018

@winkyao @zimulala @XuHuaiyu PTAL

@shenli
Copy link
Member

shenli commented Apr 23, 2018

/run-all-tests

@shenli shenli added the DDL label Apr 23, 2018
ddl/ddl_api.go Outdated
@@ -536,6 +536,15 @@ func checkTooManyColumns(colDefs []*ast.ColumnDef) error {
return nil
}

func checkContainDotColumn(tableName string, colDefs []*ast.ColumnDef) error {
Copy link
Member

Choose a reason for hiding this comment

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

Add comments for the function.

ddl/ddl_api.go Outdated
@@ -536,6 +536,16 @@ func checkTooManyColumns(colDefs []*ast.ColumnDef) error {
return nil
}

// checkContainDotColumns check field contains the table name.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/check/checks

ddl/ddl_api.go Outdated
@@ -536,6 +536,16 @@ func checkTooManyColumns(colDefs []*ast.ColumnDef) error {
return nil
}

// checkContainDotColumns check field contains the table name.
func checkContainDotColumns(tableName string, colDefs []*ast.ColumnDef) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we check it in the function of Preprocess?

@ciscoxll
Copy link
Contributor Author

/run-all-tests

@ciscoxll
Copy link
Contributor Author

@winkyao @zimulala @XuHuaiyu PTAL

@@ -462,6 +467,16 @@ func isIncorrectName(name string) bool {
return false
}

// checksContainDotColumns check field contains the table name.
func checksContainDotColumns(tableName string, colDefs []*ast.ColumnDef) (string, bool) {
Copy link
Member

Choose a reason for hiding this comment

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

  1. s/checksContainDotColumns/checkContainDotColumn/
  2. make this function as a member function of preprocessor and we can set p.err inside this function, thus we can only return a bool value to indicate whether there is a column whose name contains a dot symbol.

@@ -462,6 +467,16 @@ func isIncorrectName(name string) bool {
return false
}

// checksContainDotColumns check field contains the table name.
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment is not that explicit, better put an example here?

tk.MustExec("drop table if exists test.t1")
tk.MustExec("create table test.t1(t1.a char)")
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t2(a char, t2.b int)")
Copy link
Contributor

Choose a reason for hiding this comment

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

add test for
create table t(s.a char);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Already add.

@@ -224,6 +224,11 @@ func (p *preprocessor) checkCreateTableGrammar(stmt *ast.CreateTableStmt) {
return
}

if tableName, ok := checksContainDotColumns(tName, stmt.Cols); ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

add some comments

@winkyao
Copy link
Contributor

winkyao commented Apr 25, 2018

Please address comments

Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -52,6 +52,7 @@ func (p *preprocessor) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
case *ast.CreateTableStmt:
p.inCreateOrDropTable = true
p.checkCreateTableGrammar(node)
p.checkContainDotColumn(node)
Copy link
Contributor

Choose a reason for hiding this comment

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

call it in p.checkCreateTableGrammar is better.

@ciscoxll ciscoxll added the status/LGT1 Indicates that a PR has LGTM 1. label Apr 25, 2018
@ciscoxll
Copy link
Contributor Author

@winkyao @zimulala @XuHuaiyu PTAL

@ciscoxll
Copy link
Contributor Author

/run-all-tests

Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

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

LGTM

@ciscoxll ciscoxll changed the title ddl:missed extra validation for column with dot in create table state… ddl: enhance validation of column names when creating table Apr 25, 2018
@ciscoxll ciscoxll added the status/LGT2 Indicates that a PR has LGTM 2. label Apr 25, 2018
@ciscoxll ciscoxll removed the status/LGT1 Indicates that a PR has LGTM 1. label Apr 25, 2018
@ciscoxll
Copy link
Contributor Author

/run-all-tests

for _, colDef := range stmt.Cols {
// check table name.
if colDef.Name.Table.O != tName && len(colDef.Name.Table.O) != 0 {
p.err = ddl.ErrWrongTableName.GenByArgs(colDef.Name.Table.O)
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 we can break here.

@ciscoxll
Copy link
Contributor Author

@winkyao @zimulala PTAL

@ciscoxll ciscoxll merged commit 284e70d into pingcap:master Apr 26, 2018
@ciscoxll ciscoxll deleted the check-column branch April 26, 2018 11:00
@sre-bot sre-bot added the contribution This PR is from a community contributor. label Dec 18, 2019
@you06 you06 added the sig/sql-infra SIG: SQL Infra label Mar 4, 2020
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. sig/sql-infra SIG: SQL Infra status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants