diff --git a/plan/plan.go b/plan/plan.go index 982fdd371e..d33f952de0 100644 --- a/plan/plan.go +++ b/plan/plan.go @@ -47,6 +47,7 @@ type Plan interface { Next(ctx context.Context) (row *Row, err error) // Close closes the underlying iterator of the plan. + // If you call Next after Close, it will start iteration from beginning. Close() error } diff --git a/plan/plans/distinct.go b/plan/plans/distinct.go index f0fd840abd..a64a3fec92 100644 --- a/plan/plans/distinct.go +++ b/plan/plans/distinct.go @@ -147,6 +147,8 @@ func (r *DistinctDefaultPlan) fetchAll(ctx context.Context) error { // Close implements plan.Plan Close interface. func (r *DistinctDefaultPlan) Close() error { + r.rows = nil + r.cursor = 0 return r.Src.Close() } diff --git a/plan/plans/explain.go b/plan/plans/explain.go index abea078298..3fc5c02d0e 100644 --- a/plan/plans/explain.go +++ b/plan/plans/explain.go @@ -92,6 +92,8 @@ func (r *ExplainDefaultPlan) Next(ctx context.Context) (row *plan.Row, err error // Close implements plan.Plan Close interface. func (r *ExplainDefaultPlan) Close() error { + r.lines = nil + r.cursor = 0 return nil } diff --git a/plan/plans/fields.go b/plan/plans/fields.go index 96aa9ca5b6..df768b3716 100644 --- a/plan/plans/fields.go +++ b/plan/plans/fields.go @@ -167,6 +167,7 @@ func (s *SelectEmptyFieldListPlan) Next(ctx context.Context) (row *plan.Row, err // Close implements plan.Plan Close interface. func (s *SelectEmptyFieldListPlan) Close() error { + s.done = false return nil } diff --git a/plan/plans/fields_test.go b/plan/plans/fields_test.go index c2f3d2437c..2fd0ecda09 100644 --- a/plan/plans/fields_test.go +++ b/plan/plans/fields_test.go @@ -76,10 +76,10 @@ func (s *testFieldsSuit) TestDefaultFieldsPlan(c *C) { }, }, } - + tblPlan.Close() rset.Plan = &plans.SelectFieldsDefaultPlan{ SelectList: sl2, - Src: tblPlan.reset(), + Src: tblPlan, } rset.Do(func(data []interface{}) (bool, error) { @@ -99,9 +99,10 @@ func (s *testFieldsSuit) TestDefaultFieldsPlan(c *C) { }, }, } + tblPlan.Close() rset.Plan = &plans.SelectFieldsDefaultPlan{ SelectList: sl3, - Src: tblPlan.reset(), + Src: tblPlan, } err := rset.Do(func(data []interface{}) (bool, error) { return true, nil diff --git a/plan/plans/final.go b/plan/plans/final.go index e662e33c57..19401f5125 100644 --- a/plan/plans/final.go +++ b/plan/plans/final.go @@ -107,6 +107,7 @@ func (r *SelectFinalPlan) Next(ctx context.Context) (row *plan.Row, err error) { // Close implements plan.Plan Close interface. func (r *SelectFinalPlan) Close() error { + r.infered = false return r.Src.Close() } diff --git a/plan/plans/from.go b/plan/plans/from.go index f8d2260a75..f4509ccc71 100644 --- a/plan/plans/from.go +++ b/plan/plans/from.go @@ -124,7 +124,10 @@ func (r *TableNilPlan) Next(ctx context.Context) (row *plan.Row, err error) { // Close implements plan.Plan Close interface. func (r *TableNilPlan) Close() error { - r.iter.Close() + if r.iter != nil { + r.iter.Close() + } + r.iter = nil return nil } @@ -411,7 +414,10 @@ func (r *TableDefaultPlan) Next(ctx context.Context) (row *plan.Row, err error) // Close implements plan.Plan Close interface. func (r *TableDefaultPlan) Close() error { - r.iter.Close() + if r.iter != nil { + r.iter.Close() + } + r.iter = nil return nil } diff --git a/plan/plans/index.go b/plan/plans/index.go index be5328995b..ac84c6dd78 100644 --- a/plan/plans/index.go +++ b/plan/plans/index.go @@ -427,7 +427,10 @@ func (r *indexPlan) Next(ctx context.Context) (row *plan.Row, err error) { func (r *indexPlan) Close() error { if r.iter != nil { r.iter.Close() + r.iter = nil } + r.cursor = 0 + r.skipLowCmp = false return nil } diff --git a/plan/plans/info.go b/plan/plans/info.go index 21356d533d..8975afad79 100644 --- a/plan/plans/info.go +++ b/plan/plans/info.go @@ -584,6 +584,8 @@ func (isp *InfoSchemaPlan) fetchCharacterSets() { // Close implements plan.Plan Close interface. func (isp *InfoSchemaPlan) Close() error { + isp.rows = nil + isp.cursor = 0 return nil } diff --git a/plan/plans/limit.go b/plan/plans/limit.go index 32d6136281..d30869a900 100644 --- a/plan/plans/limit.go +++ b/plan/plans/limit.go @@ -89,6 +89,7 @@ func (r *LimitDefaultPlan) Next(ctx context.Context) (row *plan.Row, err error) // Close implements plan.Plan Close interface. func (r *LimitDefaultPlan) Close() error { + r.cursor = 0 return r.Src.Close() } @@ -147,6 +148,7 @@ func (r *OffsetDefaultPlan) Next(ctx context.Context) (row *plan.Row, err error) // Close implements plan.Plan Close interface. func (r *OffsetDefaultPlan) Close() error { + r.cursor = 0 return r.Src.Close() } diff --git a/plan/plans/limit_test.go b/plan/plans/limit_test.go index f1a9f1fecc..e0349f8d4e 100644 --- a/plan/plans/limit_test.go +++ b/plan/plans/limit_test.go @@ -73,6 +73,7 @@ func (p *testTablePlan) Next(ctx context.Context) (row *plan.Row, err error) { } func (p *testTablePlan) Close() error { + p.cursor = 0 return nil } @@ -80,11 +81,6 @@ func (p *testTablePlan) UseNext() bool { return true } -func (p *testTablePlan) reset() *testTablePlan { - p.cursor = 0 - return p -} - type testLimitSuit struct { data []*testRowData sess tidb.Session diff --git a/plan/plans/orderby.go b/plan/plans/orderby.go index dc6a972e31..04221773ee 100644 --- a/plan/plans/orderby.go +++ b/plan/plans/orderby.go @@ -261,6 +261,8 @@ func (r *OrderByDefaultPlan) fetchAll(ctx context.Context) error { // Close implements plan.Plan Close interface. func (r *OrderByDefaultPlan) Close() error { + r.ordTable = nil + r.cursor = 0 return r.Src.Close() } diff --git a/plan/plans/show.go b/plan/plans/show.go index 4330067f7f..dac8e11551 100644 --- a/plan/plans/show.go +++ b/plan/plans/show.go @@ -369,6 +369,8 @@ func (s *ShowPlan) fetchAll(ctx context.Context) error { // Close implements plan.Plan Close interface. func (s *ShowPlan) Close() error { + s.rows = nil + s.cursor = 0 return nil } @@ -376,9 +378,3 @@ func (s *ShowPlan) Close() error { func (s *ShowPlan) UseNext() bool { return true } - -// Reset used for test -func (s *ShowPlan) Reset() { - s.rows = nil - s.cursor = 0 -} diff --git a/plan/plans/show_test.go b/plan/plans/show_test.go index 8258a3b38b..32d8001b5f 100644 --- a/plan/plans/show_test.go +++ b/plan/plans/show_test.go @@ -91,7 +91,7 @@ func (p *testShowSuit) TestShowVariables(c *C) { c.Assert(v, Equals, "latin1") // Set session variable to utf8 sessionVars.Systems["character_set_results"] = "utf8" - pln.Reset() + pln.Close() rset.Do(func(data []interface{}) (bool, error) { ret[data[0].(string)] = data[1].(string) return true, nil @@ -103,7 +103,7 @@ func (p *testShowSuit) TestShowVariables(c *C) { c.Assert(v, Equals, "latin1") pln.GlobalScope = false - pln.Reset() + pln.Close() rset.Do(func(data []interface{}) (bool, error) { ret[data[0].(string)] = data[1].(string) return true, nil