From 113b975fde96c854fdc8dfc6fbd020f7665d5979 Mon Sep 17 00:00:00 2001 From: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com> Date: Mon, 15 Apr 2024 18:44:18 +0300 Subject: [PATCH] `schemadiff`: `DROP COLUMN` not eligible for `INSTANT` algorithm if covered by an index (#15714) Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com> --- go/vt/schemadiff/capability.go | 14 ++++++++++++++ go/vt/schemadiff/capability_test.go | 12 ++++++++++++ 2 files changed, 26 insertions(+) diff --git a/go/vt/schemadiff/capability.go b/go/vt/schemadiff/capability.go index 532cfd72fe8..fd4c8389240 100644 --- a/go/vt/schemadiff/capability.go +++ b/go/vt/schemadiff/capability.go @@ -21,6 +21,16 @@ func alterOptionCapableOfInstantDDL(alterOption sqlparser.AlterOption, createTab } return nil } + findIndexCoveringColumn := func(colName string) *sqlparser.IndexDefinition { + for _, index := range createTable.TableSpec.Indexes { + for _, col := range index.Columns { + if col.Column.String() == colName { + return index + } + } + } + return nil + } findTableOption := func(optName string) *sqlparser.TableOption { if createTable == nil { return nil @@ -90,6 +100,10 @@ func alterOptionCapableOfInstantDDL(alterOption sqlparser.AlterOption, createTab return false, nil } } + if findIndexCoveringColumn(opt.Name.Name.String()) != nil { + // not supported if the column is part of an index + return false, nil + } if isVirtualColumn(opt.Name.Name.String()) { // supported by all 8.0 versions return capableOf(capabilities.InstantAddDropVirtualColumnFlavorCapability) diff --git a/go/vt/schemadiff/capability_test.go b/go/vt/schemadiff/capability_test.go index b417c3589a3..4bbcb0ecf54 100644 --- a/go/vt/schemadiff/capability_test.go +++ b/go/vt/schemadiff/capability_test.go @@ -85,6 +85,18 @@ func TestAlterTableCapableOfInstantDDL(t *testing.T) { alter: "alter table t drop column i1", expectCapableOfInstantDDL: false, }, + { + name: "drop column fail due to index", + create: "create table t(id int, i1 int not null, i2 int not null, primary key(id), key i1_idx (i1))", + alter: "alter table t drop column i1", + expectCapableOfInstantDDL: false, + }, + { + name: "drop column fail due to multicolumn index", + create: "create table t(id int, i1 int not null, i2 int not null, primary key(id), key i21_idx (i2, i1))", + alter: "alter table t drop column i1", + expectCapableOfInstantDDL: false, + }, { name: "add two columns", create: "create table t(id int, i1 int not null, primary key(id))",