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

bindinfo: remove last semicolon of bind sqls (#14110) #14113

Merged
merged 2 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ func (s *testSuite) TestExplain(c *C) {
tk.MustExec("create table t1(id int)")
tk.MustExec("create table t2(id int)")

tk.MustQuery("explain SELECT * from t1,t2 where t1.id = t2.id").Check(testkit.Rows(
tk.MustQuery("explain SELECT * from t1,t2 where t1.id = t2.id;").Check(testkit.Rows(
"HashLeftJoin_8 12487.50 root inner join, inner:TableReader_15, equal:[eq(test.t1.id, test.t2.id)]",
"├─TableReader_12 9990.00 root data:Selection_11",
"│ └─Selection_11 9990.00 cop not(isnull(test.t1.id))",
Expand All @@ -399,7 +399,7 @@ func (s *testSuite) TestExplain(c *C) {
" └─TableScan_13 10000.00 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo",
))

tk.MustQuery("explain SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id").Check(testkit.Rows(
tk.MustQuery("explain SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id;").Check(testkit.Rows(
"MergeJoin_7 12487.50 root inner join, left key:test.t1.id, right key:test.t2.id",
"├─Sort_11 9990.00 root test.t1.id:asc",
"│ └─TableReader_10 9990.00 root data:Selection_9",
Expand All @@ -413,7 +413,7 @@ func (s *testSuite) TestExplain(c *C) {

tk.MustExec("create global binding for SELECT * from t1,t2 where t1.id = t2.id using SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id")

tk.MustQuery("explain SELECT * from t1,t2 where t1.id = t2.id").Check(testkit.Rows(
tk.MustQuery("explain SELECT * from t1,t2 where t1.id = t2.id;").Check(testkit.Rows(
"MergeJoin_7 12487.50 root inner join, left key:test.t1.id, right key:test.t2.id",
"├─Sort_11 9990.00 root test.t1.id:asc",
"│ └─TableReader_10 9990.00 root data:Selection_9",
Expand Down Expand Up @@ -445,7 +445,7 @@ func (s *testSuite) TestBindingSymbolList(c *C) {
tk.MustExec(`create global binding for select a, b from t where a = 1 limit 0, 1 using select a, b from t use index (ib) where a = 1 limit 0, 1`)

// after binding
tk.MustQuery("select a, b from t where a = 3 limit 1, 100")
tk.MustQuery("select a, b from t where a = 3 limit 1, 100;")
c.Assert(tk.Se.GetSessionVars().StmtCtx.IndexNames[0], Equals, "t:ib")
c.Assert(tk.MustUseIndex("select a, b from t where a = 3 limit 1, 100", "b"), IsTrue)

Expand Down Expand Up @@ -509,11 +509,11 @@ 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)
Expand Down
2 changes: 2 additions & 0 deletions executor/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ func addHint(ctx sessionctx.Context, stmtNode ast.StmtNode) ast.StmtNode {
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:]
Expand All @@ -393,6 +394,7 @@ func addHint(ctx sessionctx.Context, stmtNode ast.StmtNode) ast.StmtNode {
}
return x
case *ast.SelectStmt:
plannercore.EraseLastSemicolon(x)
normalizeSQL, hash := parser.NormalizeDigest(x.Text())
return addHintForSelect(hash, normalizeSQL, ctx, x)
default:
Expand Down
12 changes: 12 additions & 0 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ 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)
case *ast.DropBindingStmt:
EraseLastSemicolon(node.OriginSel)
case *ast.RecoverTableStmt:
// The specified table in recover table statement maybe already been dropped.
// So skip check table name here, otherwise, recover table [table_name] syntax will return
Expand All @@ -128,6 +132,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(createBindingStmt *ast.CreateBindingStmt) {
originSQL := parser.Normalize(createBindingStmt.OriginSel.(*ast.SelectStmt).Text())
hintedSQL := parser.Normalize(createBindingStmt.HintedSel.(*ast.SelectStmt).Text())
Expand Down