diff --git a/executor/explainfor_test.go b/executor/explainfor_test.go index f960e63704921..794fdc7e84cae 100644 --- a/executor/explainfor_test.go +++ b/executor/explainfor_test.go @@ -608,15 +608,15 @@ func (s *testPrepareSerialSuite) TestIssue28259(c *C) { tk.MustExec("set @a=-1696020282760139948, @b=-2619168038882941276, @c=-4004648990067362699;") tk.MustQuery("execute stmt using @a,@b,@c;").Check(testkit.Rows()) - tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1")) + tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0")) tk.MustQuery("execute stmt using @a,@b,@c;").Check(testkit.Rows()) tkProcess = tk.Se.ShowProcess() ps = []*util.ProcessInfo{tkProcess} tk.Se.SetSessionManager(&mockSessionManager1{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) - c.Assert(len(res.Rows()), Equals, 3) + c.Assert(len(res.Rows()), Equals, 4) c.Assert(res.Rows()[0][0], Matches, ".*Selection.*") - c.Assert(res.Rows()[2][0], Matches, ".*IndexFullScan.*") + c.Assert(res.Rows()[3][0], Matches, ".*IndexFullScan.*") res = tk.MustQuery("explain format = 'brief' select col1 from UK_GCOL_VIRTUAL_18588 use index(UK_COL1) " + "where col1 between -1696020282760139948 and -2619168038882941276 or col1 < -4004648990067362699;") @@ -646,7 +646,7 @@ func (s *testPrepareSerialSuite) TestIssue28259(c *C) { tk.MustExec("set @a=2, @b=1, @c=1;") tk.MustQuery("execute stmt using @a,@b,@c;").Check(testkit.Rows()) - tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1")) + tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0")) tk.MustQuery("execute stmt using @a,@b,@c;").Check(testkit.Rows()) tkProcess = tk.Se.ShowProcess() ps = []*util.ProcessInfo{tkProcess} @@ -656,7 +656,7 @@ func (s *testPrepareSerialSuite) TestIssue28259(c *C) { c.Assert(res.Rows()[1][0], Matches, ".*Selection.*") c.Assert(res.Rows()[1][4], Equals, "lt(test.t.b, 1), or(and(ge(test.t.a, 2), le(test.t.a, 1)), lt(test.t.a, 1))") c.Assert(res.Rows()[2][0], Matches, ".*IndexReader.*") - c.Assert(res.Rows()[4][0], Matches, ".*IndexFullScan.*") + c.Assert(res.Rows()[4][0], Matches, ".*IndexRangeScan.*") res = tk.MustQuery("explain format = 'brief' select a from t use index(idx) " + "where (a between 0 and 2 or a < 2) and b < 1;") @@ -693,7 +693,7 @@ func (s *testPrepareSerialSuite) TestIssue28259(c *C) { tk.MustExec("set @a=2, @b=1, @c=1;") tk.MustQuery("execute stmt using @a,@b,@c;").Check(testkit.Rows()) - tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1")) + tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0")) tk.MustQuery("execute stmt using @a,@b,@c;").Check(testkit.Rows()) tkProcess = tk.Se.ShowProcess() ps = []*util.ProcessInfo{tkProcess} @@ -702,7 +702,7 @@ func (s *testPrepareSerialSuite) TestIssue28259(c *C) { c.Assert(len(res.Rows()), Equals, 6) c.Assert(res.Rows()[1][0], Matches, ".*Selection.*") c.Assert(res.Rows()[2][0], Matches, ".*IndexLookUp.*") - c.Assert(res.Rows()[3][0], Matches, ".*IndexFullScan.*") + c.Assert(res.Rows()[3][0], Matches, ".*IndexRangeScan.*") c.Assert(res.Rows()[4][0], Matches, ".*Selection.*") c.Assert(res.Rows()[5][0], Matches, ".*TableRowIDScan.*") diff --git a/planner/core/common_plans.go b/planner/core/common_plans.go index 1377c404f85ba..7f47741af6ced 100644 --- a/planner/core/common_plans.go +++ b/planner/core/common_plans.go @@ -735,6 +735,9 @@ func (e *Execute) buildRangeForTableScan(sctx sessionctx.Context, ts *PhysicalTa if err != nil { return err } + if len(res.AccessConds) != len(ts.AccessCondition) { + return errors.New("rebuild range for cached plan failed") + } ts.Ranges = res.Ranges } else { ts.Ranges = ranger.FullRange() @@ -767,6 +770,9 @@ func (e *Execute) buildRangeForIndexScan(sctx sessionctx.Context, is *PhysicalIn if err != nil { return err } + if len(res.AccessConds) != len(is.AccessCondition) { + return errors.New("rebuild range for cached plan failed") + } is.Ranges = res.Ranges return } diff --git a/planner/core/prepare_test.go b/planner/core/prepare_test.go index cec574ca0d138..c5e8724da2c01 100644 --- a/planner/core/prepare_test.go +++ b/planner/core/prepare_test.go @@ -1662,6 +1662,45 @@ func (s *testPlanSerialSuite) TestIssue23671(c *C) { tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1")) } +func (s *testPrepareSerialSuite) TestIssue29296(c *C) { + defer testleak.AfterTest(c)() + store, dom, err := newStoreWithBootstrap() + c.Assert(err, IsNil) + tk := testkit.NewTestKit(c, store) + orgEnable := core.PreparedPlanCacheEnabled() + defer func() { + dom.Close() + err = store.Close() + c.Assert(err, IsNil) + core.SetPreparedPlanCache(orgEnable) + }() + core.SetPreparedPlanCache(true) + tk.Se, err = session.CreateSession4TestWithOpt(store, &session.Opt{ + PreparedPlanCache: kvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64), + }) + c.Assert(err, IsNil) + + tk.MustExec(`use test`) + tk.MustExec(`drop table if exists UK_MU14722`) + tk.MustExec(`CREATE TABLE UK_MU14722 ( + COL1 tinytext DEFAULT NULL, + COL2 tinyint(16) DEFAULT NULL, + COL3 datetime DEFAULT NULL, + COL4 int(11) DEFAULT NULL, + UNIQUE KEY U_M_COL (COL1(10)), + UNIQUE KEY U_M_COL2 (COL2), + UNIQUE KEY U_M_COL3 (COL3))`) + tk.MustExec(`insert into UK_MU14722 values("輮睅麤敜溺她晁瀪襄頮鹛涓誗钷廔筪惌嶙鎢塴", -121, "3383-02-19 07:58:28" , -639457963), + ("偧孇鱓鼂瘠钻篝醗時鷷聽箌磇砀玸眞扦鸇祈灇", 127, "7902-03-05 08:54:04", -1094128660), + ("浀玡慃淛漉围甧鴎史嬙砊齄w章炢忲噑硓哈樘", -127, "5813-04-16 03:07:20", -333397107), + ("鑝粼啎鸼贖桖弦簼赭蠅鏪鐥蕿捐榥疗耹岜鬓槊", -117, "7753-11-24 10:14:24", 654872077)`) + tk.MustExec(`prepare stmt from 'SELECT * FROM UK_MU14722 WHERE col2 > ? OR col2 BETWEEN ? AND ? ORDER BY COL2 + ? LIMIT 3'`) + tk.MustExec(`set @a=30410, @b=3937, @c=22045, @d=-4374`) + tk.MustQuery(`execute stmt using @a,@b,@c,@d`).Check(testkit.Rows()) + tk.MustExec(`set @a=127, @b=127, @c=127, @d=127`) + tk.MustQuery(`execute stmt using @a,@b,@c,@d`).Check(testkit.Rows(`偧孇鱓鼂瘠钻篝醗時鷷聽箌磇砀玸眞扦鸇祈灇 127 7902-03-05 08:54:04 -1094128660`)) +} + func (s *testPrepareSerialSuite) TestIssue28246(c *C) { defer testleak.AfterTest(c)() store, dom, err := newStoreWithBootstrap()