@@ -1563,12 +1563,12 @@ func checkRangeColumnsPartitionValue(ctx sessionctx.Context, tbInfo *model.Table
15631563 // Range columns partition key supports multiple data types with integer、datetime、string.
15641564 defs := pi .Definitions
15651565 if len (defs ) < 1 {
1566- return errors . Trace ( ErrPartitionsMustBeDefined )
1566+ return ast . ErrPartitionsMustBeDefined . GenWithStackByArgs ( "RANGE" )
15671567 }
15681568
15691569 curr := & defs [0 ]
15701570 if len (curr .LessThan ) != len (pi .Columns ) {
1571- return errors .Trace (ErrPartitionColumnList )
1571+ return errors .Trace (ast . ErrPartitionColumnList )
15721572 }
15731573 for i := 1 ; i < len (defs ); i ++ {
15741574 prev , curr := curr , & defs [i ]
@@ -1585,7 +1585,7 @@ func checkRangeColumnsPartitionValue(ctx sessionctx.Context, tbInfo *model.Table
15851585
15861586func checkTwoRangeColumns (ctx sessionctx.Context , curr , prev * model.PartitionDefinition , pi * model.PartitionInfo , tbInfo * model.TableInfo ) (bool , error ) {
15871587 if len (curr .LessThan ) != len (pi .Columns ) {
1588- return false , errors .Trace (ErrPartitionColumnList )
1588+ return false , errors .Trace (ast . ErrPartitionColumnList )
15891589 }
15901590 for i := 0 ; i < len (pi .Columns ); i ++ {
15911591 // Special handling for MAXVALUE.
@@ -1792,8 +1792,7 @@ func resolveAlterTableSpec(ctx sessionctx.Context, specs []*ast.AlterTableSpec)
17921792 validSpecs = append (validSpecs , spec )
17931793 }
17941794
1795- if len (validSpecs ) != 1 {
1796- // TODO: Hanlde len(validSpecs) == 0.
1795+ if len (validSpecs ) > 1 {
17971796 // Now we only allow one schema changing at the same time.
17981797 return nil , errRunMultiSchemaChanges
17991798 }
@@ -1880,6 +1879,9 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A
18801879 err = ErrUnsupportedModifyPrimaryKey .GenWithStackByArgs ("drop" )
18811880 case ast .AlterTableRenameIndex :
18821881 err = d .RenameIndex (ctx , ident , spec )
1882+ case ast .AlterTablePartition :
1883+ // Prevent silent succeed if user executes ALTER TABLE x PARTITION BY ...
1884+ err = errors .New ("alter table partition is unsupported" )
18831885 case ast .AlterTableOption :
18841886 for i , opt := range spec .Options {
18851887 switch opt .Tp {
@@ -2104,11 +2106,7 @@ func (d *ddl) AddTablePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
21042106
21052107 meta := t .Meta ()
21062108 if meta .GetPartitionInfo () == nil {
2107- return errors .Trace (ErrPartitionMgmtOnNonpartitioned )
2108- }
2109- // We don't support add hash type partition now.
2110- if meta .Partition .Type == model .PartitionTypeHash {
2111- return errors .Trace (ErrUnsupportedAddPartition )
2109+ return errors .Trace (ast .ErrPartitionColumnList )
21122110 }
21132111
21142112 partInfo , err := buildPartitionInfo (meta , d , spec )
@@ -2161,20 +2159,28 @@ func (d *ddl) CoalescePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
21612159 return errors .Trace (ErrPartitionMgmtOnNonpartitioned )
21622160 }
21632161
2164- // Coalesce partition can only be used on hash/key partitions.
2165- if meta .Partition .Type == model .PartitionTypeRange {
2166- return errors .Trace (ErrCoalesceOnlyOnHashPartition )
2167- }
2168-
2162+ switch meta .Partition .Type {
21692163 // We don't support coalesce partitions hash type partition now.
2170- if meta . Partition . Type == model .PartitionTypeHash {
2164+ case model .PartitionTypeHash :
21712165 return errors .Trace (ErrUnsupportedCoalescePartition )
2166+
2167+ // Key type partition cannot be constructed currently, ignoring it for now.
2168+ case model .PartitionTypeKey :
2169+
2170+ // Coalesce partition can only be used on hash/key partitions.
2171+ default :
2172+ return errors .Trace (ErrCoalesceOnlyOnHashPartition )
21722173 }
21732174
21742175 return errors .Trace (err )
21752176}
21762177
21772178func (d * ddl ) TruncateTablePartition (ctx sessionctx.Context , ident ast.Ident , spec * ast.AlterTableSpec ) error {
2179+ // TODO: Support truncate multiple partitions
2180+ if len (spec .PartitionNames ) != 1 {
2181+ return errRunMultiSchemaChanges
2182+ }
2183+
21782184 is := d .infoHandle .Get ()
21792185 schema , ok := is .SchemaByName (ident .Schema )
21802186 if ! ok {
@@ -2190,7 +2196,7 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
21902196 }
21912197
21922198 var pid int64
2193- pid , err = tables .FindPartitionByName (meta , spec .Name )
2199+ pid , err = tables .FindPartitionByName (meta , spec .PartitionNames [ 0 ]. L )
21942200 if err != nil {
21952201 return errors .Trace (err )
21962202 }
@@ -2212,6 +2218,11 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
22122218}
22132219
22142220func (d * ddl ) DropTablePartition (ctx sessionctx.Context , ident ast.Ident , spec * ast.AlterTableSpec ) error {
2221+ // TODO: Support drop multiple partitions
2222+ if len (spec .PartitionNames ) != 1 {
2223+ return errRunMultiSchemaChanges
2224+ }
2225+
22152226 is := d .infoHandle .Get ()
22162227 schema , ok := is .SchemaByName (ident .Schema )
22172228 if ! ok {
@@ -2225,7 +2236,8 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *
22252236 if meta .GetPartitionInfo () == nil {
22262237 return errors .Trace (ErrPartitionMgmtOnNonpartitioned )
22272238 }
2228- err = checkDropTablePartition (meta , spec .Name )
2239+ partName := spec .PartitionNames [0 ].L
2240+ err = checkDropTablePartition (meta , partName )
22292241 if err != nil {
22302242 return errors .Trace (err )
22312243 }
@@ -2235,7 +2247,7 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *
22352247 TableID : meta .ID ,
22362248 Type : model .ActionDropTablePartition ,
22372249 BinlogInfo : & model.HistoryInfo {},
2238- Args : []interface {}{spec . Name },
2250+ Args : []interface {}{partName },
22392251 }
22402252
22412253 err = d .doDDLJob (ctx , job )
@@ -3219,9 +3231,15 @@ func validateCommentLength(vars *variable.SessionVars, comment string, maxLen in
32193231}
32203232
32213233func buildPartitionInfo (meta * model.TableInfo , d * ddl , spec * ast.AlterTableSpec ) (* model.PartitionInfo , error ) {
3222- if meta .Partition .Type == model .PartitionTypeRange && len (spec .PartDefinitions ) == 0 {
3223- return nil , errors .Trace (ErrPartitionsMustBeDefined )
3234+ if meta .Partition .Type == model .PartitionTypeRange {
3235+ if len (spec .PartDefinitions ) == 0 {
3236+ return nil , ast .ErrPartitionsMustBeDefined .GenWithStackByArgs (meta .Partition .Type )
3237+ }
3238+ } else {
3239+ // we don't support ADD PARTITION for all other partition types yet.
3240+ return nil , errors .Trace (ErrUnsupportedAddPartition )
32243241 }
3242+
32253243 part := & model.PartitionInfo {
32263244 Type : meta .Partition .Type ,
32273245 Expr : meta .Partition .Expr ,
@@ -3234,7 +3252,12 @@ func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec)
32343252 }
32353253 buf := new (bytes.Buffer )
32363254 for ith , def := range spec .PartDefinitions {
3237- for _ , expr := range def .LessThan {
3255+ if err := def .Clause .Validate (part .Type , len (part .Columns )); err != nil {
3256+ return nil , errors .Trace (err )
3257+ }
3258+ // For RANGE partition only VALUES LESS THAN should be possible.
3259+ clause := def .Clause .(* ast.PartitionDefinitionClauseLessThan )
3260+ for _ , expr := range clause .Exprs {
32383261 tp := expr .GetType ().Tp
32393262 if len (part .Columns ) == 0 {
32403263 // Partition by range.
@@ -3249,14 +3272,15 @@ func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec)
32493272 }
32503273 // Partition by range columns if len(part.Columns) != 0.
32513274 }
3275+ comment , _ := def .Comment ()
32523276 piDef := model.PartitionDefinition {
32533277 Name : def .Name ,
32543278 ID : genIDs [ith ],
3255- Comment : def . Comment ,
3279+ Comment : comment ,
32563280 }
32573281
32583282 buf := new (bytes.Buffer )
3259- for _ , expr := range def . LessThan {
3283+ for _ , expr := range clause . Exprs {
32603284 expr .Format (buf )
32613285 piDef .LessThan = append (piDef .LessThan , buf .String ())
32623286 buf .Reset ()
0 commit comments