Skip to content

Commit

Permalink
v16: Online DDL: enforce ALGORITHM=COPY on shadow table (#12522)
Browse files Browse the repository at this point in the history
* Online DDL: enforce ALGORITHM=COPY on shadow table

Signed-off-by: Shlomi Noach <[email protected]>

* unit tests

Signed-off-by: Shlomi Noach <[email protected]>

* resolve conflict

Signed-off-by: Shlomi Noach <[email protected]>

* algorithm format

Signed-off-by: Shlomi Noach <[email protected]>

* revert change to sql.y

Signed-off-by: Shlomi Noach <[email protected]>

---------

Signed-off-by: Shlomi Noach <[email protected]>
  • Loading branch information
shlomi-noach authored Mar 2, 2023
1 parent fbfc366 commit 7666f16
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2200,7 +2200,7 @@ func (node *AddColumns) Format(buf *TrackedBuffer) {

// Format formats the node.
func (node AlgorithmValue) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "algorithm = %s", string(node))
buf.astPrintf(node, "algorithm = %#s", string(node))
}

// Format formats the node
Expand Down
6 changes: 6 additions & 0 deletions go/vt/sqlparser/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ const (
AddSequenceStr = "add sequence"
AddAutoIncStr = "add auto_increment"

// ALTER TABLE ALGORITHM string.
DefaultStr = "default"
CopyStr = "copy"
InplaceStr = "inplace"
InstantStr = "instant"

// Partition and subpartition type strings
HashTypeStr = "hash"
KeyTypeStr = "key"
Expand Down
6 changes: 6 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,12 @@ var (
input: "alter table a convert to character set utf32",
}, {
input: "alter table `By` add column foo int, algorithm = default",
}, {
input: "alter table `By` add column foo int, algorithm = copy",
}, {
input: "alter table `By` add column foo int, algorithm = inplace",
}, {
input: "alter table `By` add column foo int, algorithm = INPLACE",
}, {
input: "alter table `By` add column foo int, algorithm = instant",
}, {
Expand Down
7 changes: 6 additions & 1 deletion go/vt/vttablet/onlineddl/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ var vexecInsertTemplates = []string{

var emptyResult = &sqltypes.Result{}
var acceptableDropTableIfExistsErrorCodes = []int{mysql.ERCantFindFile, mysql.ERNoSuchTable}
var copyAlgorithm = sqlparser.AlgorithmValue(sqlparser.CopyStr)

var (
ghostOverridePath string
Expand Down Expand Up @@ -1171,6 +1172,9 @@ func (e *Executor) validateAndEditAlterTableStatement(ctx context.Context, onlin
for i := range alterTable.AlterOptions {
opt := alterTable.AlterOptions[i]
switch opt := opt.(type) {
case sqlparser.AlgorithmValue:
// we do not pass ALGORITHM. We choose our own ALGORITHM.
continue
case *sqlparser.AddIndexDefinition:
if opt.IndexDefinition.Info.Fulltext {
countAddFullTextStatements++
Expand All @@ -1179,7 +1183,7 @@ func (e *Executor) validateAndEditAlterTableStatement(ctx context.Context, onlin
// in the same statement
extraAlterTable := &sqlparser.AlterTable{
Table: alterTable.Table,
AlterOptions: []sqlparser.AlterOption{opt},
AlterOptions: []sqlparser.AlterOption{opt, copyAlgorithm},
}
alters = append(alters, extraAlterTable)
continue
Expand All @@ -1189,6 +1193,7 @@ func (e *Executor) validateAndEditAlterTableStatement(ctx context.Context, onlin
redactedOptions = append(redactedOptions, opt)
}
alterTable.AlterOptions = redactedOptions
alterTable.AlterOptions = append(alterTable.AlterOptions, copyAlgorithm)
return alters, nil
}

Expand Down
24 changes: 12 additions & 12 deletions go/vt/vttablet/onlineddl/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,51 +182,51 @@ func TestValidateAndEditAlterTableStatement(t *testing.T) {
}{
{
alter: "alter table t add column i int",
expect: []string{"alter table t add column i int"},
expect: []string{"alter table t add column i int, algorithm = copy"},
},
{
alter: "alter table t add column i int, add fulltext key name1_ft (name1)",
expect: []string{"alter table t add column i int, add fulltext key name1_ft (name1)"},
expect: []string{"alter table t add column i int, add fulltext key name1_ft (name1), algorithm = copy"},
},
{
alter: "alter table t add column i int, add fulltext key name1_ft (name1), add fulltext key name2_ft (name2)",
expect: []string{"alter table t add column i int, add fulltext key name1_ft (name1)", "alter table t add fulltext key name2_ft (name2)"},
expect: []string{"alter table t add column i int, add fulltext key name1_ft (name1), algorithm = copy", "alter table t add fulltext key name2_ft (name2), algorithm = copy"},
},
{
alter: "alter table t add fulltext key name0_ft (name0), add column i int, add fulltext key name1_ft (name1), add fulltext key name2_ft (name2)",
expect: []string{"alter table t add fulltext key name0_ft (name0), add column i int", "alter table t add fulltext key name1_ft (name1)", "alter table t add fulltext key name2_ft (name2)"},
expect: []string{"alter table t add fulltext key name0_ft (name0), add column i int, algorithm = copy", "alter table t add fulltext key name1_ft (name1), algorithm = copy", "alter table t add fulltext key name2_ft (name2), algorithm = copy"},
},
{
alter: "alter table t add constraint check (id != 1)",
expect: []string{"alter table t add constraint chk_aulpn7bjeortljhguy86phdn9 check (id != 1)"},
expect: []string{"alter table t add constraint chk_aulpn7bjeortljhguy86phdn9 check (id != 1), algorithm = copy"},
},
{
alter: "alter table t add constraint t_chk_1 check (id != 1)",
expect: []string{"alter table t add constraint chk_1_aulpn7bjeortljhguy86phdn9 check (id != 1)"},
expect: []string{"alter table t add constraint chk_1_aulpn7bjeortljhguy86phdn9 check (id != 1), algorithm = copy"},
},
{
alter: "alter table t add constraint some_check check (id != 1)",
expect: []string{"alter table t add constraint some_check_aulpn7bjeortljhguy86phdn9 check (id != 1)"},
expect: []string{"alter table t add constraint some_check_aulpn7bjeortljhguy86phdn9 check (id != 1), algorithm = copy"},
},
{
alter: "alter table t add constraint some_check check (id != 1), add constraint another_check check (id != 2)",
expect: []string{"alter table t add constraint some_check_aulpn7bjeortljhguy86phdn9 check (id != 1), add constraint another_check_4fa197273p3w96267pzm3gfi3 check (id != 2)"},
expect: []string{"alter table t add constraint some_check_aulpn7bjeortljhguy86phdn9 check (id != 1), add constraint another_check_4fa197273p3w96267pzm3gfi3 check (id != 2), algorithm = copy"},
},
{
alter: "alter table t add foreign key (parent_id) references onlineddl_test_parent (id) on delete no action",
expect: []string{"alter table t add constraint fk_6fmhzdlya89128u5j3xapq34i foreign key (parent_id) references onlineddl_test_parent (id) on delete no action"},
expect: []string{"alter table t add constraint fk_6fmhzdlya89128u5j3xapq34i foreign key (parent_id) references onlineddl_test_parent (id) on delete no action, algorithm = copy"},
},
{
alter: "alter table t add constraint myfk foreign key (parent_id) references onlineddl_test_parent (id) on delete no action",
expect: []string{"alter table t add constraint myfk_6fmhzdlya89128u5j3xapq34i foreign key (parent_id) references onlineddl_test_parent (id) on delete no action"},
expect: []string{"alter table t add constraint myfk_6fmhzdlya89128u5j3xapq34i foreign key (parent_id) references onlineddl_test_parent (id) on delete no action, algorithm = copy"},
},
{
alter: "alter table t add constraint t_fk_1 foreign key (parent_id) references onlineddl_test_parent (id) on delete no action",
expect: []string{"alter table t add constraint fk_1_6fmhzdlya89128u5j3xapq34i foreign key (parent_id) references onlineddl_test_parent (id) on delete no action"},
expect: []string{"alter table t add constraint fk_1_6fmhzdlya89128u5j3xapq34i foreign key (parent_id) references onlineddl_test_parent (id) on delete no action, algorithm = copy"},
},
{
alter: "alter table t add constraint t_fk_1 foreign key (parent_id) references onlineddl_test_parent (id) on delete no action, add constraint some_check check (id != 1)",
expect: []string{"alter table t add constraint fk_1_6fmhzdlya89128u5j3xapq34i foreign key (parent_id) references onlineddl_test_parent (id) on delete no action, add constraint some_check_aulpn7bjeortljhguy86phdn9 check (id != 1)"},
expect: []string{"alter table t add constraint fk_1_6fmhzdlya89128u5j3xapq34i foreign key (parent_id) references onlineddl_test_parent (id) on delete no action, add constraint some_check_aulpn7bjeortljhguy86phdn9 check (id != 1), algorithm = copy"},
},
}
for _, tc := range tt {
Expand Down

0 comments on commit 7666f16

Please sign in to comment.