Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: fix analyze update panic cause by duplicate call analyze executor Close method #20390

Merged
merged 11 commits into from
Oct 12, 2020
45 changes: 20 additions & 25 deletions executor/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,38 +72,33 @@ func (e *ExplainExec) Next(ctx context.Context, req *chunk.Chunk) error {
return nil
}

func (e *ExplainExec) generateExplainInfo(ctx context.Context) (rows [][]string, err error) {
closed := false
defer func() {
if !closed && e.analyzeExec != nil {
err = e.analyzeExec.Close()
closed = true
}
}()
func (e *ExplainExec) executeAnalyzeExec(ctx context.Context) (err error) {
if e.analyzeExec != nil && !e.executed {
defer func() {
err1 := e.analyzeExec.Close()
if err1 != nil {
if err != nil {
err = errors.New(err.Error() + ", " + err1.Error())
} else {
err = err1
}
}
}()
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
e.executed = true
chk := newFirstChunk(e.analyzeExec)
var nextErr, closeErr error
for {
nextErr = Next(ctx, e.analyzeExec, chk)
if nextErr != nil || chk.NumRows() == 0 {
err = Next(ctx, e.analyzeExec, chk)
if err != nil || chk.NumRows() == 0 {
break
}
}
closeErr = e.analyzeExec.Close()
closed = true
if nextErr != nil {
if closeErr != nil {
err = errors.New(nextErr.Error() + ", " + closeErr.Error())
} else {
err = nextErr
}
} else if closeErr != nil {
err = closeErr
}
if err != nil {
return nil, err
}
}
return err
}

func (e *ExplainExec) generateExplainInfo(ctx context.Context) (rows [][]string, err error) {
if err = e.executeAnalyzeExec(ctx); err != nil {
return nil, err
}
if err = e.explain.RenderResult(); err != nil {
return nil, err
Expand Down
11 changes: 6 additions & 5 deletions executor/explain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,15 @@ func (s *testSuite1) TestExplainWrite(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int)")
tk.MustExec("explain analyze insert into t select 1")
tk.MustQuery("explain analyze insert into t select 1")
tk.MustQuery("select * from t").Check(testkit.Rows("1"))
tk.MustExec("explain analyze update t set a=2 where a=1")
tk.MustQuery("explain analyze update t set a=2 where a=1")
tk.MustQuery("select * from t").Check(testkit.Rows("2"))
tk.MustExec("explain insert into t select 1")
tk.MustQuery("explain insert into t select 1")
tk.MustQuery("select * from t").Check(testkit.Rows("2"))
tk.MustExec("explain analyze insert into t select 1")
tk.MustQuery("select * from t order by a").Check(testkit.Rows("1", "2"))
tk.MustQuery("explain analyze insert into t select 1")
tk.MustQuery("explain analyze replace into t values (3)")
tk.MustQuery("select * from t order by a").Check(testkit.Rows("1", "2", "3"))
}

func (s *testSuite1) TestExplainAnalyzeMemory(c *C) {
Expand Down
4 changes: 4 additions & 0 deletions executor/explain_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func TestExplainAnalyzeInvokeNextAndClose(t *testing.T) {
t.Errorf(err.Error())
}
// mockErrorOperator panic
explainExec = &ExplainExec{
baseExecutor: baseExec,
explain: nil,
}
mockOpr = mockErrorOperator{baseExec, true, false}
explainExec.analyzeExec = &mockOpr
defer func() {
Expand Down