Skip to content

Commit

Permalink
planner: fix a panic of a cached prepared statement with IndexScan (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dbjoa authored and zz-jason committed Oct 25, 2018
1 parent d606b0d commit b556638
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,11 @@ func (is *PhysicalIndexScan) initSchema(id int, idx *model.IndexInfo, isDoubleRe
for _, col := range idx.Columns {
colFound := is.dataSourceSchema.FindColumnByName(col.Name.L)
if colFound == nil {
colFound = &expression.Column{ColName: col.Name, UniqueID: is.ctx.GetSessionVars().AllocPlanColumnID()}
colFound = &expression.Column{
ColName: col.Name,
RetType: &is.Table.Columns[col.Offset].FieldType,
UniqueID: is.ctx.GetSessionVars().AllocPlanColumnID(),
}
} else {
colFound = colFound.Clone().(*expression.Column)
}
Expand Down
26 changes: 26 additions & 0 deletions planner/core/prepare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,29 @@ func (s *testPrepareSuite) TestPrepareCache(c *C) {
tk.MustQuery("execute stmt5").Check(testkit.Rows("1", "2", "2", "3", "4", "5"))
tk.MustQuery("execute stmt5").Check(testkit.Rows("1", "2", "2", "3", "4", "5"))
}

func (s *testPrepareSuite) TestPrepareCacheIndexScan(c *C) {
defer testleak.AfterTest(c)()
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
tk := testkit.NewTestKit(c, store)
orgEnable := core.PreparedPlanCacheEnabled()
orgCapacity := core.PreparedPlanCacheCapacity
defer func() {
dom.Close()
store.Close()
core.SetPreparedPlanCache(orgEnable)
core.PreparedPlanCacheCapacity = orgCapacity
}()
core.SetPreparedPlanCache(true)
core.PreparedPlanCacheCapacity = 100
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int, c int, primary key (a, b))")
tk.MustExec("insert into t values(1, 1, 2), (1, 2, 3), (1, 3, 3), (2, 1, 2), (2, 2, 3), (2, 3, 3)")
tk.MustExec(`prepare stmt1 from "select a, c from t where a = ? and c = ?"`)
tk.MustExec("set @a=1, @b=3")
// When executing one statement at the first time, we don't use cache, so we need to execute it at least twice to test the cache.
tk.MustQuery("execute stmt1 using @a, @b").Check(testkit.Rows("1 3", "1 3"))
tk.MustQuery("execute stmt1 using @a, @b").Check(testkit.Rows("1 3", "1 3"))
}

0 comments on commit b556638

Please sign in to comment.