Skip to content

Commit

Permalink
schemadiff: RangeRotationDistinctStatements partitioning hint (#10309)
Browse files Browse the repository at this point in the history
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
  • Loading branch information
shlomi-noach committed May 15, 2022
1 parent eb07fa2 commit 3891d40
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 33 deletions.
25 changes: 5 additions & 20 deletions go/vt/schemadiff/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,22 +602,11 @@ func (c *CreateTableEntity) TableDiff(other *CreateTableEntity, hints *DiffHints
return nil, err
}
}
if len(alterTable.AlterOptions) == 0 && alterTable.PartitionOption == nil && alterTable.PartitionSpec == nil && len(partitionSpecs) == 0 {
// it's possible that the table definitions are different, and still there's no
// "real" difference. Reasons could be:
// - reordered keys -- we treat that as non-diff
return nil, nil
}
if len(partitionSpecs) == 0 {
return &AlterTableEntityDiff{alterTable: alterTable, from: c, to: other}, nil
}
// partitionSpecs has multiple entries
if len(alterTable.AlterOptions) > 0 ||
alterTable.PartitionOption != nil ||
alterTable.PartitionSpec != nil {
return nil, ErrMixedPartitionAndNonPartitionChanges
}
var parentAlterTableEntityDiff *AlterTableEntityDiff
tableSpecHasChanged := len(alterTable.AlterOptions) > 0 || alterTable.PartitionOption != nil || alterTable.PartitionSpec != nil
if tableSpecHasChanged {
parentAlterTableEntityDiff = &AlterTableEntityDiff{alterTable: alterTable, from: c, to: other}
}
for _, partitionSpec := range partitionSpecs {
alterTable := &sqlparser.AlterTable{
Table: otherStmt.Table,
Expand Down Expand Up @@ -942,11 +931,7 @@ func (c *CreateTableEntity) diffPartitions(alterTable *sqlparser.AlterTable,
switch hints.RangeRotationStrategy {
case RangeRotationIgnore:
return nil, nil
case RangeRotationStatements:
if len(partitionSpecs) == 1 {
alterTable.PartitionSpec = partitionSpecs[0]
partitionSpecs = nil
}
case RangeRotationDistinctStatements:
return partitionSpecs, nil
case RangeRotationFullSpec:
// proceed to return a full rebuild
Expand Down
24 changes: 12 additions & 12 deletions go/vt/schemadiff/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,57 +445,57 @@ func TestCreateTableDiff(t *testing.T) {
name: "change partitioning range: statements, drop",
from: "create table t1 (id int primary key) partition by range (id) (partition p1 values less than (10), partition p2 values less than (20), partition p3 values less than (30))",
to: "create table t1 (id int primary key) partition by range (id) (partition p2 values less than (20), partition p3 values less than (30))",
rotation: RangeRotationStatements,
rotation: RangeRotationDistinctStatements,
diff: "alter table t1 drop partition p1",
cdiff: "ALTER TABLE `t1` DROP PARTITION `p1`",
},
{
name: "change partitioning range: statements, add",
from: "create table t1 (id int primary key) partition by range (id) (partition p1 values less than (10), partition p2 values less than (20))",
to: "create table t1 (id int primary key) partition by range (id) (partition p1 values less than (10), partition p2 values less than (20), partition p3 values less than (30))",
rotation: RangeRotationStatements,
rotation: RangeRotationDistinctStatements,
diff: "alter table t1 add partition (partition p3 values less than (30))",
cdiff: "ALTER TABLE `t1` ADD PARTITION (PARTITION `p3` VALUES LESS THAN (30))",
},
{
name: "change partitioning range: statements, multiple drops",
from: "create table t1 (id int primary key) partition by range (id) (partition p1 values less than (10), partition p2 values less than (20), partition p3 values less than (30))",
to: "create table t1 (id int primary key) partition by range (id) (partition p3 values less than (30))",
rotation: RangeRotationStatements,
rotation: RangeRotationDistinctStatements,
diffs: []string{"alter table t1 drop partition p1", "alter table t1 drop partition p2"},
cdiffs: []string{"ALTER TABLE `t1` DROP PARTITION `p1`", "ALTER TABLE `t1` DROP PARTITION `p2`"},
},
{
name: "change partitioning range: statements, multiple adds",
from: "create table t1 (id int primary key) partition by range (id) (partition p1 values less than (10))",
to: "create table t1 (id int primary key) partition by range (id) (partition p1 values less than (10), partition p2 values less than (20), partition p3 values less than (30))",
rotation: RangeRotationStatements,
rotation: RangeRotationDistinctStatements,
diffs: []string{"alter table t1 add partition (partition p2 values less than (20))", "alter table t1 add partition (partition p3 values less than (30))"},
cdiffs: []string{"ALTER TABLE `t1` ADD PARTITION (PARTITION `p2` VALUES LESS THAN (20))", "ALTER TABLE `t1` ADD PARTITION (PARTITION `p3` VALUES LESS THAN (30))"},
},
{
name: "change partitioning range: statements, multiple, assorted",
from: "create table t1 (id int primary key) partition by range (id) (partition p1 values less than (10), partition p2 values less than (20), partition p3 values less than (30))",
to: "create table t1 (id int primary key) partition by range (id) (partition p2 values less than (20), partition p3 values less than (30), partition p4 values less than (40))",
rotation: RangeRotationStatements,
rotation: RangeRotationDistinctStatements,
diffs: []string{"alter table t1 drop partition p1", "alter table t1 add partition (partition p4 values less than (40))"},
cdiffs: []string{"ALTER TABLE `t1` DROP PARTITION `p1`", "ALTER TABLE `t1` ADD PARTITION (PARTITION `p4` VALUES LESS THAN (40))"},
},
{
name: "change partitioning range: mixed with nonpartition changes",
from: "create table t1 (id int primary key) partition by range (id) (partition p1 values less than (10), partition p2 values less than (20), partition p3 values less than (30))",
to: "create table t1 (id int primary key, i int) partition by range (id) (partition p3 values less than (30))",
rotation: RangeRotationStatements,
isError: true,
errorMsg: ErrMixedPartitionAndNonPartitionChanges.Error(),
rotation: RangeRotationDistinctStatements,
diffs: []string{"alter table t1 add column i int", "alter table t1 drop partition p1", "alter table t1 drop partition p2"},
cdiffs: []string{"ALTER TABLE `t1` ADD COLUMN `i` int", "ALTER TABLE `t1` DROP PARTITION `p1`", "ALTER TABLE `t1` DROP PARTITION `p2`"},
},
{
name: "change partitioning range: single partition change, no mix error",
name: "change partitioning range: single partition change, mixed with nonpartition changes",
from: "create table t1 (id int primary key) partition by range (id) (partition p1 values less than (10), partition p2 values less than (20))",
to: "create table t1 (id int primary key, i int) partition by range (id) (partition p2 values less than (20))",
rotation: RangeRotationStatements,
diff: "alter table t1 add column i int, drop partition p1",
cdiff: "ALTER TABLE `t1` ADD COLUMN `i` int, DROP PARTITION `p1`",
rotation: RangeRotationDistinctStatements,
diffs: []string{"alter table t1 add column i int", "alter table t1 drop partition p1"},
cdiffs: []string{"ALTER TABLE `t1` ADD COLUMN `i` int", "ALTER TABLE `t1` DROP PARTITION `p1`"},
},
{
name: "change partitioning range: mixed with nonpartition changes, full spec",
Expand Down
2 changes: 1 addition & 1 deletion go/vt/schemadiff/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const (

const (
RangeRotationFullSpec = iota
RangeRotationStatements
RangeRotationDistinctStatements
RangeRotationIgnore
)

Expand Down

0 comments on commit 3891d40

Please sign in to comment.