Skip to content

Commit 6a67d6d

Browse files
committed
executor,sessionctx: Add correctness for more system variables (pingcap#7196)
1 parent 08cde60 commit 6a67d6d

File tree

3 files changed

+219
-107
lines changed

3 files changed

+219
-107
lines changed

executor/set_test.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,141 @@ func (s *testSuite) TestValidateSetVar(c *C) {
327327
tk.MustExec("set time_zone='SySTeM'")
328328
result = tk.MustQuery("select @@time_zone;")
329329
result.Check(testkit.Rows("SYSTEM"))
330+
331+
// The following cases test value out of range and illegal type when setting system variables.
332+
// See https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html for more details.
333+
tk.MustExec("set @@global.max_connections=100001")
334+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect max_connections value: '100001'"))
335+
result = tk.MustQuery("select @@global.max_connections;")
336+
result.Check(testkit.Rows("100000"))
337+
338+
tk.MustExec("set @@global.max_connections=-1")
339+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect max_connections value: '-1'"))
340+
result = tk.MustQuery("select @@global.max_connections;")
341+
result.Check(testkit.Rows("1"))
342+
343+
_, err = tk.Exec("set @@global.max_connections='hello'")
344+
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)
345+
346+
tk.MustExec("set @@global.max_connect_errors=18446744073709551615")
347+
348+
tk.MustExec("set @@global.max_connect_errors=-1")
349+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect max_connect_errors value: '-1'"))
350+
result = tk.MustQuery("select @@global.max_connect_errors;")
351+
result.Check(testkit.Rows("1"))
352+
353+
_, err = tk.Exec("set @@global.max_connect_errors=18446744073709551616")
354+
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)
355+
356+
tk.MustExec("set @@global.max_connections=100001")
357+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect max_connections value: '100001'"))
358+
result = tk.MustQuery("select @@global.max_connections;")
359+
result.Check(testkit.Rows("100000"))
360+
361+
tk.MustExec("set @@global.max_connections=-1")
362+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect max_connections value: '-1'"))
363+
result = tk.MustQuery("select @@global.max_connections;")
364+
result.Check(testkit.Rows("1"))
365+
366+
_, err = tk.Exec("set @@global.max_connections='hello'")
367+
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)
368+
369+
tk.MustExec("set @@max_sort_length=1")
370+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect max_sort_length value: '1'"))
371+
result = tk.MustQuery("select @@max_sort_length;")
372+
result.Check(testkit.Rows("4"))
373+
374+
tk.MustExec("set @@max_sort_length=-100")
375+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect max_sort_length value: '-100'"))
376+
result = tk.MustQuery("select @@max_sort_length;")
377+
result.Check(testkit.Rows("4"))
378+
379+
tk.MustExec("set @@max_sort_length=8388609")
380+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect max_sort_length value: '8388609'"))
381+
result = tk.MustQuery("select @@max_sort_length;")
382+
result.Check(testkit.Rows("8388608"))
383+
384+
_, err = tk.Exec("set @@max_sort_length='hello'")
385+
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)
386+
387+
tk.MustExec("set @@global.table_definition_cache=399")
388+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect table_definition_cache value: '399'"))
389+
result = tk.MustQuery("select @@global.table_definition_cache;")
390+
result.Check(testkit.Rows("400"))
391+
392+
tk.MustExec("set @@global.table_definition_cache=-1")
393+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect table_definition_cache value: '-1'"))
394+
result = tk.MustQuery("select @@global.table_definition_cache;")
395+
result.Check(testkit.Rows("400"))
396+
397+
tk.MustExec("set @@global.table_definition_cache=524289")
398+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect table_definition_cache value: '524289'"))
399+
result = tk.MustQuery("select @@global.table_definition_cache;")
400+
result.Check(testkit.Rows("524288"))
401+
402+
_, err = tk.Exec("set @@global.table_definition_cache='hello'")
403+
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)
404+
405+
tk.MustExec("set @@old_passwords=-1")
406+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect old_passwords value: '-1'"))
407+
result = tk.MustQuery("select @@old_passwords;")
408+
result.Check(testkit.Rows("0"))
409+
410+
tk.MustExec("set @@old_passwords=3")
411+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect old_passwords value: '3'"))
412+
result = tk.MustQuery("select @@old_passwords;")
413+
result.Check(testkit.Rows("2"))
414+
415+
_, err = tk.Exec("set @@old_passwords='hello'")
416+
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)
417+
418+
tk.MustExec("set @@tmp_table_size=-1")
419+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect tmp_table_size value: '-1'"))
420+
result = tk.MustQuery("select @@tmp_table_size;")
421+
result.Check(testkit.Rows("1024"))
422+
423+
tk.MustExec("set @@tmp_table_size=1020")
424+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect tmp_table_size value: '1020'"))
425+
result = tk.MustQuery("select @@tmp_table_size;")
426+
result.Check(testkit.Rows("1024"))
427+
428+
tk.MustExec("set @@tmp_table_size=167772161")
429+
result = tk.MustQuery("select @@tmp_table_size;")
430+
result.Check(testkit.Rows("167772161"))
431+
432+
tk.MustExec("set @@tmp_table_size=18446744073709551615")
433+
result = tk.MustQuery("select @@tmp_table_size;")
434+
result.Check(testkit.Rows("18446744073709551615"))
435+
436+
_, err = tk.Exec("set @@tmp_table_size=18446744073709551616")
437+
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)
438+
439+
_, err = tk.Exec("set @@tmp_table_size='hello'")
440+
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)
441+
442+
tk.MustExec("set @@global.connect_timeout=1")
443+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect connect_timeout value: '1'"))
444+
result = tk.MustQuery("select @@global.connect_timeout;")
445+
result.Check(testkit.Rows("2"))
446+
447+
tk.MustExec("set @@global.connect_timeout=31536000")
448+
result = tk.MustQuery("select @@global.connect_timeout;")
449+
result.Check(testkit.Rows("31536000"))
450+
451+
tk.MustExec("set @@global.connect_timeout=31536001")
452+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect connect_timeout value: '31536001'"))
453+
result = tk.MustQuery("select @@global.connect_timeout;")
454+
result.Check(testkit.Rows("31536000"))
455+
456+
result = tk.MustQuery("select @@sql_select_limit;")
457+
result.Check(testkit.Rows("18446744073709551615"))
458+
tk.MustExec("set @@sql_select_limit=default")
459+
result = tk.MustQuery("select @@sql_select_limit;")
460+
result.Check(testkit.Rows("18446744073709551615"))
461+
462+
tk.MustExec("set @@global.flush_time=31536001")
463+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect flush_time value: '31536001'"))
464+
465+
tk.MustExec("set @@global.interactive_timeout=31536001")
466+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect interactive_timeout value: '31536001'"))
330467
}

