Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schemadiff: RangeRotationDistinctStatements partitioning hint #10309

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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