Skip to content

Commit

Permalink
ddl: fix a bug that adding range partition meets error when value is …
Browse files Browse the repository at this point in the history
…negative (#11407)
  • Loading branch information
tiancaiamao authored and sre-bot committed Aug 1, 2019
1 parent f13f7bb commit 10e8498
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
16 changes: 16 additions & 0 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,19 @@ func (s *testIntegrationSuite5) TestAlterTableAddPartition(c *C) {

sql10 := "alter table table3 add partition partitions 4;"
assertErrorCode(c, tk, sql10, tmysql.ErrPartitionsMustBeDefined)

tk.MustExec("alter table table3 add partition (partition p3 values less than (2001 + 10))")

// less than value can be negative or expression.
tk.MustExec(`CREATE TABLE tt5 (
c3 bigint(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
PARTITION BY RANGE ( c3 ) (
PARTITION p0 VALUES LESS THAN (-3),
PARTITION p1 VALUES LESS THAN (-2)
);`)
tk.MustExec(`ALTER TABLE tt5 add partition ( partition p2 values less than (-1) );`)
tk.MustExec(`ALTER TABLE tt5 add partition ( partition p3 values less than (5-1) );`)
}

func (s *testIntegrationSuite5) TestAlterTableDropPartition(c *C) {
Expand Down Expand Up @@ -1453,6 +1466,9 @@ func (s *testIntegrationSuite3) TestPartitionErrorCode(c *C) {
_, err := tk.Exec("alter table employees add partition partitions 8;")
c.Assert(ddl.ErrUnsupportedAddPartition.Equal(err), IsTrue)

_, err = tk.Exec("alter table employees add partition (partition p5 values less than (42));")
c.Assert(ddl.ErrUnsupportedAddPartition.Equal(err), IsTrue)

// coalesce partition
tk.MustExec(`create table clients (
id int,
Expand Down
26 changes: 8 additions & 18 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2156,7 +2156,8 @@ func (d *ddl) AddTablePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
}

meta := t.Meta()
if meta.GetPartitionInfo() == nil {
pi := meta.GetPartitionInfo()
if pi == nil {
return errors.Trace(ErrPartitionMgmtOnNonpartitioned)
}

Expand All @@ -2179,7 +2180,11 @@ func (d *ddl) AddTablePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
return errors.Trace(err)
}

err = checkAddPartitionValue(meta, partInfo)
// partInfo contains only the new added partition, we have to combine it with the
// old partitions to check all partitions is strictly increasing.
tmp := *partInfo
tmp.Definitions = append(pi.Definitions, tmp.Definitions...)
err = checkCreatePartitionValue(ctx, meta, &tmp, t.Cols())
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -3422,32 +3427,17 @@ func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec)
Columns: meta.Partition.Columns,
Enable: meta.Partition.Enable,
}

genIDs, err := d.genGlobalIDs(len(spec.PartDefinitions))
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
for ith, def := range spec.PartDefinitions {
if err := def.Clause.Validate(part.Type, len(part.Columns)); err != nil {
return nil, errors.Trace(err)
}
// For RANGE partition only VALUES LESS THAN should be possible.
clause := def.Clause.(*ast.PartitionDefinitionClauseLessThan)
for _, expr := range clause.Exprs {
tp := expr.GetType().Tp
if len(part.Columns) == 0 {
// Partition by range.
if !(tp == mysql.TypeLong || tp == mysql.TypeLonglong) {
expr.Format(buf)
if strings.EqualFold(buf.String(), "MAXVALUE") {
continue
}
buf.Reset()
return nil, infoschema.ErrColumnNotExists.GenWithStackByArgs(buf.String(), "partition function")
}
}
// Partition by range columns if len(part.Columns) != 0.
}
comment, _ := def.Comment()
piDef := model.PartitionDefinition{
Name: def.Name,
Expand Down

0 comments on commit 10e8498

Please sign in to comment.