From 284e70d920b3eb7fc66d91bd7f7491bb2ee5c13c Mon Sep 17 00:00:00 2001 From: ciscoxll Date: Thu, 26 Apr 2018 19:00:05 +0800 Subject: [PATCH] ddl: enhance validation of column names when creating table (#6349) Missed extra validation for column with dot in create table statement --- ddl/ddl_db_test.go | 2 ++ executor/executor_test.go | 15 +++++++++++++++ plan/preprocess.go | 16 +++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/ddl/ddl_db_test.go b/ddl/ddl_db_test.go index b3d87d98948d1..04fb0cceb2701 100644 --- a/ddl/ddl_db_test.go +++ b/ddl/ddl_db_test.go @@ -162,6 +162,8 @@ func (s *testDBSuite) TestMySQLErrorCode(c *C) { s.testErrorCode(c, sql, tmysql.ErrUnknownCharacterSet) sql = "create table t1(a int) character set laitn1;" s.testErrorCode(c, sql, tmysql.ErrUnknownCharacterSet) + sql = "create table t2(c1.c2 blob default null);" + s.testErrorCode(c, sql, tmysql.ErrWrongTableName) // add column sql = "alter table test_error_code_succ add column c1 int" diff --git a/executor/executor_test.go b/executor/executor_test.go index 8fec110353dad..c4eb431965cf7 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -2524,6 +2524,21 @@ func (s *testSuite) TestIssue5341(c *C) { tk.MustQuery("select * from test.t where a < 1 order by a limit 0;").Check(testkit.Rows()) } +func (s *testSuite) TestContainDotColumn(c *C) { + tk := testkit.NewTestKit(c, s.store) + + tk.MustExec("use test") + 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)") + + tk.MustExec("drop table if exists t3") + _, err := tk.Exec("create table t3(s.a char);") + terr := errors.Trace(err).(*errors.Err).Cause().(*terror.Error) + c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.ErrWrongTableName)) +} + func (s *testSuite) TestCheckIndex(c *C) { s.ctx = mock.NewContext() s.ctx.Store = s.store diff --git a/plan/preprocess.go b/plan/preprocess.go index 25b6d9121cc48..2d2f50b5f3a59 100644 --- a/plan/preprocess.go +++ b/plan/preprocess.go @@ -238,7 +238,7 @@ func (p *preprocessor) checkCreateTableGrammar(stmt *ast.CreateTableStmt) { p.err = ddl.ErrWrongTableName.GenByArgs(tName) return } - + p.checkContainDotColumn(stmt) countPrimaryKey := 0 for _, colDef := range stmt.Cols { if err := checkColumn(colDef); err != nil { @@ -492,6 +492,20 @@ func isIncorrectName(name string) bool { return false } +// checkContainDotColumn checks field contains the table name. +// for example :create table t (c1.c2 int default null). +func (p *preprocessor) checkContainDotColumn(stmt *ast.CreateTableStmt) { + tName := stmt.Table.Name.String() + + 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) + return + } + } +} + func (p *preprocessor) handleTableName(tn *ast.TableName) { if tn.Schema.L == "" { currentDB := p.ctx.GetSessionVars().CurrentDB