From dcc9dcc1ea75a26c1db00e7e4c1e01490e3916bf Mon Sep 17 00:00:00 2001 From: Jiaqiang Huang Date: Wed, 27 Nov 2024 03:24:38 +0800 Subject: [PATCH] ddl: `tidb_scatter_region` variable supports setting value in both upper/lower case (#57677) close pingcap/tidb#57669 --- pkg/ddl/table_split_test.go | 25 +++++++++++++++++++++++++ pkg/sessionctx/variable/sysvar.go | 7 ++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/pkg/ddl/table_split_test.go b/pkg/ddl/table_split_test.go index 7ee9ebec021c4..b47b6939988f0 100644 --- a/pkg/ddl/table_split_test.go +++ b/pkg/ddl/table_split_test.go @@ -97,6 +97,10 @@ func TestScatterRegion(t *testing.T) { tk.MustQuery("select @@tidb_scatter_region;").Check(testkit.Rows("table")) tk.MustExec("set @@tidb_scatter_region = 'global';") tk.MustQuery("select @@tidb_scatter_region;").Check(testkit.Rows("global")) + tk.MustExec("set @@tidb_scatter_region = 'TABLE';") + tk.MustQuery("select @@tidb_scatter_region;").Check(testkit.Rows("table")) + tk.MustExec("set @@tidb_scatter_region = 'GLOBAL';") + tk.MustQuery("select @@tidb_scatter_region;").Check(testkit.Rows("global")) tk.MustExec("set @@tidb_scatter_region = '';") tk.MustQuery("select @@tidb_scatter_region;").Check(testkit.Rows("")) @@ -111,13 +115,34 @@ func TestScatterRegion(t *testing.T) { tk.MustQuery("select @@global.tidb_scatter_region;").Check(testkit.Rows("global")) tk.MustExec("set global tidb_scatter_region = '';") tk.MustQuery("select @@global.tidb_scatter_region;").Check(testkit.Rows("")) + tk2 = testkit.NewTestKit(t, store) + tk2.MustQuery("select @@tidb_scatter_region;").Check(testkit.Rows("")) + + tk.MustExec("set global tidb_scatter_region = 'TABLE';") + tk.MustQuery("select @@global.tidb_scatter_region;").Check(testkit.Rows("table")) + tk.MustQuery("select @@tidb_scatter_region;").Check(testkit.Rows("")) + tk2 = testkit.NewTestKit(t, store) + tk2.MustQuery("select @@tidb_scatter_region;").Check(testkit.Rows("table")) + + tk.MustExec("set global tidb_scatter_region = 'GLOBAL';") + tk.MustQuery("select @@global.tidb_scatter_region;").Check(testkit.Rows("global")) + tk.MustExec("set global tidb_scatter_region = '';") + tk.MustQuery("select @@global.tidb_scatter_region;").Check(testkit.Rows("")) err := tk.ExecToErr("set @@tidb_scatter_region = 'test';") require.ErrorContains(t, err, "invalid value for 'test', it should be either '', 'table' or 'global'") + err = tk.ExecToErr("set @@tidb_scatter_region = 'te st';") + require.ErrorContains(t, err, "invalid value for 'te st', it should be either '', 'table' or 'global'") err = tk.ExecToErr("set @@tidb_scatter_region = '1';") require.ErrorContains(t, err, "invalid value for '1', it should be either '', 'table' or 'global'") err = tk.ExecToErr("set @@tidb_scatter_region = 0;") require.ErrorContains(t, err, "invalid value for '0', it should be either '', 'table' or 'global'") + + tk.MustQuery("select @@tidb_scatter_region;").Check(testkit.Rows("")) + tk.MustExec("set @@tidb_scatter_region = 'TaBlE';") + tk.MustQuery("select @@tidb_scatter_region;").Check(testkit.Rows("table")) + tk.MustExec("set @@tidb_scatter_region = 'gLoBaL';") + tk.MustQuery("select @@tidb_scatter_region;").Check(testkit.Rows("global")) } type kvStore interface { diff --git a/pkg/sessionctx/variable/sysvar.go b/pkg/sessionctx/variable/sysvar.go index 80c88266ce97c..5afeca38d14a7 100644 --- a/pkg/sessionctx/variable/sysvar.go +++ b/pkg/sessionctx/variable/sysvar.go @@ -818,10 +818,11 @@ var defaultSysVars = []*SysVar{ return vars.ScatterRegion, nil }, Validation: func(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) { - if normalizedValue != ScatterOff && normalizedValue != ScatterTable && normalizedValue != ScatterGlobal { - return "", fmt.Errorf("invalid value for '%s', it should be either '%s', '%s' or '%s'", normalizedValue, ScatterOff, ScatterTable, ScatterGlobal) + lowerVal := strings.ToLower(normalizedValue) + if lowerVal != ScatterOff && lowerVal != ScatterTable && lowerVal != ScatterGlobal { + return "", fmt.Errorf("invalid value for '%s', it should be either '%s', '%s' or '%s'", lowerVal, ScatterOff, ScatterTable, ScatterGlobal) } - return normalizedValue, nil + return lowerVal, nil }, }, {Scope: ScopeGlobal, Name: TiDBEnableStmtSummary, Value: BoolToOnOff(DefTiDBEnableStmtSummary), Type: TypeBool, AllowEmpty: true,