@@ -1510,12 +1510,12 @@ func checkRangeColumnsPartitionValue(ctx sessionctx.Context, tbInfo *model.Table
15101510 // Range columns partition key supports multiple data types with integer、datetime、string.
15111511 defs := pi .Definitions
15121512 if len (defs ) < 1 {
1513- return errors . Trace ( ErrPartitionsMustBeDefined )
1513+ return ast . ErrPartitionsMustBeDefined . GenWithStackByArgs ( "RANGE" )
15141514 }
15151515
15161516 curr := & defs [0 ]
15171517 if len (curr .LessThan ) != len (pi .Columns ) {
1518- return errors .Trace (ErrPartitionColumnList )
1518+ return errors .Trace (ast . ErrPartitionColumnList )
15191519 }
15201520 for i := 1 ; i < len (defs ); i ++ {
15211521 prev , curr := curr , & defs [i ]
@@ -1532,7 +1532,7 @@ func checkRangeColumnsPartitionValue(ctx sessionctx.Context, tbInfo *model.Table
15321532
15331533func checkTwoRangeColumns (ctx sessionctx.Context , curr , prev * model.PartitionDefinition , pi * model.PartitionInfo , tbInfo * model.TableInfo ) (bool , error ) {
15341534 if len (curr .LessThan ) != len (pi .Columns ) {
1535- return false , errors .Trace (ErrPartitionColumnList )
1535+ return false , errors .Trace (ast . ErrPartitionColumnList )
15361536 }
15371537 for i := 0 ; i < len (pi .Columns ); i ++ {
15381538 // Special handling for MAXVALUE.
@@ -1740,8 +1740,7 @@ func resolveAlterTableSpec(ctx sessionctx.Context, specs []*ast.AlterTableSpec)
17401740 validSpecs = append (validSpecs , spec )
17411741 }
17421742
1743- if len (validSpecs ) != 1 {
1744- // TODO: Hanlde len(validSpecs) == 0.
1743+ if len (validSpecs ) > 1 {
17451744 // Now we only allow one schema changing at the same time.
17461745 return nil , errRunMultiSchemaChanges
17471746 }
@@ -1828,6 +1827,9 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A
18281827 err = ErrUnsupportedModifyPrimaryKey .GenWithStackByArgs ("drop" )
18291828 case ast .AlterTableRenameIndex :
18301829 err = d .RenameIndex (ctx , ident , spec )
1830+ case ast .AlterTablePartition :
1831+ // Prevent silent succeed if user executes ALTER TABLE x PARTITION BY ...
1832+ err = errors .New ("alter table partition is unsupported" )
18311833 case ast .AlterTableOption :
18321834 for i , opt := range spec .Options {
18331835 switch opt .Tp {
@@ -2053,10 +2055,6 @@ func (d *ddl) AddTablePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
20532055 if meta .GetPartitionInfo () == nil {
20542056 return errors .Trace (ErrPartitionMgmtOnNonpartitioned )
20552057 }
2056- // We don't support add hash type partition now.
2057- if meta .Partition .Type == model .PartitionTypeHash {
2058- return errors .Trace (ErrUnsupportedAddPartition )
2059- }
20602058
20612059 partInfo , err := buildPartitionInfo (meta , d , spec )
20622060 if err != nil {
@@ -2108,20 +2106,28 @@ func (d *ddl) CoalescePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
21082106 return errors .Trace (ErrPartitionMgmtOnNonpartitioned )
21092107 }
21102108
2111- // Coalesce partition can only be used on hash/key partitions.
2112- if meta .Partition .Type == model .PartitionTypeRange {
2113- return errors .Trace (ErrCoalesceOnlyOnHashPartition )
2114- }
2115-
2109+ switch meta .Partition .Type {
21162110 // We don't support coalesce partitions hash type partition now.
2117- if meta . Partition . Type == model .PartitionTypeHash {
2111+ case model .PartitionTypeHash :
21182112 return errors .Trace (ErrUnsupportedCoalescePartition )
2113+
2114+ // Key type partition cannot be constructed currently, ignoring it for now.
2115+ case model .PartitionTypeKey :
2116+
2117+ // Coalesce partition can only be used on hash/key partitions.
2118+ default :
2119+ return errors .Trace (ErrCoalesceOnlyOnHashPartition )
21192120 }
21202121
21212122 return errors .Trace (err )
21222123}
21232124
21242125func (d * ddl ) TruncateTablePartition (ctx sessionctx.Context , ident ast.Ident , spec * ast.AlterTableSpec ) error {
2126+ // TODO: Support truncate multiple partitions
2127+ if len (spec .PartitionNames ) != 1 {
2128+ return errRunMultiSchemaChanges
2129+ }
2130+
21252131 is := d .infoHandle .Get ()
21262132 schema , ok := is .SchemaByName (ident .Schema )
21272133 if ! ok {
@@ -2137,7 +2143,7 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
21372143 }
21382144
21392145 var pid int64
2140- pid , err = tables .FindPartitionByName (meta , spec .Name )
2146+ pid , err = tables .FindPartitionByName (meta , spec .PartitionNames [ 0 ]. L )
21412147 if err != nil {
21422148 return errors .Trace (err )
21432149 }
@@ -2159,6 +2165,11 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
21592165}
21602166
21612167func (d * ddl ) DropTablePartition (ctx sessionctx.Context , ident ast.Ident , spec * ast.AlterTableSpec ) error {
2168+ // TODO: Support drop multiple partitions
2169+ if len (spec .PartitionNames ) != 1 {
2170+ return errRunMultiSchemaChanges
2171+ }
2172+
21622173 is := d .infoHandle .Get ()
21632174 schema , ok := is .SchemaByName (ident .Schema )
21642175 if ! ok {
@@ -2172,7 +2183,9 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *
21722183 if meta .GetPartitionInfo () == nil {
21732184 return errors .Trace (ErrPartitionMgmtOnNonpartitioned )
21742185 }
2175- err = checkDropTablePartition (meta , spec .Name )
2186+
2187+ partName := spec .PartitionNames [0 ].L
2188+ err = checkDropTablePartition (meta , partName )
21762189 if err != nil {
21772190 return errors .Trace (err )
21782191 }
@@ -2182,7 +2195,7 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *
21822195 TableID : meta .ID ,
21832196 Type : model .ActionDropTablePartition ,
21842197 BinlogInfo : & model.HistoryInfo {},
2185- Args : []interface {}{spec . Name },
2198+ Args : []interface {}{partName },
21862199 }
21872200
21882201 err = d .doDDLJob (ctx , job )
@@ -3140,9 +3153,15 @@ func validateCommentLength(vars *variable.SessionVars, comment string, maxLen in
31403153}
31413154
31423155func buildPartitionInfo (meta * model.TableInfo , d * ddl , spec * ast.AlterTableSpec ) (* model.PartitionInfo , error ) {
3143- if meta .Partition .Type == model .PartitionTypeRange && len (spec .PartDefinitions ) == 0 {
3144- return nil , errors .Trace (ErrPartitionsMustBeDefined )
3156+ if meta .Partition .Type == model .PartitionTypeRange {
3157+ if len (spec .PartDefinitions ) == 0 {
3158+ return nil , ast .ErrPartitionsMustBeDefined .GenWithStackByArgs (meta .Partition .Type )
3159+ }
3160+ } else {
3161+ // we don't support ADD PARTITION for all other partition types yet.
3162+ return nil , errors .Trace (ErrUnsupportedAddPartition )
31453163 }
3164+
31463165 part := & model.PartitionInfo {
31473166 Type : meta .Partition .Type ,
31483167 Expr : meta .Partition .Expr ,
@@ -3151,7 +3170,12 @@ func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec)
31513170 }
31523171 buf := new (bytes.Buffer )
31533172 for _ , def := range spec .PartDefinitions {
3154- for _ , expr := range def .LessThan {
3173+ if err := def .Clause .Validate (part .Type , len (part .Columns )); err != nil {
3174+ return nil , errors .Trace (err )
3175+ }
3176+ // For RANGE partition only VALUES LESS THAN should be possible.
3177+ clause := def .Clause .(* ast.PartitionDefinitionClauseLessThan )
3178+ for _ , expr := range clause .Exprs {
31553179 tp := expr .GetType ().Tp
31563180 if len (part .Columns ) == 0 {
31573181 // Partition by range.
@@ -3170,14 +3194,15 @@ func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec)
31703194 if err1 != nil {
31713195 return nil , errors .Trace (err1 )
31723196 }
3197+ comment , _ := def .Comment ()
31733198 piDef := model.PartitionDefinition {
31743199 Name : def .Name ,
31753200 ID : pid ,
3176- Comment : def . Comment ,
3201+ Comment : comment ,
31773202 }
31783203
31793204 buf := new (bytes.Buffer )
3180- for _ , expr := range def . LessThan {
3205+ for _ , expr := range clause . Exprs {
31813206 expr .Format (buf )
31823207 piDef .LessThan = append (piDef .LessThan , buf .String ())
31833208 buf .Reset ()
0 commit comments