Skip to content

Commit 5a61bb3

Browse files
authored
fix: dropping an IVFFlat index fails to clean up all associated tables. (#22604)
- Drop all index tables of an ivfflat index. Approved by: @heni02, @ouyuanning, @aunjgr
1 parent 287f51f commit 5a61bb3

File tree

7 files changed

+846
-848
lines changed

7 files changed

+846
-848
lines changed

pkg/pb/plan/plan.pb.go

Lines changed: 780 additions & 831 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/sql/compile/ddl.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,7 +2107,7 @@ func (s *Scope) DropIndex(c *Compile) error {
21072107
if err != nil {
21082108
return err
21092109
}
2110-
newCt, err := makeNewDropConstraint(oldCt, qry.GetIndexName())
2110+
newCt, dropIndexTableNames, err := makeNewDropConstraint(oldCt, qry.GetIndexName())
21112111
if err != nil {
21122112
return err
21132113
}
@@ -2117,19 +2117,18 @@ func (s *Scope) DropIndex(c *Compile) error {
21172117
}
21182118

21192119
//2. drop index table
2120-
if qry.IndexTableName != "" {
2121-
if _, err = d.Relation(c.proc.Ctx, qry.IndexTableName, nil); err != nil {
2120+
for _, indexTableName := range dropIndexTableNames {
2121+
if _, err = d.Relation(c.proc.Ctx, indexTableName, nil); err != nil {
21222122
return err
21232123
}
21242124

2125-
if err = maybeDeleteAutoIncrement(c.proc.Ctx, c.proc.GetService(), d, qry.IndexTableName, c.proc.GetTxnOperator()); err != nil {
2125+
if err = maybeDeleteAutoIncrement(c.proc.Ctx, c.proc.GetService(), d, indexTableName, c.proc.GetTxnOperator()); err != nil {
21262126
return err
21272127
}
21282128

2129-
if err = d.Delete(c.proc.Ctx, qry.IndexTableName); err != nil {
2129+
if err = d.Delete(c.proc.Ctx, indexTableName); err != nil {
21302130
return err
21312131
}
2132-
21332132
}
21342133

21352134
//3. delete index object from mo_catalog.mo_indexes
@@ -2141,7 +2140,8 @@ func (s *Scope) DropIndex(c *Compile) error {
21412140
return nil
21422141
}
21432142

2144-
func makeNewDropConstraint(oldCt *engine.ConstraintDef, dropName string) (*engine.ConstraintDef, error) {
2143+
func makeNewDropConstraint(oldCt *engine.ConstraintDef, dropName string) (*engine.ConstraintDef, []string, error) {
2144+
dropIndexTableNames := []string{}
21452145
// must fount dropName because of being checked in plan
21462146
for i := 0; i < len(oldCt.Cts); i++ {
21472147
ct := oldCt.Cts[i]
@@ -2154,13 +2154,16 @@ func makeNewDropConstraint(oldCt *engine.ConstraintDef, dropName string) (*engin
21542154
oldCt.Cts[i] = def
21552155
case *engine.IndexDef:
21562156
pred := func(index *plan.IndexDef) bool {
2157+
if index.IndexName == dropName && len(index.IndexTableName) > 0 {
2158+
dropIndexTableNames = append(dropIndexTableNames, index.IndexTableName)
2159+
}
21572160
return index.IndexName == dropName
21582161
}
21592162
def.Indexes = plan2.RemoveIf[*plan.IndexDef](def.Indexes, pred)
21602163
oldCt.Cts[i] = def
21612164
}
21622165
}
2163-
return oldCt, nil
2166+
return oldCt, dropIndexTableNames, nil
21642167
}
21652168

21662169
func MakeNewCreateConstraint(oldCt *engine.ConstraintDef, c engine.Constraint) (*engine.ConstraintDef, error) {

pkg/sql/plan/build_ddl.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3422,7 +3422,6 @@ func buildDropIndex(stmt *tree.DropIndex, ctx CompilerContext) (*Plan, error) {
34223422

34233423
for _, indexdef := range tableDef.Indexes {
34243424
if dropIndex.IndexName == indexdef.IndexName {
3425-
dropIndex.IndexTableName = indexdef.IndexTableName
34263425
found = true
34273426
break
34283427
}

pkg/sql/plan/deepcopy.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,9 @@ func DeepCopyDataDefinition(old *plan.DataDefinition) *plan.DataDefinition {
721721
case *plan.DataDefinition_DropIndex:
722722
newDf.Definition = &plan.DataDefinition_DropIndex{
723723
DropIndex: &plan.DropIndex{
724-
Database: df.DropIndex.Database,
725-
Table: df.DropIndex.Table,
726-
IndexName: df.DropIndex.IndexName,
727-
IndexTableName: df.DropIndex.IndexTableName,
724+
Database: df.DropIndex.Database,
725+
Table: df.DropIndex.Table,
726+
IndexName: df.DropIndex.IndexName,
728727
},
729728
}
730729

proto/plan.proto

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,6 @@ message DropIndex {
14941494
string database = 1;
14951495
string table = 2;
14961496
string index_name = 3;
1497-
string index_table_name = 4;
14981497
}
14991498

15001499
message TruncateTable {
@@ -1651,4 +1650,4 @@ message CloneTable {
16511650
ObjectRef src_obj_def = 4;
16521651
string dst_database_name = 5;
16531652
string dst_table_name = 6;
1654-
}
1653+
}

test/distributed/cases/array/array_index.result

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,5 +634,29 @@ a b c d
634634
9776 [10, 3, 8, 5, 48, 26, 5, 16, 17, 0, 0, 2, 132, 53, 1, 16, 112, 6, 0, 0, 7, 2, 1, 48, 48, 15, 18, 31, 3, 0, 0, 9, 6, 10, 19, 27, 50, 46, 17, 9, 18, 1, 4, 48, 132, 23, 3, 5, 132, 9, 4, 3, 11, 0, 2, 46, 84, 12, 10, 10, 1, 0, 12, 76, 26, 22, 16, 26, 35, 15, 3, 16, 15, 1, 51, 132, 125, 8, 1, 2, 132, 51, 67, 91, 8, 0, 0, 30, 126, 39, 32, 38, 4, 0, 1, 12, 24, 2, 2, 2, 4, 7, 2, 19, 93, 19, 70, 92, 2, 3, 1, 21, 36, 58, 132, 94, 0, 0, 0, 0, 21, 25, 57, 48, 1, 0, 0, 1] 3 [0, 0, 0]
635635
9777 [16, 15, 0, 0, 5, 46, 5, 5, 4, 0, 0, 0, 28, 118, 12, 5, 75, 44, 5, 0, 6, 32, 6, 49, 41, 74, 9, 1, 0, 0, 0, 9, 1, 9, 16, 41, 71, 80, 3, 0, 0, 4, 3, 5, 51, 106, 11, 3, 112, 28, 13, 1, 4, 8, 3, 104, 118, 14, 1, 1, 0, 0, 0, 88, 3, 27, 46, 118, 108, 49, 2, 0, 1, 46, 118, 118, 27, 12, 0, 0, 33, 118, 118, 8, 0, 0, 0, 4, 118, 95, 40, 0, 0, 0, 1, 11, 27, 38, 12, 12, 18, 29, 3, 2, 13, 30, 94, 78, 30, 19, 9, 3, 31, 45, 70, 42, 15, 1, 3, 12, 14, 22, 16, 2, 3, 17, 24, 13] 4 [0, 0, 0]
636636
9778 [41, 0, 0, 7, 1, 1, 20, 67, 9, 0, 0, 0, 0, 31, 120, 61, 25, 0, 0, 0, 0, 10, 120, 90, 32, 0, 0, 1, 13, 11, 22, 50, 4, 0, 2, 93, 40, 15, 37, 18, 12, 2, 2, 19, 8, 44, 120, 25, 120, 5, 0, 0, 0, 2, 48, 97, 102, 14, 3, 3, 11, 9, 34, 41, 0, 0, 4, 120, 56, 3, 4, 5, 6, 15, 37, 116, 28, 0, 0, 3, 120, 120, 24, 6, 2, 0, 1, 28, 53, 90, 51, 11, 11, 2, 12, 14, 8, 6, 4, 30, 9, 1, 4, 22, 25, 79, 120, 66, 5, 0, 0, 6, 42, 120, 91, 43, 15, 2, 4, 39, 12, 9, 9, 12, 15, 5, 24, 36] 4 [0, 0, 0]
637-
SET experimental_ivf_index = 0;
638637
drop database vecdb2;
638+
create database vecdb3;
639+
use vecdb3;
640+
CREATE TABLE `rag_table` (
641+
`id` int NOT NULL AUTO_INCREMENT,
642+
`title` varchar(200) DEFAULT NULL,
643+
`embedding` vecf32(8) DEFAULT NULL,
644+
`content` text DEFAULT NULL,
645+
PRIMARY KEY (`id`),
646+
KEY `embedding_idx` USING ivfflat (`embedding`) lists = 20 op_type 'vector_l2_ops'
647+
);
648+
select count(*) from mo_catalog.mo_indexes as i join mo_catalog.mo_tables as t on i.table_id = t.rel_id and t.relname = "rag_table" and i.algo="ivfflat"; -- 3 ivf index tables
649+
count(*)
650+
3
651+
select count(*) from mo_catalog.mo_tables where reldatabase = "vecdb3"; -- rag_table + 3 ivf index tables
652+
count(*)
653+
4
654+
drop index embedding_idx on rag_table;
655+
select count(*) from mo_catalog.mo_indexes as i join mo_catalog.mo_tables as t on i.table_id = t.rel_id and t.relname = "rag_table" and i.algo="ivfflat"; -- 0 ivf index tables
656+
count(*)
657+
0
658+
select count(*) from mo_catalog.mo_tables where reldatabase = "vecdb3"; -- rag_table + 0 ivf index tables
659+
count(*)
660+
1
661+
SET experimental_ivf_index = 0;
662+
drop database vecdb3;

test/distributed/cases/array/array_index.sql

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,5 +694,30 @@ alter table vector_index_08 add column d vecf32(3) not null after c;
694694
select * from vector_index_08;
695695

696696
-- post
697-
SET experimental_ivf_index = 0;
698697
drop database vecdb2;
698+
699+
700+
create database vecdb3;
701+
use vecdb3;
702+
703+
CREATE TABLE `rag_table` (
704+
`id` int NOT NULL AUTO_INCREMENT,
705+
`title` varchar(200) DEFAULT NULL,
706+
`embedding` vecf32(8) DEFAULT NULL,
707+
`content` text DEFAULT NULL,
708+
PRIMARY KEY (`id`),
709+
KEY `embedding_idx` USING ivfflat (`embedding`) lists = 20 op_type 'vector_l2_ops'
710+
);
711+
712+
select count(*) from mo_catalog.mo_indexes as i join mo_catalog.mo_tables as t on i.table_id = t.rel_id and t.relname = "rag_table" and i.algo="ivfflat"; -- 3 ivf index tables
713+
714+
select count(*) from mo_catalog.mo_tables where reldatabase = "vecdb3"; -- rag_table + 3 ivf index tables
715+
716+
drop index embedding_idx on rag_table;
717+
718+
select count(*) from mo_catalog.mo_indexes as i join mo_catalog.mo_tables as t on i.table_id = t.rel_id and t.relname = "rag_table" and i.algo="ivfflat"; -- 0 ivf index tables
719+
720+
select count(*) from mo_catalog.mo_tables where reldatabase = "vecdb3"; -- rag_table + 0 ivf index tables
721+
722+
SET experimental_ivf_index = 0;
723+
drop database vecdb3;

0 commit comments

Comments
 (0)