Skip to content

Commit

Permalink
*: rewrite the logic of do statement. (pingcap#2215)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanfei1991 authored and coocood committed Dec 9, 2016
1 parent b723e72 commit e149b0c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
13 changes: 0 additions & 13 deletions executor/executor_simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/ngaut/log"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/evaluator"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/meta"
Expand Down Expand Up @@ -64,8 +63,6 @@ func (e *SimpleExec) Next() (*Row, error) {
err = e.executeUse(x)
case *ast.FlushTableStmt:
err = e.executeFlushTable(x)
case *ast.DoStmt:
err = e.executeDo(x)
case *ast.BeginStmt:
err = e.executeBegin(x)
case *ast.CommitStmt:
Expand Down Expand Up @@ -114,16 +111,6 @@ func (e *SimpleExec) executeUse(s *ast.UseStmt) error {
return nil
}

func (e *SimpleExec) executeDo(s *ast.DoStmt) error {
for _, expr := range s.Exprs {
_, err := evaluator.Eval(e.ctx, expr)
if err != nil {
return errors.Trace(err)
}
}
return nil
}

func (e *SimpleExec) executeBegin(s *ast.BeginStmt) error {
_, err := e.ctx.GetTxn(true)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion executor/executor_simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func (s *testSuite) TestCharsetDatabase(c *C) {
func (s *testSuite) TestDo(c *C) {
defer testleak.AfterTest(c)()
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("do 1, 2")
tk.MustExec("do 1, @a:=1")
tk.MustQuery("select @a").Check(testkit.Rows("1"))
}

func (s *testSuite) TestTransaction(c *C) {
Expand Down
28 changes: 27 additions & 1 deletion plan/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ func (b *planBuilder) build(node ast.Node) Plan {
return b.buildUpdate(x)
case *ast.ShowStmt:
return b.buildShow(x)
case *ast.AnalyzeTableStmt, *ast.BinlogStmt, *ast.FlushTableStmt, *ast.UseStmt, *ast.SetStmt, *ast.DoStmt,
case *ast.DoStmt:
return b.buildDo(x)
case *ast.AnalyzeTableStmt, *ast.BinlogStmt, *ast.FlushTableStmt, *ast.UseStmt, *ast.SetStmt,
*ast.BeginStmt, *ast.CommitStmt, *ast.RollbackStmt, *ast.CreateUserStmt, *ast.SetPwdStmt,
*ast.GrantStmt, *ast.DropUserStmt, *ast.AlterUserStmt:
return b.buildSimple(node.(ast.StmtNode))
Expand All @@ -104,6 +106,30 @@ func (b *planBuilder) build(node ast.Node) Plan {
return nil
}

func (b *planBuilder) buildDo(v *ast.DoStmt) Plan {
exprs := make([]expression.Expression, 0, len(v.Exprs))
dual := &TableDual{
baseLogicalPlan: newBaseLogicalPlan(Dual, b.allocator),
}
dual.self = dual
for _, astExpr := range v.Exprs {
expr, _, err := b.rewrite(astExpr, dual, nil, true)
if err != nil {
b.err = errors.Trace(err)
return nil
}
exprs = append(exprs, expr)
}
p := &Projection{
Exprs: exprs,
baseLogicalPlan: newBaseLogicalPlan(Proj, b.allocator),
}
p.initIDAndContext(b.ctx)
addChild(p, dual)
p.self = p
return p
}

// Detect aggregate function or groupby clause.
func (b *planBuilder) detectSelectAgg(sel *ast.SelectStmt) bool {
if sel.GroupBy != nil {
Expand Down

0 comments on commit e149b0c

Please sign in to comment.