Skip to content

Commit

Permalink
bindinfo: remove last semicolon of bind sqls (pingcap#14110)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx authored and sre-bot committed Dec 18, 2019
1 parent fc603b6 commit dddb85c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
10 changes: 5 additions & 5 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,9 @@ func (s *testSuite) TestUseMultiplyBindings(c *C) {
tk.MustExec("create binding for select * from t where a >= 1 and b >= 1 and c = 0 using select * from t use index(idx_a) where a >= 1 and b >= 1 and c = 0")
tk.MustExec("create binding for select * from t where a >= 1 and b >= 1 and c = 0 using select * from t use index(idx_b) where a >= 1 and b >= 1 and c = 0")
// It cannot choose table path although it has lowest cost.
tk.MustQuery("select * from t where a >= 4 and b >= 1 and c = 0")
tk.MustQuery("select * from t where a >= 4 and b >= 1 and c = 0;")
c.Assert(tk.Se.GetSessionVars().StmtCtx.IndexNames[0], Equals, "t:idx_a")
tk.MustQuery("select * from t where a >= 1 and b >= 4 and c = 0")
tk.MustQuery("select * from t where a >= 1 and b >= 4 and c = 0;")
c.Assert(tk.Se.GetSessionVars().StmtCtx.IndexNames[0], Equals, "t:idx_b")
}

Expand Down Expand Up @@ -572,18 +572,18 @@ func (s *testSuite) TestBindingCache(c *C) {
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int, index idx(a))")
tk.MustExec("create global binding for select * from t using select * from t use index(idx)")
tk.MustExec("create global binding for select * from t using select * from t use index(idx);")
tk.MustExec("create database tmp")
tk.MustExec("use tmp")
tk.MustExec("create table t(a int, b int, index idx(a))")
tk.MustExec("create global binding for select * from t using select * from t use index(idx)")
tk.MustExec("create global binding for select * from t using select * from t use index(idx);")

c.Assert(s.domain.BindHandle().Update(false), IsNil)
c.Assert(s.domain.BindHandle().Update(false), IsNil)
res := tk.MustQuery("show global bindings")
c.Assert(len(res.Rows()), Equals, 2)

tk.MustExec("drop global binding for select * from t")
tk.MustExec("drop global binding for select * from t;")
c.Assert(s.domain.BindHandle().Update(false), IsNil)
c.Assert(len(s.domain.BindHandle().GetAllBindRecord()), Equals, 1)
}
Expand Down
12 changes: 12 additions & 0 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,13 @@ func (p *preprocessor) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
case *ast.Join:
p.checkNonUniqTableAlias(node)
case *ast.CreateBindingStmt:
EraseLastSemicolon(node.OriginSel)
EraseLastSemicolon(node.HintedSel)
p.checkBindGrammar(node.OriginSel, node.HintedSel)
case *ast.DropBindingStmt:
EraseLastSemicolon(node.OriginSel)
if node.HintedSel != nil {
EraseLastSemicolon(node.HintedSel)
p.checkBindGrammar(node.OriginSel, node.HintedSel)
}
case *ast.RecoverTableStmt, *ast.FlashBackTableStmt:
Expand All @@ -140,6 +144,14 @@ func (p *preprocessor) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
return in, p.err != nil
}

// EraseLastSemicolon removes last semicolon of sql.
func EraseLastSemicolon(stmt ast.StmtNode) {
sql := stmt.Text()
if len(sql) > 0 && sql[len(sql)-1] == ';' {
stmt.SetText(sql[:len(sql)-1])
}
}

func (p *preprocessor) checkBindGrammar(originSel, hintedSel ast.StmtNode) {
originSQL := parser.Normalize(originSel.(*ast.SelectStmt).Text())
hintedSQL := parser.Normalize(hintedSel.(*ast.SelectStmt).Text())
Expand Down
2 changes: 2 additions & 0 deletions planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,15 @@ func extractSelectAndNormalizeDigest(stmtNode ast.StmtNode) (*ast.SelectStmt, st
case *ast.ExplainStmt:
switch x.Stmt.(type) {
case *ast.SelectStmt:
plannercore.EraseLastSemicolon(x)
normalizeExplainSQL := parser.Normalize(x.Text())
idx := strings.Index(normalizeExplainSQL, "select")
normalizeSQL := normalizeExplainSQL[idx:]
hash := parser.DigestNormalized(normalizeSQL)
return x.Stmt.(*ast.SelectStmt), normalizeSQL, hash
}
case *ast.SelectStmt:
plannercore.EraseLastSemicolon(x)
normalizedSQL, hash := parser.NormalizeDigest(x.Text())
return x, normalizedSQL, hash
}
Expand Down

0 comments on commit dddb85c

Please sign in to comment.