@@ -1546,12 +1546,12 @@ func checkRangeColumnsPartitionValue(ctx sessionctx.Context, tbInfo *model.Table
15461546 // Range columns partition key supports multiple data types with integer、datetime、string.
15471547 defs := pi .Definitions
15481548 if len (defs ) < 1 {
1549- return errors . Trace ( ErrPartitionsMustBeDefined )
1549+ return ast . ErrPartitionsMustBeDefined . GenWithStackByArgs ( "RANGE" )
15501550 }
15511551
15521552 curr := & defs [0 ]
15531553 if len (curr .LessThan ) != len (pi .Columns ) {
1554- return errors .Trace (ErrPartitionColumnList )
1554+ return errors .Trace (ast . ErrPartitionColumnList )
15551555 }
15561556 for i := 1 ; i < len (defs ); i ++ {
15571557 prev , curr := curr , & defs [i ]
@@ -1568,7 +1568,7 @@ func checkRangeColumnsPartitionValue(ctx sessionctx.Context, tbInfo *model.Table
15681568
15691569func checkTwoRangeColumns (ctx sessionctx.Context , curr , prev * model.PartitionDefinition , pi * model.PartitionInfo , tbInfo * model.TableInfo ) (bool , error ) {
15701570 if len (curr .LessThan ) != len (pi .Columns ) {
1571- return false , errors .Trace (ErrPartitionColumnList )
1571+ return false , errors .Trace (ast . ErrPartitionColumnList )
15721572 }
15731573 for i := 0 ; i < len (pi .Columns ); i ++ {
15741574 // Special handling for MAXVALUE.
@@ -1775,8 +1775,7 @@ func resolveAlterTableSpec(ctx sessionctx.Context, specs []*ast.AlterTableSpec)
17751775 validSpecs = append (validSpecs , spec )
17761776 }
17771777
1778- if len (validSpecs ) != 1 {
1779- // TODO: Hanlde len(validSpecs) == 0.
1778+ if len (validSpecs ) > 1 {
17801779 // Now we only allow one schema changing at the same time.
17811780 return nil , errRunMultiSchemaChanges
17821781 }
@@ -1863,6 +1862,9 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A
18631862 err = ErrUnsupportedModifyPrimaryKey .GenWithStackByArgs ("drop" )
18641863 case ast .AlterTableRenameIndex :
18651864 err = d .RenameIndex (ctx , ident , spec )
1865+ case ast .AlterTablePartition :
1866+ // Prevent silent succeed if user executes ALTER TABLE x PARTITION BY ...
1867+ err = errors .New ("alter table partition is unsupported" )
18661868 case ast .AlterTableOption :
18671869 for i , opt := range spec .Options {
18681870 switch opt .Tp {
@@ -2089,10 +2091,6 @@ func (d *ddl) AddTablePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
20892091 if meta .GetPartitionInfo () == nil {
20902092 return errors .Trace (ErrPartitionMgmtOnNonpartitioned )
20912093 }
2092- // We don't support add hash type partition now.
2093- if meta .Partition .Type == model .PartitionTypeHash {
2094- return errors .Trace (ErrUnsupportedAddPartition )
2095- }
20962094
20972095 partInfo , err := buildPartitionInfo (meta , d , spec )
20982096 if err != nil {
@@ -2144,20 +2142,28 @@ func (d *ddl) CoalescePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
21442142 return errors .Trace (ErrPartitionMgmtOnNonpartitioned )
21452143 }
21462144
2147- // Coalesce partition can only be used on hash/key partitions.
2148- if meta .Partition .Type == model .PartitionTypeRange {
2149- return errors .Trace (ErrCoalesceOnlyOnHashPartition )
2150- }
2151-
2145+ switch meta .Partition .Type {
21522146 // We don't support coalesce partitions hash type partition now.
2153- if meta . Partition . Type == model .PartitionTypeHash {
2147+ case model .PartitionTypeHash :
21542148 return errors .Trace (ErrUnsupportedCoalescePartition )
2149+
2150+ // Key type partition cannot be constructed currently, ignoring it for now.
2151+ case model .PartitionTypeKey :
2152+
2153+ // Coalesce partition can only be used on hash/key partitions.
2154+ default :
2155+ return errors .Trace (ErrCoalesceOnlyOnHashPartition )
21552156 }
21562157
21572158 return errors .Trace (err )
21582159}
21592160
21602161func (d * ddl ) TruncateTablePartition (ctx sessionctx.Context , ident ast.Ident , spec * ast.AlterTableSpec ) error {
2162+ // TODO: Support truncate multiple partitions
2163+ if len (spec .PartitionNames ) != 1 {
2164+ return errRunMultiSchemaChanges
2165+ }
2166+
21612167 is := d .infoHandle .Get ()
21622168 schema , ok := is .SchemaByName (ident .Schema )
21632169 if ! ok {
@@ -2173,7 +2179,7 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
21732179 }
21742180
21752181 var pid int64
2176- pid , err = tables .FindPartitionByName (meta , spec .Name )
2182+ pid , err = tables .FindPartitionByName (meta , spec .PartitionNames [ 0 ]. L )
21772183 if err != nil {
21782184 return errors .Trace (err )
21792185 }
@@ -2195,6 +2201,11 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
21952201}
21962202
21972203func (d * ddl ) DropTablePartition (ctx sessionctx.Context , ident ast.Ident , spec * ast.AlterTableSpec ) error {
2204+ // TODO: Support drop multiple partitions
2205+ if len (spec .PartitionNames ) != 1 {
2206+ return errRunMultiSchemaChanges
2207+ }
2208+
21982209 is := d .infoHandle .Get ()
21992210 schema , ok := is .SchemaByName (ident .Schema )
22002211 if ! ok {
@@ -2208,7 +2219,9 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *
22082219 if meta .GetPartitionInfo () == nil {
22092220 return errors .Trace (ErrPartitionMgmtOnNonpartitioned )
22102221 }
2211- err = checkDropTablePartition (meta , spec .Name )
2222+
2223+ partName := spec .PartitionNames [0 ].L
2224+ err = checkDropTablePartition (meta , partName )
22122225 if err != nil {
22132226 return errors .Trace (err )
22142227 }
@@ -2218,7 +2231,7 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *
22182231 TableID : meta .ID ,
22192232 Type : model .ActionDropTablePartition ,
22202233 BinlogInfo : & model.HistoryInfo {},
2221- Args : []interface {}{spec . Name },
2234+ Args : []interface {}{partName },
22222235 }
22232236
22242237 err = d .doDDLJob (ctx , job )
@@ -3180,9 +3193,15 @@ func validateCommentLength(vars *variable.SessionVars, comment string, maxLen in
31803193}
31813194
31823195func buildPartitionInfo (meta * model.TableInfo , d * ddl , spec * ast.AlterTableSpec ) (* model.PartitionInfo , error ) {
3183- if meta .Partition .Type == model .PartitionTypeRange && len (spec .PartDefinitions ) == 0 {
3184- return nil , errors .Trace (ErrPartitionsMustBeDefined )
3196+ if meta .Partition .Type == model .PartitionTypeRange {
3197+ if len (spec .PartDefinitions ) == 0 {
3198+ return nil , ast .ErrPartitionsMustBeDefined .GenWithStackByArgs (meta .Partition .Type )
3199+ }
3200+ } else {
3201+ // we don't support ADD PARTITION for all other partition types yet.
3202+ return nil , errors .Trace (ErrUnsupportedAddPartition )
31853203 }
3204+
31863205 part := & model.PartitionInfo {
31873206 Type : meta .Partition .Type ,
31883207 Expr : meta .Partition .Expr ,
@@ -3195,7 +3214,12 @@ func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec)
31953214 }
31963215 buf := new (bytes.Buffer )
31973216 for ith , def := range spec .PartDefinitions {
3198- for _ , expr := range def .LessThan {
3217+ if err := def .Clause .Validate (part .Type , len (part .Columns )); err != nil {
3218+ return nil , err
3219+ }
3220+ // For RANGE partition only VALUES LESS THAN should be possible.
3221+ clause := def .Clause .(* ast.PartitionDefinitionClauseLessThan )
3222+ for _ , expr := range clause .Exprs {
31993223 tp := expr .GetType ().Tp
32003224 if len (part .Columns ) == 0 {
32013225 // Partition by range.
@@ -3210,14 +3234,15 @@ func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec)
32103234 }
32113235 // Partition by range columns if len(part.Columns) != 0.
32123236 }
3237+ comment , _ := def .Comment ()
32133238 piDef := model.PartitionDefinition {
32143239 Name : def .Name ,
32153240 ID : genIDs [ith ],
3216- Comment : def . Comment ,
3241+ Comment : comment ,
32173242 }
32183243
32193244 buf := new (bytes.Buffer )
3220- for _ , expr := range def . LessThan {
3245+ for _ , expr := range clause . Exprs {
32213246 expr .Format (buf )
32223247 piDef .LessThan = append (piDef .LessThan , buf .String ())
32233248 buf .Reset ()
0 commit comments