From f22dc78bf558cfee4b3d063bdea39d2689e33d77 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Wed, 26 Aug 2020 13:59:18 +0800 Subject: [PATCH] ddl: exit add index on generated column with `case-when` expression parse error (#19330) (#19395) --- ddl/db_test.go | 17 +++++++++++++++++ ddl/index.go | 10 ++++++++++ 2 files changed, 27 insertions(+) diff --git a/ddl/db_test.go b/ddl/db_test.go index 2a252c24edcfb..4c3cea1e73624 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -4676,6 +4676,23 @@ func (s *testSerialDBSuite) TestDDLJobErrorCount(c *C) { } } +// TestAddIndexFailOnCaseWhenCanExit is used to close #19325. +func (s *testSerialDBSuite) TestAddIndexFailOnCaseWhenCanExit(c *C) { + c.Assert(failpoint.Enable("github.com/pingcap/tidb/ddl/MockCaseWhenParseFailure", `return(true)`), IsNil) + defer func() { + c.Assert(failpoint.Disable("github.com/pingcap/tidb/ddl/MockCaseWhenParseFailure"), IsNil) + }() + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int, b int)") + tk.MustExec("insert into t values(1, 1)") + _, err := tk.Exec("alter table t add index idx(b)") + c.Assert(err, NotNil) + c.Assert(err.Error(), Equals, "[ddl:8214]Cancelled DDL job") + tk.MustExec("drop table if exists t") +} + func init() { // Make sure it will only be executed once. domain.SchemaOutOfDateRetryInterval = int64(50 * time.Millisecond) diff --git a/ddl/index.go b/ddl/index.go index 334aa60284102..1e9daf418e6c5 100644 --- a/ddl/index.go +++ b/ddl/index.go @@ -1361,6 +1361,16 @@ func (w *worker) addPhysicalTableIndex(t table.PhysicalTable, indexInfo *model.I logutil.BgLogger().Info("[ddl] start to add table index", zap.String("job", job.String()), zap.String("reorgInfo", reorgInfo.String())) totalAddedCount := job.GetRowCount() + if err := w.isReorgRunnable(reorgInfo.d); err != nil { + return errors.Trace(err) + } + + failpoint.Inject("MockCaseWhenParseFailure", func(val failpoint.Value) { + if val.(bool) { + failpoint.Return(errors.New("job.ErrCount:" + strconv.Itoa(int(job.ErrorCount)) + ", mock unknown type: ast.whenClause.")) + } + }) + startHandle, endHandle := reorgInfo.StartHandle, reorgInfo.EndHandle sessCtx := newContext(reorgInfo.d.store) decodeColMap, err := makeupDecodeColMap(sessCtx, t, indexInfo)