diff --git a/session/nontransactional.go b/session/nontransactional.go index aceb46bc1d..8358a5253c 100644 --- a/session/nontransactional.go +++ b/session/nontransactional.go @@ -450,7 +450,7 @@ func selectShardColumn(stmt *ast.NonTransactionalDeleteStmt, se Session, tableNa } for _, index := range tbl.Indices() { - if index.Meta().State != model.StatePublic { + if index.Meta().State != model.StatePublic || index.Meta().Invisible { continue } indexColumns := index.Meta().Columns diff --git a/session/nontransactional_test.go b/session/nontransactional_test.go index 7f210cf777..5ea4f731f3 100644 --- a/session/nontransactional_test.go +++ b/session/nontransactional_test.go @@ -206,3 +206,23 @@ func TestNonTransactionalDeleteAutoDetectShardColumn(t *testing.T) { testFunc(table, false) } } + +func TestNonTransactionalDeleteInvisibleIndex(t *testing.T) { + store, clean := createStorage(t) + defer clean() + tk := testkit.NewTestKit(t, store) + tk.MustExec("set @@tidb_max_chunk_size=35") + tk.MustExec("use test") + tk.MustExec("create table t(a int, b int)") + for i := 0; i < 100; i++ { + tk.MustExec(fmt.Sprintf("insert into t values (%d, %d)", i, i*2)) + } + err := tk.ExecToErr("split on a limit 10 delete from t") + require.Error(t, err) + tk.MustExec("CREATE UNIQUE INDEX c1 ON t (a) INVISIBLE") + err = tk.ExecToErr("split on a limit 10 delete from t") + require.Error(t, err) + tk.MustExec("CREATE UNIQUE INDEX c2 ON t (a)") + tk.MustExec("split on a limit 10 delete from t") + tk.MustQuery("select count(*) from t").Check(testkit.Rows("0")) +}