Skip to content

Commit

Permalink
executor,sessionctx: Add correctness for more system variables (#7196) (
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored and zz-jason committed Feb 25, 2019
1 parent e587a09 commit c71588e
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 107 deletions.
137 changes: 137 additions & 0 deletions executor/set_test.go
Expand Up @@ -327,4 +327,141 @@ func (s *testSuite) TestValidateSetVar(c *C) {
tk.MustExec("set time_zone='SySTeM'")
result = tk.MustQuery("select @@time_zone;")
result.Check(testkit.Rows("SYSTEM"))

// The following cases test value out of range and illegal type when setting system variables.
// See https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html for more details.
tk.MustExec("set @@global.max_connections=100001")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect max_connections value: '100001'"))
result = tk.MustQuery("select @@global.max_connections;")
result.Check(testkit.Rows("100000"))

tk.MustExec("set @@global.max_connections=-1")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect max_connections value: '-1'"))
result = tk.MustQuery("select @@global.max_connections;")
result.Check(testkit.Rows("1"))

_, err = tk.Exec("set @@global.max_connections='hello'")
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)

tk.MustExec("set @@global.max_connect_errors=18446744073709551615")

tk.MustExec("set @@global.max_connect_errors=-1")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect max_connect_errors value: '-1'"))
result = tk.MustQuery("select @@global.max_connect_errors;")
result.Check(testkit.Rows("1"))

_, err = tk.Exec("set @@global.max_connect_errors=18446744073709551616")
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)

tk.MustExec("set @@global.max_connections=100001")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect max_connections value: '100001'"))
result = tk.MustQuery("select @@global.max_connections;")
result.Check(testkit.Rows("100000"))

tk.MustExec("set @@global.max_connections=-1")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect max_connections value: '-1'"))
result = tk.MustQuery("select @@global.max_connections;")
result.Check(testkit.Rows("1"))

_, err = tk.Exec("set @@global.max_connections='hello'")
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)

tk.MustExec("set @@max_sort_length=1")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect max_sort_length value: '1'"))
result = tk.MustQuery("select @@max_sort_length;")
result.Check(testkit.Rows("4"))

tk.MustExec("set @@max_sort_length=-100")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect max_sort_length value: '-100'"))
result = tk.MustQuery("select @@max_sort_length;")
result.Check(testkit.Rows("4"))

tk.MustExec("set @@max_sort_length=8388609")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect max_sort_length value: '8388609'"))
result = tk.MustQuery("select @@max_sort_length;")
result.Check(testkit.Rows("8388608"))

_, err = tk.Exec("set @@max_sort_length='hello'")
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)

tk.MustExec("set @@global.table_definition_cache=399")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect table_definition_cache value: '399'"))
result = tk.MustQuery("select @@global.table_definition_cache;")
result.Check(testkit.Rows("400"))

tk.MustExec("set @@global.table_definition_cache=-1")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect table_definition_cache value: '-1'"))
result = tk.MustQuery("select @@global.table_definition_cache;")
result.Check(testkit.Rows("400"))

tk.MustExec("set @@global.table_definition_cache=524289")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect table_definition_cache value: '524289'"))
result = tk.MustQuery("select @@global.table_definition_cache;")
result.Check(testkit.Rows("524288"))

_, err = tk.Exec("set @@global.table_definition_cache='hello'")
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)

tk.MustExec("set @@old_passwords=-1")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect old_passwords value: '-1'"))
result = tk.MustQuery("select @@old_passwords;")
result.Check(testkit.Rows("0"))

tk.MustExec("set @@old_passwords=3")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect old_passwords value: '3'"))
result = tk.MustQuery("select @@old_passwords;")
result.Check(testkit.Rows("2"))

_, err = tk.Exec("set @@old_passwords='hello'")
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)

tk.MustExec("set @@tmp_table_size=-1")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect tmp_table_size value: '-1'"))
result = tk.MustQuery("select @@tmp_table_size;")
result.Check(testkit.Rows("1024"))

tk.MustExec("set @@tmp_table_size=1020")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect tmp_table_size value: '1020'"))
result = tk.MustQuery("select @@tmp_table_size;")
result.Check(testkit.Rows("1024"))

tk.MustExec("set @@tmp_table_size=167772161")
result = tk.MustQuery("select @@tmp_table_size;")
result.Check(testkit.Rows("167772161"))

tk.MustExec("set @@tmp_table_size=18446744073709551615")
result = tk.MustQuery("select @@tmp_table_size;")
result.Check(testkit.Rows("18446744073709551615"))

_, err = tk.Exec("set @@tmp_table_size=18446744073709551616")
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)

_, err = tk.Exec("set @@tmp_table_size='hello'")
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)

tk.MustExec("set @@global.connect_timeout=1")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect connect_timeout value: '1'"))
result = tk.MustQuery("select @@global.connect_timeout;")
result.Check(testkit.Rows("2"))

tk.MustExec("set @@global.connect_timeout=31536000")
result = tk.MustQuery("select @@global.connect_timeout;")
result.Check(testkit.Rows("31536000"))

