Skip to content

Commit

Permalink
planner: fix create binding panic when status.record-db-qp enable (pi…
Browse files Browse the repository at this point in the history
  • Loading branch information
Reminiscent authored Nov 8, 2021
1 parent 49d995d commit 7feb0e6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
27 changes: 19 additions & 8 deletions executor/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,22 @@ func getStmtDbLabel(stmtNode ast.StmtNode) map[string]struct{} {
}
case *ast.CreateBindingStmt:
var resNode ast.ResultSetNode
var tableRef *ast.TableRefsClause
if x.OriginNode != nil {
switch n := x.OriginNode.(type) {
case *ast.SelectStmt:
resNode = n.From.TableRefs
tableRef = n.From
case *ast.DeleteStmt:
resNode = n.TableRefs.TableRefs
tableRef = n.TableRefs
case *ast.UpdateStmt:
resNode = n.TableRefs.TableRefs
tableRef = n.TableRefs
case *ast.InsertStmt:
resNode = n.Table.TableRefs
tableRef = n.Table
}
if tableRef != nil {
resNode = tableRef.TableRefs
} else {
resNode = nil
}
dbLabels := getDbFromResultNode(resNode)
for _, db := range dbLabels {
Expand All @@ -255,13 +261,18 @@ func getStmtDbLabel(stmtNode ast.StmtNode) map[string]struct{} {
if len(dbLabelSet) == 0 && x.HintedNode != nil {
switch n := x.HintedNode.(type) {
case *ast.SelectStmt:
resNode = n.From.TableRefs
tableRef = n.From
case *ast.DeleteStmt:
resNode = n.TableRefs.TableRefs
tableRef = n.TableRefs
case *ast.UpdateStmt:
resNode = n.TableRefs.TableRefs
tableRef = n.TableRefs
case *ast.InsertStmt:
resNode = n.Table.TableRefs
tableRef = n.Table
}
if tableRef != nil {
resNode = tableRef.TableRefs
} else {
resNode = nil
}
dbLabels := getDbFromResultNode(resNode)
for _, db := range dbLabels {
Expand Down
18 changes: 18 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4397,6 +4397,24 @@ func (s *testIntegrationSuite) TestIssue26559(c *C) {
tk.MustQuery("select greatest(a, b) from t union select null;").Sort().Check(testkit.Rows("2020-07-29 09:07:01", "<nil>"))
}

func (s *testIntegrationSuite) TestIssue29503(c *C) {
tk := testkit.NewTestKit(c, s.store)
defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.Status.RecordQPSbyDB = true
})

tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a int);")
err := tk.ExecToErr("create binding for select 1 using select 1;")
c.Assert(err, Equals, nil)
err = tk.ExecToErr("create binding for select a from t using select a from t;")
c.Assert(err, Equals, nil)
res := tk.MustQuery("show session bindings;")
c.Assert(len(res.Rows()), Equals, 2)
}

func (s *testIntegrationSuite) TestHeuristicIndexSelection(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
4 changes: 3 additions & 1 deletion planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5946,7 +5946,9 @@ func unfoldSelectList(list *ast.SetOprSelectList, unfoldList *ast.SetOprSelectLi
func extractTableList(node ast.Node, input []*ast.TableName, asName bool) []*ast.TableName {
switch x := node.(type) {
case *ast.SelectStmt:
input = extractTableList(x.From.TableRefs, input, asName)
if x.From != nil {
input = extractTableList(x.From.TableRefs, input, asName)
}
if x.Where != nil {
input = extractTableList(x.Where, input, asName)
}
Expand Down
4 changes: 4 additions & 0 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ func (p *preprocessor) checkBindGrammar(originNode, hintedNode ast.StmtNode, def
p.err = ddl.ErrOptOnTemporaryTable.GenWithStackByArgs("create binding")
return
}
tableInfo := tbl.Meta()
dbInfo, _ := p.ensureInfoSchema().SchemaByTable(tableInfo)
tn.TableInfo = tableInfo
tn.DBInfo = dbInfo
}

originSQL := parser.Normalize(utilparser.RestoreWithDefaultDB(originNode, defaultDB, originNode.Text()))
Expand Down

0 comments on commit 7feb0e6

Please sign in to comment.