From af1aba6e22f94dc68faa03e72dae181191af0dc5 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Mon, 20 Mar 2023 10:34:40 +0800 Subject: [PATCH] planner: fix plan cache rebuild range error (#42220) (#42380) close pingcap/tidb#42150 --- executor/prepared_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/executor/prepared_test.go b/executor/prepared_test.go index e45f07d1f3e5f..176b4c6306020 100644 --- a/executor/prepared_test.go +++ b/executor/prepared_test.go @@ -1457,3 +1457,28 @@ INSERT INTO PK_LP9463 VALUES (-7415279,'笚綷想摻癫梒偆荈湩窐曋繾鏫 tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0")) tk.MustQuery(`SELECT *, rank() OVER (PARTITION BY col2 ORDER BY COL1) FROM PK_LP9463 WHERE col1 != 16614 and col1 < 16614`).Sort().Check(rows.Rows()) } + +func TestIssue42150(t *testing.T) { + store, clean := testkit.CreateMockStore(t) + defer clean() + orgEnable := plannercore.PreparedPlanCacheEnabled() + defer func() { + plannercore.SetPreparedPlanCache(orgEnable) + }() + plannercore.SetPreparedPlanCache(true) // requires plan cache enable + tk := testkit.NewTestKit(t, store) + tk.MustExec(`use test`) + tk.MustExec("drop table if exists t1, t2") + tk.MustExec("CREATE TABLE `t1` (`c_int` int(11) NOT NULL, `c_str` varchar(40) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, `c_datetime` datetime DEFAULT NULL, `c_timestamp` timestamp NULL DEFAULT NULL, `c_double` double DEFAULT NULL, `c_decimal` decimal(12,6) DEFAULT NULL, `c_enum` enum('blue','green','red','yellow','white','orange','purple') NOT NULL, PRIMARY KEY (`c_int`,`c_enum`) /*T![clustered_index] CLUSTERED */, KEY `c_decimal` (`c_decimal`), UNIQUE KEY `c_datetime` (`c_datetime`), UNIQUE KEY `c_timestamp` (`c_timestamp`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;") + tk.MustExec("create table t (a int, b int, primary key(a), key(b))") + + tk.MustExec("prepare stmt from 'select c_enum from t1'") + tk.MustExec("execute stmt;") + tk.MustExec("execute stmt;") + tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1")) + + tk.MustExec("prepare st from 'select a from t use index(b)'") + tk.MustExec("execute st") + tk.MustExec("execute st") + tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1")) +}