Skip to content

Commit

Permalink
ddl: Added check for partition name ending with space (#31785)
Browse files Browse the repository at this point in the history
close #31535
  • Loading branch information
mjonss authored Feb 14, 2022
1 parent 25d60e5 commit f5eb1e9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
23 changes: 23 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3823,3 +3823,26 @@ func TestIssue29326(t *testing.T) {
err = tk.ExecToErr("create definer=`root`@`127.0.0.1` view v1 as select t1.id, t2.id from t1,t2 where t1.id=t2.id")
require.True(t, infoschema.ErrColumnExists.Equal(err))
}

func TestInvalidPartitionNameWhenCreateTable(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)

tk.MustExec("create database invalidPartitionNames")
defer tk.MustExec("drop database invalidPartitionNames")
tk.MustExec("USE invalidPartitionNames")

_, err := tk.Exec("create table t(a int) partition by range (a) (partition p0 values less than (0), partition `p1 ` values less than (3))")
require.Error(t, err)
require.Truef(t, terror.ErrorEqual(err, ddl.ErrWrongPartitionName), "err %v", err)

_, err = tk.Exec("create table t(a int) partition by range (a) (partition `` values less than (0), partition `p1` values less than (3))")
require.Error(t, err)
require.Truef(t, terror.ErrorEqual(err, ddl.ErrWrongPartitionName), "err %v", err)

tk.MustExec("create table t(a int) partition by range (a) (partition `p0` values less than (0), partition `p1` values less than (3))")
_, err = tk.Exec("alter table t add partition (partition `p2 ` values less than (5))")
require.Error(t, err)
require.Truef(t, terror.ErrorEqual(err, ddl.ErrWrongPartitionName), "err %v", err)
}
2 changes: 2 additions & 0 deletions ddl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ var (
ErrWrongTableName = dbterror.ClassDDL.NewStd(mysql.ErrWrongTableName)
// ErrWrongColumnName returns for wrong column name.
ErrWrongColumnName = dbterror.ClassDDL.NewStd(mysql.ErrWrongColumnName)
// ErrWrongPartitionName returns for wrong partition name.
ErrWrongPartitionName = dbterror.ClassDDL.NewStd(mysql.ErrWrongPartitionName)
// ErrWrongUsage returns for wrong ddl syntax usage.
ErrWrongUsage = dbterror.ClassDDL.NewStd(mysql.ErrWrongUsage)
// ErrInvalidGroupFuncUse returns for using invalid group functions.
Expand Down
5 changes: 5 additions & 0 deletions errors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,11 @@ error = '''
This partition function is not allowed
'''

["ddl:1567"]
error = '''
Incorrect partition name
'''

["ddl:1652"]
error = '''
Duplicate partition field name '%-.192s'
Expand Down
18 changes: 18 additions & 0 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,16 @@ func (p *preprocessor) checkCreateTableGrammar(stmt *ast.CreateTableStmt) {
p.err = ddl.ErrTableMustHaveColumns
return
}

if stmt.Partition != nil {
for _, def := range stmt.Partition.Definitions {
pName := def.Name.String()
if isIncorrectName(pName) {
p.err = ddl.ErrWrongPartitionName.GenWithStackByArgs(pName)
return
}
}
}
}

func (p *preprocessor) checkCreateViewGrammar(stmt *ast.CreateViewStmt) {
Expand Down Expand Up @@ -1107,6 +1117,14 @@ func (p *preprocessor) checkAlterTableGrammar(stmt *ast.AlterTableStmt) {
p.err = ErrInternal.GenWithStack(msg)
return
}
case ast.AlterTableAddPartitions:
for _, def := range spec.PartDefinitions {
pName := def.Name.String()
if isIncorrectName(pName) {
p.err = ddl.ErrWrongPartitionName.GenWithStackByArgs(pName)
return
}
}
default:
// Nothing to do now.
}
Expand Down

0 comments on commit f5eb1e9

Please sign in to comment.