diff --git a/planner/core/find_best_task.go b/planner/core/find_best_task.go index 89dd20e89fb1e..3c4dde6cd2ce9 100644 --- a/planner/core/find_best_task.go +++ b/planner/core/find_best_task.go @@ -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) } diff --git a/planner/core/prepare_test.go b/planner/core/prepare_test.go index 191780284504a..1a456fbc679b3 100644 --- a/planner/core/prepare_test.go +++ b/planner/core/prepare_test.go @@ -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")) +}