From 38792204f961622ecfacd6c6aee711bbebf2d07b Mon Sep 17 00:00:00 2001 From: Zijie Lu Date: Thu, 24 Dec 2020 16:09:30 +0800 Subject: [PATCH] executor, session, sessionctx: make last_plan_from_cache and last_plan_from_binding read-only (#21953) --- executor/set.go | 8 -------- session/session_test.go | 9 +++++++++ sessionctx/variable/sysvar.go | 4 ++-- sessionctx/variable/varsutil_test.go | 12 ++---------- 4 files changed, 13 insertions(+), 20 deletions(-) diff --git a/executor/set.go b/executor/set.go index f1944bc1e69db..503871fbe4a55 100644 --- a/executor/set.go +++ b/executor/set.go @@ -180,14 +180,6 @@ func (e *SetExecutor) setSysVariable(name string, v *expression.VarAssignment) e if name == variable.TxnIsolationOneShot && sessionVars.InTxn() { return errors.Trace(ErrCantChangeTxCharacteristics) } - if name == variable.TiDBFoundInPlanCache { - sessionVars.StmtCtx.AppendWarning(fmt.Errorf("Set operation for '%s' will not take effect", variable.TiDBFoundInPlanCache)) - return nil - } - if name == variable.TiDBFoundInBinding { - sessionVars.StmtCtx.AppendWarning(fmt.Errorf("Set operation for '%s' will not take effect", variable.TiDBFoundInBinding)) - return nil - } err = variable.SetSessionSystemVar(sessionVars, name, value) if err != nil { return err diff --git a/session/session_test.go b/session/session_test.go index 811a3f6f11919..4fcf91414c187 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -3810,3 +3810,12 @@ func (s *testSessionSerialSuite) TestIssue21944(c *C) { _, err := tk1.Exec("set @@tidb_current_ts=1;") c.Assert(err.Error(), Equals, "[variable:1238]Variable 'tidb_current_ts' is a read only variable") } + +func (s *testSessionSerialSuite) TestIssue21943(c *C) { + tk := testkit.NewTestKitWithInit(c, s.store) + _, err := tk.Exec("set @@last_plan_from_binding='123';") + c.Assert(err.Error(), Equals, "[variable:1238]Variable 'last_plan_from_binding' is a read only variable") + + _, err = tk.Exec("set @@last_plan_from_cache='123';") + c.Assert(err.Error(), Equals, "[variable:1238]Variable 'last_plan_from_cache' is a read only variable") +} diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index 7b65bb3d06867..eb57e10f8b4e1 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -1131,8 +1131,8 @@ var defaultSysVars = []*SysVar{ {Scope: ScopeSession, Name: TiDBEnableSlowLog, Value: BoolToOnOff(logutil.DefaultTiDBEnableSlowLog), Type: TypeBool}, {Scope: ScopeSession, Name: TiDBQueryLogMaxLen, Value: strconv.Itoa(logutil.DefaultQueryLogMaxLen), Type: TypeInt, MinValue: -1, MaxValue: math.MaxInt64}, {Scope: ScopeSession, Name: TiDBCheckMb4ValueInUTF8, Value: BoolToOnOff(config.GetGlobalConfig().CheckMb4ValueInUTF8), Type: TypeBool}, - {Scope: ScopeSession, Name: TiDBFoundInPlanCache, Value: BoolToOnOff(DefTiDBFoundInPlanCache), Type: TypeBool}, - {Scope: ScopeSession, Name: TiDBFoundInBinding, Value: BoolToOnOff(DefTiDBFoundInBinding), Type: TypeBool}, + {Scope: ScopeSession, Name: TiDBFoundInPlanCache, Value: BoolToOnOff(DefTiDBFoundInPlanCache), Type: TypeBool, ReadOnly: true}, + {Scope: ScopeSession, Name: TiDBFoundInBinding, Value: BoolToOnOff(DefTiDBFoundInBinding), Type: TypeBool, ReadOnly: true}, {Scope: ScopeSession, Name: TiDBEnableCollectExecutionInfo, Value: BoolToOnOff(DefTiDBEnableCollectExecutionInfo), Type: TypeBool}, {Scope: ScopeGlobal | ScopeSession, Name: TiDBAllowAutoRandExplicitInsert, Value: BoolToOnOff(DefTiDBAllowAutoRandExplicitInsert), Type: TypeBool}, {Scope: ScopeGlobal | ScopeSession, Name: TiDBEnableClusteredIndex, Value: BoolToOnOff(DefTiDBEnableClusteredIndex), Type: TypeBool}, diff --git a/sessionctx/variable/varsutil_test.go b/sessionctx/variable/varsutil_test.go index f8e49d6f2ff67..c5b56363c3338 100644 --- a/sessionctx/variable/varsutil_test.go +++ b/sessionctx/variable/varsutil_test.go @@ -466,18 +466,10 @@ func (s *testVarsutilSuite) TestVarsutil(c *C) { c.Assert(err, ErrorMatches, ".*Incorrect argument type to variable 'tidb_stmt_summary_max_sql_length'") err = SetSessionSystemVar(v, TiDBFoundInPlanCache, types.NewStringDatum("1")) - c.Assert(err, IsNil) - val, err = GetSessionSystemVar(v, TiDBFoundInPlanCache) - c.Assert(err, IsNil) - c.Assert(val, Equals, "OFF") - c.Assert(v.systems[TiDBFoundInPlanCache], Equals, "ON") + c.Assert(err, ErrorMatches, ".*]Variable 'last_plan_from_cache' is a read only variable") err = SetSessionSystemVar(v, TiDBFoundInBinding, types.NewStringDatum("1")) - c.Assert(err, IsNil) - val, err = GetSessionSystemVar(v, TiDBFoundInBinding) - c.Assert(err, IsNil) - c.Assert(val, Equals, "OFF") - c.Assert(v.systems[TiDBFoundInBinding], Equals, "ON") + c.Assert(err, ErrorMatches, ".*]Variable 'last_plan_from_binding' is a read only variable") err = SetSessionSystemVar(v, TiDBEnableChangeColumnType, types.NewStringDatum("ON")) c.Assert(err, IsNil)