1 /* Copyright (c) 2006, 2020, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 /**
24   @file
25   File containing constants that can be used throughout the server.
26 
27   @note This file shall not contain any includes of any kinds.
28 */
29 
30 #ifndef SQL_CONST_INCLUDED
31 #define SQL_CONST_INCLUDED
32 
33 #include "my_inttypes.h"
34 
35 #define LIBLEN FN_REFLEN - FN_LEN /* Max l{ngd p} dev */
36 /**
37   The maximum length of a key in the table definition cache.
38 
39   The key consists of the schema name, a '\0' character, the table
40   name and a '\0' character. Hence NAME_LEN * 2 + 1 + 1.
41 
42   Additionally, the key can be suffixed with either 4 + 4 extra bytes
43   for slave tmp tables, or with a single extra byte for tables in a
44   secondary storage engine. Add 4 + 4 to account for either of these
45   suffixes.
46 */
47 #define MAX_DBKEY_LENGTH (NAME_LEN * 2 + 1 + 1 + 4 + 4)
48 #define MAX_ALIAS_NAME 256
49 #define MAX_FIELD_NAME 34 /* Max colum name length +2 */
50 #define MAX_SYS_VAR_LENGTH 32
51 #define MAX_KEY MAX_INDEXES  /* Max used keys */
52 #define MAX_REF_PARTS 16U    /* Max parts used as ref */
53 #define MAX_KEY_LENGTH 3072U /* max possible key */
54 #define MAX_REFLENGTH 8      /* Max length for record ref */
55 
56 #define MAX_MBWIDTH 3 /* Max multibyte sequence */
57 #define MAX_FIELD_CHARLENGTH 255
58 #define MAX_FIELD_VARCHARLENGTH 65535
59 #define MAX_FIELD_BLOBLENGTH UINT_MAX32 /* cf field_blob::get_length() */
60 /**
61   CHAR and VARCHAR fields longer than this number of characters are converted
62   to BLOB.
63   Non-character fields longer than this number of bytes are converted to BLOB.
64   Comparisons should be '>' or '<='.
65 */
66 #define CONVERT_IF_BIGGER_TO_BLOB 512 /* Used for CREATE ... SELECT */
67 
68 /* Max column width +1 */
69 #define MAX_FIELD_WIDTH (MAX_FIELD_CHARLENGTH * MAX_MBWIDTH + 1)
70 
71 #define MAX_BIT_FIELD_LENGTH 64 /* Max length in bits for bit fields */
72 
73 #define MAX_DATE_WIDTH 10                /* YYYY-MM-DD */
74 #define MAX_TIME_WIDTH 10                /* -838:59:59 */
75 #define MAX_TIME_FULL_WIDTH 23           /* -DDDDDD HH:MM:SS.###### */
76 #define MAX_DATETIME_FULL_WIDTH 29       /* YYYY-MM-DD HH:MM:SS.###### AM */
77 #define MAX_DATETIME_WIDTH 19            /* YYYY-MM-DD HH:MM:SS */
78 #define MAX_DATETIME_COMPRESSED_WIDTH 14 /* YYYYMMDDHHMMSS */
79 
80 #define DATE_INT_DIGITS 8      /* YYYYMMDD       */
81 #define TIME_INT_DIGITS 7      /* hhhmmss        */
82 #define DATETIME_INT_DIGITS 14 /* YYYYMMDDhhmmss */
83 
84 /**
85   MAX_TABLES and xxx_TABLE_BIT are used in optimization of table factors and
86   expressions, and in join plan generation.
87   MAX_TABLES counts the maximum number of tables that can be handled in a
88   join operation. It is the number of bits in the table_map, minus the
89   number of pseudo table bits (bits that do not represent actual tables, but
90   still need to be handled by our algorithms). The pseudo table bits are:
91   INNER_TABLE_BIT is set for all expressions that contain a parameter,
92   a subquery that accesses tables, or a function that accesses tables.
93   An expression that has only INNER_TABLE_BIT is constant for the duration
94   of a query expression, but must be evaluated at least once during execution.
95   OUTER_REF_TABLE_BIT is set for expressions that contain a column that
96   is resolved as an outer reference. Also notice that all subquery items
97   between the column reference and the query block where the column is
98   resolved, have this bit set. Expressions that are represented by this bit
99   are constant for the duration of the subquery they are defined in.
100   RAND_TABLE_BIT is set for expressions containing a non-deterministic
101   element, such as a random function or a non-deterministic function.
102   Expressions containing this bit cannot be evaluated once and then cached,
103   they must be evaluated at latest possible point.
104   MAX_TABLES_FOR_SIZE adds the pseudo bits and is used for sizing purposes only.
105 */
106 #define MAX_TABLES_FOR_SIZE (sizeof(table_map) * 8)  ///< Use for sizing ONLY
107 #define MAX_TABLES (MAX_TABLES_FOR_SIZE - 3)         ///< Max tables in join
108 #define INNER_TABLE_BIT (((table_map)1) << (MAX_TABLES + 0))
109 #define OUTER_REF_TABLE_BIT (((table_map)1) << (MAX_TABLES + 1))
110 #define RAND_TABLE_BIT (((table_map)1) << (MAX_TABLES + 2))
111 #define PSEUDO_TABLE_BITS \
112   (INNER_TABLE_BIT | OUTER_REF_TABLE_BIT | RAND_TABLE_BIT)
113 #define MAX_FIELDS 4096 /* Maximum number of columns */
114 #define MAX_PARTITIONS 8192
115 
116 #define MAX_ENUM_VALUES 65535         /* Max number of enumeration values */
117 #define MAX_INTERVAL_VALUE_LENGTH 255 /* Max length of enum/set values */
118 
119 #define MAX_SELECT_NESTING (sizeof(nesting_map) * 8 - 1)
120 
121 #define DEFAULT_SORT_MEMORY (256UL * 1024UL)
122 #define MIN_SORT_MEMORY (32UL * 1024UL)
123 
124 /* Some portable defines */
125 
126 #define STRING_BUFFER_USUAL_SIZE 80
127 
128 /* Memory allocated when parsing a statement / saving a statement */
129 #define MEM_ROOT_BLOCK_SIZE 8192
130 #define MEM_ROOT_PREALLOC 8192
131 #define TRANS_MEM_ROOT_BLOCK_SIZE 4096
132 #define TRANS_MEM_ROOT_PREALLOC 4096
133 
134 #define DEFAULT_ERROR_COUNT 1024
135 #define EXTRA_RECORDS 10      /* Extra records in sort */
136 #define SCROLL_EXTRA 5        /* Extra scroll-rows. */
137 #define FERR -1               /* Error from my_functions */
138 #define CREATE_MODE 0         /* Default mode on new files */
139 #define NAMES_SEP_CHAR '\377' /* Char to sep. names */
140 
141 #define READ_RECORD_BUFFER (uint)(IO_SIZE * 8) /* Pointer_buffer_size */
142 #define DISK_BUFFER_SIZE (uint)(IO_SIZE * 16)  /* Size of diskbuffer */
143 
144 /***************************************************************************
145   Configuration parameters
146 ****************************************************************************/
147 
148 #define ACL_CACHE_SIZE 256
149 #define MAX_PASSWORD_LENGTH 32
150 #define HOST_CACHE_SIZE 128
151 #define MAX_ACCEPT_RETRY 10  // Test accept this many times
152 #define MAX_FIELDS_BEFORE_HASH 32
153 #define USER_VARS_HASH_SIZE 16
154 #define TABLE_OPEN_CACHE_MIN 400
155 #define TABLE_OPEN_CACHE_DEFAULT 4000
156 static const ulong TABLE_DEF_CACHE_DEFAULT = 400;
157 static const ulong SCHEMA_DEF_CACHE_DEFAULT = 256;
158 static const ulong STORED_PROGRAM_DEF_CACHE_DEFAULT = 256;
159 static const ulong TABLESPACE_DEF_CACHE_DEFAULT = 256;
160 static const ulong EVENT_DEF_CACHE_DEFAULT = 256;
161 
162 /**
163   Maximum number of connections default value.
164   151 is larger than Apache's default max children,
165   to avoid "too many connections" error in a common setup.
166 */
167 #define MAX_CONNECTIONS_DEFAULT 151
168 /**
169   We must have room for at least 400 table definitions in the table
170   cache, since otherwise there is no chance prepared
171   statements that use these many tables can work.
172   Prepared statements use table definition cache ids (table_map_id)
173   as table version identifiers. If the table definition
174   cache size is less than the number of tables used in a statement,
175   the contents of the table definition cache is guaranteed to rotate
176   between a prepare and execute. This leads to stable validation
177   errors. In future we shall use more stable version identifiers,
178   for now the only solution is to ensure that the table definition
179   cache can contain at least all tables of a given statement.
180 */
181 static const ulong TABLE_DEF_CACHE_MIN = 400;
182 static const ulong SCHEMA_DEF_CACHE_MIN = 256;
183 static const ulong STORED_PROGRAM_DEF_CACHE_MIN = 256;
184 static const ulong TABLESPACE_DEF_CACHE_MIN = 256;
185 static const ulong EVENT_DEF_CACHE_MIN = 256;
186 
187 /*
188   Stack reservation.
189   Feel free to raise this by the smallest amount you can to get the
190   "execution_constants" test to pass.
191 */
192 #if defined HAVE_UBSAN && SIZEOF_CHARP == 4
193 #define STACK_MIN_SIZE 30000  // Abort if less stack during eval.
194 #else
195 #define STACK_MIN_SIZE 20000  // Abort if less stack during eval.
196 #endif
197 
198 #define STACK_MIN_SIZE_FOR_OPEN 1024 * 80
199 
200 #if defined(__SUNPRO_CC)
201 #define STACK_BUFF_ALLOC 352 * 2  ///< For stack overrun checks
202 #else
203 #define STACK_BUFF_ALLOC 352  ///< For stack overrun checks
204 #endif
205 
206 #ifndef MYSQLD_NET_RETRY_COUNT
207 #define MYSQLD_NET_RETRY_COUNT 10  ///< Abort read after this many int.
208 #endif
209 
210 #define QUERY_ALLOC_BLOCK_SIZE 8192
211 #define QUERY_ALLOC_PREALLOC_SIZE 8192
212 #define TRANS_ALLOC_BLOCK_SIZE 4096
213 #define TRANS_ALLOC_PREALLOC_SIZE 4096
214 #define RANGE_ALLOC_BLOCK_SIZE 4096
215 #define ACL_ALLOC_BLOCK_SIZE 1024
216 #define UDF_ALLOC_BLOCK_SIZE 1024
217 #define TABLE_ALLOC_BLOCK_SIZE 1024
218 #define WARN_ALLOC_BLOCK_SIZE 2048
219 
220 /*
221   The following parameters is to decide when to use an extra cache to
222   optimise seeks when reading a big table in sorted order
223 */
224 #define MIN_FILE_LENGTH_TO_USE_ROW_CACHE (10L * 1024 * 1024)
225 #define MIN_ROWS_TO_USE_TABLE_CACHE 100
226 #define MIN_ROWS_TO_USE_BULK_INSERT 100
227 
228 /*
229   For sequential disk seeks the cost formula is:
230     DISK_SEEK_BASE_COST + DISK_SEEK_PROP_COST * #blocks_to_skip
231 
232   The cost of average seek
233     DISK_SEEK_BASE_COST + DISK_SEEK_PROP_COST*BLOCKS_IN_AVG_SEEK =1.0.
234 */
235 #define DISK_SEEK_BASE_COST (0.9)
236 
237 #define BLOCKS_IN_AVG_SEEK 128
238 
239 #define DISK_SEEK_PROP_COST (0.1 / BLOCKS_IN_AVG_SEEK)
240 
241 /**
242   Number of rows in a reference table when refereed through a not unique key.
243   This value is only used when we don't know anything about the key
244   distribution.
245 */
246 #define MATCHING_ROWS_IN_OTHER_TABLE 10
247 
248 #define MY_CHARSET_BIN_MB_MAXLEN 1
249 
250 /** Don't pack string keys shorter than this (if PACK_KEYS=1 isn't used). */
251 #define KEY_DEFAULT_PACK_LENGTH 8
252 
253 /** Characters shown for the command in 'show processlist'. */
254 #define PROCESS_LIST_WIDTH 100
255 /* Characters shown for the command in 'information_schema.processlist' */
256 #define PROCESS_LIST_INFO_WIDTH 65535
257 
258 #define PRECISION_FOR_DOUBLE 53
259 #define PRECISION_FOR_FLOAT 24
260 
261 /* -[digits].E+## */
262 #define MAX_FLOAT_STR_LENGTH (FLT_DIG + 6)
263 /* -[digits].E+### */
264 #define MAX_DOUBLE_STR_LENGTH (DBL_DIG + 7)
265 
266 /*
267   Default time to wait before aborting a new client connection
268   that does not respond to "initial server greeting" timely
269 */
270 #define CONNECT_TIMEOUT 10
271 
272 /* The following can also be changed from the command line */
273 #define DEFAULT_CONCURRENCY 10
274 #define DELAYED_LIMIT 100 /**< pause after xxx inserts */
275 #define DELAYED_QUEUE_SIZE 1000
276 #define DELAYED_WAIT_TIMEOUT 5 * 60 /**< Wait for delayed insert */
277 
278 #define LONG_TIMEOUT ((ulong)3600L * 24L * 365L)
279 
280 /**
281   Maximum length of time zone name that we support (Time zone name is
282   char(64) in db). mysqlbinlog needs it.
283 */
284 #define MAX_TIME_ZONE_NAME_LENGTH (NAME_LEN + 1)
285 
286 #if defined(_WIN32)
287 #define INTERRUPT_PRIOR -2
288 #define CONNECT_PRIOR -1
289 #define WAIT_PRIOR 0
290 #define QUERY_PRIOR 2
291 #else
292 #define INTERRUPT_PRIOR 10
293 #define CONNECT_PRIOR 9
294 #define WAIT_PRIOR 8
295 #define QUERY_PRIOR 6
296 #endif /* _WIN32 */
297 
298 /*
299   Flags below are set when we perform
300   context analysis of the statement and make
301   subqueries non-const. It prevents subquery
302   evaluation at context analysis stage.
303 */
304 
305 /*
306   Don't evaluate this subquery during statement prepare even if
307   it's a constant one. The flag is switched off in the end of
308   mysqld_stmt_prepare.
309 */
310 #define CONTEXT_ANALYSIS_ONLY_PREPARE 1
311 /*
312   Special SELECT_LEX::prepare mode: changing of query is prohibited.
313   When creating a view, we need to just check its syntax omitting
314   any optimizations: afterwards definition of the view will be
315   reconstructed by means of ::print() methods and written to
316   to an .frm file. We need this definition to stay untouched.
317 */
318 #define CONTEXT_ANALYSIS_ONLY_VIEW 2
319 /*
320   Don't evaluate this subquery during derived table prepare even if
321   it's a constant one.
322 */
323 #define CONTEXT_ANALYSIS_ONLY_DERIVED 4
324 
325 /* @@optimizer_switch flags. These must be in sync with optimizer_switch_typelib
326  */
327 #define OPTIMIZER_SWITCH_INDEX_MERGE (1ULL << 0)
328 #define OPTIMIZER_SWITCH_INDEX_MERGE_UNION (1ULL << 1)
329 #define OPTIMIZER_SWITCH_INDEX_MERGE_SORT_UNION (1ULL << 2)
330 #define OPTIMIZER_SWITCH_INDEX_MERGE_INTERSECT (1ULL << 3)
331 #define OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN (1ULL << 4)
332 #define OPTIMIZER_SWITCH_INDEX_CONDITION_PUSHDOWN (1ULL << 5)
333 /** If this is off, MRR is never used. */
334 #define OPTIMIZER_SWITCH_MRR (1ULL << 6)
335 /**
336    If OPTIMIZER_SWITCH_MRR is on and this is on, MRR is used depending on a
337    cost-based choice ("automatic"). If OPTIMIZER_SWITCH_MRR is on and this is
338    off, MRR is "forced" (i.e. used as long as the storage engine is capable of
339    doing it).
340 */
341 #define OPTIMIZER_SWITCH_MRR_COST_BASED (1ULL << 7)
342 #define OPTIMIZER_SWITCH_BNL (1ULL << 8)
343 #define OPTIMIZER_SWITCH_BKA (1ULL << 9)
344 #define OPTIMIZER_SWITCH_MATERIALIZATION (1ULL << 10)
345 #define OPTIMIZER_SWITCH_SEMIJOIN (1ULL << 11)
346 #define OPTIMIZER_SWITCH_LOOSE_SCAN (1ULL << 12)
347 #define OPTIMIZER_SWITCH_FIRSTMATCH (1ULL << 13)
348 #define OPTIMIZER_SWITCH_DUPSWEEDOUT (1ULL << 14)
349 #define OPTIMIZER_SWITCH_SUBQ_MAT_COST_BASED (1ULL << 15)
350 #define OPTIMIZER_SWITCH_USE_INDEX_EXTENSIONS (1ULL << 16)
351 #define OPTIMIZER_SWITCH_COND_FANOUT_FILTER (1ULL << 17)
352 #define OPTIMIZER_SWITCH_DERIVED_MERGE (1ULL << 18)
353 #define OPTIMIZER_SWITCH_USE_INVISIBLE_INDEXES (1ULL << 19)
354 #define OPTIMIZER_SKIP_SCAN (1ULL << 20)
355 #define OPTIMIZER_SWITCH_HASH_JOIN (1ULL << 21)
356 #define OPTIMIZER_SWITCH_SUBQUERY_TO_DERIVED (1ULL << 22)
357 #define OPTIMIZER_SWITCH_PREFER_ORDERING_INDEX (1ULL << 23)
358 #define OPTIMIZER_SWITCH_LAST (1ULL << 24)
359 
360 // Including the switch in this set, makes its default 'on'
361 #define OPTIMIZER_SWITCH_DEFAULT                                          \
362   (OPTIMIZER_SWITCH_INDEX_MERGE | OPTIMIZER_SWITCH_INDEX_MERGE_UNION |    \
363    OPTIMIZER_SWITCH_INDEX_MERGE_SORT_UNION |                              \
364    OPTIMIZER_SWITCH_INDEX_MERGE_INTERSECT |                               \
365    OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN |                           \
366    OPTIMIZER_SWITCH_INDEX_CONDITION_PUSHDOWN | OPTIMIZER_SWITCH_MRR |     \
367    OPTIMIZER_SWITCH_MRR_COST_BASED | OPTIMIZER_SWITCH_BNL |               \
368    OPTIMIZER_SWITCH_MATERIALIZATION | OPTIMIZER_SWITCH_SEMIJOIN |         \
369    OPTIMIZER_SWITCH_LOOSE_SCAN | OPTIMIZER_SWITCH_FIRSTMATCH |            \
370    OPTIMIZER_SWITCH_DUPSWEEDOUT | OPTIMIZER_SWITCH_SUBQ_MAT_COST_BASED |  \
371    OPTIMIZER_SWITCH_USE_INDEX_EXTENSIONS |                                \
372    OPTIMIZER_SWITCH_COND_FANOUT_FILTER | OPTIMIZER_SWITCH_DERIVED_MERGE | \
373    OPTIMIZER_SKIP_SCAN | OPTIMIZER_SWITCH_HASH_JOIN |                     \
374    OPTIMIZER_SWITCH_PREFER_ORDERING_INDEX)
375 
376 enum SHOW_COMP_OPTION { SHOW_OPTION_YES, SHOW_OPTION_NO, SHOW_OPTION_DISABLED };
377 
378 enum enum_mark_columns {
379   MARK_COLUMNS_NONE,
380   MARK_COLUMNS_READ,
381   MARK_COLUMNS_WRITE,
382   MARK_COLUMNS_TEMP
383 };
384 
385 /*
386   Exit code used by mysqld_exit, exit and _exit function
387   to indicate successful termination of mysqld.
388 */
389 #define MYSQLD_SUCCESS_EXIT 0
390 /*
391   Exit code used by mysqld_exit, exit and _exit function to
392   signify unsuccessful termination of mysqld. The exit
393   code signifies the server should NOT BE RESTARTED AUTOMATICALLY
394   by init systems like systemd.
395 */
396 #define MYSQLD_ABORT_EXIT 1
397 /*
398   Exit code used by mysqld_exit, exit and _exit function to
399   signify unsuccessful termination of mysqld. The exit code
400   signifies the server should be RESTARTED AUTOMATICALLY by
401   init systems like systemd.
402 */
403 #define MYSQLD_FAILURE_EXIT 2
404 /*
405   Exit code used by mysqld_exit, my_thread_exit function which allows
406   for external programs like systemd, mysqld_safe to restart mysqld
407   server. The exit code  16 is choosen so it is safe as InnoDB code
408   exit directly with values like 3.
409 */
410 #define MYSQLD_RESTART_EXIT 16
411 
412 #define UUID_LENGTH (8 + 1 + 4 + 1 + 4 + 1 + 4 + 1 + 12)
413 
414 /*
415   This enumeration type is used only by the function find_item_in_list
416   to return the info on how an item has been resolved against a list
417   of possibly aliased items.
418   The item can be resolved:
419    - against an alias name of the list's element (RESOLVED_AGAINST_ALIAS)
420    - against non-aliased field name of the list  (RESOLVED_WITH_NO_ALIAS)
421    - against an aliased field name of the list   (RESOLVED_BEHIND_ALIAS)
422    - ignoring the alias name in cases when SQL requires to ignore aliases
423      (e.g. when the resolved field reference contains a table name or
424      when the resolved item is an expression)   (RESOLVED_IGNORING_ALIAS)
425 */
426 enum enum_resolution_type {
427   NOT_RESOLVED = 0,
428   RESOLVED_BEHIND_ALIAS,
429   RESOLVED_AGAINST_ALIAS,
430   RESOLVED_WITH_NO_ALIAS,
431   RESOLVED_IGNORING_ALIAS
432 };
433 
434 /// Enumeration for {Item,SELECT_LEX[_UNIT],Table_function}::walk
435 enum class enum_walk {
436   PREFIX = 0x01,
437   POSTFIX = 0x02,
438   SUBQUERY = 0x04,
439   SUBQUERY_PREFIX = 0x05,  // Combine prefix and subquery traversal
440   SUBQUERY_POSTFIX = 0x06  // Combine postfix and subquery traversal
441 };
442 
443 inline enum_walk operator|(enum_walk lhs, enum_walk rhs) {
444   return enum_walk(int(lhs) | int(rhs));
445 }
446 
447 inline bool operator&(enum_walk lhs, enum_walk rhs) {
448   return (int(lhs) & int(rhs)) != 0;
449 }
450 
451 class Item;
452 /// Processor type for {Item,SELECT_LEX[_UNIT],Table_function}::walk
453 typedef bool (Item::*Item_processor)(uchar *arg);
454 
455 /// Enumeration for SELECT_LEX::condition_context.
456 /// If the expression being resolved belongs to a condition clause (WHERE, etc),
457 /// it is connected to the clause's root through a chain of Items; tells if this
458 /// chain matches ^(AND)*$ ("is top-level"), ^(AND|OR)*$, or neither.
459 enum class enum_condition_context {
460   NEITHER,
461   ANDS,
462   ANDS_ORS,
463 };
464 
465 /// Used to uniquely name expressions in derived tables
466 #define SYNTHETIC_FIELD_NAME "Name_exp_"
467 #endif /* SQL_CONST_INCLUDED */
468