github.com/vedadiyan/sqlparser@v1.0.0/pkg/sysvars/sysvars.go (about)

     1  /*
     2  Copyright 2020 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sysvars
    18  
    19  import "sync"
    20  
    21  // This information lives here, because it's needed from the vtgate planbuilder, the vtgate engine,
    22  // and the AST rewriter, that happens to live in sqlparser.
    23  
    24  // SystemVariable is a system variable that Vitess handles in queries such as:
    25  // select @@sql_mode
    26  // set skip_query_plan_cache = true
    27  type SystemVariable struct {
    28  	// IsBoolean is used to signal necessary type coercion so that strings
    29  	// and numbers can be evaluated to a boolean value
    30  	IsBoolean bool
    31  
    32  	// IdentifierAsString allows identifiers (a.k.a. ColName) from the AST to be handled as if they are strings.
    33  	// SET transaction_mode = two_pc => SET transaction_mode = 'two_pc'
    34  	IdentifierAsString bool
    35  
    36  	// Default is the default value, if none is given
    37  	Default string
    38  
    39  	Name string
    40  
    41  	SupportSetVar bool
    42  
    43  	Case StorageCase
    44  }
    45  
    46  type StorageCase int
    47  
    48  const (
    49  	SCSame StorageCase = iota
    50  	SCUpper
    51  	SCLower
    52  )
    53  
    54  // System Settings
    55  var (
    56  	on      = "1"
    57  	off     = "0"
    58  	utf8mb4 = "'utf8mb4'"
    59  
    60  	Autocommit                  = SystemVariable{Name: "autocommit", IsBoolean: true, Default: on}
    61  	Charset                     = SystemVariable{Name: "charset", Default: utf8mb4, IdentifierAsString: true}
    62  	ClientFoundRows             = SystemVariable{Name: "client_found_rows", IsBoolean: true, Default: off}
    63  	SessionEnableSystemSettings = SystemVariable{Name: "enable_system_settings", IsBoolean: true, Default: on}
    64  	Names                       = SystemVariable{Name: "names", Default: utf8mb4, IdentifierAsString: true}
    65  	SessionUUID                 = SystemVariable{Name: "session_uuid", IdentifierAsString: true}
    66  	SkipQueryPlanCache          = SystemVariable{Name: "skip_query_plan_cache", IsBoolean: true, Default: off}
    67  	Socket                      = SystemVariable{Name: "socket", Default: off}
    68  	SQLSelectLimit              = SystemVariable{Name: "sql_select_limit", Default: off, SupportSetVar: true}
    69  	TransactionMode             = SystemVariable{Name: "transaction_mode", IdentifierAsString: true}
    70  	TransactionReadOnly         = SystemVariable{Name: "transaction_read_only", IsBoolean: true, Default: off}
    71  	TxReadOnly                  = SystemVariable{Name: "tx_read_only", IsBoolean: true, Default: off}
    72  	Workload                    = SystemVariable{Name: "workload", IdentifierAsString: true}
    73  	QueryTimeout                = SystemVariable{Name: "query_timeout"}
    74  
    75  	// Online DDL
    76  	DDLStrategy    = SystemVariable{Name: "ddl_strategy", IdentifierAsString: true}
    77  	Version        = SystemVariable{Name: "version"}
    78  	VersionComment = SystemVariable{Name: "version_comment"}
    79  
    80  	// Read After Write settings
    81  	ReadAfterWriteGTID    = SystemVariable{Name: "read_after_write_gtid"}
    82  	ReadAfterWriteTimeOut = SystemVariable{Name: "read_after_write_timeout"}
    83  	SessionTrackGTIDs     = SystemVariable{Name: "session_track_gtids", IdentifierAsString: true}
    84  
    85  	VitessAware = []SystemVariable{
    86  		Autocommit,
    87  		ClientFoundRows,
    88  		SkipQueryPlanCache,
    89  		TxReadOnly,
    90  		TransactionReadOnly,
    91  		SQLSelectLimit,
    92  		TransactionMode,
    93  		DDLStrategy,
    94  		Workload,
    95  		Charset,
    96  		Names,
    97  		SessionUUID,
    98  		SessionEnableSystemSettings,
    99  		ReadAfterWriteGTID,
   100  		ReadAfterWriteTimeOut,
   101  		SessionTrackGTIDs,
   102  		QueryTimeout,
   103  	}
   104  
   105  	ReadOnly = []SystemVariable{
   106  		Socket,
   107  		Version,
   108  		VersionComment,
   109  	}
   110  
   111  	IgnoreThese = []SystemVariable{
   112  		{Name: "big_tables", IsBoolean: true, SupportSetVar: true},
   113  		{Name: "bulk_insert_buffer_size", SupportSetVar: true},
   114  		{Name: "debug"},
   115  		{Name: "default_storage_engine"},
   116  		{Name: "default_tmp_storage_engine", SupportSetVar: true},
   117  		{Name: "innodb_strict_mode", IsBoolean: true},
   118  		{Name: "innodb_support_xa", IsBoolean: true},
   119  		{Name: "innodb_table_locks", IsBoolean: true},
   120  		{Name: "innodb_tmpdir"},
   121  		{Name: "join_buffer_size", SupportSetVar: true},
   122  		{Name: "keep_files_on_create", IsBoolean: true},
   123  		{Name: "lc_messages"},
   124  		{Name: "long_query_time"},
   125  		{Name: "low_priority_updates", IsBoolean: true},
   126  		{Name: "max_delayed_threads"},
   127  		{Name: "max_insert_delayed_threads"},
   128  		{Name: "multi_range_count"},
   129  		{Name: "net_buffer_length"},
   130  		{Name: "new", IsBoolean: true},
   131  		{Name: "query_cache_type"},
   132  		{Name: "query_cache_wlock_invalidate", IsBoolean: true},
   133  		{Name: "query_prealloc_size"},
   134  		{Name: "sql_buffer_result", IsBoolean: true, SupportSetVar: true},
   135  		{Name: "transaction_alloc_block_size"},
   136  		{Name: "wait_timeout"},
   137  	}
   138  
   139  	NotSupported = []SystemVariable{
   140  		{Name: "audit_log_read_buffer_size"},
   141  		{Name: "auto_increment_increment", SupportSetVar: true},
   142  		{Name: "auto_increment_offset", SupportSetVar: true},
   143  		{Name: "binlog_direct_non_transactional_updates"},
   144  		{Name: "binlog_row_image"},
   145  		{Name: "binlog_rows_query_log_events"},
   146  		{Name: "innodb_ft_enable_stopword"},
   147  		{Name: "innodb_ft_user_stopword_table"},
   148  		{Name: "max_points_in_geometry", SupportSetVar: true},
   149  		{Name: "max_sp_recursion_depth"},
   150  		{Name: "myisam_repair_threads"},
   151  		{Name: "myisam_sort_buffer_size"},
   152  		{Name: "myisam_stats_method"},
   153  		{Name: "ndb_allow_copying_alter_table"},
   154  		{Name: "ndb_autoincrement_prefetch_sz"},
   155  		{Name: "ndb_blob_read_batch_bytes"},
   156  		{Name: "ndb_blob_write_batch_bytes"},
   157  		{Name: "ndb_deferred_constraints"},
   158  		{Name: "ndb_force_send"},
   159  		{Name: "ndb_fully_replicated"},
   160  		{Name: "ndb_index_stat_enable"},
   161  		{Name: "ndb_index_stat_option"},
   162  		{Name: "ndb_join_pushdown"},
   163  		{Name: "ndb_log_bin"},
   164  		{Name: "ndb_log_exclusive_reads"},
   165  		{Name: "ndb_row_checksum"},
   166  		{Name: "ndb_use_exact_count"},
   167  		{Name: "ndb_use_transactions"},
   168  		{Name: "ndbinfo_max_bytes"},
   169  		{Name: "ndbinfo_max_rows"},
   170  		{Name: "ndbinfo_show_hidden"},
   171  		{Name: "ndbinfo_table_prefix"},
   172  		{Name: "old_alter_table"},
   173  		{Name: "preload_buffer_size"},
   174  		{Name: "rbr_exec_mode"},
   175  		{Name: "sql_log_off"},
   176  		{Name: "thread_pool_high_priority_connection"},
   177  		{Name: "thread_pool_prio_kickup_timer"},
   178  		{Name: "transaction_write_set_extraction"},
   179  	}
   180  	UseReservedConn = []SystemVariable{
   181  		{Name: "default_week_format"},
   182  		{Name: "end_markers_in_json", IsBoolean: true, SupportSetVar: true},
   183  		{Name: "eq_range_index_dive_limit", SupportSetVar: true},
   184  		{Name: "explicit_defaults_for_timestamp"},
   185  		{Name: "foreign_key_checks", IsBoolean: true, SupportSetVar: true},
   186  		{Name: "group_concat_max_len", SupportSetVar: true},
   187  		{Name: "information_schema_stats_expiry"},
   188  		{Name: "max_heap_table_size", SupportSetVar: true},
   189  		{Name: "max_seeks_for_key", SupportSetVar: true},
   190  		{Name: "max_tmp_tables"},
   191  		{Name: "min_examined_row_limit"},
   192  		{Name: "old_passwords"},
   193  		{Name: "optimizer_prune_level", SupportSetVar: true},
   194  		{Name: "optimizer_search_depth", SupportSetVar: true},
   195  		{Name: "optimizer_switch", SupportSetVar: true},
   196  		{Name: "optimizer_trace"},
   197  		{Name: "optimizer_trace_features"},
   198  		{Name: "optimizer_trace_limit"},
   199  		{Name: "optimizer_trace_max_mem_size"},
   200  		{Name: "optimizer_trace_offset"},
   201  		{Name: "parser_max_mem_size"},
   202  		{Name: "profiling", IsBoolean: true},
   203  		{Name: "profiling_history_size"},
   204  		{Name: "query_alloc_block_size"},
   205  		{Name: "range_alloc_block_size", SupportSetVar: true},
   206  		{Name: "range_optimizer_max_mem_size", SupportSetVar: true},
   207  		{Name: "read_buffer_size", SupportSetVar: true},
   208  		{Name: "read_rnd_buffer_size", SupportSetVar: true},
   209  		{Name: "show_create_table_verbosity", IsBoolean: true},
   210  		{Name: "show_old_temporals", IsBoolean: true},
   211  		{Name: "sort_buffer_size", SupportSetVar: true},
   212  		{Name: "sql_big_selects", IsBoolean: true, SupportSetVar: true},
   213  		{Name: "sql_mode", SupportSetVar: true},
   214  		{Name: "sql_notes", IsBoolean: true},
   215  		{Name: "sql_quote_show_create", IsBoolean: true},
   216  		{Name: "sql_safe_updates", IsBoolean: true, SupportSetVar: true},
   217  		{Name: "sql_warnings", IsBoolean: true},
   218  		{Name: "time_zone"},
   219  		{Name: "tmp_table_size", SupportSetVar: true},
   220  		{Name: "transaction_isolation", Case: SCUpper},
   221  		{Name: "transaction_prealloc_size"},
   222  		{Name: "tx_isolation", Case: SCUpper},
   223  		{Name: "unique_checks", IsBoolean: true, SupportSetVar: true},
   224  		{Name: "updatable_views_with_limit", IsBoolean: true, SupportSetVar: true},
   225  	}
   226  	CheckAndIgnore = []SystemVariable{
   227  		// TODO: Most of these settings should be moved into SysSetOpAware, and change Vitess behaviour.
   228  		// Until then, SET statements against these settings are allowed
   229  		// as long as they have the same value as the underlying database
   230  		{Name: "binlog_format"},
   231  		{Name: "block_encryption_mode"},
   232  		{Name: "character_set_client"},
   233  		{Name: "character_set_connection"},
   234  		{Name: "character_set_database"},
   235  		{Name: "character_set_filesystem"},
   236  		{Name: "character_set_results"},
   237  		{Name: "character_set_server"},
   238  		{Name: "collation_connection"},
   239  		{Name: "collation_database"},
   240  		{Name: "collation_server"},
   241  		{Name: "completion_type"},
   242  		{Name: "div_precision_increment", SupportSetVar: true},
   243  		{Name: "innodb_lock_wait_timeout"},
   244  		{Name: "interactive_timeout"},
   245  		{Name: "lc_time_names"},
   246  		{Name: "lock_wait_timeout", SupportSetVar: true},
   247  		{Name: "max_allowed_packet"},
   248  		{Name: "max_error_count", SupportSetVar: true},
   249  		{Name: "max_execution_time", SupportSetVar: true},
   250  		{Name: "max_join_size", SupportSetVar: true},
   251  		{Name: "max_length_for_sort_data", SupportSetVar: true},
   252  		{Name: "max_sort_length", SupportSetVar: true},
   253  		{Name: "max_user_connections"},
   254  		{Name: "net_read_timeout"},
   255  		{Name: "net_retry_count"},
   256  		{Name: "net_write_timeout"},
   257  		{Name: "session_track_schema", IsBoolean: true},
   258  		{Name: "session_track_state_change", IsBoolean: true},
   259  		{Name: "session_track_system_variables"},
   260  		{Name: "session_track_transaction_info"},
   261  		{Name: "sql_auto_is_null", IsBoolean: true, SupportSetVar: true},
   262  		{Name: "version_tokens_session"},
   263  	}
   264  )
   265  
   266  // GetInterestingVariables is used to return all the variables that may be listed in a SHOW VARIABLES command.
   267  func GetInterestingVariables() []string {
   268  	var res []string
   269  	// Add all the vitess aware variables
   270  	for _, variable := range VitessAware {
   271  		res = append(res, variable.Name)
   272  	}
   273  	// Also add version and version comment
   274  	res = append(res, Version.Name)
   275  	res = append(res, VersionComment.Name)
   276  	res = append(res, Socket.Name)
   277  
   278  	for _, variable := range UseReservedConn {
   279  		if variable.SupportSetVar {
   280  			res = append(res, variable.Name)
   281  		}
   282  	}
   283  	return res
   284  }
   285  
   286  var vitessAwareVariableNames map[string]struct{}
   287  var vitessAwareInit sync.Once
   288  
   289  func IsVitessAware(sysv string) bool {
   290  	vitessAwareInit.Do(func() {
   291  		vitessAwareVariableNames = make(map[string]struct{}, len(VitessAware))
   292  		for _, v := range VitessAware {
   293  			vitessAwareVariableNames[v.Name] = struct{}{}
   294  		}
   295  	})
   296  	_, found := vitessAwareVariableNames[sysv]
   297  	return found
   298  }