@@ -1517,12 +1517,12 @@ func checkRangeColumnsPartitionValue(ctx sessionctx.Context, tbInfo *model.Table
15171517 // Range columns partition key supports multiple data types with integer、datetime、string.
15181518 defs := pi .Definitions
15191519 if len (defs ) < 1 {
1520- return errors . Trace ( ErrPartitionsMustBeDefined )
1520+ return ast . ErrPartitionsMustBeDefined . GenWithStackByArgs ( "RANGE" )
15211521 }
15221522
15231523 curr := & defs [0 ]
15241524 if len (curr .LessThan ) != len (pi .Columns ) {
1525- return errors .Trace (ErrPartitionColumnList )
1525+ return errors .Trace (ast . ErrPartitionColumnList )
15261526 }
15271527 for i := 1 ; i < len (defs ); i ++ {
15281528 prev , curr := curr , & defs [i ]
@@ -1539,7 +1539,7 @@ func checkRangeColumnsPartitionValue(ctx sessionctx.Context, tbInfo *model.Table
15391539
15401540func checkTwoRangeColumns (ctx sessionctx.Context , curr , prev * model.PartitionDefinition , pi * model.PartitionInfo , tbInfo * model.TableInfo ) (bool , error ) {
15411541 if len (curr .LessThan ) != len (pi .Columns ) {
1542- return false , errors .Trace (ErrPartitionColumnList )
1542+ return false , errors .Trace (ast . ErrPartitionColumnList )
15431543 }
15441544 for i := 0 ; i < len (pi .Columns ); i ++ {
15451545 // Special handling for MAXVALUE.
@@ -1747,8 +1747,7 @@ func resolveAlterTableSpec(ctx sessionctx.Context, specs []*ast.AlterTableSpec)
17471747 validSpecs = append (validSpecs , spec )
17481748 }
17491749
1750- if len (validSpecs ) != 1 {
1751- // TODO: Hanlde len(validSpecs) == 0.
1750+ if len (validSpecs ) > 1 {
17521751 // Now we only allow one schema changing at the same time.
17531752 return nil , errRunMultiSchemaChanges
17541753 }
@@ -1835,6 +1834,9 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A
18351834 err = ErrUnsupportedModifyPrimaryKey .GenWithStackByArgs ("drop" )
18361835 case ast .AlterTableRenameIndex :
18371836 err = d .RenameIndex (ctx , ident , spec )
1837+ case ast .AlterTablePartition :
1838+ // Prevent silent succeed if user executes ALTER TABLE x PARTITION BY ...
1839+ err = errors .New ("alter table partition is unsupported" )
18381840 case ast .AlterTableOption :
18391841 for i , opt := range spec .Options {
18401842 switch opt .Tp {
@@ -2060,10 +2062,6 @@ func (d *ddl) AddTablePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
20602062 if meta .GetPartitionInfo () == nil {
20612063 return errors .Trace (ErrPartitionMgmtOnNonpartitioned )
20622064 }
2063- // We don't support add hash type partition now.
2064- if meta .Partition .Type == model .PartitionTypeHash {
2065- return errors .Trace (ErrUnsupportedAddPartition )
2066- }
20672065
20682066 partInfo , err := buildPartitionInfo (meta , d , spec )
20692067 if err != nil {
@@ -2115,20 +2113,27 @@ func (d *ddl) CoalescePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
21152113 return errors .Trace (ErrPartitionMgmtOnNonpartitioned )
21162114 }
21172115
2116+ switch meta .Partition .Type {
21182117 // Coalesce partition can only be used on hash/key partitions.
2119- if meta . Partition . Type == model . PartitionTypeRange {
2118+ default :
21202119 return errors .Trace (ErrCoalesceOnlyOnHashPartition )
2121- }
21222120
21232121 // We don't support coalesce partitions hash type partition now.
2124- if meta . Partition . Type == model .PartitionTypeHash {
2122+ case model .PartitionTypeHash :
21252123 return errors .Trace (ErrUnsupportedCoalescePartition )
2124+
2125+ case model .PartitionTypeKey :
21262126 }
21272127
21282128 return errors .Trace (err )
21292129}
21302130
21312131func (d * ddl ) TruncateTablePartition (ctx sessionctx.Context , ident ast.Ident , spec * ast.AlterTableSpec ) error {
2132+ // TODO: Support truncate multiple partitions
2133+ if len (spec .PartitionNames ) != 1 {
2134+ return errRunMultiSchemaChanges
2135+ }
2136+
21322137 is := d .infoHandle .Get ()
21332138 schema , ok := is .SchemaByName (ident .Schema )
21342139 if ! ok {
@@ -2144,7 +2149,7 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
21442149 }
21452150
21462151 var pid int64
2147- pid , err = tables .FindPartitionByName (meta , spec .Name )
2152+ pid , err = tables .FindPartitionByName (meta , spec .PartitionNames [ 0 ]. L )
21482153 if err != nil {
21492154 return errors .Trace (err )
21502155 }
@@ -2166,6 +2171,11 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
21662171}
21672172
21682173func (d * ddl ) DropTablePartition (ctx sessionctx.Context , ident ast.Ident , spec * ast.AlterTableSpec ) error {
2174+ // TODO: Support drop multiple partitions
2175+ if len (spec .PartitionNames ) != 1 {
2176+ return errRunMultiSchemaChanges
2177+ }
2178+
21692179 is := d .infoHandle .Get ()
21702180 schema , ok := is .SchemaByName (ident .Schema )
21712181 if ! ok {
@@ -2179,7 +2189,9 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *
21792189 if meta .GetPartitionInfo () == nil {
21802190 return errors .Trace (ErrPartitionMgmtOnNonpartitioned )
21812191 }
2182- err = checkDropTablePartition (meta , spec .Name )
2192+
2193+ partName := spec .PartitionNames [0 ].L
2194+ err = checkDropTablePartition (meta , partName )
21832195 if err != nil {
21842196 return errors .Trace (err )
21852197 }
@@ -2189,7 +2201,7 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *
21892201 TableID : meta .ID ,
21902202 Type : model .ActionDropTablePartition ,
21912203 BinlogInfo : & model.HistoryInfo {},
2192- Args : []interface {}{spec . Name },
2204+ Args : []interface {}{partName },
21932205 }
21942206
21952207 err = d .doDDLJob (ctx , job )
@@ -3147,9 +3159,15 @@ func validateCommentLength(vars *variable.SessionVars, comment string, maxLen in
31473159}
31483160
31493161func buildPartitionInfo (meta * model.TableInfo , d * ddl , spec * ast.AlterTableSpec ) (* model.PartitionInfo , error ) {
3150- if meta .Partition .Type == model .PartitionTypeRange && len (spec .PartDefinitions ) == 0 {
3151- return nil , errors .Trace (ErrPartitionsMustBeDefined )
3162+ if meta .Partition .Type == model .PartitionTypeRange {
3163+ if len (spec .PartDefinitions ) == 0 {
3164+ return nil , ast .ErrPartitionsMustBeDefined .GenWithStackByArgs (meta .Partition .Type )
3165+ }
3166+ } else {
3167+ // we don't support ADD PARTITION for all other partition types yet.
3168+ return nil , errors .Trace (ErrUnsupportedAddPartition )
31523169 }
3170+
31533171 part := & model.PartitionInfo {
31543172 Type : meta .Partition .Type ,
31553173 Expr : meta .Partition .Expr ,
@@ -3158,7 +3176,12 @@ func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec)
31583176 }
31593177 buf := new (bytes.Buffer )
31603178 for _ , def := range spec .PartDefinitions {
3161- for _ , expr := range def .LessThan {
3179+ if err := def .Clause .Validate (part .Type , len (part .Columns )); err != nil {
3180+ return nil , errors .Trace (err )
3181+ }
3182+ // For RANGE partition only VALUES LESS THAN should be possible.
3183+ clause := def .Clause .(* ast.PartitionDefinitionClauseLessThan )
3184+ for _ , expr := range clause .Exprs {
31623185 tp := expr .GetType ().Tp
31633186 if len (part .Columns ) == 0 {
31643187 // Partition by range.
@@ -3177,14 +3200,15 @@ func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec)
31773200 if err1 != nil {
31783201 return nil , errors .Trace (err1 )
31793202 }
3203+ comment , _ := def .Comment ()
31803204 piDef := model.PartitionDefinition {
31813205 Name : def .Name ,
31823206 ID : pid ,
3183- Comment : def . Comment ,
3207+ Comment : comment ,
31843208 }
31853209
31863210 buf := new (bytes.Buffer )
3187- for _ , expr := range def . LessThan {
3211+ for _ , expr := range clause . Exprs {
31883212 expr .Format (buf )
31893213 piDef .LessThan = append (piDef .LessThan , buf .String ())
31903214 buf .Reset ()
0 commit comments