From f9b475f54d120375a441a56f84afab1bebfd1ca3 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Mon, 20 Feb 2023 10:37:04 +0800 Subject: [PATCH] planner: skip plan cache if the plan contains Shuffle operators (#41185) (#41568) close pingcap/tidb#38335 --- planner/core/optimizer.go | 20 ------------------- planner/core/plan_cache.go | 35 ++++++++++++++++++++++++++++++++- planner/core/plan_cache_test.go | 25 +++++++++++++++++++++++ 3 files changed, 59 insertions(+), 21 deletions(-) diff --git a/planner/core/optimizer.go b/planner/core/optimizer.go index 7d0291230ee58..1e877ed114f24 100644 --- a/planner/core/optimizer.go +++ b/planner/core/optimizer.go @@ -812,26 +812,6 @@ func propagateProbeParents(plan PhysicalPlan, probeParents []PhysicalPlan) { } } -// useTiFlash used to check whether the plan use the TiFlash engine. -func useTiFlash(p PhysicalPlan) bool { - switch x := p.(type) { - case *PhysicalTableReader: - switch x.StoreType { - case kv.TiFlash: - return true - default: - return false - } - default: - if len(p.Children()) > 0 { - for _, plan := range p.Children() { - return useTiFlash(plan) - } - } - } - return false -} - func enableParallelApply(sctx sessionctx.Context, plan PhysicalPlan) PhysicalPlan { if !sctx.GetSessionVars().EnableParallelApply { return plan diff --git a/planner/core/plan_cache.go b/planner/core/plan_cache.go index 0f832a3ecfa05..a2002a6f18010 100644 --- a/planner/core/plan_cache.go +++ b/planner/core/plan_cache.go @@ -331,7 +331,10 @@ func checkPlanCacheability(sctx sessionctx.Context, p Plan, paramNum int) { return } - // TODO: plans accessing MVIndex are un-cacheable + if containShuffleOperator(pp) { + stmtCtx.SetSkipPlanCache(errors.New("skip plan-cache: get a Shuffle plan")) + return + } } // RebuildPlan4CachedPlan will rebuild this plan under current user parameters. @@ -715,6 +718,36 @@ func containTableDual(p PhysicalPlan) bool { return childContainTableDual } +func containShuffleOperator(p PhysicalPlan) bool { + if _, isShuffle := p.(*PhysicalShuffle); isShuffle { + return true + } + if _, isShuffleRecv := p.(*PhysicalShuffleReceiverStub); isShuffleRecv { + return true + } + return false +} + +// useTiFlash used to check whether the plan use the TiFlash engine. +func useTiFlash(p PhysicalPlan) bool { + switch x := p.(type) { + case *PhysicalTableReader: + switch x.StoreType { + case kv.TiFlash: + return true + default: + return false + } + default: + if len(p.Children()) > 0 { + for _, plan := range p.Children() { + return useTiFlash(plan) + } + } + } + return false +} + // GetBindSQL4PlanCache used to get the bindSQL for plan cache to build the plan cache key. func GetBindSQL4PlanCache(sctx sessionctx.Context, stmt *PlanCacheStmt) (string, bool) { useBinding := sctx.GetSessionVars().UsePlanBaselines diff --git a/planner/core/plan_cache_test.go b/planner/core/plan_cache_test.go index 8b7034ca5904f..b63e6f0f2f3d0 100644 --- a/planner/core/plan_cache_test.go +++ b/planner/core/plan_cache_test.go @@ -391,6 +391,31 @@ func TestIssue40679(t *testing.T) { tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 skip plan-cache: '1.1' may be converted to INT")) } +func TestIssue38335(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec(`CREATE TABLE PK_LP9463 ( + COL1 mediumint NOT NULL DEFAULT '77' COMMENT 'NUMERIC PK', + COL2 varchar(20) COLLATE utf8mb4_bin DEFAULT NULL, + COL4 datetime DEFAULT NULL, + COL3 bigint DEFAULT NULL, + COL5 float DEFAULT NULL, + PRIMARY KEY (COL1))`) + tk.MustExec(` +INSERT INTO PK_LP9463 VALUES (-7415279,'笚綷想摻癫梒偆荈湩窐曋繾鏫蘌憬稁渣½隨苆','1001-11-02 05:11:33',-3745331437675076296,-3.21618e38), +(-7153863,'鯷氤衡椻闍饑堀鱟垩啵緬氂哨笂序鉲秼摀巽茊','6800-06-20 23:39:12',-7871155140266310321,-3.04829e38), +(77,'娥藨潰眤徕菗柢礥蕶浠嶲憅榩椻鍙鑜堋ᛀ暵氎','4473-09-13 01:18:59',4076508026242316746,-1.9525e38), +(16614,'阖旕雐盬皪豧篣哙舄糗悄蟊鯴瞶珧赺潴嶽簤彉','2745-12-29 00:29:06',-4242415439257105874,2.71063e37)`) + tk.MustExec(`prepare stmt from 'SELECT *, rank() OVER (PARTITION BY col2 ORDER BY COL1) FROM PK_LP9463 WHERE col1 != ? AND col1 < ?'`) + tk.MustExec(`set @a=-8414766051197, @b=-8388608`) + tk.MustExec(`execute stmt using @a,@b`) + tk.MustExec(`set @a=16614, @b=16614`) + rows := tk.MustQuery(`execute stmt using @a,@b`).Sort() + 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 TestIssue41032(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store)