@@ -1583,12 +1583,12 @@ func checkRangeColumnsPartitionValue(ctx sessionctx.Context, tbInfo *model.Table
15831583 // Range columns partition key supports multiple data types with integer、datetime、string.
15841584 defs := pi .Definitions
15851585 if len (defs ) < 1 {
1586- return errors . Trace ( ErrPartitionsMustBeDefined )
1586+ return ast . ErrPartitionsMustBeDefined . GenWithStackByArgs ( "RANGE" )
15871587 }
15881588
15891589 curr := & defs [0 ]
15901590 if len (curr .LessThan ) != len (pi .Columns ) {
1591- return errors .Trace (ErrPartitionColumnList )
1591+ return errors .Trace (ast . ErrPartitionColumnList )
15921592 }
15931593 for i := 1 ; i < len (defs ); i ++ {
15941594 prev , curr := curr , & defs [i ]
@@ -1605,7 +1605,7 @@ func checkRangeColumnsPartitionValue(ctx sessionctx.Context, tbInfo *model.Table
16051605
16061606func checkTwoRangeColumns (ctx sessionctx.Context , curr , prev * model.PartitionDefinition , pi * model.PartitionInfo , tbInfo * model.TableInfo ) (bool , error ) {
16071607 if len (curr .LessThan ) != len (pi .Columns ) {
1608- return false , errors .Trace (ErrPartitionColumnList )
1608+ return false , errors .Trace (ast . ErrPartitionColumnList )
16091609 }
16101610 for i := 0 ; i < len (pi .Columns ); i ++ {
16111611 // Special handling for MAXVALUE.
@@ -1803,8 +1803,7 @@ func resolveAlterTableSpec(ctx sessionctx.Context, specs []*ast.AlterTableSpec)
18031803 validSpecs = append (validSpecs , spec )
18041804 }
18051805
1806- if len (validSpecs ) != 1 {
1807- // TODO: Hanlde len(validSpecs) == 0.
1806+ if len (validSpecs ) > 1 {
18081807 // Now we only allow one schema changing at the same time.
18091808 return nil , errRunMultiSchemaChanges
18101809 }
@@ -1891,6 +1890,9 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A
18911890 err = ErrUnsupportedModifyPrimaryKey .GenWithStackByArgs ("drop" )
18921891 case ast .AlterTableRenameIndex :
18931892 err = d .RenameIndex (ctx , ident , spec )
1893+ case ast .AlterTablePartition :
1894+ // Prevent silent succeed if user executes ALTER TABLE x PARTITION BY ...
1895+ err = errors .New ("alter table partition is unsupported" )
18941896 case ast .AlterTableOption :
18951897 for i , opt := range spec .Options {
18961898 switch opt .Tp {
@@ -2118,10 +2120,6 @@ func (d *ddl) AddTablePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
21182120 if pi == nil {
21192121 return errors .Trace (ErrPartitionMgmtOnNonpartitioned )
21202122 }
2121- // We don't support add hash type partition now.
2122- if meta .Partition .Type == model .PartitionTypeHash {
2123- return errors .Trace (ErrUnsupportedAddPartition )
2124- }
21252123
21262124 partInfo , err := buildPartitionInfo (meta , d , spec )
21272125 if err != nil {
@@ -2177,20 +2175,28 @@ func (d *ddl) CoalescePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
21772175 return errors .Trace (ErrPartitionMgmtOnNonpartitioned )
21782176 }
21792177
2180- // Coalesce partition can only be used on hash/key partitions.
2181- if meta .Partition .Type == model .PartitionTypeRange {
2182- return errors .Trace (ErrCoalesceOnlyOnHashPartition )
2183- }
2184-
2178+ switch meta .Partition .Type {
21852179 // We don't support coalesce partitions hash type partition now.
2186- if meta . Partition . Type == model .PartitionTypeHash {
2180+ case model .PartitionTypeHash :
21872181 return errors .Trace (ErrUnsupportedCoalescePartition )
2182+
2183+ // Key type partition cannot be constructed currently, ignoring it for now.
2184+ case model .PartitionTypeKey :
2185+
2186+ // Coalesce partition can only be used on hash/key partitions.
2187+ default :
2188+ return errors .Trace (ErrCoalesceOnlyOnHashPartition )
21882189 }
21892190
21902191 return errors .Trace (err )
21912192}
21922193
21932194func (d * ddl ) TruncateTablePartition (ctx sessionctx.Context , ident ast.Ident , spec * ast.AlterTableSpec ) error {
2195+ // TODO: Support truncate multiple partitions
2196+ if len (spec .PartitionNames ) != 1 {
2197+ return errRunMultiSchemaChanges
2198+ }
2199+
21942200 is := d .infoHandle .Get ()
21952201 schema , ok := is .SchemaByName (ident .Schema )
21962202 if ! ok {
@@ -2206,7 +2212,7 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
22062212 }
22072213
22082214 var pid int64
2209- pid , err = tables .FindPartitionByName (meta , spec .Name )
2215+ pid , err = tables .FindPartitionByName (meta , spec .PartitionNames [ 0 ]. L )
22102216 if err != nil {
22112217 return errors .Trace (err )
22122218 }
@@ -2228,6 +2234,11 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
22282234}
22292235
22302236func (d * ddl ) DropTablePartition (ctx sessionctx.Context , ident ast.Ident , spec * ast.AlterTableSpec ) error {
2237+ // TODO: Support drop multiple partitions
2238+ if len (spec .PartitionNames ) != 1 {
2239+ return errRunMultiSchemaChanges
2240+ }
2241+
22312242 is := d .infoHandle .Get ()
22322243 schema , ok := is .SchemaByName (ident .Schema )
22332244 if ! ok {
@@ -2241,7 +2252,9 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *
22412252 if meta .GetPartitionInfo () == nil {
22422253 return errors .Trace (ErrPartitionMgmtOnNonpartitioned )
22432254 }
2244- err = checkDropTablePartition (meta , spec .Name )
2255+
2256+ partName := spec .PartitionNames [0 ].L
2257+ err = checkDropTablePartition (meta , partName )
22452258 if err != nil {
22462259 return errors .Trace (err )
22472260 }
@@ -2251,7 +2264,7 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *
22512264 TableID : meta .ID ,
22522265 Type : model .ActionDropTablePartition ,
22532266 BinlogInfo : & model.HistoryInfo {},
2254- Args : []interface {}{spec . Name },
2267+ Args : []interface {}{partName },
22552268 }
22562269
22572270 err = d .doDDLJob (ctx , job )
@@ -3266,9 +3279,15 @@ func validateCommentLength(vars *variable.SessionVars, comment string, maxLen in
32663279}
32673280
32683281func buildPartitionInfo (meta * model.TableInfo , d * ddl , spec * ast.AlterTableSpec ) (* model.PartitionInfo , error ) {
3269- if meta .Partition .Type == model .PartitionTypeRange && len (spec .PartDefinitions ) == 0 {
3270- return nil , errors .Trace (ErrPartitionsMustBeDefined )
3282+ if meta .Partition .Type == model .PartitionTypeRange {
3283+ if len (spec .PartDefinitions ) == 0 {
3284+ return nil , ast .ErrPartitionsMustBeDefined .GenWithStackByArgs (meta .Partition .Type )
3285+ }
3286+ } else {
3287+ // we don't support ADD PARTITION for all other partition types yet.
3288+ return nil , errors .Trace (ErrUnsupportedAddPartition )
32713289 }
3290+
32723291 part := & model.PartitionInfo {
32733292 Type : meta .Partition .Type ,
32743293 Expr : meta .Partition .Expr ,
@@ -3281,14 +3300,21 @@ func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec)
32813300 return nil , err
32823301 }
32833302 for ith , def := range spec .PartDefinitions {
3303+ if err := def .Clause .Validate (part .Type , len (part .Columns )); err != nil {
3304+ return nil , err
3305+ }
3306+ // For RANGE partition only VALUES LESS THAN should be possible.
3307+ clause := def .Clause .(* ast.PartitionDefinitionClauseLessThan )
3308+
3309+ comment , _ := def .Comment ()
32843310 piDef := model.PartitionDefinition {
32853311 Name : def .Name ,
32863312 ID : genIDs [ith ],
3287- Comment : def . Comment ,
3313+ Comment : comment ,
32883314 }
32893315
32903316 buf := new (bytes.Buffer )
3291- for _ , expr := range def . LessThan {
3317+ for _ , expr := range clause . Exprs {
32923318 expr .Format (buf )
32933319 piDef .LessThan = append (piDef .LessThan , buf .String ())
32943320 buf .Reset ()
0 commit comments