Skip to content

Commit b556638

Browse files
dbjoazz-jason
authored andcommitted
planner: fix a panic of a cached prepared statement with IndexScan (#8017)
1 parent d606b0d commit b556638

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

planner/core/find_best_task.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,11 @@ func (is *PhysicalIndexScan) initSchema(id int, idx *model.IndexInfo, isDoubleRe
427427
for _, col := range idx.Columns {
428428
colFound := is.dataSourceSchema.FindColumnByName(col.Name.L)
429429
if colFound == nil {
430-
colFound = &expression.Column{ColName: col.Name, UniqueID: is.ctx.GetSessionVars().AllocPlanColumnID()}
430+
colFound = &expression.Column{
431+
ColName: col.Name,
432+
RetType: &is.Table.Columns[col.Offset].FieldType,
433+
UniqueID: is.ctx.GetSessionVars().AllocPlanColumnID(),
434+
}
431435
} else {
432436
colFound = colFound.Clone().(*expression.Column)
433437
}

planner/core/prepare_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,29 @@ func (s *testPrepareSuite) TestPrepareCache(c *C) {
6363
tk.MustQuery("execute stmt5").Check(testkit.Rows("1", "2", "2", "3", "4", "5"))
6464
tk.MustQuery("execute stmt5").Check(testkit.Rows("1", "2", "2", "3", "4", "5"))
6565
}
66+
67+
func (s *testPrepareSuite) TestPrepareCacheIndexScan(c *C) {
68+
defer testleak.AfterTest(c)()
69+
store, dom, err := newStoreWithBootstrap()
70+
c.Assert(err, IsNil)
71+
tk := testkit.NewTestKit(c, store)
72+
orgEnable := core.PreparedPlanCacheEnabled()
73+
orgCapacity := core.PreparedPlanCacheCapacity
74+
defer func() {
75+
dom.Close()
76+
store.Close()
77+
core.SetPreparedPlanCache(orgEnable)
78+
core.PreparedPlanCacheCapacity = orgCapacity
79+
}()
80+
core.SetPreparedPlanCache(true)
81+
core.PreparedPlanCacheCapacity = 100
82+
tk.MustExec("use test")
83+
tk.MustExec("drop table if exists t")
84+
tk.MustExec("create table t(a int, b int, c int, primary key (a, b))")
85+
tk.MustExec("insert into t values(1, 1, 2), (1, 2, 3), (1, 3, 3), (2, 1, 2), (2, 2, 3), (2, 3, 3)")
86+
tk.MustExec(`prepare stmt1 from "select a, c from t where a = ? and c = ?"`)
87+
tk.MustExec("set @a=1, @b=3")
88+
// 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.
89+
tk.MustQuery("execute stmt1 using @a, @b").Check(testkit.Rows("1 3", "1 3"))
90+
tk.MustQuery("execute stmt1 using @a, @b").Check(testkit.Rows("1 3", "1 3"))
91+
}

0 commit comments

Comments
 (0)