sessionctx/variable/sysvar.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ var defaultSysVars = []*SysVar{
127127
{ScopeSession, "rand_seed2", ""},
128128
{ScopeGlobal, "validate_password_number_count", "1"},
129129
{ScopeSession, "gtid_next", ""},
130-
{ScopeGlobal | ScopeSession, "sql_select_limit", "18446744073709551615"},
130+
{ScopeGlobal | ScopeSession, SQLSelectLimit, "18446744073709551615"},
131131
{ScopeGlobal, "ndb_show_foreign_key_mock_tables", ""},
132132
{ScopeNone, "multi_range_count", "256"},
133133
{ScopeGlobal | ScopeSession, DefaultWeekFormat, "0"},
134134
{ScopeGlobal | ScopeSession, "binlog_error_action", "IGNORE_ERROR"},
135135
{ScopeGlobal, "slave_transaction_retries", "10"},
136136
{ScopeGlobal | ScopeSession, "default_storage_engine", "InnoDB"},
137137
{ScopeNone, "ft_query_expansion_limit", "20"},
138-
{ScopeGlobal, "max_connect_errors", "100"},
138+
{ScopeGlobal, MaxConnectErrors, "100"},
139139
{ScopeGlobal, "sync_binlog", "0"},
140140
{ScopeNone, "max_digest_length", "1024"},
141141
{ScopeNone, "innodb_force_load_corrupted", "OFF"},
@@ -145,15 +145,15 @@ var defaultSysVars = []*SysVar{
145145
{ScopeGlobal, "log_backward_compatible_user_definitions", ""},
146146
{ScopeNone, "lc_messages_dir", "/usr/local/mysql-5.6.25-osx10.8-x86_64/share/"},
147147
{ScopeGlobal, "ft_boolean_syntax", "+ -><()~*:\"\"&|"},
148-
{ScopeGlobal, "table_definition_cache", "1400"},
148+
{ScopeGlobal, TableDefinitionCache, "-1"},
149149
{ScopeNone, SkipNameResolve, "0"},
150150
{ScopeNone, "performance_schema_max_file_handles", "32768"},
151151
{ScopeSession, "transaction_allow_batching", ""},
152152
{ScopeGlobal | ScopeSession, SQLModeVar, mysql.DefaultSQLMode},
153153
{ScopeNone, "performance_schema_max_statement_classes", "168"},
154154
{ScopeGlobal, "server_id", "0"},
155155
{ScopeGlobal, "innodb_flushing_avg_loops", "30"},
156-
{ScopeGlobal | ScopeSession, "tmp_table_size", "16777216"},
156+
{ScopeGlobal | ScopeSession, TmpTableSize, "16777216"},
157157
{ScopeGlobal, "innodb_max_purge_lag", "0"},
158158
{ScopeGlobal | ScopeSession, "preload_buffer_size", "32768"},
159159
{ScopeGlobal, "slave_checkpoint_period", "300"},
@@ -162,8 +162,8 @@ var defaultSysVars = []*SysVar{
162162
{ScopeGlobal, "innodb_flush_log_at_timeout", "1"},
163163
{ScopeGlobal, "innodb_max_undo_log_size", ""},
164164
{ScopeGlobal | ScopeSession, "range_alloc_block_size", "4096"},
165-
{ScopeGlobal, "connect_timeout", "10"},
166-
{ScopeGlobal | ScopeSession, "collation_server", "latin1_swedish_ci"},
165+
{ScopeGlobal, ConnectTimeout, "10"},
166+
{ScopeGlobal | ScopeSession, "collation_server", charset.CollationUTF8},
167167
{ScopeNone, "have_rtree_keys", "YES"},
168168
{ScopeGlobal, "innodb_old_blocks_pct", "37"},
169169
{ScopeGlobal, "innodb_file_format", "Antelope"},
@@ -742,6 +742,16 @@ const (
742742
ErrorCount = "error_count"
743743
// BlockEncryptionMode is the name for 'block_encryption_mode' system variable.
744744
BlockEncryptionMode = "block_encryption_mode"
745+
// SQLSelectLimit is the name for 'sql_select_limit' system variable.
746+
SQLSelectLimit = "sql_select_limit"
747+
// MaxConnectErrors is the name for 'max_connect_errors' system variable.
748+
MaxConnectErrors = "max_connect_errors"
749+
// TableDefinitionCache is the name for 'table_definition_cache' system variable.
750+
TableDefinitionCache = "table_definition_cache"
751+
// TmpTableSize is the name for 'tmp_table_size' system variable.
752+
TmpTableSize = "tmp_table_size"
753+
// ConnectTimeout is the name for 'connect_timeout' system variable.
754+
ConnectTimeout = "connect_timeout"
745755
)
746756

747757
// GlobalVarAccessor is the interface for accessing global scope system and status variables.

0 commit comments

Comments
 (0)