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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ddl/ddl_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
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.


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
Expand Down
16 changes: 15 additions & 1 deletion plan/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down