tk.MustExec("set @@global.connect_timeout=31536001")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect connect_timeout value: '31536001'"))
result = tk.MustQuery("select @@global.connect_timeout;")
result.Check(testkit.Rows("31536000"))

result = tk.MustQuery("select @@sql_select_limit;")
result.Check(testkit.Rows("18446744073709551615"))
tk.MustExec("set @@sql_select_limit=default")
result = tk.MustQuery("select @@sql_select_limit;")
result.Check(testkit.Rows("18446744073709551615"))

tk.MustExec("set @@global.flush_time=31536001")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect flush_time value: '31536001'"))

tk.MustExec("set @@global.interactive_timeout=31536001")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect interactive_timeout value: '31536001'"))
}
23 changes: 17 additions & 6 deletions sessionctx/variable/sysvar.go
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/util/charset"
"github.com/pingcap/tidb/util/logutil"
)

Expand Down Expand Up @@ -127,15 +128,15 @@ var defaultSysVars = []*SysVar{
{ScopeSession, "rand_seed2", ""},
{ScopeGlobal, "validate_password_number_count", "1"},
{ScopeSession, "gtid_next", ""},
{ScopeGlobal | ScopeSession, "sql_select_limit", "18446744073709551615"},
{ScopeGlobal | ScopeSession, SQLSelectLimit, "18446744073709551615"},
{ScopeGlobal, "ndb_show_foreign_key_mock_tables", ""},
{ScopeNone, "multi_range_count", "256"},
{ScopeGlobal | ScopeSession, DefaultWeekFormat, "0"},
{ScopeGlobal | ScopeSession, "binlog_error_action", "IGNORE_ERROR"},
{ScopeGlobal, "slave_transaction_retries", "10"},
{ScopeGlobal | ScopeSession, "default_storage_engine", "InnoDB"},
{ScopeNone, "ft_query_expansion_limit", "20"},
{ScopeGlobal, "max_connect_errors", "100"},
{ScopeGlobal, MaxConnectErrors, "100"},
{ScopeGlobal, "sync_binlog", "0"},
{ScopeNone, "max_digest_length", "1024"},
{ScopeNone, "innodb_force_load_corrupted", "OFF"},
Expand All @@ -145,15 +146,15 @@ var defaultSysVars = []*SysVar{
{ScopeGlobal, "log_backward_compatible_user_definitions", ""},
{ScopeNone, "lc_messages_dir", "/usr/local/mysql-5.6.25-osx10.8-x86_64/share/"},
{ScopeGlobal, "ft_boolean_syntax", "+ -><()~*:\"\"&|"},
{ScopeGlobal, "table_definition_cache", "1400"},
{ScopeGlobal, TableDefinitionCache, "-1"},
{ScopeNone, SkipNameResolve, "0"},
{ScopeNone, "performance_schema_max_file_handles", "32768"},
{ScopeSession, "transaction_allow_batching", ""},
{ScopeGlobal | ScopeSession, SQLModeVar, mysql.DefaultSQLMode},
{ScopeNone, "performance_schema_max_statement_classes", "168"},
{ScopeGlobal, "server_id", "0"},
{ScopeGlobal, "innodb_flushing_avg_loops", "30"},
{ScopeGlobal | ScopeSession, "tmp_table_size", "16777216"},
{ScopeGlobal | ScopeSession, TmpTableSize, "16777216"},
{ScopeGlobal, "innodb_max_purge_lag", "0"},
{ScopeGlobal | ScopeSession, "preload_buffer_size", "32768"},
{ScopeGlobal, "slave_checkpoint_period", "300"},
Expand All @@ -162,8 +163,8 @@ var defaultSysVars = []*SysVar{
{ScopeGlobal, "innodb_flush_log_at_timeout", "1"},
{ScopeGlobal, "innodb_max_undo_log_size", ""},
{ScopeGlobal | ScopeSession, "range_alloc_block_size", "4096"},
{ScopeGlobal, "connect_timeout", "10"},
{ScopeGlobal | ScopeSession, "collation_server", "latin1_swedish_ci"},
{ScopeGlobal, ConnectTimeout, "10"},
{ScopeGlobal | ScopeSession, "collation_server", charset.CollationUTF8},
{ScopeNone, "have_rtree_keys", "YES"},
{ScopeGlobal, "innodb_old_blocks_pct", "37"},
{ScopeGlobal, "innodb_file_format", "Antelope"},
Expand Down Expand Up @@ -742,6 +743,16 @@ const (
ErrorCount = "error_count"
// BlockEncryptionMode is the name for 'block_encryption_mode' system variable.
BlockEncryptionMode = "block_encryption_mode"
// SQLSelectLimit is the name for 'sql_select_limit' system variable.
SQLSelectLimit = "sql_select_limit"
// MaxConnectErrors is the name for 'max_connect_errors' system variable.
MaxConnectErrors = "max_connect_errors"
// TableDefinitionCache is the name for 'table_definition_cache' system variable.
TableDefinitionCache = "table_definition_cache"
// TmpTableSize is the name for 'tmp_table_size' system variable.
TmpTableSize = "tmp_table_size"
// ConnectTimeout is the name for 'connect_timeout' system variable.
ConnectTimeout = "connect_timeout"
)

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

0 comments on commit c71588e

Please sign in to comment.