1 /*
2 Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include "sql/table.h"
25
26 #include "my_config.h"
27
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <algorithm>
32 #include <memory>
33 #include <string>
34 #include <unordered_map>
35 #include <utility>
36
37 #include "ft_global.h"
38 #include "m_string.h"
39 #include "map_helpers.h"
40 #include "my_alloc.h"
41 #include "my_byteorder.h"
42 #include "my_dbug.h"
43 #include "my_io.h"
44 #include "my_loglevel.h"
45 #include "my_macros.h"
46 #include "my_pointer_arithmetic.h"
47 #include "my_psi_config.h"
48 #include "my_sqlcommand.h"
49 #include "my_thread_local.h"
50 #include "myisam.h" // MI_MAX_KEY_LENGTH
51 #include "mysql/components/services/log_builtins.h"
52 #include "mysql/components/services/log_shared.h"
53 #include "mysql/mysql_lex_string.h"
54 #include "mysql/plugin.h"
55 #include "mysql/psi/mysql_file.h"
56 #include "mysql/psi/mysql_mutex.h"
57 #include "mysql/psi/mysql_table.h"
58 #include "mysql/psi/psi_base.h"
59 #include "mysql/psi/psi_table.h"
60 #include "mysql/service_mysql_alloc.h"
61 #include "mysql/udf_registration_types.h"
62 #include "mysql_com.h"
63 #include "mysql_version.h" // MYSQL_VERSION_ID
64 #include "mysqld_error.h"
65 #include "sql/auth/auth_acls.h"
66 #include "sql/auth/auth_common.h" // acl_getroot
67 #include "sql/auth/sql_security_ctx.h"
68 #include "sql/binlog.h" // mysql_bin_log
69 #include "sql/dd/cache/dictionary_client.h" // dd::cache_Dictionary_client
70 #include "sql/dd/dd.h" // dd::get_dictionary
71 #include "sql/dd/dictionary.h" // dd::Dictionary
72 #include "sql/dd/types/abstract_table.h"
73 #include "sql/dd/types/table.h" // dd::Table
74 #include "sql/dd/types/view.h" // dd::View
75 #include "sql/debug_sync.h" // DEBUG_SYNC
76 #include "sql/derror.h" // ER_THD
77 #include "sql/error_handler.h" // Strict_error_handler
78 #include "sql/field.h"
79 #include "sql/filesort.h" // filesort_free_buffers
80 #include "sql/gis/srid.h"
81 #include "sql/item.h"
82 #include "sql/item_cmpfunc.h" // and_conds
83 #include "sql/item_json_func.h" // Item_func_array_cast
84 #include "sql/json_diff.h" // Json_diff_vector
85 #include "sql/json_dom.h" // Json_wrapper
86 #include "sql/json_path.h"
87 #include "sql/key.h" // find_ref_key
88 #include "sql/log.h"
89 #include "sql/my_decimal.h"
90 #include "sql/mysqld.h" // reg_ext key_file_frm ...
91 #include "sql/nested_join.h"
92 #include "sql/opt_trace.h" // opt_trace_disable_if_no_security_...
93 #include "sql/opt_trace_context.h"
94 #include "sql/parse_file.h" // sql_parse_prepare
95 #include "sql/partition_info.h" // partition_info
96 #include "sql/psi_memory_key.h"
97 #include "sql/query_options.h"
98 #include "sql/query_result.h" // Query_result
99 #include "sql/sql_base.h"
100 #include "sql/sql_check_constraint.h" // Sql_table_check_constraint
101 #include "sql/sql_class.h" // THD
102 #include "sql/sql_error.h"
103 #include "sql/sql_lex.h"
104 #include "sql/sql_opt_exec_shared.h"
105 #include "sql/sql_parse.h" // check_stack_overrun
106 #include "sql/sql_partition.h" // mysql_unpack_partition
107 #include "sql/sql_plugin.h" // plugin_unlock
108 #include "sql/sql_select.h" // actual_key_parts
109 #include "sql/sql_table.h" // build_table_filename
110 #include "sql/sql_tablespace.h" // validate_tablespace_name())
111 #include "sql/strfunc.h" // find_type
112 #include "sql/system_variables.h"
113 #include "sql/table_cache.h" // table_cache_manager
114 #include "sql/table_trigger_dispatcher.h" // Table_trigger_dispatcher
115 #include "sql/thd_raii.h"
116 #include "sql/thr_malloc.h"
117 #include "sql/trigger_def.h"
118 #include "sql_const.h"
119 #include "sql_string.h"
120 #include "template_utils.h" // down_cast
121 #include "thr_mutex.h"
122 /* INFORMATION_SCHEMA name */
123 LEX_CSTRING INFORMATION_SCHEMA_NAME = {STRING_WITH_LEN("information_schema")};
124
125 /* PERFORMANCE_SCHEMA name */
126 LEX_CSTRING PERFORMANCE_SCHEMA_DB_NAME = {
127 STRING_WITH_LEN("performance_schema")};
128
129 /* MYSQL_SCHEMA name */
130 LEX_CSTRING MYSQL_SCHEMA_NAME = {STRING_WITH_LEN("mysql")};
131
132 /* MYSQL_TABLESPACE name */
133 LEX_CSTRING MYSQL_TABLESPACE_NAME = {STRING_WITH_LEN("mysql")};
134
135 /* GENERAL_LOG name */
136 LEX_CSTRING GENERAL_LOG_NAME = {STRING_WITH_LEN("general_log")};
137
138 /* SLOW_LOG name */
139 LEX_CSTRING SLOW_LOG_NAME = {STRING_WITH_LEN("slow_log")};
140
141 /* RLI_INFO name */
142 LEX_CSTRING RLI_INFO_NAME = {STRING_WITH_LEN("slave_relay_log_info")};
143
144 /* MI_INFO name */
145 LEX_CSTRING MI_INFO_NAME = {STRING_WITH_LEN("slave_master_info")};
146
147 /* WORKER_INFO name */
148 LEX_CSTRING WORKER_INFO_NAME = {STRING_WITH_LEN("slave_worker_info")};
149
150 /* GTID_EXECUTED name */
151 LEX_CSTRING GTID_EXECUTED_NAME = {STRING_WITH_LEN("gtid_executed")};
152
153 /* Keyword for parsing generated column functions */
154 LEX_CSTRING PARSE_GCOL_KEYWORD = {STRING_WITH_LEN("parse_gcol_expr")};
155
156 /* Functions defined in this file */
157
158 static Item *create_view_field(THD *thd, TABLE_LIST *view, Item **field_ref,
159 const char *name,
160 Name_resolution_context *context);
161 static void open_table_error(THD *thd, TABLE_SHARE *share, int error,
162 int db_errno);
163
164 inline bool is_system_table_name(const char *name, size_t length);
165
166 /**************************************************************************
167 Object_creation_ctx implementation.
168 **************************************************************************/
169
set_n_backup(THD * thd)170 Object_creation_ctx *Object_creation_ctx::set_n_backup(THD *thd) {
171 Object_creation_ctx *backup_ctx;
172 DBUG_TRACE;
173
174 backup_ctx = create_backup_ctx(thd);
175 change_env(thd);
176
177 return backup_ctx;
178 }
179
restore_env(THD * thd,Object_creation_ctx * backup_ctx)180 void Object_creation_ctx::restore_env(THD *thd,
181 Object_creation_ctx *backup_ctx) {
182 if (!backup_ctx) return;
183
184 backup_ctx->change_env(thd);
185 backup_ctx->delete_backup_ctx();
186 }
187
188 /**************************************************************************
189 Default_object_creation_ctx implementation.
190 **************************************************************************/
191
Default_object_creation_ctx(THD * thd)192 Default_object_creation_ctx::Default_object_creation_ctx(THD *thd)
193 : m_client_cs(thd->variables.character_set_client),
194 m_connection_cl(thd->variables.collation_connection) {}
195
Default_object_creation_ctx(const CHARSET_INFO * client_cs,const CHARSET_INFO * connection_cl)196 Default_object_creation_ctx::Default_object_creation_ctx(
197 const CHARSET_INFO *client_cs, const CHARSET_INFO *connection_cl)
198 : m_client_cs(client_cs), m_connection_cl(connection_cl) {}
199
create_backup_ctx(THD * thd) const200 Object_creation_ctx *Default_object_creation_ctx::create_backup_ctx(
201 THD *thd) const {
202 return new Default_object_creation_ctx(thd);
203 }
204
delete_backup_ctx()205 void Default_object_creation_ctx::delete_backup_ctx() { delete this; }
206
change_env(THD * thd) const207 void Default_object_creation_ctx::change_env(THD *thd) const {
208 thd->variables.character_set_client = m_client_cs;
209 thd->variables.collation_connection = m_connection_cl;
210
211 thd->update_charset();
212 }
213
214 /**************************************************************************
215 View_creation_ctx implementation.
216 **************************************************************************/
217
create(THD * thd)218 View_creation_ctx *View_creation_ctx::create(THD *thd) {
219 View_creation_ctx *ctx = new (thd->mem_root) View_creation_ctx(thd);
220
221 return ctx;
222 }
223
224 /*************************************************************************/
225
create(THD * thd,TABLE_LIST * view)226 View_creation_ctx *View_creation_ctx::create(THD *thd, TABLE_LIST *view) {
227 View_creation_ctx *ctx = new (thd->mem_root) View_creation_ctx(thd);
228
229 /* Throw a warning if there is NULL cs name. */
230
231 if (!view->view_client_cs_name.str || !view->view_connection_cl_name.str) {
232 push_warning_printf(thd, Sql_condition::SL_NOTE, ER_VIEW_NO_CREATION_CTX,
233 ER_THD(thd, ER_VIEW_NO_CREATION_CTX), view->db,
234 view->table_name);
235
236 ctx->m_client_cs = system_charset_info;
237 ctx->m_connection_cl = system_charset_info;
238
239 return ctx;
240 }
241
242 /* Resolve cs names. Throw a warning if there is unknown cs name. */
243
244 bool invalid_creation_ctx;
245
246 invalid_creation_ctx = resolve_charset(
247 view->view_client_cs_name.str, system_charset_info, &ctx->m_client_cs);
248
249 invalid_creation_ctx =
250 resolve_collation(view->view_connection_cl_name.str, system_charset_info,
251 &ctx->m_connection_cl) ||
252 invalid_creation_ctx;
253
254 if (invalid_creation_ctx) {
255 LogErr(WARNING_LEVEL, ER_VIEW_UNKNOWN_CHARSET_OR_COLLATION, view->db,
256 view->table_name, view->view_client_cs_name.str,
257 view->view_connection_cl_name.str);
258
259 push_warning_printf(
260 thd, Sql_condition::SL_NOTE, ER_VIEW_INVALID_CREATION_CTX,
261 ER_THD(thd, ER_VIEW_INVALID_CREATION_CTX), view->db, view->table_name);
262 }
263
264 return ctx;
265 }
266
267 /*************************************************************************/
268
GRANT_INFO()269 GRANT_INFO::GRANT_INFO() {
270 grant_table = nullptr;
271 version = 0;
272 privilege = NO_ACCESS;
273 }
274
275 /**
276 Returns pointer to '.frm' extension of the file name.
277
278 @param name file name
279
280 Checks file name part starting with the rightmost '.' character,
281 and returns it if it is equal to '.frm'.
282
283 @todo
284 It is a good idea to get rid of this function modifying the code
285 to garantee that the functions presently calling fn_rext() always
286 get arguments in the same format: either with '.frm' or without '.frm'.
287
288 @return
289 Pointer to the '.frm' extension. If there is no extension,
290 or extension is not '.frm', pointer at the end of file name.
291 */
292
fn_rext(char * name)293 char *fn_rext(char *name) {
294 char *res = strrchr(name, '.');
295 if (res && !strcmp(res, reg_ext)) return res;
296 return name + strlen(name);
297 }
298
get_table_category(const LEX_CSTRING & db,const LEX_CSTRING & name)299 TABLE_CATEGORY get_table_category(const LEX_CSTRING &db,
300 const LEX_CSTRING &name) {
301 DBUG_ASSERT(db.str != nullptr);
302 DBUG_ASSERT(name.str != nullptr);
303
304 if (is_infoschema_db(db.str, db.length)) return TABLE_CATEGORY_INFORMATION;
305
306 if (is_perfschema_db(db.str, db.length)) return TABLE_CATEGORY_PERFORMANCE;
307
308 if ((db.length == MYSQL_SCHEMA_NAME.length) &&
309 (my_strcasecmp(system_charset_info, MYSQL_SCHEMA_NAME.str, db.str) ==
310 0)) {
311 if (is_system_table_name(name.str, name.length))
312 return TABLE_CATEGORY_SYSTEM;
313
314 if ((name.length == GENERAL_LOG_NAME.length) &&
315 (my_strcasecmp(system_charset_info, GENERAL_LOG_NAME.str, name.str) ==
316 0))
317 return TABLE_CATEGORY_LOG;
318
319 if ((name.length == SLOW_LOG_NAME.length) &&
320 (my_strcasecmp(system_charset_info, SLOW_LOG_NAME.str, name.str) == 0))
321 return TABLE_CATEGORY_LOG;
322
323 if ((name.length == RLI_INFO_NAME.length) &&
324 (my_strcasecmp(system_charset_info, RLI_INFO_NAME.str, name.str) == 0))
325 return TABLE_CATEGORY_RPL_INFO;
326
327 if ((name.length == MI_INFO_NAME.length) &&
328 (my_strcasecmp(system_charset_info, MI_INFO_NAME.str, name.str) == 0))
329 return TABLE_CATEGORY_RPL_INFO;
330
331 if ((name.length == WORKER_INFO_NAME.length) &&
332 (my_strcasecmp(system_charset_info, WORKER_INFO_NAME.str, name.str) ==
333 0))
334 return TABLE_CATEGORY_RPL_INFO;
335
336 if ((name.length == GTID_EXECUTED_NAME.length) &&
337 (my_strcasecmp(system_charset_info, GTID_EXECUTED_NAME.str, name.str) ==
338 0))
339 return TABLE_CATEGORY_GTID;
340
341 if (dd::get_dictionary()->is_dd_table_name(MYSQL_SCHEMA_NAME.str, name.str))
342 return TABLE_CATEGORY_DICTIONARY;
343 }
344
345 return TABLE_CATEGORY_USER;
346 }
347
348 /**
349 Allocate and setup a TABLE_SHARE structure
350
351 @param db schema name.
352 @param table_name table name.
353 @param key table cache key (db \0 table_name \0...)
354 @param key_length length of the key
355 @param open_secondary true if the TABLE_SHARE represents a table
356 in a secondary storage engine
357
358 @return pointer to allocated table share
359 @retval NULL error (out of memory, too long path name)
360 */
361
alloc_table_share(const char * db,const char * table_name,const char * key,size_t key_length,bool open_secondary)362 TABLE_SHARE *alloc_table_share(const char *db, const char *table_name,
363 const char *key, size_t key_length,
364 bool open_secondary) {
365 MEM_ROOT mem_root;
366 TABLE_SHARE *share = nullptr;
367 char *key_buff, *path_buff;
368 char path[FN_REFLEN + 1];
369 size_t path_length;
370 Table_cache_element **cache_element_array;
371 bool was_truncated = false;
372 DBUG_TRACE;
373 DBUG_PRINT("enter", ("table: '%s'.'%s'", db, table_name));
374
375 /*
376 There are FN_REFLEN - reg_ext_length bytes available for the
377 file path and the trailing '\0', which may be padded to the right
378 of the length indicated by the length parameter. The returned
379 path length does not include the trailing '\0'.
380 */
381 path_length = build_table_filename(path, sizeof(path) - 1 - reg_ext_length,
382 db, table_name, "", 0, &was_truncated);
383
384 /*
385 The path now misses extension, but includes '\0'. Unless it was
386 truncated, everything should be ok.
387 */
388 if (was_truncated) {
389 my_error(ER_IDENT_CAUSES_TOO_LONG_PATH, MYF(0), sizeof(path) - 1, path);
390 return nullptr;
391 }
392
393 init_sql_alloc(key_memory_table_share, &mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
394 if (multi_alloc_root(&mem_root, &share, sizeof(*share), &key_buff, key_length,
395 &path_buff, path_length + 1, &cache_element_array,
396 table_cache_instances * sizeof(*cache_element_array),
397 NULL)) {
398 new (share) TABLE_SHARE(refresh_version, open_secondary);
399
400 share->set_table_cache_key(key_buff, key, key_length);
401
402 share->path.str = path_buff;
403 share->path.length = path_length;
404 my_stpcpy(share->path.str, path);
405 share->normalized_path.str = share->path.str;
406 share->normalized_path.length = path_length;
407
408 /*
409 Since alloc_table_share() can be called without any locking (for
410 example, ha_create_table... functions), we do not assign a table
411 map id here. Instead we assign a value that is not used
412 elsewhere, and then assign a table map id inside open_table()
413 under the protection of the LOCK_open mutex.
414 */
415 share->table_map_id = ~0ULL;
416 share->cached_row_logging_check = -1;
417
418 share->m_flush_tickets.empty();
419
420 memset(cache_element_array, 0,
421 table_cache_instances * sizeof(*cache_element_array));
422 share->cache_element = cache_element_array;
423
424 share->mem_root = std::move(mem_root);
425 mysql_mutex_init(key_TABLE_SHARE_LOCK_ha_data, &share->LOCK_ha_data,
426 MY_MUTEX_INIT_FAST);
427 }
428 return share;
429 }
430
431 /**
432 Initialize share for temporary tables
433
434 @param thd thread handle
435 @param share Share to fill
436 @param key Table_cache_key, as generated from create_table_def_key.
437 must start with db name.
438 @param key_length Length of key
439 @param table_name Table name
440 @param path Path to file (possible in lower case) without .frm
441 @param mem_root MEM_ROOT to transfer (move) to the TABLE_SHARE; if
442 NULL a new one is initialized.
443
444 @note
445 This is different from alloc_table_share() because temporary tables
446 don't have to be shared between threads or put into the table def
447 cache, so we can do some things notable simpler and faster
448
449 If table is not put in thd->temporary_tables (happens only when
450 one uses OPEN TEMPORARY) then one can specify 'db' as key and
451 use key_length= 0 as neither table_cache_key or key_length will be used).
452 */
453
init_tmp_table_share(THD * thd,TABLE_SHARE * share,const char * key,size_t key_length,const char * table_name,const char * path,MEM_ROOT * mem_root)454 void init_tmp_table_share(THD *thd, TABLE_SHARE *share, const char *key,
455 size_t key_length, const char *table_name,
456 const char *path, MEM_ROOT *mem_root) {
457 DBUG_TRACE;
458 DBUG_PRINT("enter", ("table: '%s'.'%s'", key, table_name));
459
460 new (share) TABLE_SHARE();
461
462 if (mem_root)
463 share->mem_root = std::move(*mem_root);
464 else
465 init_sql_alloc(key_memory_table_share, &share->mem_root,
466 TABLE_ALLOC_BLOCK_SIZE, 0);
467
468 share->table_category = TABLE_CATEGORY_TEMPORARY;
469 share->tmp_table = INTERNAL_TMP_TABLE;
470 share->db.str = key;
471 share->db.length = strlen(key);
472 share->table_cache_key.str = key;
473 share->table_cache_key.length = key_length;
474 share->table_name.str = table_name;
475 share->table_name.length = strlen(table_name);
476 share->path.str = const_cast<char *>(path);
477 share->normalized_path.str = path;
478 share->path.length = share->normalized_path.length = strlen(path);
479
480 share->cached_row_logging_check = -1;
481
482 /*
483 table_map_id is also used for MERGE tables to suppress repeated
484 compatibility checks.
485 */
486 share->table_map_id = (ulonglong)thd->query_id;
487
488 share->m_flush_tickets.empty();
489 }
490
usable_indexes(const THD * thd) const491 Key_map TABLE_SHARE::usable_indexes(const THD *thd) const {
492 Key_map usable_indexes(keys_in_use);
493 if (!thd->optimizer_switch_flag(OPTIMIZER_SWITCH_USE_INVISIBLE_INDEXES))
494 usable_indexes.intersect(visible_indexes);
495 return usable_indexes;
496 }
497
498 #ifndef DBUG_OFF
499 /**
500 Assert that the #LOCK_open mutex is held when the reference count of
501 a TABLE_SHARE is accessed.
502
503 @param share the TABLE_SHARE
504 @return true if the assertion holds, terminates the process otherwise
505 */
assert_ref_count_is_locked(const TABLE_SHARE * share)506 bool assert_ref_count_is_locked(const TABLE_SHARE *share) {
507 // The mutex is not needed while the TABLE_SHARE is being
508 // constructed, or if it is for a temporary table.
509 if (share->table_category != TABLE_UNKNOWN_CATEGORY &&
510 share->tmp_table == NO_TMP_TABLE) {
511 mysql_mutex_assert_owner(&LOCK_open);
512 }
513 return true;
514 }
515 #endif
516
clear_version()517 void TABLE_SHARE::clear_version() {
518 table_cache_manager.assert_owner_all_and_tdc();
519 m_version = 0;
520 }
521
522 /**
523 Release resources (plugins) used by the share and free its memory.
524 TABLE_SHARE is self-contained -- it's stored in its own MEM_ROOT.
525 Free this MEM_ROOT.
526 */
527
destroy()528 void TABLE_SHARE::destroy() {
529 uint idx;
530 KEY *info_it;
531
532 DBUG_TRACE;
533 DBUG_PRINT("info", ("db: %s table: %s", db.str, table_name.str));
534 if (ha_share) {
535 delete ha_share;
536 ha_share = nullptr;
537 }
538 if (m_part_info) {
539 ::destroy(m_part_info);
540 m_part_info = nullptr;
541 }
542 /* The mutex is initialized only for shares that are part of the TDC */
543 if (tmp_table == NO_TMP_TABLE) mysql_mutex_destroy(&LOCK_ha_data);
544 delete name_hash;
545 name_hash = nullptr;
546
547 delete m_histograms;
548 m_histograms = nullptr;
549
550 plugin_unlock(nullptr, db_plugin);
551 db_plugin = nullptr;
552
553 /* Release fulltext parsers */
554 info_it = key_info;
555 for (idx = keys; idx; idx--, info_it++) {
556 if (info_it->flags & HA_USES_PARSER) {
557 plugin_unlock(nullptr, info_it->parser);
558 info_it->flags = 0;
559 }
560 }
561
562 /* Destroy dd::Table object associated with temporary table's share. */
563 delete tmp_table_def;
564 tmp_table_def = nullptr;
565
566 /* Delete the view object. */
567 delete view_object;
568 view_object = nullptr;
569
570 #ifdef HAVE_PSI_TABLE_INTERFACE
571 PSI_TABLE_CALL(release_table_share)(m_psi);
572 #endif
573
574 /*
575 Make a copy since the share is allocated in its own root,
576 and free_root() updates its argument after freeing the memory.
577 */
578 MEM_ROOT own_root = std::move(mem_root);
579 free_root(&own_root, MYF(0));
580 }
581
582 /**
583 Free table share and memory used by it
584
585 @param share Table share
586 */
587
free_table_share(TABLE_SHARE * share)588 void free_table_share(TABLE_SHARE *share) {
589 DBUG_TRACE;
590 DBUG_PRINT("enter", ("table: %s.%s", share->db.str, share->table_name.str));
591 DBUG_ASSERT(share->ref_count() == 0);
592
593 if (share->m_flush_tickets.is_empty()) {
594 /*
595 No threads are waiting for this share to be flushed (the
596 share is not old, is for a temporary table, or just nobody
597 happens to be waiting for it). Destroy it.
598 */
599 share->destroy();
600 } else {
601 Wait_for_flush_list::Iterator it(share->m_flush_tickets);
602 Wait_for_flush *ticket;
603 /*
604 We're about to iterate over a list that is used
605 concurrently. Make sure this never happens without a lock.
606 */
607 mysql_mutex_assert_owner(&LOCK_open);
608
609 while ((ticket = it++))
610 (void)ticket->get_ctx()->m_wait.set_status(MDL_wait::GRANTED);
611 /*
612 If there are threads waiting for this share to be flushed,
613 the last one to receive the notification will destroy the
614 share. At this point the share is removed from the table
615 definition cache, so is OK to proceed here without waiting
616 for this thread to do the work.
617 */
618 }
619 }
620
621 /**
622 Return true if a table name matches one of the system table names.
623 Currently these are:
624
625 help_category, help_keyword, help_relation, help_topic,
626 proc, event
627 time_zone, time_zone_leap_second, time_zone_name, time_zone_transition,
628 time_zone_transition_type
629
630 This function trades accuracy for speed, so may return false
631 positives. Presumably mysql.* database is for internal purposes only
632 and should not contain user tables.
633 */
634
is_system_table_name(const char * name,size_t length)635 inline bool is_system_table_name(const char *name, size_t length) {
636 CHARSET_INFO *ci = system_charset_info;
637
638 return (
639 /* mysql.proc table */
640 (length == 4 && my_tolower(ci, name[0]) == 'p' &&
641 my_tolower(ci, name[1]) == 'r' && my_tolower(ci, name[2]) == 'o' &&
642 my_tolower(ci, name[3]) == 'c') ||
643
644 (length > 4 &&
645 (
646 /* one of mysql.help* tables */
647 (my_tolower(ci, name[0]) == 'h' && my_tolower(ci, name[1]) == 'e' &&
648 my_tolower(ci, name[2]) == 'l' && my_tolower(ci, name[3]) == 'p') ||
649
650 /* one of mysql.time_zone* tables */
651 (my_tolower(ci, name[0]) == 't' && my_tolower(ci, name[1]) == 'i' &&
652 my_tolower(ci, name[2]) == 'm' && my_tolower(ci, name[3]) == 'e') ||
653
654 /* mysql.event table */
655 (my_tolower(ci, name[0]) == 'e' && my_tolower(ci, name[1]) == 'v' &&
656 my_tolower(ci, name[2]) == 'e' && my_tolower(ci, name[3]) == 'n' &&
657 my_tolower(ci, name[4]) == 't'))));
658 }
659
660 /**
661 Initialize key_part_flag from source field.
662 */
663
init_flags()664 void KEY_PART_INFO::init_flags() {
665 DBUG_ASSERT(field);
666 if (field->type() == MYSQL_TYPE_BLOB || field->type() == MYSQL_TYPE_GEOMETRY)
667 key_part_flag |= HA_BLOB_PART;
668 else if (field->real_type() == MYSQL_TYPE_VARCHAR)
669 key_part_flag |= HA_VAR_LENGTH_PART;
670 else if (field->type() == MYSQL_TYPE_BIT)
671 key_part_flag |= HA_BIT_PART;
672 }
673
674 /**
675 Initialize KEY_PART_INFO from the given field.
676
677 @param fld The field to initialize keypart from
678 */
679
init_from_field(Field * fld)680 void KEY_PART_INFO::init_from_field(Field *fld) {
681 field = fld;
682 fieldnr = field->field_index() + 1;
683 null_bit = field->null_bit;
684 null_offset = field->null_offset();
685 offset = field->offset(field->table->record[0]);
686 length = (uint16)field->key_length();
687 store_length = length;
688 key_part_flag = 0;
689
690 if (field->is_nullable()) store_length += HA_KEY_NULL_LENGTH;
691 if (field->type() == MYSQL_TYPE_BLOB ||
692 field->real_type() == MYSQL_TYPE_VARCHAR ||
693 field->type() == MYSQL_TYPE_GEOMETRY) {
694 store_length += HA_KEY_BLOB_LENGTH;
695 }
696 init_flags();
697
698 ha_base_keytype key_type = field->key_type();
699 type = (uint8)key_type;
700 bin_cmp = key_type != HA_KEYTYPE_TEXT && key_type != HA_KEYTYPE_VARTEXT1 &&
701 key_type != HA_KEYTYPE_VARTEXT2;
702 }
703
704 /**
705 Setup key-related fields of Field object for given key and key part.
706
707 @param[in] share Pointer to TABLE_SHARE
708 @param[in] handler_file Pointer to handler
709 @param[in] primary_key_n Primary key number
710 @param[in] keyinfo Pointer to processed key
711 @param[in] key_n Processed key number
712 @param[in] key_part_n Processed key part number
713 @param[in,out] usable_parts Pointer to usable_parts variable
714 @param[in] part_of_key_not_extended Set when column is part of the Key
715 and not appended by the storage
716 engine from primary key columns.
717 */
718
setup_key_part_field(TABLE_SHARE * share,handler * handler_file,uint primary_key_n,KEY * keyinfo,uint key_n,uint key_part_n,uint * usable_parts,bool part_of_key_not_extended)719 void setup_key_part_field(TABLE_SHARE *share, handler *handler_file,
720 uint primary_key_n, KEY *keyinfo, uint key_n,
721 uint key_part_n, uint *usable_parts,
722 bool part_of_key_not_extended) {
723 KEY_PART_INFO *key_part = &keyinfo->key_part[key_part_n];
724 Field *field = key_part->field;
725
726 /* Flag field as unique if it is the only keypart in a unique index */
727 if (key_part_n == 0 && key_n != primary_key_n)
728 field->set_flag(
729 ((keyinfo->flags & HA_NOSAME) && (keyinfo->user_defined_key_parts == 1))
730 ? UNIQUE_KEY_FLAG
731 : MULTIPLE_KEY_FLAG);
732 if (key_part_n == 0) field->key_start.set_bit(key_n);
733 field->m_indexed = true;
734
735 const bool full_length_key_part =
736 field->key_length() == key_part->length && !field->is_flag_set(BLOB_FLAG);
737 /*
738 part_of_key contains all non-prefix keys, part_of_prefixkey
739 contains prefix keys.
740 Note that prefix keys in the extended PK key parts
741 (part_of_key_not_extended is false) are not considered.
742 */
743 if (full_length_key_part) {
744 field->part_of_key.set_bit(key_n);
745 if (part_of_key_not_extended)
746 field->part_of_key_not_extended.set_bit(key_n);
747 } else if (part_of_key_not_extended) {
748 field->part_of_prefixkey.set_bit(key_n);
749 }
750 if ((handler_file->index_flags(key_n, key_part_n, false) & HA_KEYREAD_ONLY) &&
751 field->type() != MYSQL_TYPE_GEOMETRY) {
752 // Set the key as 'keys_for_keyread' even if it is prefix key.
753 share->keys_for_keyread.set_bit(key_n);
754 }
755
756 if (full_length_key_part &&
757 (handler_file->index_flags(key_n, key_part_n, true) & HA_READ_ORDER))
758 field->part_of_sortkey.set_bit(key_n);
759
760 if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
761 *usable_parts == key_part_n)
762 (*usable_parts)++; // For FILESORT
763 }
764
765 /**
766 Generate extended secondary keys by adding primary key parts to the
767 existing secondary key. A primary key part is added if such part doesn't
768 present in the secondary key or the part in the secondary key is a
769 prefix of the key field. Key parts are added till:
770 .) all parts were added
771 .) number of key parts became bigger that MAX_REF_PARTS
772 .) total key length became longer than MAX_REF_LENGTH
773 depending on what occurs first first.
774 Unlike existing secondary key parts which are initialized at
775 open_binary_frm(), newly added ones are initialized here by copying
776 KEY_PART_INFO structure from primary key part and calling
777 setup_key_part_field().
778
779 Function updates sk->actual/unused_key_parts and sk->actual_flags.
780
781 @param[in] sk Secondary key
782 @param[in] sk_n Secondary key number
783 @param[in] pk Primary key
784 @param[in] pk_n Primary key number
785 @param[in] share Pointer to TABLE_SHARE
786 @param[in] handler_file Pointer to handler
787 @param[in,out] usable_parts Pointer to usable_parts variable
788
789 @retval Number of added key parts
790 */
791
add_pk_parts_to_sk(KEY * sk,uint sk_n,KEY * pk,uint pk_n,TABLE_SHARE * share,handler * handler_file,uint * usable_parts)792 uint add_pk_parts_to_sk(KEY *sk, uint sk_n, KEY *pk, uint pk_n,
793 TABLE_SHARE *share, handler *handler_file,
794 uint *usable_parts) {
795 uint max_key_length = sk->key_length;
796 bool is_unique_key = false;
797 KEY_PART_INFO *current_key_part = &sk->key_part[sk->user_defined_key_parts];
798
799 /*
800 For each keypart in the primary key: check if the keypart is
801 already part of the secondary key and add it if not.
802 */
803 for (uint pk_part = 0; pk_part < pk->user_defined_key_parts; pk_part++) {
804 KEY_PART_INFO *pk_key_part = &pk->key_part[pk_part];
805 /* MySQL does not supports more key parts than MAX_REF_LENGTH */
806 if (sk->actual_key_parts >= MAX_REF_PARTS) goto end;
807
808 bool pk_field_is_in_sk = false;
809 for (uint j = 0; j < sk->user_defined_key_parts; j++) {
810 if (sk->key_part[j].fieldnr == pk_key_part->fieldnr &&
811 share->field[pk_key_part->fieldnr - 1]->key_length() ==
812 sk->key_part[j].length) {
813 pk_field_is_in_sk = true;
814 break;
815 }
816 }
817
818 /* Add PK field to secondary key if it's not already part of the key. */
819 if (!pk_field_is_in_sk) {
820 /* MySQL does not supports keys longer than MAX_KEY_LENGTH */
821 if (max_key_length + pk_key_part->length > MAX_KEY_LENGTH) goto end;
822
823 *current_key_part = *pk_key_part;
824 setup_key_part_field(share, handler_file, pk_n, sk, sk_n,
825 sk->actual_key_parts, usable_parts, false);
826 sk->actual_key_parts++;
827 sk->unused_key_parts--;
828 sk->rec_per_key[sk->actual_key_parts - 1] = 0;
829 sk->set_records_per_key(sk->actual_key_parts - 1, REC_PER_KEY_UNKNOWN);
830 current_key_part++;
831 max_key_length += pk_key_part->length;
832 /*
833 Secondary key will be unique if the key does not exceed
834 key length limitation and key parts limitation.
835 */
836 is_unique_key = true;
837 }
838 }
839 if (is_unique_key) sk->actual_flags |= HA_NOSAME;
840
841 end:
842 return (sk->actual_key_parts - sk->user_defined_key_parts);
843 }
844
845 //////////////////////////////////////////////////////////////////////////
846
847 /*
848 The following section adds code for the interface with the .frm file.
849 These defines and functions comes from the file sql/field.h in 5.7
850
851 Note:
852 These functions should not be used any where else in the code.
853 They are only used in upgrade scenario for migrating old data directory
854 to be compatible with current server. They will be removed in future
855 release.
856
857 Any new code should not be added in this section.
858 */
859
860 #define FIELDFLAG_DECIMAL 1
861 #define FIELDFLAG_BINARY 1 // Shares same flag
862 #define FIELDFLAG_NUMBER 2
863 #define FIELDFLAG_ZEROFILL 4
864 #define FIELDFLAG_PACK 120 // Bits used for packing
865 #define FIELDFLAG_INTERVAL 256 // mangled with decimals!
866 #define FIELDFLAG_BITFIELD 512 // mangled with decimals!
867 #define FIELDFLAG_BLOB 1024 // mangled with decimals!
868 #define FIELDFLAG_GEOM 2048 // mangled with decimals!
869 #define FIELDFLAG_JSON \
870 4096 /* mangled with decimals and \
871 with bitfields! */
872
873 #define FIELDFLAG_TREAT_BIT_AS_CHAR 4096 /* use Field_bit_as_char */
874
875 #define FIELDFLAG_LEFT_FULLSCREEN 8192
876 #define FIELDFLAG_RIGHT_FULLSCREEN 16384
877 #define FIELDFLAG_FORMAT_NUMBER 16384 // predit: ###,,## in output
878 #define FIELDFLAG_NO_DEFAULT 16384 /* sql */
879 #define FIELDFLAG_SUM ((uint)32768) // predit: +#fieldflag
880 #define FIELDFLAG_MAYBE_NULL ((uint)32768) // sql
881 #define FIELDFLAG_PACK_SHIFT 3
882 #define FIELDFLAG_DEC_SHIFT 8
883 #define FIELDFLAG_MAX_DEC 31
884 #define FIELDFLAG_NUM_SCREEN_TYPE 0x7F01
885 #define FIELDFLAG_ALFA_SCREEN_TYPE 0x7800
886
887 #define MTYP_TYPENR(type) (type & 127) /* Remove bits from type */
888
889 #define FIELD_NR_MASK 16383 /* To get fieldnumber */
890
f_is_dec(int x)891 inline int f_is_dec(int x) { return (x & FIELDFLAG_DECIMAL); }
f_is_num(int x)892 inline int f_is_num(int x) { return (x & FIELDFLAG_NUMBER); }
f_is_zerofill(int x)893 inline int f_is_zerofill(int x) { return (x & FIELDFLAG_ZEROFILL); }
f_is_packed(int x)894 inline int f_is_packed(int x) { return (x & FIELDFLAG_PACK); }
f_packtype(int x)895 inline int f_packtype(int x) { return ((x >> FIELDFLAG_PACK_SHIFT) & 15); }
f_decimals(int x)896 inline uint8 f_decimals(int x) {
897 return ((uint8)((x >> FIELDFLAG_DEC_SHIFT) & FIELDFLAG_MAX_DEC));
898 }
f_is_alpha(int x)899 inline int f_is_alpha(int x) { return (!f_is_num(x)); }
f_is_binary(int x)900 inline int f_is_binary(int x) {
901 return (x & FIELDFLAG_BINARY); // 4.0- compatibility
902 }
f_is_enum(int x)903 inline int f_is_enum(int x) {
904 return ((x & (FIELDFLAG_INTERVAL | FIELDFLAG_NUMBER)) == FIELDFLAG_INTERVAL);
905 }
f_is_bitfield(int x)906 inline int f_is_bitfield(int x) {
907 return ((x & (FIELDFLAG_BITFIELD | FIELDFLAG_NUMBER)) == FIELDFLAG_BITFIELD);
908 }
909
f_is_blob(int x)910 inline int f_is_blob(int x) {
911 return ((x & (FIELDFLAG_BLOB | FIELDFLAG_NUMBER)) == FIELDFLAG_BLOB);
912 }
f_is_geom(int x)913 inline int f_is_geom(int x) {
914 return ((x & (FIELDFLAG_GEOM | FIELDFLAG_NUMBER)) == FIELDFLAG_GEOM);
915 }
f_is_json(int x)916 inline int f_is_json(int x) {
917 return ((x & (FIELDFLAG_JSON | FIELDFLAG_NUMBER | FIELDFLAG_BITFIELD)) ==
918 FIELDFLAG_JSON);
919 }
f_is_equ(int x)920 inline int f_is_equ(int x) { return (x & (1 + 2 + FIELDFLAG_PACK + 31 * 256)); }
f_settype(int x)921 inline int f_settype(int x) { return (x << FIELDFLAG_PACK_SHIFT); }
f_maybe_null(int x)922 inline int f_maybe_null(int x) { return (x & FIELDFLAG_MAYBE_NULL); }
f_no_default(int x)923 inline int f_no_default(int x) { return (x & FIELDFLAG_NO_DEFAULT); }
f_bit_as_char(int x)924 inline int f_bit_as_char(int x) { return (x & FIELDFLAG_TREAT_BIT_AS_CHAR); }
925
926 /**
927 Read string from a file with malloc
928
929 @note We add an \0 at end of the read string to make reading of C strings
930 easier. This function is added to read .frm file in upgrade scenario. It
931 should not be used any where else in the code. This function will be removed
932 later.
933
934 @param[in] file file handler
935 @param[out] to pointer to read string
936 @param[in] length length of string
937
938 @retval 0 Error
939 @retval 1 Success
940 */
941
read_string(File file,uchar ** to,size_t length)942 static int read_string(File file, uchar **to, size_t length) {
943 DBUG_TRACE;
944
945 my_free(*to);
946 if (!(*to = (uchar *)my_malloc(PSI_NOT_INSTRUMENTED, length + 1,
947 MYF(MY_WME))) ||
948 mysql_file_read(file, *to, length, MYF(MY_NABP))) {
949 my_free(*to); /* purecov: inspected */
950 *to = nullptr; /* purecov: inspected */
951 return 1; /* purecov: inspected */
952 }
953 *((char *)*to + length) = '\0';
954 return 0;
955 } /* read_string */
956
957 /**
958 Un-hex all elements in a typelib.
959
960 @param[in] interval TYPELIB (struct of pointer to values + lengths + count)
961
962 @note This function is added to read .frm file in upgrade scenario. It should
963 not be used any where else in the code. This function will be removed later.
964 */
965
unhex_type2(TYPELIB * interval)966 static void unhex_type2(TYPELIB *interval) {
967 for (uint pos = 0; pos < interval->count; pos++) {
968 char *from, *to;
969 for (from = to = const_cast<char *>(interval->type_names[pos]); *from;) {
970 /*
971 Note, hexchar_to_int(*from++) doesn't work
972 one some compilers, e.g. IRIX. Looks like a compiler
973 bug in inline functions in combination with arguments
974 that have a side effect. So, let's use from[0] and from[1]
975 and increment 'from' by two later.
976 */
977
978 *to++ = (char)(hexchar_to_int(from[0]) << 4) + hexchar_to_int(from[1]);
979 from += 2;
980 }
981 interval->type_lengths[pos] /= 2;
982 }
983 }
984
985 /**
986 Search after a field with given start & length
987 If an exact field isn't found, return longest field with starts
988 at right position.
989
990 @note This is needed because in some .frm fields 'fieldnr' was saved wrong.
991 This function is added to read .frm file in upgrade scenario. It should not
992 be used any where else in the code. This function will be removed later.
993
994 @retval 0 error
995 @retval field number +1 success
996 */
997
find_field(Field ** fields,uchar * record,uint start,uint length)998 static uint find_field(Field **fields, uchar *record, uint start, uint length) {
999 Field **field;
1000 uint i, pos;
1001
1002 pos = 0;
1003 for (field = fields, i = 1; *field; i++, field++) {
1004 if ((*field)->offset(record) == start) {
1005 if ((*field)->key_length() == length) return (i);
1006 if (!pos || fields[pos - 1]->pack_length() < (*field)->pack_length())
1007 pos = i;
1008 }
1009 }
1010 return (pos);
1011 }
1012
1013 /**
1014 fix a str_type to a array type
1015 typeparts separated with some char. differents types are separated
1016 with a '\0'
1017
1018 @note This function is added to read .frm file in upgrade scenario. It
1019 should not be used any where else in the code. This function will be
1020 removed later.
1021
1022 @param[out] array Pointer to interval array
1023 @param[in] point_to_type Pointer to intervals
1024 @param[in] types number of intervals
1025 @param[out] names name of intervals
1026
1027 */
1028
fix_type_pointers(const char *** array,TYPELIB * point_to_type,uint types,char ** names)1029 static void fix_type_pointers(const char ***array, TYPELIB *point_to_type,
1030 uint types, char **names) {
1031 char *type_name, *ptr;
1032 char chr;
1033
1034 ptr = *names;
1035 while (types--) {
1036 point_to_type->name = nullptr;
1037 point_to_type->type_names = *array;
1038
1039 if ((chr = *ptr)) /* Test if empty type */
1040 {
1041 while ((type_name = strchr(ptr + 1, chr)) != NullS) {
1042 *((*array)++) = ptr + 1;
1043 *type_name = '\0'; /* End string */
1044 ptr = type_name;
1045 }
1046 ptr += 2; /* Skip end mark and last 0 */
1047 } else
1048 ptr++;
1049 point_to_type->count = (uint)(*array - point_to_type->type_names);
1050 point_to_type++;
1051 *((*array)++) = NullS; /* End of type */
1052 }
1053 *names = ptr; /* Update end */
1054 return;
1055 } /* fix_type_pointers */
1056
1057 /**
1058 Find where a form starts.
1059
1060 @note This function is added to read .frm file in upgrade scenario. It should
1061 not be used any where else in the code. This function will be removed later.
1062
1063 @param[in] file File handler
1064 @param[in] head The start of the form file.
1065
1066 @remark If formname is NULL then only formnames is read.
1067
1068 @retval The form position.
1069 */
1070
get_form_pos(File file,uchar * head)1071 static ulong get_form_pos(File file, uchar *head) {
1072 uchar *pos, *buf;
1073 uint names, length;
1074 ulong ret_value = 0;
1075 DBUG_TRACE;
1076
1077 names = uint2korr(head + 8);
1078
1079 if (!(names = uint2korr(head + 8))) return 0;
1080
1081 length = uint2korr(head + 4);
1082
1083 mysql_file_seek(file, 64L, MY_SEEK_SET, MYF(0));
1084
1085 if (!(buf = (uchar *)my_malloc(PSI_NOT_INSTRUMENTED, length + names * 4,
1086 MYF(MY_WME))))
1087 return 0;
1088
1089 if (mysql_file_read(file, buf, length + names * 4, MYF(MY_NABP))) {
1090 my_free(buf);
1091 return 0;
1092 }
1093
1094 pos = buf + length;
1095 ret_value = uint4korr(pos);
1096
1097 my_free(buf);
1098
1099 return ret_value;
1100 }
1101
1102 #define STORAGE_TYPE_MASK 7
1103 #define COLUMN_FORMAT_MASK 7
1104 #define COLUMN_FORMAT_SHIFT 3
1105
1106 /**
1107 Auxiliary function which creates Field object from in-memory
1108 representation of .FRM file.
1109
1110 NOTES:
1111 This function is added to read .frm file in upgrade scenario. It should not
1112 be used any where else in the code. This function will be removed later.
1113
1114 @param thd Connection context.
1115 @param share TABLE_SHARE for which Field object
1116 needs to be constructed.
1117 @param frm_context FRM_context for the structures removed
1118 from TABLE_SHARE.
1119 @param new_frm_ver .FRM file version.
1120 @param use_hash Indicates whether we use hash or linear
1121 search to lookup fields by name.
1122 @param field_idx Field index in TABLE_SHARE::field array.
1123 @param strpos Pointer to part of .FRM's screens
1124 section describing the field to be
1125 created.
1126 @param format_section_fields Array where each byte contains packed
1127 values of COLUMN_FORMAT/STORAGE options
1128 for corresponding column.
1129 @param[in,out] comment_pos Pointer to part of column comments
1130 section of .FRM which corresponds
1131 to current field. Advanced to the
1132 position corresponding to comment
1133 for the next column.
1134 @param[in,out] gcol_screen_pos Pointer to part of generated columns
1135 section of .FRM which corresponds
1136 to current generated field. If field
1137 to be created is generated advanced
1138 to the position for the next column
1139 @param[in,out] null_pos Current byte in the record preamble
1140 to be used for field's null/leftover
1141 bits if necessary.
1142 @param[in,out] null_bit_pos Current bit in the current preamble
1143 byte to be used for field's null/
1144 leftover bits if necessary.
1145 @param[out] errarg Additional argument for the error to
1146 be reported.
1147
1148 @retval 0 Success.
1149 @retval non-0 Error number (@sa open_table_def() for details).
1150 */
1151
make_field_from_frm(THD * thd,TABLE_SHARE * share,FRM_context * frm_context,uint new_frm_ver,bool use_hash,uint field_idx,uchar * strpos,uchar * format_section_fields,char ** comment_pos,char ** gcol_screen_pos,uchar ** null_pos,uint * null_bit_pos,int * errarg)1152 static int make_field_from_frm(THD *thd, TABLE_SHARE *share,
1153 FRM_context *frm_context, uint new_frm_ver,
1154 bool use_hash, uint field_idx, uchar *strpos,
1155 uchar *format_section_fields, char **comment_pos,
1156 char **gcol_screen_pos, uchar **null_pos,
1157 uint *null_bit_pos, int *errarg) {
1158 uint pack_flag, interval_nr, unireg_type, recpos, field_length;
1159 uint gcol_info_length = 0;
1160 enum_field_types field_type;
1161 const CHARSET_INFO *charset = nullptr;
1162 Field::geometry_type geom_type = Field::GEOM_GEOMETRY;
1163 LEX_CSTRING comment;
1164 Value_generator *gcol_info = nullptr;
1165 bool fld_stored_in_db = true;
1166 Field *reg_field;
1167
1168 if (new_frm_ver >= 3) {
1169 /* new frm file in 4.1 */
1170 field_length = uint2korr(strpos + 3);
1171 recpos = uint3korr(strpos + 5);
1172 pack_flag = uint2korr(strpos + 8);
1173 unireg_type = (uint)strpos[10];
1174 interval_nr = (uint)strpos[12];
1175 uint comment_length = uint2korr(strpos + 15);
1176 field_type = (enum_field_types)(uint)strpos[13];
1177
1178 /* charset and geometry_type share the same byte in frm */
1179 if (field_type == MYSQL_TYPE_GEOMETRY) {
1180 geom_type = (Field::geometry_type)strpos[14];
1181 charset = &my_charset_bin;
1182 } else {
1183 uint csid = strpos[14] + (((uint)strpos[11]) << 8);
1184 if (!csid)
1185 charset = &my_charset_bin;
1186 else if (!(charset = get_charset(csid, MYF(0)))) {
1187 // Unknown or unavailable charset
1188 *errarg = (int)csid;
1189 return 5;
1190 }
1191 }
1192
1193 if (!comment_length) {
1194 comment.str = "";
1195 comment.length = 0;
1196 } else {
1197 comment.str = *comment_pos;
1198 comment.length = comment_length;
1199 (*comment_pos) += comment_length;
1200 }
1201
1202 if (unireg_type & FRM_context::GENERATED_FIELD) {
1203 /*
1204 Get generated column data stored in the .frm file as follows:
1205 byte 1 = 1 (always 1 to allow for future extensions)
1206 byte 2,3 = expression length
1207 byte 4 = flags, as of now:
1208 0 - no flags
1209 1 - field is physically stored
1210 byte 5-... = generated column expression (text data)
1211 */
1212 gcol_info = new (thd->mem_root) Value_generator();
1213 if ((uint)(*gcol_screen_pos)[0] != 1) return 4;
1214
1215 gcol_info_length = uint2korr(*gcol_screen_pos + 1);
1216 DBUG_ASSERT(gcol_info_length); // Expect non-null expression
1217
1218 fld_stored_in_db = (bool)(uint)(*gcol_screen_pos)[3];
1219 gcol_info->set_field_stored(fld_stored_in_db);
1220 gcol_info->dup_expr_str(&share->mem_root,
1221 *gcol_screen_pos + (uint)FRM_GCOL_HEADER_SIZE,
1222 gcol_info_length);
1223 (*gcol_screen_pos) += gcol_info_length + FRM_GCOL_HEADER_SIZE;
1224 share->vfields++;
1225 }
1226 } else {
1227 field_length = (uint)strpos[3];
1228 recpos = uint2korr(strpos + 4), pack_flag = uint2korr(strpos + 6);
1229 pack_flag &= ~FIELDFLAG_NO_DEFAULT; // Safety for old files
1230 unireg_type = (uint)strpos[8];
1231 interval_nr = (uint)strpos[10];
1232
1233 /* old frm file */
1234 field_type = (enum_field_types)f_packtype(pack_flag);
1235 if (f_is_binary(pack_flag)) {
1236 /*
1237 Try to choose the best 4.1 type:
1238 - for 4.0 "CHAR(N) BINARY" or "VARCHAR(N) BINARY"
1239 try to find a binary collation for character set.
1240 - for other types (e.g. BLOB) just use my_charset_bin.
1241 */
1242 if (!f_is_blob(pack_flag)) {
1243 // 3.23 or 4.0 string
1244 if (!(charset = get_charset_by_csname(share->table_charset->csname,
1245 MY_CS_BINSORT, MYF(0))))
1246 charset = &my_charset_bin;
1247 } else
1248 charset = &my_charset_bin;
1249 } else
1250 charset = share->table_charset;
1251 memset(&comment, 0, sizeof(comment));
1252 }
1253
1254 if (interval_nr && charset->mbminlen > 1) {
1255 /* Unescape UCS2 intervals from HEX notation */
1256 TYPELIB *interval = share->intervals + interval_nr - 1;
1257 unhex_type2(interval);
1258 }
1259
1260 if (field_type == MYSQL_TYPE_NEWDECIMAL && !share->mysql_version) {
1261 /*
1262 Fix pack length of old decimal values from 5.0.3 -> 5.0.4
1263 The difference is that in the old version we stored precision
1264 in the .frm table while we now store the display_length
1265 */
1266 uint decimals = f_decimals(pack_flag);
1267 field_length = my_decimal_precision_to_length(field_length, decimals,
1268 f_is_dec(pack_flag) == 0);
1269 LogErr(ERROR_LEVEL, ER_TABLE_INCOMPATIBLE_DECIMAL_FIELD,
1270 frm_context->fieldnames.type_names[field_idx], share->table_name.str,
1271 share->table_name.str);
1272 push_warning_printf(thd, Sql_condition::SL_WARNING, ER_CRASHED_ON_USAGE,
1273 ER_THD(thd, ER_TABLE_INCOMPATIBLE_DECIMAL_FIELD),
1274 frm_context->fieldnames.type_names[field_idx],
1275 share->table_name.str, share->table_name.str);
1276 share->crashed = true; // Marker for CHECK TABLE
1277 }
1278
1279 if (field_type == MYSQL_TYPE_YEAR && field_length != 4) {
1280 LogErr(ERROR_LEVEL, ER_TABLE_INCOMPATIBLE_YEAR_FIELD,
1281 frm_context->fieldnames.type_names[field_idx], share->table_name.str,
1282 share->table_name.str);
1283 push_warning_printf(thd, Sql_condition::SL_WARNING, ER_CRASHED_ON_USAGE,
1284 ER_THD(thd, ER_TABLE_INCOMPATIBLE_YEAR_FIELD),
1285 frm_context->fieldnames.type_names[field_idx],
1286 share->table_name.str, share->table_name.str);
1287 share->crashed = true;
1288 }
1289
1290 FRM_context::utype unireg = (FRM_context::utype)MTYP_TYPENR(unireg_type);
1291 // Construct auto_flag
1292 uchar auto_flags = Field::NONE;
1293
1294 if (unireg == FRM_context::TIMESTAMP_DN_FIELD ||
1295 unireg == FRM_context::TIMESTAMP_DNUN_FIELD)
1296 auto_flags |= Field::DEFAULT_NOW;
1297 if (unireg == FRM_context::TIMESTAMP_UN_FIELD ||
1298 unireg == FRM_context::TIMESTAMP_DNUN_FIELD)
1299 auto_flags |= Field::ON_UPDATE_NOW;
1300
1301 if (unireg == FRM_context::NEXT_NUMBER) auto_flags |= Field::NEXT_NUMBER;
1302
1303 share->field[field_idx] = reg_field = make_field(
1304 thd->mem_root, share,
1305 share->default_values - 1 + recpos, // recpos starts from 1.
1306 (uint32)field_length, *null_pos, *null_bit_pos, field_type, charset,
1307 geom_type, auto_flags,
1308 (interval_nr ? share->intervals + interval_nr - 1 : (TYPELIB *)nullptr),
1309 frm_context->fieldnames.type_names[field_idx], f_maybe_null(pack_flag),
1310 f_is_zerofill(pack_flag) != 0, f_is_dec(pack_flag) == 0,
1311 f_decimals(pack_flag), f_bit_as_char(pack_flag), 0, {},
1312 // Array fields aren't supported in .frm-based tables
1313 false);
1314 if (!reg_field) {
1315 // Not supported field type
1316 return 4;
1317 }
1318
1319 reg_field->set_field_index(field_idx);
1320 reg_field->comment = comment;
1321 reg_field->gcol_info = gcol_info;
1322 reg_field->stored_in_db = fld_stored_in_db;
1323 if (field_type == MYSQL_TYPE_BIT && !f_bit_as_char(pack_flag)) {
1324 if (((*null_bit_pos) += field_length & 7) > 7) {
1325 (*null_pos)++;
1326 (*null_bit_pos) -= 8;
1327 }
1328 }
1329 if (!reg_field->is_flag_set(NOT_NULL_FLAG)) {
1330 if (!(*null_bit_pos = (*null_bit_pos + 1) & 7)) (*null_pos)++;
1331 }
1332 if (f_no_default(pack_flag)) reg_field->set_flag(NO_DEFAULT_VALUE_FLAG);
1333
1334 if (unireg == FRM_context::NEXT_NUMBER)
1335 share->found_next_number_field = share->field + field_idx;
1336
1337 if (use_hash) {
1338 Field **field = share->field + field_idx;
1339 share->name_hash->emplace((*field)->field_name, field);
1340 }
1341
1342 if (format_section_fields) {
1343 const uchar field_flags = format_section_fields[field_idx];
1344 const uchar field_storage = (field_flags & STORAGE_TYPE_MASK);
1345 const uchar field_column_format =
1346 ((field_flags >> COLUMN_FORMAT_SHIFT) & COLUMN_FORMAT_MASK);
1347 DBUG_PRINT("debug", ("field flags: %u, storage: %u, column_format: %u",
1348 field_flags, field_storage, field_column_format));
1349 reg_field->set_storage_type((ha_storage_media)field_storage);
1350 reg_field->set_column_format((column_format_type)field_column_format);
1351 }
1352
1353 if (!reg_field->stored_in_db) {
1354 frm_context->stored_fields--;
1355 if (share->stored_rec_length >= recpos)
1356 share->stored_rec_length = recpos - 1;
1357 }
1358
1359 return 0;
1360 }
1361
1362 static const longlong FRM_VER = 6;
1363 static const longlong FRM_VER_TRUE_VARCHAR = (FRM_VER + 4); /* 10 */
1364
1365 /**
1366 Read data from a binary .frm file from MySQL 3.23 - 5.0 into TABLE_SHARE
1367
1368 @note Much of the logic here is duplicated in create_tmp_table()
1369 (see sql_select.cc). Hence, changes to this function may have to be
1370 repeated there.
1371
1372 This function is added to read .frm file in upgrade scenario. It should not
1373 be used any where else in the code. This function will be removed later.
1374
1375 @param thd thread handle
1376 @param share TABLE_SHARE to be populated.
1377 @param frm_context structures removed from TABLE_SHARE
1378 @param head frm file header
1379 @param file File handle
1380 */
1381
open_binary_frm(THD * thd,TABLE_SHARE * share,FRM_context * frm_context,uchar * head,File file)1382 static int open_binary_frm(THD *thd, TABLE_SHARE *share,
1383 FRM_context *frm_context, uchar *head, File file) {
1384 int error, errarg = 0;
1385 uint new_frm_ver, field_pack_length, new_field_pack_flag;
1386 uint interval_count, interval_parts, read_length, int_length;
1387 uint db_create_options, keys, key_parts, n_length;
1388 uint key_info_length, com_length, null_bit_pos, gcol_screen_length;
1389 uint extra_rec_buf_length;
1390 uint i, j;
1391 bool use_extended_sk; // Supported extending of secondary keys with PK parts
1392 bool use_hash;
1393 char *keynames, *names, *comment_pos, *gcol_screen_pos;
1394 char *orig_comment_pos, *orig_gcol_screen_pos;
1395 uchar forminfo[288];
1396 uchar *record;
1397 uchar *disk_buff, *strpos, *null_flags, *null_pos;
1398 ulong pos, record_offset, *rec_per_key, rec_buff_length;
1399 rec_per_key_t *rec_per_key_float;
1400 handler *handler_file = nullptr;
1401 KEY *keyinfo;
1402 KEY_PART_INFO *key_part;
1403 Field **field_ptr;
1404 const char **interval_array;
1405 enum legacy_db_type legacy_db_type;
1406 my_bitmap_map *bitmaps;
1407 uchar *extra_segment_buff = nullptr;
1408 const uint format_section_header_size = 8;
1409 uchar *format_section_fields = nullptr;
1410 bool has_vgc = false;
1411 DBUG_TRACE;
1412
1413 new_field_pack_flag = head[27];
1414 new_frm_ver = (head[2] - FRM_VER);
1415 field_pack_length = new_frm_ver < 2 ? 11 : 17;
1416 disk_buff = nullptr;
1417
1418 error = 3;
1419 /* Position of the form in the form file. */
1420 if (!(pos = get_form_pos(file, head))) goto err; /* purecov: inspected */
1421
1422 mysql_file_seek(file, pos, MY_SEEK_SET, MYF(0));
1423 if (mysql_file_read(file, forminfo, 288, MYF(MY_NABP))) goto err;
1424 frm_context->frm_version = head[2];
1425 /*
1426 Check if .frm file created by MySQL 5.0. In this case we want to
1427 display CHAR fields as CHAR and not as VARCHAR.
1428 We do it this way as we want to keep the old frm version to enable
1429 MySQL 4.1 to read these files.
1430 */
1431 if (frm_context->frm_version == FRM_VER_TRUE_VARCHAR - 1 && head[33] == 5)
1432 frm_context->frm_version = FRM_VER_TRUE_VARCHAR;
1433
1434 if (*(head + 61) &&
1435 !(frm_context->default_part_db_type = ha_checktype(
1436 thd, (enum legacy_db_type)(uint) * (head + 61), true, false)))
1437 goto err;
1438 DBUG_PRINT("info", ("default_part_db_type = %u", head[61]));
1439 legacy_db_type = (enum legacy_db_type)(uint) * (head + 3);
1440 DBUG_ASSERT(share->db_plugin == nullptr);
1441 /*
1442 if the storage engine is dynamic, no point in resolving it by its
1443 dynamically allocated legacy_db_type. We will resolve it later by name.
1444 */
1445 if (legacy_db_type > DB_TYPE_UNKNOWN &&
1446 legacy_db_type < DB_TYPE_FIRST_DYNAMIC)
1447 share->db_plugin = ha_lock_engine(
1448 nullptr, ha_checktype(thd, legacy_db_type, false, false));
1449 share->db_create_options = db_create_options = uint2korr(head + 30);
1450 share->db_options_in_use = share->db_create_options;
1451 share->mysql_version = uint4korr(head + 51);
1452 frm_context->null_field_first = false;
1453 if (!head[32]) // New frm file in 3.23
1454 {
1455 share->avg_row_length = uint4korr(head + 34);
1456 share->row_type = (row_type)head[40];
1457 share->table_charset =
1458 get_charset((((uint)head[41]) << 8) + (uint)head[38], MYF(0));
1459 frm_context->null_field_first = true;
1460 share->stats_sample_pages = uint2korr(head + 42);
1461 share->stats_auto_recalc = static_cast<enum_stats_auto_recalc>(head[44]);
1462 }
1463 if (!share->table_charset) {
1464 /* unknown charset in head[38] or pre-3.23 frm */
1465 if (use_mb(default_charset_info)) {
1466 /* Warn that we may be changing the size of character columns */
1467 LogErr(WARNING_LEVEL, ER_INVALID_CHARSET_AND_DEFAULT_IS_MB,
1468 share->path.str);
1469 }
1470 share->table_charset = default_charset_info;
1471 }
1472 /* Set temporarily a good value for db_low_byte_first */
1473 share->db_low_byte_first = (legacy_db_type != DB_TYPE_ISAM);
1474 error = 4;
1475 share->max_rows = uint4korr(head + 18);
1476 share->min_rows = uint4korr(head + 22);
1477
1478 /* Read keyinformation */
1479 key_info_length = (uint)uint2korr(head + 28);
1480 mysql_file_seek(file, (ulong)uint2korr(head + 6), MY_SEEK_SET, MYF(0));
1481 if (read_string(file, &disk_buff, key_info_length))
1482 goto err; /* purecov: inspected */
1483 if (disk_buff[0] & 0x80) {
1484 share->keys = keys = (disk_buff[1] << 7) | (disk_buff[0] & 0x7f);
1485 share->key_parts = key_parts = uint2korr(disk_buff + 2);
1486 } else {
1487 share->keys = keys = disk_buff[0];
1488 share->key_parts = key_parts = disk_buff[1];
1489 }
1490 share->visible_indexes.init(0);
1491 share->keys_for_keyread.init(0);
1492 share->keys_in_use.init(keys);
1493
1494 strpos = disk_buff + 6;
1495
1496 use_extended_sk = ha_check_storage_engine_flag(share->db_type(),
1497 HTON_SUPPORTS_EXTENDED_KEYS);
1498
1499 uint total_key_parts;
1500 if (use_extended_sk) {
1501 uint primary_key_parts =
1502 keys ? (new_frm_ver >= 3) ? (uint)strpos[4] : (uint)strpos[3] : 0;
1503 total_key_parts = key_parts + primary_key_parts * (keys - 1);
1504 } else
1505 total_key_parts = key_parts;
1506 n_length = keys * sizeof(KEY) + total_key_parts * sizeof(KEY_PART_INFO);
1507
1508 /*
1509 Allocate memory for the KEY object, the key part array, and the
1510 two rec_per_key arrays.
1511 */
1512 if (!multi_alloc_root(&share->mem_root, &keyinfo,
1513 n_length + uint2korr(disk_buff + 4), &rec_per_key,
1514 sizeof(ulong) * total_key_parts, &rec_per_key_float,
1515 sizeof(rec_per_key_t) * total_key_parts, NULL))
1516 goto err; /* purecov: inspected */
1517
1518 memset(keyinfo, 0, n_length);
1519 share->key_info = keyinfo;
1520 key_part = reinterpret_cast<KEY_PART_INFO *>(keyinfo + keys);
1521
1522 for (i = 0; i < keys; i++, keyinfo++) {
1523 keyinfo->table = nullptr; // Updated in open_frm
1524 if (new_frm_ver >= 3) {
1525 keyinfo->flags = (uint)uint2korr(strpos) ^ HA_NOSAME;
1526 keyinfo->key_length = (uint)uint2korr(strpos + 2);
1527 keyinfo->user_defined_key_parts = (uint)strpos[4];
1528 keyinfo->algorithm = (enum ha_key_alg)strpos[5];
1529 keyinfo->block_size = uint2korr(strpos + 6);
1530 strpos += 8;
1531 } else {
1532 keyinfo->flags = ((uint)strpos[0]) ^ HA_NOSAME;
1533 keyinfo->key_length = (uint)uint2korr(strpos + 1);
1534 keyinfo->user_defined_key_parts = (uint)strpos[3];
1535 // The algorithm was HA_KEY_ALG_UNDEF in 5.7
1536 keyinfo->algorithm = HA_KEY_ALG_SE_SPECIFIC;
1537 strpos += 4;
1538 }
1539
1540 keyinfo->key_part = key_part;
1541 keyinfo->set_rec_per_key_array(rec_per_key, rec_per_key_float);
1542 keyinfo->set_in_memory_estimate(IN_MEMORY_ESTIMATE_UNKNOWN);
1543
1544 for (j = keyinfo->user_defined_key_parts; j--; key_part++) {
1545 *rec_per_key++ = 0;
1546 *rec_per_key_float++ = REC_PER_KEY_UNKNOWN;
1547
1548 key_part->fieldnr = (uint16)(uint2korr(strpos) & FIELD_NR_MASK);
1549 key_part->offset = (uint)uint2korr(strpos + 2) - 1;
1550 // key_part->field= (Field*) 0; // Will be fixed later
1551 if (new_frm_ver >= 1) {
1552 key_part->key_part_flag = *(strpos + 4);
1553 key_part->length = (uint)uint2korr(strpos + 7);
1554 strpos += 9;
1555 } else {
1556 key_part->length = *(strpos + 4);
1557 key_part->key_part_flag = 0;
1558 if (key_part->length > 128) {
1559 key_part->length &= 127; /* purecov: inspected */
1560 key_part->key_part_flag = HA_REVERSE_SORT; /* purecov: inspected */
1561 }
1562 strpos += 7;
1563 }
1564 key_part->store_length = key_part->length;
1565 }
1566 /*
1567 Add primary key parts if engine supports primary key extension for
1568 secondary keys. Here we add unique first key parts to the end of
1569 secondary key parts array and increase actual number of key parts.
1570 Note that primary key is always first if exists. Later if there is no
1571 primary key in the table then number of actual keys parts is set to
1572 user defined key parts.
1573 */
1574 keyinfo->actual_key_parts = keyinfo->user_defined_key_parts;
1575 keyinfo->actual_flags = keyinfo->flags;
1576 if (use_extended_sk && i && !(keyinfo->flags & HA_NOSAME)) {
1577 const uint primary_key_parts = share->key_info->user_defined_key_parts;
1578 keyinfo->unused_key_parts = primary_key_parts;
1579 key_part += primary_key_parts;
1580 rec_per_key += primary_key_parts;
1581 rec_per_key_float += primary_key_parts;
1582 share->key_parts += primary_key_parts;
1583 }
1584 }
1585 keynames = (char *)key_part;
1586 strpos += (my_stpcpy(keynames, (char *)strpos) - keynames) + 1;
1587
1588 // reading index comments
1589 for (keyinfo = share->key_info, i = 0; i < keys; i++, keyinfo++) {
1590 if (keyinfo->flags & HA_USES_COMMENT) {
1591 keyinfo->comment.length = uint2korr(strpos);
1592 keyinfo->comment.str = strmake_root(&share->mem_root, (char *)strpos + 2,
1593 keyinfo->comment.length);
1594 strpos += 2 + keyinfo->comment.length;
1595 }
1596 DBUG_ASSERT(((keyinfo->flags & HA_USES_COMMENT) != 0) ==
1597 (keyinfo->comment.length > 0));
1598 }
1599
1600 share->reclength = uint2korr((head + 16));
1601 share->stored_rec_length = share->reclength;
1602 if (*(head + 26) == 1) share->system = true; /* one-record-database */
1603
1604 record_offset = (ulong)(uint2korr(head + 6) + ((uint2korr(head + 14) == 0xffff
1605 ? uint4korr(head + 47)
1606 : uint2korr(head + 14))));
1607
1608 if ((n_length = uint4korr(head + 55))) {
1609 /* Read extra data segment */
1610 uchar *next_chunk, *buff_end;
1611 DBUG_PRINT("info", ("extra segment size is %u bytes", n_length));
1612 if (!(extra_segment_buff =
1613 (uchar *)my_malloc(PSI_NOT_INSTRUMENTED, n_length, MYF(MY_WME))))
1614 goto err;
1615 next_chunk = extra_segment_buff;
1616 if (mysql_file_pread(file, extra_segment_buff, n_length,
1617 record_offset + share->reclength, MYF(MY_NABP))) {
1618 goto err;
1619 }
1620 share->connect_string.length = uint2korr(next_chunk);
1621 if (!(share->connect_string.str =
1622 strmake_root(&share->mem_root, (char *)next_chunk + 2,
1623 share->connect_string.length))) {
1624 goto err;
1625 }
1626 next_chunk += share->connect_string.length + 2;
1627 buff_end = extra_segment_buff + n_length;
1628 if (next_chunk + 2 < buff_end) {
1629 uint str_db_type_length = uint2korr(next_chunk);
1630 LEX_CSTRING name;
1631 name.str = (char *)next_chunk + 2;
1632 name.length = str_db_type_length;
1633
1634 plugin_ref tmp_plugin = ha_resolve_by_name(thd, &name, false);
1635 if (tmp_plugin != nullptr &&
1636 !plugin_equals(tmp_plugin, share->db_plugin)) {
1637 if (legacy_db_type > DB_TYPE_UNKNOWN &&
1638 legacy_db_type < DB_TYPE_FIRST_DYNAMIC &&
1639 legacy_db_type !=
1640 ha_legacy_type(plugin_data<handlerton *>(tmp_plugin))) {
1641 /* bad file, legacy_db_type did not match the name */
1642 goto err;
1643 }
1644 /*
1645 tmp_plugin is locked with a local lock.
1646 we unlock the old value of share->db_plugin before
1647 replacing it with a globally locked version of tmp_plugin
1648 */
1649 plugin_unlock(nullptr, share->db_plugin);
1650 share->db_plugin = my_plugin_lock(nullptr, &tmp_plugin);
1651 DBUG_PRINT("info", ("setting dbtype to '%.*s' (%d)", str_db_type_length,
1652 next_chunk + 2, ha_legacy_type(share->db_type())));
1653 } else if (!tmp_plugin && name.length == 18 &&
1654 !strncmp(name.str, "PERFORMANCE_SCHEMA", name.length)) {
1655 /*
1656 A FRM file is present on disk,
1657 for a PERFORMANCE_SCHEMA table,
1658 but this server binary is not compiled with the performance_schema,
1659 as ha_resolve_by_name() did not find the storage engine.
1660 This can happen:
1661 - in production, when random binaries (without P_S) are thrown
1662 on top of random installed database instances on disk (with P_S).
1663 For the sake of robustness, pretend the table simply does not exist,
1664 so that in particular it does not pollute the information_schema
1665 with errors when scanning the disk for FRM files.
1666 Note that ER_NO_SUCH_TABLE has a special treatment
1667 in fill_schema_table_by_open()
1668 */
1669 error = 1;
1670 my_error(ER_NO_SUCH_TABLE, MYF(0), share->db.str,
1671 share->table_name.str);
1672 goto err;
1673 } else if (!tmp_plugin) {
1674 /* purecov: begin inspected */
1675 error = 8;
1676 const_cast<char *>(name.str)[name.length] = 0;
1677 my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), name.str);
1678 goto err;
1679 /* purecov: end */
1680 }
1681 next_chunk += str_db_type_length + 2;
1682 }
1683 if (next_chunk + 5 < buff_end) {
1684 uint32 partition_info_str_len = uint4korr(next_chunk);
1685 if ((share->partition_info_str_len = partition_info_str_len)) {
1686 if (!(share->partition_info_str =
1687 (char *)memdup_root(&share->mem_root, next_chunk + 4,
1688 partition_info_str_len + 1))) {
1689 goto err;
1690 }
1691 }
1692 next_chunk += 5 + partition_info_str_len;
1693 }
1694 if (share->mysql_version >= 50110 && next_chunk < buff_end) {
1695 /* New auto_partitioned indicator introduced in 5.1.11 */
1696 share->auto_partitioned = *next_chunk;
1697 next_chunk++;
1698 }
1699 keyinfo = share->key_info;
1700 for (i = 0; i < keys; i++, keyinfo++) {
1701 if (keyinfo->flags & HA_USES_PARSER) {
1702 if (next_chunk >= buff_end) {
1703 DBUG_PRINT("error",
1704 ("fulltext key uses parser that is not defined in .frm"));
1705 goto err;
1706 }
1707 LEX_CSTRING parser_name = {
1708 reinterpret_cast<char *>(next_chunk),
1709 strlen(reinterpret_cast<char *>(next_chunk))};
1710 next_chunk += parser_name.length + 1;
1711 keyinfo->parser =
1712 my_plugin_lock_by_name(nullptr, parser_name, MYSQL_FTPARSER_PLUGIN);
1713 if (!keyinfo->parser) {
1714 my_error(ER_PLUGIN_IS_NOT_LOADED, MYF(0), parser_name.str);
1715 goto err;
1716 }
1717 }
1718 }
1719 if (forminfo[46] == (uchar)255) {
1720 // reading long table comment
1721 if (next_chunk + 2 > buff_end) {
1722 DBUG_PRINT("error", ("long table comment is not defined in .frm"));
1723 goto err;
1724 }
1725 share->comment.length = uint2korr(next_chunk);
1726 if (!(share->comment.str =
1727 strmake_root(&share->mem_root, (char *)next_chunk + 2,
1728 share->comment.length))) {
1729 goto err;
1730 }
1731 next_chunk += 2 + share->comment.length;
1732 }
1733
1734 if (next_chunk + format_section_header_size < buff_end) {
1735 /*
1736 New extra data segment called "format section" with additional
1737 table and column properties introduced by MySQL Cluster
1738 based on 5.1.20
1739
1740 Table properties:
1741 TABLESPACE <ts> and STORAGE [DISK|MEMORY]
1742
1743 Column properties:
1744 COLUMN_FORMAT [DYNAMIC|FIXED] and STORAGE [DISK|MEMORY]
1745 */
1746 DBUG_PRINT("info", ("Found format section"));
1747
1748 /* header */
1749 const uint format_section_length = uint2korr(next_chunk);
1750 const uint format_section_flags = uint4korr(next_chunk + 2);
1751 /* 2 bytes unused */
1752
1753 if (next_chunk + format_section_length > buff_end) {
1754 DBUG_PRINT("error", ("format section length too long: %u",
1755 format_section_length));
1756 goto err;
1757 }
1758 DBUG_PRINT("info", ("format_section_length: %u, format_section_flags: %u",
1759 format_section_length, format_section_flags));
1760
1761 share->default_storage_media =
1762 (enum ha_storage_media)(format_section_flags & 0x7);
1763
1764 /* tablespace */
1765 const char *tablespace =
1766 (const char *)next_chunk + format_section_header_size;
1767 const size_t tablespace_length = strlen(tablespace);
1768 share->tablespace = nullptr;
1769 if (tablespace_length) {
1770 Tablespace_name_error_handler error_handler;
1771 thd->push_internal_handler(&error_handler);
1772 bool name_check_error = validate_tablespace_name_length(tablespace);
1773 thd->pop_internal_handler();
1774 if (!name_check_error &&
1775 !(share->tablespace = strmake_root(&share->mem_root, tablespace,
1776 tablespace_length + 1))) {
1777 goto err;
1778 }
1779 }
1780 DBUG_PRINT("info", ("tablespace: '%s'",
1781 share->tablespace ? share->tablespace : "<null>"));
1782
1783 /* pointer to format section for fields */
1784 format_section_fields =
1785 next_chunk + format_section_header_size + tablespace_length + 1;
1786
1787 next_chunk += format_section_length;
1788 }
1789
1790 if (next_chunk + 2 <= buff_end) {
1791 share->compress.length = uint2korr(next_chunk);
1792 if (!(share->compress.str =
1793 strmake_root(&share->mem_root, (char *)next_chunk + 2,
1794 share->compress.length))) {
1795 goto err;
1796 }
1797 next_chunk += 2 + share->compress.length;
1798 }
1799
1800 if (next_chunk + 2 <= buff_end) {
1801 share->encrypt_type.length = uint2korr(next_chunk);
1802 if (!(share->encrypt_type.str =
1803 strmake_root(&share->mem_root, (char *)next_chunk + 2,
1804 share->encrypt_type.length))) {
1805 goto err;
1806 }
1807 next_chunk += 2 + share->encrypt_type.length;
1808 }
1809 }
1810 share->key_block_size = uint2korr(head + 62);
1811
1812 error = 4;
1813 extra_rec_buf_length = uint2korr(head + 59);
1814 rec_buff_length = ALIGN_SIZE(share->reclength + 1 + extra_rec_buf_length);
1815 share->rec_buff_length = rec_buff_length;
1816 if (!(record = (uchar *)share->mem_root.Alloc(rec_buff_length)))
1817 goto err; /* purecov: inspected */
1818 share->default_values = record;
1819 if (mysql_file_pread(file, record, (size_t)share->reclength, record_offset,
1820 MYF(MY_NABP)))
1821 goto err; /* purecov: inspected */
1822
1823 mysql_file_seek(file, pos + 288, MY_SEEK_SET, MYF(0));
1824
1825 share->fields = uint2korr(forminfo + 258);
1826 pos = uint2korr(forminfo + 260); /* Length of all screens */
1827 n_length = uint2korr(forminfo + 268);
1828 interval_count = uint2korr(forminfo + 270);
1829 interval_parts = uint2korr(forminfo + 272);
1830 int_length = uint2korr(forminfo + 274);
1831 share->null_fields = uint2korr(forminfo + 282);
1832 com_length = uint2korr(forminfo + 284);
1833 gcol_screen_length = uint2korr(forminfo + 286);
1834 share->vfields = 0;
1835 frm_context->stored_fields = share->fields;
1836 if (forminfo[46] != (uchar)255) {
1837 share->comment.length = (int)(forminfo[46]);
1838 share->comment.str = strmake_root(&share->mem_root, (char *)forminfo + 47,
1839 share->comment.length);
1840 }
1841
1842 DBUG_PRINT("info", ("i_count: %d i_parts: %d index: %d n_length: %d "
1843 "int_length: %d com_length: %d gcol_screen_length: %d",
1844 interval_count, interval_parts, share->keys, n_length,
1845 int_length, com_length, gcol_screen_length));
1846 if (!(field_ptr = (Field **)share->mem_root.Alloc((uint)(
1847 (share->fields + 1) * sizeof(Field *) +
1848 interval_count * sizeof(TYPELIB) +
1849 (share->fields + interval_parts + keys + 3) * sizeof(char *) +
1850 (n_length + int_length + com_length + gcol_screen_length)))))
1851 goto err; /* purecov: inspected */
1852
1853 share->field = field_ptr;
1854 read_length =
1855 (uint)(share->fields * field_pack_length + pos +
1856 (uint)(n_length + int_length + com_length + gcol_screen_length));
1857 if (read_string(file, &disk_buff, read_length))
1858 goto err; /* purecov: inspected */
1859
1860 strpos = disk_buff + pos;
1861
1862 share->intervals = (TYPELIB *)(field_ptr + share->fields + 1);
1863 interval_array = (const char **)(share->intervals + interval_count);
1864 names = (char *)(interval_array + share->fields + interval_parts + keys + 3);
1865 if (!interval_count) share->intervals = nullptr; // For better debugging
1866 memcpy(names, strpos + (share->fields * field_pack_length),
1867 (uint)(n_length + int_length));
1868 orig_comment_pos = comment_pos = names + (n_length + int_length);
1869 memcpy(comment_pos, disk_buff + read_length - com_length - gcol_screen_length,
1870 com_length);
1871 orig_gcol_screen_pos = gcol_screen_pos =
1872 names + (n_length + int_length + com_length);
1873 memcpy(gcol_screen_pos, disk_buff + read_length - gcol_screen_length,
1874 gcol_screen_length);
1875
1876 fix_type_pointers(&interval_array, &frm_context->fieldnames, 1, &names);
1877 if (frm_context->fieldnames.count != share->fields) goto err;
1878 fix_type_pointers(&interval_array, share->intervals, interval_count, &names);
1879
1880 {
1881 /* Set ENUM and SET lengths */
1882 TYPELIB *interval;
1883 for (interval = share->intervals;
1884 interval < share->intervals + interval_count; interval++) {
1885 uint count = (uint)(interval->count + 1) * sizeof(uint);
1886 if (!(interval->type_lengths = (uint *)share->mem_root.Alloc(count)))
1887 goto err;
1888 for (count = 0; count < interval->count; count++) {
1889 const char *val = interval->type_names[count];
1890 interval->type_lengths[count] = strlen(val);
1891 }
1892 interval->type_lengths[count] = 0;
1893 }
1894 }
1895
1896 if (keynames)
1897 fix_type_pointers(&interval_array, &share->keynames, 1, &keynames);
1898
1899 /* Allocate handler */
1900 if (!(handler_file =
1901 get_new_handler(share, share->partition_info_str_len != 0,
1902 thd->mem_root, share->db_type())))
1903 goto err;
1904
1905 if (handler_file->set_ha_share_ref(&share->ha_share)) goto err;
1906
1907 if (frm_context->null_field_first) {
1908 null_flags = null_pos = share->default_values;
1909 null_bit_pos = (db_create_options & HA_OPTION_PACK_RECORD) ? 0 : 1;
1910 /*
1911 null_bytes below is only correct under the condition that
1912 there are no bit fields. Correct values is set below after the
1913 table struct is initialized
1914 */
1915 share->null_bytes = (share->null_fields + null_bit_pos + 7) / 8;
1916 } else {
1917 share->null_bytes = (share->null_fields + 7) / 8;
1918 null_flags = null_pos =
1919 share->default_values + share->reclength - share->null_bytes;
1920 null_bit_pos = 0;
1921 }
1922
1923 use_hash = share->fields >= MAX_FIELDS_BEFORE_HASH;
1924 if (use_hash)
1925 share->name_hash = new collation_unordered_map<std::string, Field **>(
1926 system_charset_info, PSI_INSTRUMENT_ME);
1927
1928 for (i = 0; i < share->fields; i++, strpos += field_pack_length) {
1929 if (new_frm_ver >= 3 &&
1930 (strpos[10] &
1931 FRM_context::GENERATED_FIELD) && // former Field::unireg_check
1932 !(bool)(uint)(gcol_screen_pos[3])) // Field::stored_in_db
1933 {
1934 /*
1935 Skip virtual generated columns as we will do separate pass for them.
1936
1937 We still need to advance pointers to current comment and generated
1938 column info in for such fields.
1939 */
1940 comment_pos += uint2korr(strpos + 15);
1941 gcol_screen_pos += uint2korr(gcol_screen_pos + 1) + FRM_GCOL_HEADER_SIZE;
1942 has_vgc = true;
1943 } else {
1944 if ((error = make_field_from_frm(
1945 thd, share, frm_context, new_frm_ver, use_hash, i, strpos,
1946 format_section_fields, &comment_pos, &gcol_screen_pos, &null_pos,
1947 &null_bit_pos, &errarg)))
1948 goto err;
1949 }
1950 }
1951
1952 if (has_vgc) {
1953 /*
1954 We need to do separate pass through field descriptions for virtual
1955 generated columns to ensure that they get allocated null/leftover
1956 bits at the tail of record preamble.
1957 */
1958 strpos = disk_buff + pos;
1959 comment_pos = orig_comment_pos;
1960 gcol_screen_pos = orig_gcol_screen_pos;
1961 // Generated columns can be present only in new .FRMs.
1962 DBUG_ASSERT(new_frm_ver >= 3);
1963 for (i = 0; i < share->fields; i++, strpos += field_pack_length) {
1964 if ((strpos[10] &
1965 FRM_context::GENERATED_FIELD) && // former Field::unireg_check
1966 !(bool)(uint)(gcol_screen_pos[3])) // Field::stored_in_db
1967 {
1968 if ((error = make_field_from_frm(
1969 thd, share, frm_context, new_frm_ver, use_hash, i, strpos,
1970 format_section_fields, &comment_pos, &gcol_screen_pos,
1971 &null_pos, &null_bit_pos, &errarg)))
1972 goto err;
1973 } else {
1974 /*
1975 Advance pointers to current comment and generated columns
1976 info for stored fields.
1977 */
1978 comment_pos += uint2korr(strpos + 15);
1979 if (strpos[10] &
1980 FRM_context::GENERATED_FIELD) // former Field::unireg_check
1981 {
1982 gcol_screen_pos +=
1983 uint2korr(gcol_screen_pos + 1) + FRM_GCOL_HEADER_SIZE;
1984 }
1985 }
1986 }
1987 }
1988 error = 4;
1989 share->field[share->fields] = nullptr; // End marker
1990 /* Sanity checks: */
1991 DBUG_ASSERT(share->fields >= frm_context->stored_fields);
1992 DBUG_ASSERT(share->reclength >= share->stored_rec_length);
1993
1994 /* Fix key->name and key_part->field */
1995 if (key_parts) {
1996 const int pk_off =
1997 find_type(primary_key_name, &share->keynames, FIND_TYPE_NO_PREFIX);
1998 uint primary_key = (pk_off > 0 ? pk_off - 1 : MAX_KEY);
1999
2000 longlong ha_option = handler_file->ha_table_flags();
2001 keyinfo = share->key_info;
2002 key_part = keyinfo->key_part;
2003
2004 for (uint key = 0; key < share->keys; key++, keyinfo++) {
2005 uint usable_parts = 0;
2006 keyinfo->name = share->keynames.type_names[key];
2007 /* Fix fulltext keys for old .frm files */
2008 if (share->key_info[key].flags & HA_FULLTEXT)
2009 share->key_info[key].algorithm = HA_KEY_ALG_FULLTEXT;
2010
2011 if (primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME)) {
2012 /*
2013 If the UNIQUE key doesn't have NULL columns and is not a part key
2014 declare this as a primary key.
2015 */
2016 primary_key = key;
2017 for (i = 0; i < keyinfo->user_defined_key_parts; i++) {
2018 DBUG_ASSERT(key_part[i].fieldnr > 0);
2019 // Table field corresponding to the i'th key part.
2020 Field *table_field = share->field[key_part[i].fieldnr - 1];
2021
2022 // Index on virtual generated columns is not allowed to be PK
2023 // even when the conditions below are true, so this case must be
2024 // rejected here.
2025 if (table_field->is_virtual_gcol()) {
2026 primary_key = MAX_KEY; // Can't be used
2027 break;
2028 }
2029
2030 /*
2031 If the key column is of NOT NULL BLOB type, then it
2032 will definitly have key prefix. And if key part prefix size
2033 is equal to the BLOB column max size, then we can promote
2034 it to primary key.
2035 */
2036 if (!table_field->is_nullable() &&
2037 table_field->type() == MYSQL_TYPE_BLOB &&
2038 table_field->field_length == key_part[i].length)
2039 continue;
2040
2041 if (table_field->is_nullable() ||
2042 table_field->key_length() != key_part[i].length)
2043
2044 {
2045 primary_key = MAX_KEY; // Can't be used
2046 break;
2047 }
2048 }
2049 }
2050
2051 for (i = 0; i < keyinfo->user_defined_key_parts; key_part++, i++) {
2052 Field *field;
2053 if (new_field_pack_flag <= 1)
2054 key_part->fieldnr = (uint16)find_field(
2055 share->field, share->default_values, (uint)key_part->offset,
2056 (uint)key_part->length);
2057 if (!key_part->fieldnr) {
2058 error = 4; // Wrong file
2059 goto err;
2060 }
2061 field = key_part->field = share->field[key_part->fieldnr - 1];
2062 key_part->type = field->key_type();
2063 if (field->is_nullable()) {
2064 key_part->null_offset = field->null_offset(share->default_values);
2065 key_part->null_bit = field->null_bit;
2066 key_part->store_length += HA_KEY_NULL_LENGTH;
2067 keyinfo->flags |= HA_NULL_PART_KEY;
2068 keyinfo->key_length += HA_KEY_NULL_LENGTH;
2069 }
2070 if (field->type() == MYSQL_TYPE_BLOB ||
2071 field->real_type() == MYSQL_TYPE_VARCHAR ||
2072 field->type() == MYSQL_TYPE_GEOMETRY) {
2073 key_part->store_length += HA_KEY_BLOB_LENGTH;
2074 if (i + 1 <= keyinfo->user_defined_key_parts)
2075 keyinfo->key_length += HA_KEY_BLOB_LENGTH;
2076 }
2077 key_part->init_flags();
2078
2079 if (field->is_virtual_gcol()) keyinfo->flags |= HA_VIRTUAL_GEN_KEY;
2080
2081 setup_key_part_field(share, handler_file, primary_key, keyinfo, key, i,
2082 &usable_parts, true);
2083
2084 field->set_flag(PART_KEY_FLAG);
2085 if (key == primary_key) {
2086 field->set_flag(PRI_KEY_FLAG);
2087 /*
2088 If this field is part of the primary key and all keys contains
2089 the primary key, then we can use any key to find this column
2090 */
2091 if (ha_option & HA_PRIMARY_KEY_IN_READ_INDEX) {
2092 if (field->key_length() == key_part->length &&
2093 !field->is_flag_set(BLOB_FLAG))
2094 field->part_of_key = share->keys_in_use;
2095 if (field->part_of_sortkey.is_set(key))
2096 field->part_of_sortkey = share->keys_in_use;
2097 }
2098 }
2099 if (field->key_length() != key_part->length) {
2100 if (field->type() == MYSQL_TYPE_NEWDECIMAL) {
2101 /*
2102 Fix a fatal error in decimal key handling that causes crashes
2103 on Innodb. We fix it by reducing the key length so that
2104 InnoDB never gets a too big key when searching.
2105 This allows the end user to do an ALTER TABLE to fix the
2106 error.
2107 */
2108 keyinfo->key_length -= (key_part->length - field->key_length());
2109 key_part->store_length -=
2110 (uint16)(key_part->length - field->key_length());
2111 key_part->length = (uint16)field->key_length();
2112 LogErr(ERROR_LEVEL, ER_TABLE_WRONG_KEY_DEFINITION,
2113 share->table_name.str, share->table_name.str);
2114 push_warning_printf(thd, Sql_condition::SL_WARNING,
2115 ER_CRASHED_ON_USAGE,
2116 "Found wrong key definition in %s; "
2117 "Please do \"ALTER TABLE `%s` FORCE\" to fix "
2118 "it!",
2119 share->table_name.str, share->table_name.str);
2120 share->crashed = true; // Marker for CHECK TABLE
2121 continue;
2122 }
2123 key_part->key_part_flag |= HA_PART_KEY_SEG;
2124 }
2125 }
2126
2127 if (use_extended_sk && primary_key < MAX_KEY && key &&
2128 !(keyinfo->flags & HA_NOSAME))
2129 key_part +=
2130 add_pk_parts_to_sk(keyinfo, key, share->key_info, primary_key,
2131 share, handler_file, &usable_parts);
2132
2133 /* Skip unused key parts if they exist */
2134 key_part += keyinfo->unused_key_parts;
2135
2136 keyinfo->usable_key_parts = usable_parts; // Filesort
2137
2138 share->max_key_length =
2139 std::max(share->max_key_length,
2140 keyinfo->key_length + keyinfo->user_defined_key_parts);
2141 share->total_key_length += keyinfo->key_length;
2142 /*
2143 MERGE tables do not have unique indexes. But every key could be
2144 an unique index on the underlying MyISAM table. (Bug #10400)
2145 */
2146 if ((keyinfo->flags & HA_NOSAME) ||
2147 (ha_option & HA_ANY_INDEX_MAY_BE_UNIQUE))
2148 share->max_unique_length =
2149 std::max(share->max_unique_length, keyinfo->key_length);
2150 }
2151 if (primary_key < MAX_KEY && (share->keys_in_use.is_set(primary_key))) {
2152 share->primary_key = primary_key;
2153 /*
2154 If we are using an integer as the primary key then allow the user to
2155 refer to it as '_rowid'
2156 */
2157 if (share->key_info[primary_key].user_defined_key_parts == 1) {
2158 Field *field = share->key_info[primary_key].key_part[0].field;
2159 if (field && field->result_type() == INT_RESULT) {
2160 /* note that fieldnr here (and rowid_field_offset) starts from 1 */
2161 share->rowid_field_offset =
2162 (share->key_info[primary_key].key_part[0].fieldnr);
2163 }
2164 }
2165 } else
2166 share->primary_key = MAX_KEY; // we do not have a primary key
2167 } else
2168 share->primary_key = MAX_KEY;
2169 my_free(disk_buff);
2170 disk_buff = nullptr;
2171 if (new_field_pack_flag <= 1) {
2172 /* Old file format with default as not null */
2173 uint null_length = (share->null_fields + 7) / 8;
2174 memset(share->default_values + (null_flags - record), 255, null_length);
2175 }
2176
2177 if (share->found_next_number_field) {
2178 Field *reg_field = *share->found_next_number_field;
2179 if ((int)(share->next_number_index = (uint)find_ref_key(
2180 share->key_info, share->keys, share->default_values,
2181 reg_field, &share->next_number_key_offset,
2182 &share->next_number_keypart)) < 0) {
2183 /* Wrong field definition */
2184 error = 4;
2185 goto err;
2186 } else
2187 reg_field->set_flag(AUTO_INCREMENT_FLAG);
2188 }
2189
2190 if (share->blob_fields) {
2191 Field **ptr;
2192 uint k, *save;
2193
2194 /* Store offsets to blob fields to find them fast */
2195 if (!(share->blob_field = save = (uint *)share->mem_root.Alloc(
2196 (uint)(share->blob_fields * sizeof(uint)))))
2197 goto err;
2198 for (k = 0, ptr = share->field; *ptr; ptr++, k++) {
2199 if ((*ptr)->is_flag_set(BLOB_FLAG)) (*save++) = k;
2200 }
2201 }
2202
2203 /*
2204 the correct null_bytes can now be set, since bitfields have been taken
2205 into account
2206 */
2207 share->null_bytes = (null_pos - null_flags + (null_bit_pos + 7) / 8);
2208 share->last_null_bit_pos = null_bit_pos;
2209
2210 share->db_low_byte_first = handler_file->low_byte_first();
2211 share->column_bitmap_size = bitmap_buffer_size(share->fields);
2212
2213 if (!(bitmaps =
2214 (my_bitmap_map *)share->mem_root.Alloc(share->column_bitmap_size)))
2215 goto err;
2216 bitmap_init(&share->all_set, bitmaps, share->fields);
2217 bitmap_set_all(&share->all_set);
2218
2219 destroy(handler_file);
2220 my_free(extra_segment_buff);
2221 return 0;
2222
2223 err:
2224 my_free(disk_buff);
2225 my_free(extra_segment_buff);
2226 destroy(handler_file);
2227 delete share->name_hash;
2228 share->name_hash = nullptr;
2229
2230 open_table_error(thd, share, error, my_errno());
2231 return error;
2232 } /*open_binary_frm*/
2233
2234 //////////////////////////////////////////////////////////////////////////
2235
2236 /**
2237 Validate the expression to see whether there are invalid Item objects.
2238
2239 Needs to be done after fix_fields to allow checking references
2240 to other generated columns, default value expressions or check constraints.
2241
2242 @param expr Pointer to the expression
2243 @param source Source of value generator(a generated column, a regular
2244 column with generated default value or
2245 a check constraint).
2246 @param source_name Name of the source (generated column, a reguler column
2247 with generated default value or a check constraint).
2248 @param column_index The column order.
2249
2250 @retval true The generated expression has some invalid objects
2251 @retval false No illegal objects in the generated expression
2252 */
validate_value_generator_expr(Item * expr,Value_generator_source source,const char * source_name,int column_index)2253 static bool validate_value_generator_expr(Item *expr,
2254 Value_generator_source source,
2255 const char *source_name,
2256 int column_index) {
2257 DBUG_TRACE;
2258 DBUG_ASSERT(expr);
2259
2260 // Map to get actual error code from error_type for the source.
2261 enum error_type { ER_NAME_FUNCTION, ER_FUNCTION, ER_VARIABLES, MAX_ERROR };
2262 uint error_code_map[][MAX_ERROR] = {
2263 // Generated column errors.
2264 {ER_GENERATED_COLUMN_NAMED_FUNCTION_IS_NOT_ALLOWED,
2265 ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED,
2266 ER_GENERATED_COLUMN_VARIABLES},
2267 // Default expressions errors.
2268 {ER_DEFAULT_VAL_GENERATED_NAMED_FUNCTION_IS_NOT_ALLOWED,
2269 ER_DEFAULT_VAL_GENERATED_FUNCTION_IS_NOT_ALLOWED,
2270 ER_DEFAULT_VAL_GENERATED_VARIABLES},
2271 // Check constraint errors.
2272 {ER_CHECK_CONSTRAINT_NAMED_FUNCTION_IS_NOT_ALLOWED,
2273 ER_CHECK_CONSTRAINT_FUNCTION_IS_NOT_ALLOWED,
2274 ER_CHECK_CONSTRAINT_VARIABLES}};
2275 uint err_code = error_code_map[source][ER_NAME_FUNCTION];
2276
2277 // No non-deterministic functions are allowed as GC but most of them are
2278 // allowed as default value expressions
2279 if ((expr->used_tables() & RAND_TABLE_BIT &&
2280 (source == VGS_GENERATED_COLUMN))) {
2281 Item_func *func_item;
2282 if (expr->type() == Item::FUNC_ITEM &&
2283 ((func_item = down_cast<Item_func *>(expr)))) {
2284 my_error(err_code, MYF(0), source_name, func_item->func_name());
2285 return true;
2286 } else {
2287 my_error(error_code_map[source][ER_FUNCTION], MYF(0), source_name);
2288 return true;
2289 }
2290 }
2291 // System variables or parameters are not allowed
2292 else if (expr->used_tables() & INNER_TABLE_BIT) {
2293 my_error(error_code_map[source][ER_VARIABLES], MYF(0), source_name);
2294 return true;
2295 }
2296
2297 // Assert that we aren't dealing with ROW values (rejected in
2298 // pre_validate_value_generator_expr()).
2299 DBUG_ASSERT(expr->cols() == 1);
2300
2301 // Sub-queries are not allowed (already checked by parser, hence the assert)
2302 DBUG_ASSERT(!expr->has_subquery());
2303 /*
2304 Walk through the Item tree, checking the validity of items
2305 belonging to the expression.
2306 */
2307 Check_function_as_value_generator_parameters checker_args(err_code, source);
2308 checker_args.col_index = column_index;
2309 if (expr->walk(&Item::check_function_as_value_generator, enum_walk::POSTFIX,
2310 pointer_cast<uchar *>(&checker_args))) {
2311 my_error(checker_args.err_code, MYF(0), source_name,
2312 checker_args.banned_function_name);
2313 return true;
2314 }
2315
2316 // Stored programs are not allowed. This case is already covered, but still
2317 // keeping it here as a safetynet.
2318 if (expr->has_stored_program()) {
2319 /* purecov: begin deadcode */
2320 DBUG_ASSERT(false);
2321 my_error(err_code, MYF(0), source_name, "stored progam");
2322 return true;
2323 /* purecov: end */
2324 }
2325
2326 return false;
2327 }
2328
2329 /**
2330 Process the generated expression, generated default value of the column or
2331 check constraint expression.
2332
2333 @param thd The thread object
2334 @param table The table to which the column belongs
2335 @param val_generator The expression to unpack
2336 @param source Source of value generator(a generated column, a regular
2337 column with generated default value or
2338 a check constraint).
2339 @param source_name Name of the source (generated column, a reguler column
2340 with generated default value or a check constraint).
2341 @param field Field to which the val_generator is attached to for
2342 generated columns and default expression.
2343
2344 @retval true An error occurred, something was wrong with the function.
2345 @retval false Ok, generated expression is fixed sucessfully
2346 */
fix_value_generators_fields(THD * thd,TABLE * table,Value_generator * val_generator,Value_generator_source source,const char * source_name,Field * field)2347 static bool fix_value_generators_fields(THD *thd, TABLE *table,
2348 Value_generator *val_generator,
2349 Value_generator_source source,
2350 const char *source_name, Field *field) {
2351 uint dir_length, home_dir_length;
2352 bool result = true;
2353 Item *func_expr = val_generator->expr_item;
2354 TABLE_LIST tables;
2355 TABLE_LIST *save_table_list, *save_first_table, *save_last_table;
2356 int error;
2357 Name_resolution_context *context;
2358 const char *save_where;
2359 char *db_name;
2360 char db_name_string[FN_REFLEN];
2361 bool save_use_only_table_context;
2362 std::unique_ptr<Functional_index_error_handler>
2363 functional_index_error_handler;
2364 enum_mark_columns save_mark_used_columns = thd->mark_used_columns;
2365 DBUG_ASSERT(func_expr);
2366 DBUG_TRACE;
2367
2368 // Insert a error handler that takes care of converting column names to
2369 // functional index names. Since functional indexes is implemented as
2370 // indexed hidden generated columns, we may end up printing out the
2371 // auto-generated column name if we don't have an extra error handler.
2372 if (source == VGS_GENERATED_COLUMN)
2373 functional_index_error_handler =
2374 std::unique_ptr<Functional_index_error_handler>(
2375 new Functional_index_error_handler(field, thd));
2376
2377 /*
2378 Set-up the TABLE_LIST object to be a list with a single table
2379 Set alias and real name to table name and get database name from file name.
2380 */
2381
2382 tables.alias = tables.table_name = table->s->table_name.str;
2383 tables.table = table;
2384 tables.next_local = nullptr;
2385 tables.next_name_resolution_table = nullptr;
2386 my_stpmov(db_name_string, table->s->normalized_path.str);
2387 dir_length = dirname_length(db_name_string);
2388 db_name_string[dir_length ? dir_length - 1 : 0] = 0;
2389 home_dir_length = dirname_length(db_name_string);
2390 db_name = &db_name_string[home_dir_length];
2391 tables.db = db_name;
2392
2393 thd->mark_used_columns = MARK_COLUMNS_NONE;
2394
2395 context = thd->lex->current_context();
2396 table->get_fields_in_item_tree = true;
2397 save_table_list = context->table_list;
2398 save_first_table = context->first_name_resolution_table;
2399 save_last_table = context->last_name_resolution_table;
2400 context->table_list = &tables;
2401 context->first_name_resolution_table = &tables;
2402 context->last_name_resolution_table = nullptr;
2403 Item_ident::Change_context ctx(context);
2404 func_expr->walk(&Item::change_context_processor, enum_walk::POSTFIX,
2405 (uchar *)&ctx);
2406 save_where = thd->where;
2407
2408 std::string where_str;
2409 if (source == VGS_GENERATED_COLUMN || source == VGS_DEFAULT_EXPRESSION) {
2410 thd->where = field->is_field_for_functional_index()
2411 ? "functional index"
2412 : (source == VGS_GENERATED_COLUMN)
2413 ? "generated column function"
2414 : "default value expression";
2415 } else {
2416 DBUG_ASSERT(source == VGS_CHECK_CONSTRAINT);
2417 where_str.reserve(256);
2418 where_str.append(STRING_WITH_LEN("check constraint "));
2419 where_str.append(source_name);
2420 where_str.append(STRING_WITH_LEN(" expression"));
2421 thd->where = where_str.c_str();
2422 }
2423
2424 /* Save the context before fixing the fields*/
2425 save_use_only_table_context = thd->lex->use_only_table_context;
2426 thd->lex->use_only_table_context = true;
2427
2428 bool charset_switched = false;
2429 const CHARSET_INFO *saved_collation_connection = func_expr->default_charset();
2430 if (saved_collation_connection != table->s->table_charset) {
2431 thd->variables.collation_connection = table->s->table_charset;
2432 charset_switched = true;
2433 }
2434
2435 Item *new_func = func_expr;
2436 if (field && field->is_field_for_functional_index())
2437 func_expr->allow_array_cast();
2438 error = func_expr->fix_fields(thd, &new_func);
2439
2440 /* Virtual columns expressions that substitute themselves are invalid */
2441 DBUG_ASSERT(new_func == func_expr);
2442
2443 /* Restore the current connection character set and collation. */
2444 if (charset_switched)
2445 thd->variables.collation_connection = saved_collation_connection;
2446
2447 /* Restore the original context*/
2448 thd->lex->use_only_table_context = save_use_only_table_context;
2449 context->table_list = save_table_list;
2450 context->first_name_resolution_table = save_first_table;
2451 context->last_name_resolution_table = save_last_table;
2452
2453 /*
2454 Above, 'context' is either the one of unpack_value_generator()'s temporary
2455 fresh LEX 'new_lex', or the one of the top query as used in
2456 TABLE::refix_value_generator_items(). None of them reflects where the val
2457 generator is situated in the query. Moreover, a gcol_info may be shared
2458 by N references to the same gcol, each ref being in a different context
2459 (top query, subquery). So, underlying items are not situated in a defined
2460 place: give them a null context.
2461 */
2462 Item_ident::Change_context nul_ctx(nullptr);
2463 func_expr->walk(&Item::change_context_processor, enum_walk::POSTFIX,
2464 (uchar *)&nul_ctx);
2465
2466 if (unlikely(error)) {
2467 DBUG_PRINT("info",
2468 ("Field in generated column function not part of table"));
2469 goto end;
2470 }
2471 thd->where = save_where;
2472 /*
2473 Checking if all items are valid to be part of the expression.
2474 */
2475 if (validate_value_generator_expr(func_expr, source, source_name,
2476 field ? field->field_index() : 0))
2477 goto end;
2478
2479 result = false;
2480
2481 func_expr->walk(&Item::strip_db_table_name_processor, enum_walk::POSTFIX,
2482 nullptr);
2483
2484 end:
2485 table->get_fields_in_item_tree = false;
2486 thd->mark_used_columns = save_mark_used_columns;
2487 return result;
2488 }
2489
2490 /**
2491 Calculate the base_columns_map and num_non_virtual_base_cols members of
2492 this generated column
2493
2494 @param table Table with the checked field
2495
2496 @retval true if error
2497 */
2498
register_base_columns(TABLE * table)2499 bool Value_generator::register_base_columns(TABLE *table) {
2500 DBUG_TRACE;
2501 my_bitmap_map *bitbuf = static_cast<my_bitmap_map *>(
2502 table->mem_root.Alloc(bitmap_buffer_size(table->s->fields)));
2503 DBUG_ASSERT(num_non_virtual_base_cols == 0);
2504 bitmap_init(&base_columns_map, bitbuf, table->s->fields);
2505
2506 MY_BITMAP *save_old_read_set = table->read_set;
2507 table->read_set = &base_columns_map;
2508 Mark_field mark_fld(MARK_COLUMNS_TEMP);
2509 expr_item->walk(&Item::mark_field_in_map, enum_walk::PREFIX,
2510 (uchar *)&mark_fld);
2511 table->read_set = save_old_read_set;
2512
2513 /* Calculate the number of non-virtual base columns */
2514 for (uint i = 0; i < table->s->fields; i++) {
2515 Field *field = table->field[i];
2516 if (bitmap_is_set(&base_columns_map, field->field_index()) &&
2517 field->stored_in_db)
2518 num_non_virtual_base_cols++;
2519 }
2520 return false;
2521 }
2522
dup_expr_str(MEM_ROOT * root,const char * src,size_t len)2523 void Value_generator::dup_expr_str(MEM_ROOT *root, const char *src,
2524 size_t len) {
2525 expr_str.str = pointer_cast<char *>(memdup_root(root, src, len));
2526 expr_str.length = len;
2527 }
2528
print_expr(THD * thd,String * out)2529 void Value_generator::print_expr(THD *thd, String *out) {
2530 out->length(0);
2531 Sql_mode_parse_guard parse_guard(thd);
2532 // Printing db and table name is useless
2533 auto flags = enum_query_type(QT_NO_DB | QT_NO_TABLE | QT_FORCE_INTRODUCERS);
2534 expr_item->print(thd, out, flags);
2535 }
2536
unpack_value_generator(THD * thd,TABLE * table,Value_generator ** val_generator,Value_generator_source source,const char * source_name,Field * field,bool is_create_table,bool * error_reported)2537 bool unpack_value_generator(THD *thd, TABLE *table,
2538 Value_generator **val_generator,
2539 Value_generator_source source,
2540 const char *source_name, Field *field,
2541 bool is_create_table, bool *error_reported) {
2542 DBUG_TRACE;
2543 DBUG_ASSERT(field == nullptr || field->table == table);
2544
2545 LEX_STRING *val_gen_expr = &(*val_generator)->expr_str;
2546 DBUG_ASSERT(val_gen_expr);
2547 DBUG_ASSERT(!(*val_generator)->expr_item); // No Item in TABLE_SHARE
2548 /*
2549 Step 1: Construct a statement for the parser.
2550 The parsed string needs to take the following format:
2551 "PARSE_GCOL_EXPR (<expr_string_from_frm>)"
2552 */
2553 char *gcol_expr_str;
2554 int str_len = 0;
2555 const CHARSET_INFO *old_character_set_client;
2556 bool disable_strict_mode = false;
2557 bool status;
2558
2559 Strict_error_handler strict_handler;
2560
2561 LEX *const old_lex = thd->lex;
2562 LEX new_lex;
2563 thd->lex = &new_lex;
2564 if (lex_start(thd)) {
2565 thd->lex = old_lex;
2566 return true; // OOM
2567 }
2568
2569 if (!(gcol_expr_str = (char *)table->mem_root.Alloc(
2570 val_gen_expr->length + PARSE_GCOL_KEYWORD.length + 3))) {
2571 return true;
2572 }
2573 memcpy(gcol_expr_str, PARSE_GCOL_KEYWORD.str, PARSE_GCOL_KEYWORD.length);
2574 str_len = PARSE_GCOL_KEYWORD.length;
2575 memcpy(gcol_expr_str + str_len, "(", 1);
2576 str_len++;
2577 memcpy(gcol_expr_str + str_len, val_gen_expr->str, val_gen_expr->length);
2578 str_len += val_gen_expr->length;
2579 memcpy(gcol_expr_str + str_len, ")", 1);
2580 str_len++;
2581 memcpy(gcol_expr_str + str_len, "\0", 1);
2582 str_len++;
2583 Gcol_expr_parser_state parser_state;
2584 parser_state.init(thd, gcol_expr_str, str_len);
2585
2586 /*
2587 Step 2: Setup thd for parsing.
2588 */
2589 Query_arena *backup_stmt_arena_ptr = thd->stmt_arena;
2590 Query_arena backup_arena;
2591 Query_arena gcol_arena(&table->mem_root, Query_arena::STMT_REGULAR_EXECUTION);
2592 thd->swap_query_arena(gcol_arena, &backup_arena);
2593 thd->stmt_arena = &gcol_arena;
2594 ulong save_old_privilege = thd->want_privilege;
2595 thd->want_privilege = 0;
2596
2597 old_character_set_client = thd->variables.character_set_client;
2598 // Subquery is not allowed in generated expression
2599 const bool save_allow_subselects = thd->lex->expr_allows_subselect;
2600 thd->lex->expr_allows_subselect = false;
2601 // allow_sum_func is also 0, banning group aggregates and window functions.
2602
2603 /*
2604 Step 3: Use the parser to build an Item object from.
2605 */
2606 if (parse_sql(thd, &parser_state, nullptr)) {
2607 goto parse_err;
2608 }
2609 thd->lex->expr_allows_subselect = save_allow_subselects;
2610
2611 /*
2612 From now on use val_generator generated by the parser. It has an
2613 expr_item, and no expr_str.
2614 */
2615 *val_generator = parser_state.result;
2616 /* Keep attribute of generated column */
2617 if (field != nullptr) (*val_generator)->set_field_stored(field->stored_in_db);
2618
2619 DBUG_ASSERT((*val_generator)->expr_item && !(*val_generator)->expr_str.str);
2620
2621 /* Use strict mode regardless of strict mode setting when validating */
2622 if (!thd->is_strict_mode()) {
2623 thd->variables.sql_mode |= MODE_STRICT_ALL_TABLES;
2624 thd->push_internal_handler(&strict_handler);
2625 disable_strict_mode = true;
2626 }
2627
2628 /* Validate the Item tree. */
2629 status = fix_value_generators_fields(thd, table, (*val_generator), source,
2630 source_name, field);
2631
2632 // Permanent changes to the item_tree are completed.
2633 if (!thd->lex->is_ps_or_view_context_analysis())
2634 (*val_generator)->permanent_changes_completed = true;
2635
2636 if (disable_strict_mode) {
2637 thd->pop_internal_handler();
2638 thd->variables.sql_mode &= ~MODE_STRICT_ALL_TABLES;
2639 }
2640 if (status) {
2641 if (is_create_table) {
2642 /*
2643 During CREATE/ALTER TABLE it is ok to receive errors here.
2644 It is not ok if it happens during the opening of an frm
2645 file as part of a normal query.
2646 */
2647 *error_reported = true;
2648 }
2649 // Any memory allocated in this function is freed in parse_err
2650 *val_generator = nullptr;
2651 goto parse_err;
2652 }
2653 if ((*val_generator)->register_base_columns(table)) goto parse_err;
2654 lex_end(thd->lex);
2655 thd->lex = old_lex;
2656 (*val_generator)->backup_stmt_unsafe_flags(new_lex.get_stmt_unsafe_flags());
2657 thd->stmt_arena = backup_stmt_arena_ptr;
2658 thd->swap_query_arena(backup_arena, &gcol_arena);
2659 (*val_generator)->item_list = gcol_arena.item_list();
2660 thd->want_privilege = save_old_privilege;
2661 thd->lex->expr_allows_subselect = save_allow_subselects;
2662
2663 return false;
2664
2665 parse_err:
2666 // Any created window is eliminated as not allowed:
2667 thd->lex->current_select()->m_windows.empty();
2668 thd->free_items();
2669 lex_end(thd->lex);
2670 thd->lex = old_lex;
2671 thd->stmt_arena = backup_stmt_arena_ptr;
2672 thd->swap_query_arena(backup_arena, &gcol_arena);
2673 thd->variables.character_set_client = old_character_set_client;
2674 thd->want_privilege = save_old_privilege;
2675 thd->lex->expr_allows_subselect = save_allow_subselects;
2676 return true;
2677 }
2678
2679 // Unpack partition
unpack_partition_info(THD * thd,TABLE * outparam,TABLE_SHARE * share,handlerton * engine_type,bool is_create_table)2680 bool unpack_partition_info(THD *thd, TABLE *outparam, TABLE_SHARE *share,
2681 handlerton *engine_type, bool is_create_table) {
2682 /*
2683 Currently we still need to run the parser for extracting
2684 Item trees (for partition expression and COLUMNS values).
2685 To avoid too big refactoring in this patch, we still generate
2686 the syntax when reading the DD (read_from_dd_partitions) and
2687 parse it for each TABLE instance.
2688 TODO:
2689 To avoid multiple copies of information, we should try to
2690 point to the TABLE_SHARE where possible:
2691 - partition names etc. I.e. reuse the partition_elements!
2692 This is not possible with columns partitions, since they use
2693 Item for storing the values!?
2694 Also make sure that part_state is never altered without proper locks
2695 (like MDL exclusive locks on the table! since they would be shared by all
2696 instances of a table!)
2697 TODO: Use field images instead?
2698 TODO: Look on how DEFAULT values will be stored in the new DD
2699 and reuse that if possible!
2700 TODO: wl#7840 to get a more light weight parsing of expressions
2701 Create a new partition_info object on the table's mem_root,
2702 by parsing a minimalistic string generated from the share.
2703 And then fill in the missing parts from the part_info on the share.
2704 */
2705
2706 /*
2707 In this execution we must avoid calling thd->change_item_tree since
2708 we might release memory before statement is completed. We do this
2709 by changing to a new statement arena. As part of this arena we also
2710 set the memory root to be the memory root of the table since we
2711 call the parser and fix_fields which both can allocate memory for
2712 item objects. We keep the arena to ensure that we can release the
2713 item list when closing the table object.
2714 SEE Bug #21658
2715 */
2716
2717 // Can use TABLE's mem_root, as it's surely not an internal tmp table
2718 DBUG_ASSERT(share->table_category != TABLE_CATEGORY_TEMPORARY);
2719
2720 Query_arena *backup_stmt_arena_ptr = thd->stmt_arena;
2721 Query_arena backup_arena;
2722 Query_arena part_func_arena(&outparam->mem_root,
2723 Query_arena::STMT_INITIALIZED);
2724 thd->swap_query_arena(part_func_arena, &backup_arena);
2725 thd->stmt_arena = &part_func_arena;
2726 bool tmp;
2727 bool work_part_info_used;
2728
2729 tmp = mysql_unpack_partition(
2730 thd, share->partition_info_str, share->partition_info_str_len, outparam,
2731 is_create_table, engine_type, &work_part_info_used);
2732 if (tmp) {
2733 thd->stmt_arena = backup_stmt_arena_ptr;
2734 thd->swap_query_arena(backup_arena, &part_func_arena);
2735 return true;
2736 }
2737 outparam->part_info->is_auto_partitioned = share->auto_partitioned;
2738 DBUG_PRINT("info", ("autopartitioned: %u", share->auto_partitioned));
2739 /*
2740 We should perform the fix_partition_func in either local or
2741 caller's arena depending on work_part_info_used value.
2742 */
2743 if (!work_part_info_used)
2744 tmp = fix_partition_func(thd, outparam, is_create_table);
2745 thd->stmt_arena = backup_stmt_arena_ptr;
2746 thd->swap_query_arena(backup_arena, &part_func_arena);
2747 if (!tmp) {
2748 if (work_part_info_used)
2749 tmp = fix_partition_func(thd, outparam, is_create_table);
2750 }
2751 outparam->part_info->item_list = part_func_arena.item_list();
2752 // TODO: Compare with share->part_info for validation of code!
2753 DBUG_ASSERT(!share->m_part_info || share->m_part_info->column_list ==
2754 outparam->part_info->column_list);
2755 DBUG_ASSERT(!share->m_part_info ||
2756 outparam->part_info->list_of_part_fields ==
2757 share->m_part_info->list_of_part_fields);
2758
2759 // part_info->part_expr->table_name and
2760 // part_info->subpart_expr->table_name will have been set to
2761 // TABLE::alias (passed on from Field). But part_info cannot refer
2762 // to TABLE::alias since this may be changed when the table object
2763 // is reused. Traverse part_expr and subpart_expr and set table_name
2764 // to nullptr, to avoid dereferencing an invalid pointer.
2765
2766 // @todo bug#29354690: When part_info handling is refactored and properly
2767 // attached to a single TABLE object, this extra code can be
2768 // deleted.
2769 if (outparam->part_info->part_expr != nullptr) {
2770 outparam->part_info->part_expr->walk(&Item::set_table_name,
2771 enum_walk::SUBQUERY_POSTFIX, nullptr);
2772 }
2773 if (outparam->part_info->subpart_expr != nullptr) {
2774 outparam->part_info->subpart_expr->walk(
2775 &Item::set_table_name, enum_walk::SUBQUERY_POSTFIX, nullptr);
2776 }
2777 return tmp;
2778 }
2779
2780 /**
2781 Create a copy of the key_info from TABLE_SHARE object to TABLE object.
2782
2783 Wherever prefix key is present, allocate a new Field object, having its
2784 field_length set to the prefix key length, and point the table's matching
2785 key_part->field to this new Field object.
2786
2787 This ensures that unpack_partition_info() reads the correct prefix length of
2788 partitioned fields
2789 */
2790
create_key_part_field_with_prefix_length(TABLE * table,MEM_ROOT * root)2791 bool create_key_part_field_with_prefix_length(TABLE *table, MEM_ROOT *root) {
2792 DBUG_TRACE;
2793 TABLE_SHARE *share = table->s;
2794 KEY *key_info = nullptr;
2795 KEY_PART_INFO *key_part = nullptr;
2796 uint n_length;
2797
2798 DBUG_ASSERT(share->key_parts);
2799
2800 n_length =
2801 share->keys * sizeof(KEY) + share->key_parts * sizeof(KEY_PART_INFO);
2802
2803 // Allocate new memory for table.key_info
2804 if (!(key_info = static_cast<KEY *>(root->Alloc(n_length)))) return true;
2805
2806 table->key_info = key_info;
2807 key_part = (reinterpret_cast<KEY_PART_INFO *>(key_info + share->keys));
2808
2809 // Copy over the key_info from share to table.
2810 memcpy(key_info, share->key_info, sizeof(*key_info) * share->keys);
2811 memcpy(key_part, share->key_info[0].key_part,
2812 (sizeof(*key_part) * share->key_parts));
2813
2814 for (KEY *key_info_end = key_info + share->keys; key_info < key_info_end;
2815 key_info++) {
2816 key_info->table = table;
2817 key_info->key_part = key_part;
2818
2819 for (KEY_PART_INFO *key_part_end = key_part + key_info->actual_key_parts;
2820 key_part < key_part_end; key_part++) {
2821 Field *field = key_part->field = table->field[key_part->fieldnr - 1];
2822
2823 if (field->key_length() != key_part->length &&
2824 !field->is_flag_set(BLOB_FLAG)) {
2825 /*
2826 We are using only a prefix of the column as a key:
2827 Create a new field for the key part that matches the index
2828 */
2829 field = key_part->field = field->new_field(root, table);
2830 field->set_field_length(key_part->length);
2831 }
2832 }
2833
2834 // Skip unused key parts if they exist
2835 key_part += key_info->unused_key_parts;
2836 }
2837
2838 return false;
2839 }
2840
2841 /**
2842 Open a table based on a TABLE_SHARE
2843
2844 @param thd Thread handler
2845 @param share Table definition
2846 @param alias Alias for table
2847 @param db_stat Open flags (for example HA_OPEN_KEYFILE|
2848 HA_OPEN_RNDFILE..) can be 0 (example in
2849 ha_example_table)
2850 @param prgflag READ_ALL etc..
2851 @param ha_open_flags HA_OPEN_ABORT_IF_LOCKED etc..
2852 @param outparam Result table.
2853 @param is_create_table Indicates that table is opened as part
2854 of CREATE or ALTER and does not yet exist in SE.
2855 @param table_def_param dd::Table object describing the table to be
2856 opened in SE. Can be nullptr, which case this
2857 function will try to retrieve such object from
2858 the data-dictionary before opening table in SE.
2859
2860 @retval 0 ok
2861 @retval 1 Error (see open_table_error)
2862 @retval 2 Error (see open_table_error)
2863 @retval 4 Error (see open_table_error)
2864 @retval 7 Table definition has changed in engine
2865 @retval 8 Table row format has changed in engine
2866 */
2867
open_table_from_share(THD * thd,TABLE_SHARE * share,const char * alias,uint db_stat,uint prgflag,uint ha_open_flags,TABLE * outparam,bool is_create_table,const dd::Table * table_def_param)2868 int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias,
2869 uint db_stat, uint prgflag, uint ha_open_flags,
2870 TABLE *outparam, bool is_create_table,
2871 const dd::Table *table_def_param) {
2872 int error;
2873 uint records, i, bitmap_size;
2874 bool error_reported = false;
2875 bool has_default_values = false;
2876 const bool internal_tmp = share->table_category == TABLE_CATEGORY_TEMPORARY;
2877 DBUG_ASSERT(!internal_tmp || share->ref_count() != 0);
2878 uchar *record, *bitmaps;
2879 Field **field_ptr;
2880 Field *fts_doc_id_field = nullptr;
2881 ptrdiff_t move_offset;
2882 DBUG_TRACE;
2883 DBUG_PRINT("enter", ("name: '%s.%s' form: %p", share->db.str,
2884 share->table_name.str, outparam));
2885
2886 error = 1;
2887 new (outparam) TABLE();
2888 outparam->in_use = thd;
2889 outparam->s = share;
2890 outparam->db_stat = db_stat;
2891 outparam->write_row_record = nullptr;
2892
2893 MEM_ROOT *root;
2894 if (!internal_tmp) {
2895 root = &outparam->mem_root;
2896 init_sql_alloc(key_memory_TABLE, root, TABLE_ALLOC_BLOCK_SIZE, 0);
2897 } else
2898 root = &share->mem_root;
2899
2900 /*
2901 For internal temporary tables we allocate the 'alias' in the
2902 TABLE_SHARE's mem_root rather than on the heap as it gives simpler
2903 freeing.
2904 */
2905 outparam->alias = internal_tmp
2906 ? strdup_root(root, alias)
2907 : my_strdup(key_memory_TABLE, alias, MYF(MY_WME));
2908 if (!outparam->alias) goto err;
2909
2910 outparam->quick_keys.init();
2911 outparam->possible_quick_keys.init();
2912 outparam->covering_keys.init();
2913 outparam->merge_keys.init();
2914 outparam->keys_in_use_for_query.init();
2915
2916 /* Allocate handler */
2917 outparam->file = nullptr;
2918 if (!(prgflag & SKIP_NEW_HANDLER)) {
2919 if (!(outparam->file = get_new_handler(share, share->m_part_info != nullptr,
2920 root, share->db_type())))
2921 goto err;
2922 if (outparam->file->set_ha_share_ref(&share->ha_share)) goto err;
2923 } else {
2924 DBUG_ASSERT(!db_stat);
2925 }
2926
2927 error = 4;
2928 outparam->reginfo.lock_type = TL_UNLOCK;
2929 outparam->current_lock = F_UNLCK;
2930 records = 0;
2931 if ((db_stat & HA_OPEN_KEYFILE) || (prgflag & DELAYED_OPEN)) records = 1;
2932 if (prgflag & (READ_ALL + EXTRA_RECORD)) records++;
2933
2934 record = root->ArrayAlloc<uchar>(share->rec_buff_length * records +
2935 share->null_bytes);
2936 if (record == nullptr) goto err; /* purecov: inspected */
2937
2938 if (records == 0) {
2939 /* We are probably in hard repair, and the buffers should not be used */
2940 outparam->record[0] = outparam->record[1] = share->default_values;
2941 has_default_values = true;
2942 } else {
2943 outparam->record[0] = record;
2944 if (records > 1)
2945 outparam->record[1] = record + share->rec_buff_length;
2946 else
2947 outparam->record[1] = outparam->record[0]; // Safety
2948 }
2949 outparam->null_flags_saved = record + (records * share->rec_buff_length);
2950 memset(outparam->null_flags_saved, '\0', share->null_bytes);
2951
2952 if (!(field_ptr = root->ArrayAlloc<Field *>(share->fields + 1)))
2953 goto err; /* purecov: inspected */
2954
2955 outparam->field = field_ptr;
2956
2957 record = (uchar *)outparam->record[0] - 1; /* Fieldstart = 1 */
2958 outparam->null_flags = (uchar *)record + 1;
2959
2960 /*
2961 We will create fields by cloning TABLE_SHARE's fields; then we will need
2962 to make all new fields' pointers point into the new TABLE's record[0], by
2963 applying an offset to them.
2964 Calculate the "source" offset depending on table type:
2965 - For non-internal temporary tables, source is share->default_values
2966 - For internal tables, source is first TABLE's record[0], which
2967 happens to be created in same memory block as share->default_values, with
2968 offset 2 * share->rec_buff_length (see create_tmp_table()).
2969 */
2970 move_offset = outparam->record[0] - share->default_values +
2971 (internal_tmp ? 2 * share->rec_buff_length : 0);
2972
2973 /* Setup copy of fields from share, but use the right alias and record */
2974 for (i = 0; i < share->fields; i++, field_ptr++) {
2975 Field *new_field = share->field[i]->clone(root);
2976 *field_ptr = new_field;
2977 if (new_field == nullptr) goto err;
2978 new_field->init(outparam);
2979 new_field->move_field_offset(move_offset);
2980 /*
2981 Initialize Field::pack_length() number of bytes for new_field->ptr
2982 only if there are no default values for the field.
2983 */
2984 if (!has_default_values) new_field->reset();
2985 /* Check if FTS_DOC_ID column is present in the table */
2986 if (outparam->file &&
2987 (outparam->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT) &&
2988 !strcmp(outparam->field[i]->field_name, FTS_DOC_ID_COL_NAME))
2989 fts_doc_id_field = new_field;
2990 }
2991 (*field_ptr) = nullptr; // End marker
2992
2993 if (share->found_next_number_field)
2994 outparam->found_next_number_field =
2995 outparam->field[(uint)(share->found_next_number_field - share->field)];
2996
2997 /* Fix key->name and key_part->field */
2998 if (share->key_parts) {
2999 if (create_key_part_field_with_prefix_length(outparam, root)) goto err;
3000 KEY *key_info = outparam->key_info;
3001 for (KEY *key_info_end = key_info + share->keys; key_info < key_info_end;
3002 key_info++) {
3003 /* Set TABLE::fts_doc_id_field for tables with FT KEY */
3004 if ((key_info->flags & HA_FULLTEXT))
3005 outparam->fts_doc_id_field = fts_doc_id_field;
3006 }
3007 }
3008
3009 // Parse partition expression and create Items
3010 if (share->partition_info_str_len && outparam->file &&
3011 unpack_partition_info(thd, outparam, share,
3012 share->m_part_info->default_engine_type,
3013 is_create_table)) {
3014 if (is_create_table) {
3015 /*
3016 During CREATE/ALTER TABLE it is ok to receive errors here.
3017 It is not ok if it happens during the opening of an frm
3018 file as part of a normal query.
3019 */
3020 error_reported = true;
3021 }
3022 goto err;
3023 }
3024
3025 /* Check generated columns against table's storage engine. */
3026 if (share->vfields && outparam->file &&
3027 !(outparam->file->ha_table_flags() & HA_GENERATED_COLUMNS)) {
3028 my_error(ER_UNSUPPORTED_ACTION_ON_GENERATED_COLUMN, MYF(0),
3029 "Specified storage engine");
3030 error_reported = true;
3031 goto err;
3032 }
3033
3034 /*
3035 Allocate bitmaps
3036 This needs to be done prior to generated columns as they'll call
3037 fix_fields and functions might want to access bitmaps.
3038 */
3039
3040 bitmap_size = share->column_bitmap_size;
3041 if (!(bitmaps = root->ArrayAlloc<uchar>(bitmap_size * 7))) goto err;
3042 bitmap_init(&outparam->def_read_set, (my_bitmap_map *)bitmaps, share->fields);
3043 bitmap_init(&outparam->def_write_set,
3044 (my_bitmap_map *)(bitmaps + bitmap_size), share->fields);
3045 bitmap_init(&outparam->tmp_set, (my_bitmap_map *)(bitmaps + bitmap_size * 2),
3046 share->fields);
3047 bitmap_init(&outparam->cond_set, (my_bitmap_map *)(bitmaps + bitmap_size * 3),
3048 share->fields);
3049 bitmap_init(&outparam->def_fields_set_during_insert,
3050 (my_bitmap_map *)(bitmaps + bitmap_size * 4), share->fields);
3051 bitmap_init(&outparam->fields_for_functional_indexes,
3052 (my_bitmap_map *)(bitmaps + bitmap_size * 5), share->fields);
3053 bitmap_init(&outparam->pack_row_tmp_set,
3054 (my_bitmap_map *)(bitmaps + bitmap_size * 6), share->fields);
3055 outparam->default_column_bitmaps();
3056
3057 /*
3058 Process generated columns, if any.
3059 */
3060 outparam->vfield = nullptr;
3061 if (share->vfields) {
3062 Field **vfield_ptr = root->ArrayAlloc<Field *>(share->vfields + 1);
3063 if (!vfield_ptr) goto err;
3064
3065 outparam->vfield = vfield_ptr;
3066
3067 for (field_ptr = outparam->field; *field_ptr; field_ptr++) {
3068 if ((*field_ptr)->gcol_info) {
3069 if (unpack_value_generator(thd, outparam, &(*field_ptr)->gcol_info,
3070 VGS_GENERATED_COLUMN,
3071 (*field_ptr)->field_name, *field_ptr,
3072 is_create_table, &error_reported)) {
3073 *vfield_ptr = nullptr;
3074 error = 4; // in case no error is reported
3075 goto err;
3076 }
3077
3078 // Mark hidden generated columns for functional indexes.
3079 if ((*field_ptr)->is_field_for_functional_index()) {
3080 bitmap_set_bit(&outparam->fields_for_functional_indexes,
3081 (*field_ptr)->field_index());
3082 }
3083 *(vfield_ptr++) = *field_ptr;
3084 }
3085 }
3086 *vfield_ptr = nullptr; // End marker
3087 }
3088
3089 // Check default value expressions against table's storage engine
3090 if (share->gen_def_field_count && outparam->file &&
3091 (!(outparam->file->ha_table_flags() & HA_SUPPORTS_DEFAULT_EXPRESSION))) {
3092 my_error(ER_UNSUPPORTED_ACTION_ON_DEFAULT_VAL_GENERATED, MYF(0),
3093 "Specified storage engine");
3094 error_reported = true;
3095 goto err;
3096 }
3097
3098 // Unpack generated default fields and store reference to this type of fields
3099 outparam->gen_def_fields_ptr = nullptr;
3100 if (share->gen_def_field_count) {
3101 Field **gen_def_field =
3102 root->ArrayAlloc<Field *>(share->gen_def_field_count + 1);
3103 if (!gen_def_field) goto err;
3104
3105 outparam->gen_def_fields_ptr = gen_def_field;
3106 for (field_ptr = outparam->field; *field_ptr; field_ptr++) {
3107 if ((*field_ptr)->has_insert_default_general_value_expression()) {
3108 if (unpack_value_generator(
3109 thd, outparam, &(*field_ptr)->m_default_val_expr,
3110 VGS_DEFAULT_EXPRESSION, (*field_ptr)->field_name, *field_ptr,
3111 is_create_table, &error_reported)) {
3112 (*field_ptr)->m_default_val_expr = nullptr;
3113 *gen_def_field = nullptr;
3114 // In case no error is reported
3115 error = 4;
3116 goto err;
3117 }
3118 *(gen_def_field++) = *field_ptr;
3119 }
3120 }
3121 *gen_def_field = nullptr; // End marker
3122 }
3123
3124 /*
3125 Set up table check constraints from the table share and unpack check
3126 constraint expression.
3127 */
3128 if (share->check_constraint_share_list != nullptr) {
3129 DBUG_ASSERT(share->check_constraint_share_list->size() > 0);
3130
3131 outparam->table_check_constraint_list =
3132 new (root) Sql_table_check_constraint_list(root);
3133 if (outparam->table_check_constraint_list == nullptr) goto err; // OOM
3134
3135 if (outparam->table_check_constraint_list->reserve(
3136 share->check_constraint_share_list->size()))
3137 goto err; // OOM
3138
3139 for (auto &cc_share : *share->check_constraint_share_list) {
3140 // Unpack check constraint expression.
3141 Value_generator val_gen;
3142 val_gen.expr_str = to_lex_string(cc_share.expr_str());
3143 Value_generator *val_gen_ptr = &val_gen;
3144 if (unpack_value_generator(thd, outparam, &val_gen_ptr,
3145 VGS_CHECK_CONSTRAINT, cc_share.name().str,
3146 nullptr, is_create_table, &error_reported))
3147 goto err;
3148
3149 outparam->table_check_constraint_list->push_back(
3150 Sql_table_check_constraint(cc_share.name(), cc_share.expr_str(),
3151 cc_share.is_enforced(), val_gen_ptr,
3152 outparam));
3153 }
3154 }
3155
3156 /* The table struct is now initialized; Open the table */
3157 error = 2;
3158 if (db_stat) {
3159 const dd::Table *table_def = table_def_param;
3160 dd::cache::Dictionary_client::Auto_releaser releaser(thd->dd_client());
3161
3162 if (!table_def) {
3163 if (thd->dd_client()->acquire(share->db.str, share->table_name.str,
3164 &table_def)) {
3165 error_reported = true;
3166 goto err;
3167 }
3168
3169 if (!table_def) {
3170 error = 1;
3171 set_my_errno(ENOENT);
3172 goto err;
3173 }
3174 }
3175
3176 int ha_err;
3177 if ((ha_err = (outparam->file->ha_open(
3178 outparam, share->normalized_path.str,
3179 (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
3180 ((db_stat & HA_OPEN_TEMPORARY
3181 ? HA_OPEN_TMP_TABLE
3182 : (db_stat & HA_WAIT_IF_LOCKED)
3183 ? HA_OPEN_WAIT_IF_LOCKED
3184 : (db_stat & (HA_ABORT_IF_LOCKED | HA_GET_INFO))
3185 ? HA_OPEN_ABORT_IF_LOCKED
3186 : HA_OPEN_IGNORE_IF_LOCKED) |
3187 ha_open_flags),
3188 table_def)))) {
3189 /* Set a flag if the table is crashed and it can be auto. repaired */
3190 share->crashed = ((ha_err == HA_ERR_CRASHED_ON_USAGE) &&
3191 outparam->file->auto_repair() &&
3192 !(ha_open_flags & HA_OPEN_FOR_REPAIR));
3193
3194 switch (ha_err) {
3195 case HA_ERR_TABLESPACE_MISSING:
3196 /*
3197 In case of Innodb table space header may be corrupted or
3198 ibd file might be missing
3199 */
3200 error = 1;
3201 DBUG_ASSERT(my_errno() == HA_ERR_TABLESPACE_MISSING);
3202 break;
3203 case HA_ERR_NO_SUCH_TABLE:
3204 /*
3205 The table did not exists in storage engine, use same error message
3206 as if the .frm file didn't exist
3207 */
3208 error = 1;
3209 set_my_errno(ENOENT);
3210 break;
3211 case EMFILE:
3212 /*
3213 Too many files opened, use same error message as if the .frm
3214 file can't open
3215 */
3216 DBUG_PRINT("error",
3217 ("open file: %s failed, too many files opened (errno: %d)",
3218 share->normalized_path.str, ha_err));
3219 error = 1;
3220 set_my_errno(EMFILE);
3221 break;
3222 default:
3223 outparam->file->print_error(ha_err, MYF(0));
3224 error_reported = true;
3225 if (ha_err == HA_ERR_TABLE_DEF_CHANGED)
3226 error = 7;
3227 else if (ha_err == HA_ERR_ROW_FORMAT_CHANGED)
3228 error = 8;
3229 break;
3230 }
3231 goto err; /* purecov: inspected */
3232 }
3233 } else if (outparam->file) // if db_stat!=0, ha_open() set those pointers:
3234 outparam->file->change_table_ptr(outparam, share);
3235
3236 if ((share->table_category == TABLE_CATEGORY_LOG) ||
3237 (share->table_category == TABLE_CATEGORY_RPL_INFO) ||
3238 (share->table_category == TABLE_CATEGORY_GTID)) {
3239 outparam->no_replicate = true;
3240 } else if (outparam->file) {
3241 handler::Table_flags flags = outparam->file->ha_table_flags();
3242 outparam->no_replicate =
3243 !(flags & (HA_BINLOG_STMT_CAPABLE | HA_BINLOG_ROW_CAPABLE)) ||
3244 (flags & HA_HAS_OWN_BINLOGGING);
3245 } else {
3246 outparam->no_replicate = false;
3247 }
3248
3249 /* Increment the opened_tables counter, only when open flags set. */
3250 if (db_stat) thd->status_var.opened_tables++;
3251
3252 return 0;
3253
3254 err:
3255 if (!error_reported) open_table_error(thd, share, error, my_errno());
3256 destroy(outparam->file);
3257 if (outparam->part_info) free_items(outparam->part_info->item_list);
3258 if (outparam->vfield) {
3259 for (Field **vfield = outparam->vfield; *vfield; vfield++)
3260 free_items((*vfield)->gcol_info->item_list);
3261 }
3262 if (outparam->gen_def_fields_ptr) {
3263 for (Field **gen_def = outparam->gen_def_fields_ptr; *gen_def; gen_def++)
3264 free_items((*gen_def)->m_default_val_expr->item_list);
3265 }
3266 if (outparam->table_check_constraint_list != nullptr) {
3267 for (auto &table_cc : *outparam->table_check_constraint_list) {
3268 free_items(table_cc.value_generator()->item_list);
3269 }
3270 }
3271 outparam->file = nullptr; // For easier error checking
3272 outparam->db_stat = 0;
3273 if (!internal_tmp) free_root(root, MYF(0));
3274 my_free(const_cast<char *>(outparam->alias));
3275 return error;
3276 }
3277
3278 /**
3279 Free information allocated by openfrm
3280
3281 @param table TABLE object to free
3282 @param free_share Is 1 if we also want to free table_share
3283 */
3284
closefrm(TABLE * table,bool free_share)3285 int closefrm(TABLE *table, bool free_share) {
3286 int error = 0;
3287 DBUG_TRACE;
3288 DBUG_PRINT("enter", ("table: %p", table));
3289
3290 if (table->db_stat) error = table->file->ha_close();
3291 my_free(const_cast<char *>(table->alias));
3292 table->alias = nullptr;
3293 if (table->field) {
3294 for (Field **ptr = table->field; *ptr; ptr++) {
3295 if ((*ptr)->gcol_info) free_items((*ptr)->gcol_info->item_list);
3296 if ((*ptr)->m_default_val_expr)
3297 free_items((*ptr)->m_default_val_expr->item_list);
3298 destroy(*ptr);
3299 }
3300 table->field = nullptr;
3301 }
3302 if (table->table_check_constraint_list != nullptr) {
3303 for (auto &table_cc : *table->table_check_constraint_list) {
3304 free_items(table_cc.value_generator()->item_list);
3305 }
3306 }
3307 destroy(table->file);
3308 table->file = nullptr; /* For easier errorchecking */
3309 if (table->part_info) {
3310 /* Allocated through table->mem_root, freed below */
3311 free_items(table->part_info->item_list);
3312 table->part_info->item_list = nullptr;
3313 table->part_info = nullptr;
3314 }
3315 if (free_share) {
3316 if (table->s->tmp_table == NO_TMP_TABLE)
3317 release_table_share(table->s);
3318 else
3319 free_table_share(table->s);
3320 }
3321 free_root(&table->mem_root, MYF(0));
3322 return error;
3323 }
3324
3325 /* Deallocate temporary blob storage */
3326
free_blobs(TABLE * table)3327 void free_blobs(TABLE *table) {
3328 uint *ptr, *end;
3329 for (ptr = table->s->blob_field, end = ptr + table->s->blob_fields;
3330 ptr != end; ptr++) {
3331 /*
3332 Reduced TABLE objects which are used by row-based replication for
3333 type conversion might have some fields missing. Skip freeing BLOB
3334 buffers for such missing fields.
3335 */
3336 if (table->field[*ptr]) ((Field_blob *)table->field[*ptr])->mem_free();
3337 }
3338 }
3339
3340 /**
3341 Reclaims temporary blob storage which is bigger than a threshold.
3342 Resets blob pointer. Unsets m_keep_old_value.
3343
3344 @param table A handle to the TABLE object containing blob fields
3345 @param size The threshold value.
3346 */
3347
free_blob_buffers_and_reset(TABLE * table,uint32 size)3348 void free_blob_buffers_and_reset(TABLE *table, uint32 size) {
3349 uint *ptr, *end;
3350 for (ptr = table->s->blob_field, end = ptr + table->s->blob_fields;
3351 ptr != end; ptr++) {
3352 Field_blob *blob = down_cast<Field_blob *>(table->field[*ptr]);
3353 if (blob->get_field_buffer_size() > size) blob->mem_free();
3354 blob->reset();
3355
3356 if (blob->is_virtual_gcol()) blob->set_keep_old_value(false);
3357 }
3358 }
3359
3360 /* error message when opening a table defintion */
3361
open_table_error(THD * thd,TABLE_SHARE * share,int error,int db_errno)3362 static void open_table_error(THD *thd, TABLE_SHARE *share, int error,
3363 int db_errno) {
3364 int err_no;
3365 char buff[FN_REFLEN];
3366 char errbuf[MYSYS_STRERROR_SIZE];
3367 DBUG_TRACE;
3368
3369 switch (error) {
3370 case 8:
3371 case 7:
3372 case 1:
3373 switch (db_errno) {
3374 case ENOENT:
3375 my_error(ER_NO_SUCH_TABLE, MYF(0), share->db.str,
3376 share->table_name.str);
3377 break;
3378 case HA_ERR_TABLESPACE_MISSING:
3379 snprintf(errbuf, MYSYS_STRERROR_SIZE, "`%s`.`%s`", share->db.str,
3380 share->table_name.str);
3381 my_error(ER_TABLESPACE_MISSING, MYF(0), errbuf);
3382 break;
3383 default:
3384 strxmov(buff, share->normalized_path.str, reg_ext, NullS);
3385 my_error((db_errno == EMFILE) ? ER_CANT_OPEN_FILE : ER_FILE_NOT_FOUND,
3386 MYF(0), buff, db_errno,
3387 my_strerror(errbuf, sizeof(errbuf), db_errno));
3388 LogErr(ERROR_LEVEL,
3389 (db_errno == EMFILE) ? ER_SERVER_CANT_OPEN_FILE
3390 : ER_SERVER_FILE_NOT_FOUND,
3391 buff, db_errno, my_strerror(errbuf, sizeof(errbuf), db_errno));
3392 }
3393 break;
3394 case 2: {
3395 handler *file = nullptr;
3396 const char *datext = "";
3397
3398 if (share->db_type() != nullptr) {
3399 if ((file = get_new_handler(share, share->m_part_info != nullptr,
3400 thd->mem_root, share->db_type()))) {
3401 if (!file->ht->file_extensions ||
3402 !(datext = file->ht->file_extensions[0]))
3403 datext = "";
3404 }
3405 }
3406 err_no = (db_errno == ENOENT)
3407 ? ER_FILE_NOT_FOUND
3408 : (db_errno == EAGAIN) ? ER_FILE_USED : ER_CANT_OPEN_FILE;
3409 strxmov(buff, share->normalized_path.str, datext, NullS);
3410 my_error(err_no, MYF(0), buff, db_errno,
3411 my_strerror(errbuf, sizeof(errbuf), db_errno));
3412 LogErr(ERROR_LEVEL,
3413 (db_errno == ENOENT)
3414 ? ER_SERVER_FILE_NOT_FOUND
3415 : (db_errno == EAGAIN) ? ER_SERVER_FILE_USED
3416 : ER_SERVER_CANT_OPEN_FILE,
3417 buff, db_errno, my_strerror(errbuf, sizeof(errbuf), db_errno));
3418 destroy(file);
3419 break;
3420 }
3421 default: /* Better wrong error than none */
3422 case 4:
3423 strxmov(buff, share->normalized_path.str, reg_ext, NullS);
3424 my_error(ER_NOT_FORM_FILE, MYF(0), buff);
3425 LogErr(ERROR_LEVEL, ER_SERVER_NOT_FORM_FILE, buff);
3426 break;
3427 }
3428 } /* open_table_error */
3429
3430 /* Check that the integer is in the internal */
3431
set_zone(int nr,int min_zone,int max_zone)3432 int set_zone(int nr, int min_zone, int max_zone) {
3433 if (nr <= min_zone) return (min_zone);
3434 if (nr >= max_zone) return (max_zone);
3435 return (nr);
3436 } /* set_zone */
3437
3438 /**
3439 Store an SQL quoted string.
3440
3441 @param res result String
3442 @param pos string to be quoted
3443 @param length it's length
3444
3445 NOTE
3446 This function works correctly with utf8 or single-byte charset strings.
3447 May fail with some multibyte charsets though.
3448 */
3449
append_unescaped(String * res,const char * pos,size_t length)3450 void append_unescaped(String *res, const char *pos, size_t length) {
3451 const char *end = pos + length;
3452
3453 if (res->reserve(length + 2)) return;
3454
3455 res->append('\'');
3456
3457 for (; pos != end; pos++) {
3458 switch (*pos) {
3459 case 0: /* Must be escaped for 'mysql' */
3460 res->append('\\');
3461 res->append('0');
3462 break;
3463 case '\n': /* Must be escaped for logs */
3464 res->append('\\');
3465 res->append('n');
3466 break;
3467 case '\r':
3468 res->append('\\'); /* This gives better readability */
3469 res->append('r');
3470 break;
3471 case '\\':
3472 res->append('\\'); /* Because of the sql syntax */
3473 res->append('\\');
3474 break;
3475 case '\'':
3476 res->append('\''); /* Because of the sql syntax */
3477 res->append('\'');
3478 break;
3479 default:
3480 res->append(*pos);
3481 break;
3482 }
3483 }
3484 res->append('\'');
3485 }
3486
update_create_info_from_table(HA_CREATE_INFO * create_info,TABLE * table)3487 void update_create_info_from_table(HA_CREATE_INFO *create_info, TABLE *table) {
3488 TABLE_SHARE *share = table->s;
3489 DBUG_TRACE;
3490
3491 create_info->max_rows = share->max_rows;
3492 create_info->min_rows = share->min_rows;
3493 create_info->table_options = share->db_create_options;
3494 create_info->avg_row_length = share->avg_row_length;
3495 create_info->row_type = share->row_type;
3496 create_info->default_table_charset = share->table_charset;
3497 create_info->table_charset = nullptr;
3498 create_info->comment = share->comment;
3499 create_info->storage_media = share->default_storage_media;
3500 create_info->tablespace = share->tablespace;
3501 create_info->compress = share->compress;
3502 create_info->encrypt_type = share->encrypt_type;
3503 create_info->secondary_engine = share->secondary_engine;
3504 }
3505
rename_file_ext(const char * from,const char * to,const char * ext)3506 int rename_file_ext(const char *from, const char *to, const char *ext) {
3507 char from_b[FN_REFLEN], to_b[FN_REFLEN];
3508 (void)strxmov(from_b, from, ext, NullS);
3509 (void)strxmov(to_b, to, ext, NullS);
3510 return (mysql_file_rename(key_file_frm, from_b, to_b, MYF(MY_WME)));
3511 }
3512
3513 /**
3514 Allocate string field in MEM_ROOT and return it as String
3515
3516 @param mem MEM_ROOT for allocating
3517 @param field Field for retrieving of string
3518 @param res result String
3519
3520 @retval 1 string is empty
3521 @retval 0 all ok
3522 */
3523
get_field(MEM_ROOT * mem,Field * field,String * res)3524 bool get_field(MEM_ROOT *mem, Field *field, String *res) {
3525 char buff[MAX_FIELD_WIDTH], *to;
3526 String str(buff, sizeof(buff), &my_charset_bin);
3527 size_t length;
3528
3529 field->val_str(&str);
3530 if (!(length = str.length())) {
3531 res->length(0);
3532 return true;
3533 }
3534 if (!(to = strmake_root(mem, str.ptr(), length))) length = 0; // Safety fix
3535 res->set(to, length, field->charset());
3536 return false;
3537 }
3538
3539 /**
3540 Allocate string field in MEM_ROOT and return it as NULL-terminated string
3541
3542 @param mem MEM_ROOT for allocating
3543 @param field Field for retrieving of string
3544
3545 @retval NullS string is empty
3546 @retval other pointer to NULL-terminated string value of field
3547 */
3548
get_field(MEM_ROOT * mem,Field * field)3549 char *get_field(MEM_ROOT *mem, Field *field) {
3550 char buff[MAX_FIELD_WIDTH], *to;
3551 String str(buff, sizeof(buff), &my_charset_bin);
3552 size_t length;
3553
3554 field->val_str(&str);
3555 length = str.length();
3556 if (!length || !(to = (char *)mem->Alloc(length + 1))) return NullS;
3557 memcpy(to, str.ptr(), length);
3558 to[length] = 0;
3559 return to;
3560 }
3561
3562 /**
3563 Check if database name is valid
3564
3565 @param name Name of database
3566 @param length Length of name
3567
3568 @retval Ident_name_check::OK Identifier name is Ok (Success)
3569 @retval Ident_name_check::WRONG Identifier name is Wrong
3570 (ER_WRONG_TABLE_NAME)
3571 @retval Ident_name_check::TOO_LONG Identifier name is too long if it is
3572 greater than 64 characters
3573 (ER_TOO_LONG_IDENT)
3574
3575 @note In case of Ident_name_check::WRONG and Ident_name_check::TOO_LONG, this
3576 function reports an error (my_error)
3577 */
3578
check_db_name(const char * name,size_t length)3579 Ident_name_check check_db_name(const char *name, size_t length) {
3580 Ident_name_check ident_check_status;
3581
3582 if (!length || length > NAME_LEN) {
3583 my_error(ER_WRONG_DB_NAME, MYF(0), name);
3584 return Ident_name_check::WRONG;
3585 }
3586
3587 ident_check_status = check_table_name(name, length);
3588 if (ident_check_status == Ident_name_check::WRONG)
3589 my_error(ER_WRONG_DB_NAME, MYF(0), name);
3590 else if (ident_check_status == Ident_name_check::TOO_LONG)
3591 my_error(ER_TOO_LONG_IDENT, MYF(0), name);
3592 return ident_check_status;
3593 }
3594
3595 /**
3596 Check if database name is valid, and convert to lower case if necessary
3597
3598 @param org_name Name of database and length
3599 @param preserve_lettercase Preserve lettercase if true
3600
3601 @note If lower_case_table_names is true and preserve_lettercase
3602 is false then database is converted to lower case
3603
3604 @retval Ident_name_check::OK Identifier name is Ok (Success)
3605 @retval Ident_name_check::WRONG Identifier name is Wrong
3606 (ER_WRONG_TABLE_NAME)
3607 @retval Ident_name_check::TOO_LONG Identifier name is too long if it is
3608 greater than 64 characters
3609 (ER_TOO_LONG_IDENT)
3610
3611 @note In case of Ident_name_check::WRONG and Ident_name_check::TOO_LONG, this
3612 function reports an error (my_error)
3613 */
3614
check_and_convert_db_name(LEX_STRING * org_name,bool preserve_lettercase)3615 Ident_name_check check_and_convert_db_name(LEX_STRING *org_name,
3616 bool preserve_lettercase) {
3617 char *name = org_name->str;
3618 size_t name_length = org_name->length;
3619 Ident_name_check ident_check_status;
3620
3621 if (!name_length || name_length > NAME_LEN) {
3622 my_error(ER_WRONG_DB_NAME, MYF(0), org_name->str);
3623 return Ident_name_check::WRONG;
3624 }
3625
3626 if (!preserve_lettercase && lower_case_table_names && name != any_db)
3627 my_casedn_str(files_charset_info, name);
3628
3629 ident_check_status = check_table_name(name, name_length);
3630 if (ident_check_status == Ident_name_check::WRONG)
3631 my_error(ER_WRONG_DB_NAME, MYF(0), org_name->str);
3632 else if (ident_check_status == Ident_name_check::TOO_LONG)
3633 my_error(ER_TOO_LONG_IDENT, MYF(0), org_name->str);
3634 return ident_check_status;
3635 }
3636
3637 /**
3638 Function to check if table name is valid or not. If it is invalid,
3639 return appropriate error in each case to the caller.
3640
3641 @param name Table name
3642 @param length Length of table name
3643
3644 @retval Ident_name_check::OK Identifier name is Ok (Success)
3645 @retval Ident_name_check::WRONG Identifier name is Wrong
3646 (ER_WRONG_TABLE_NAME)
3647 @retval Ident_name_check::TOO_LONG Identifier name is too long if it is
3648 greater than 64 characters
3649 (ER_TOO_LONG_IDENT)
3650
3651 @note Reporting error to the user is the responsiblity of the caller.
3652 */
3653
check_table_name(const char * name,size_t length)3654 Ident_name_check check_table_name(const char *name, size_t length) {
3655 // name length in symbols
3656 size_t name_length = 0;
3657 const char *end = name + length;
3658 if (!length || length > NAME_LEN) return Ident_name_check::WRONG;
3659 bool last_char_is_space = false;
3660
3661 while (name != end) {
3662 last_char_is_space = my_isspace(system_charset_info, *name);
3663 if (use_mb(system_charset_info)) {
3664 int len = my_ismbchar(system_charset_info, name, end);
3665 if (len) {
3666 name += len;
3667 name_length++;
3668 continue;
3669 }
3670 }
3671 name++;
3672 name_length++;
3673 }
3674 if (last_char_is_space)
3675 return Ident_name_check::WRONG;
3676 else if (name_length > NAME_CHAR_LEN)
3677 return Ident_name_check::TOO_LONG;
3678 return Ident_name_check::OK;
3679 }
3680
check_column_name(const char * name)3681 bool check_column_name(const char *name) {
3682 // name length in symbols
3683 size_t name_length = 0;
3684 bool last_char_is_space = true;
3685
3686 while (*name) {
3687 last_char_is_space = my_isspace(system_charset_info, *name);
3688 if (use_mb(system_charset_info)) {
3689 int len = my_ismbchar(system_charset_info, name,
3690 name + system_charset_info->mbmaxlen);
3691 if (len) {
3692 name += len;
3693 name_length++;
3694 continue;
3695 }
3696 }
3697 if (*name == NAMES_SEP_CHAR) return true;
3698 name++;
3699 name_length++;
3700 }
3701 /* Error if empty or too long column name */
3702 return last_char_is_space || (name_length > NAME_CHAR_LEN);
3703 }
3704
3705 /**
3706 Checks whether a table is intact. Should be done *just* after the table has
3707 been opened.
3708
3709 @param[in] table The table to check
3710 @param[in] table_def Expected structure of the table (column name
3711 and type)
3712
3713 @retval false OK
3714 @retval true There was an error.
3715 */
3716
check(THD * thd MY_ATTRIBUTE ((unused)),TABLE * table,const TABLE_FIELD_DEF * table_def)3717 bool Table_check_intact::check(THD *thd MY_ATTRIBUTE((unused)), TABLE *table,
3718 const TABLE_FIELD_DEF *table_def) {
3719 uint i;
3720 bool error = false;
3721 const TABLE_FIELD_TYPE *field_def = table_def->field;
3722 DBUG_TRACE;
3723 DBUG_PRINT("info",
3724 ("table: %s expected_count: %d", table->alias, table_def->count));
3725
3726 /* Whether the table definition has already been validated. */
3727 if (table->s->table_field_def_cache == table_def) goto end;
3728
3729 if (table->s->fields != table_def->count) {
3730 DBUG_PRINT("info", ("Column count has changed, checking the definition"));
3731
3732 /* previous MySQL version */
3733 if (MYSQL_VERSION_ID > table->s->mysql_version) {
3734 report_error(ER_COL_COUNT_DOESNT_MATCH_PLEASE_UPDATE_V2,
3735 ER_THD(thd, ER_COL_COUNT_DOESNT_MATCH_PLEASE_UPDATE_V2),
3736 table->s->db.str, table->alias, table_def->count,
3737 table->s->fields, static_cast<int>(table->s->mysql_version),
3738 MYSQL_VERSION_ID);
3739 return true;
3740 } else if (MYSQL_VERSION_ID == table->s->mysql_version) {
3741 report_error(ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2,
3742 ER_THD(thd, ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2),
3743 table->s->db.str, table->s->table_name.str, table_def->count,
3744 table->s->fields);
3745 return true;
3746 }
3747 /*
3748 Something has definitely changed, but we're running an older
3749 version of MySQL with new system tables.
3750 Let's check column definitions. If a column was added at
3751 the end of the table, then we don't care much since such change
3752 is backward compatible.
3753 */
3754 }
3755 char buffer[STRING_BUFFER_USUAL_SIZE];
3756 for (i = 0; i < table_def->count; i++, field_def++) {
3757 String sql_type(buffer, sizeof(buffer), system_charset_info);
3758 sql_type.length(0);
3759 if (i < table->s->fields) {
3760 Field *field = table->field[i];
3761
3762 if (strncmp(field->field_name, field_def->name.str,
3763 field_def->name.length)) {
3764 /*
3765 Name changes are not fatal, we use ordinal numbers to access columns.
3766 Still this can be a sign of a tampered table, output an error
3767 to the error log.
3768 */
3769 report_error(0,
3770 "Incorrect definition of table %s.%s: "
3771 "expected column '%s' at position %d, found '%s'.",
3772 table->s->db.str, table->alias, field_def->name.str, i,
3773 field->field_name);
3774 }
3775 field->sql_type(sql_type);
3776 /*
3777 Generally, if column types don't match, then something is
3778 wrong.
3779
3780 However, we only compare column definitions up to the
3781 length of the original definition, since we consider the
3782 following definitions compatible:
3783
3784 1. DATETIME and DATETIM
3785 2. INT(11) and INT(11
3786 3. SET('one', 'two') and SET('one', 'two', 'more')
3787
3788 For SETs or ENUMs, if the same prefix is there it's OK to
3789 add more elements - they will get higher ordinal numbers and
3790 the new table definition is backward compatible with the
3791 original one.
3792 */
3793 if (strncmp(sql_type.c_ptr_safe(), field_def->type.str,
3794 field_def->type.length - 1)) {
3795 report_error(ER_CANNOT_LOAD_FROM_TABLE_V2,
3796 "Incorrect definition of "
3797 "table %s.%s: expected column '%s' at position %d to "
3798 "have type %s, found type %s.",
3799 table->s->db.str, table->alias, field_def->name.str, i,
3800 field_def->type.str, sql_type.c_ptr_safe());
3801 error = true;
3802 } else if (field_def->cset.str && !field->has_charset()) {
3803 report_error(ER_CANNOT_LOAD_FROM_TABLE_V2,
3804 "Incorrect definition of "
3805 "table %s.%s: expected the type of column '%s' at "
3806 "position %d to have character set '%s' but the type "
3807 "has no character set.",
3808 table->s->db.str, table->alias, field_def->name.str, i,
3809 field_def->cset.str);
3810 error = true;
3811 } else if (field_def->cset.str &&
3812 strcmp(field->charset()->csname, field_def->cset.str)) {
3813 report_error(ER_CANNOT_LOAD_FROM_TABLE_V2,
3814 "Incorrect definition of "
3815 "table %s.%s: expected the type of column '%s' at "
3816 "position %d to have character set '%s' but found "
3817 "character set '%s'.",
3818 table->s->db.str, table->alias, field_def->name.str, i,
3819 field_def->cset.str, field->charset()->csname);
3820 error = true;
3821 }
3822 } else {
3823 report_error(ER_CANNOT_LOAD_FROM_TABLE_V2,
3824 "Incorrect definition of "
3825 "table %s.%s: expected column '%s' at position %d to "
3826 "have type %s but the column is not found.",
3827 table->s->db.str, table->alias, field_def->name.str, i,
3828 field_def->type.str);
3829 error = true;
3830 }
3831 }
3832
3833 if (!error) table->s->table_field_def_cache = table_def;
3834
3835 end:
3836
3837 if (has_keys && !error && !table->key_info) {
3838 my_error(ER_MISSING_KEY, MYF(0), table->s->db.str,
3839 table->s->table_name.str);
3840 error = true;
3841 }
3842
3843 return error;
3844 }
3845
3846 /**
3847 Traverse portion of wait-for graph which is reachable through edge
3848 represented by this flush ticket in search for deadlocks.
3849
3850 @retval true A deadlock is found. A victim is remembered
3851 by the visitor.
3852 @retval false Success, no deadlocks.
3853 */
3854
accept_visitor(MDL_wait_for_graph_visitor * gvisitor)3855 bool Wait_for_flush::accept_visitor(MDL_wait_for_graph_visitor *gvisitor) {
3856 return m_share->visit_subgraph(this, gvisitor);
3857 }
3858
get_deadlock_weight() const3859 uint Wait_for_flush::get_deadlock_weight() const { return m_deadlock_weight; }
3860
3861 /**
3862 Traverse portion of wait-for graph which is reachable through this
3863 table share in search for deadlocks.
3864
3865 @param wait_for_flush Undocumented.
3866 @param gvisitor Deadlock detection visitor.
3867
3868 @retval true A deadlock is found. A victim is remembered
3869 by the visitor.
3870 @retval false No deadlocks, it's OK to begin wait.
3871 */
3872
visit_subgraph(Wait_for_flush * wait_for_flush,MDL_wait_for_graph_visitor * gvisitor)3873 bool TABLE_SHARE::visit_subgraph(Wait_for_flush *wait_for_flush,
3874 MDL_wait_for_graph_visitor *gvisitor) {
3875 TABLE *table;
3876 MDL_context *src_ctx = wait_for_flush->get_ctx();
3877 bool result = true;
3878 bool locked = false;
3879
3880 /*
3881 To protect used_tables list from being concurrently modified
3882 while we are iterating through it we acquire LOCK_open.
3883 This does not introduce deadlocks in the deadlock detector
3884 because we won't try to acquire LOCK_open while
3885 holding a write-lock on MDL_lock::m_rwlock.
3886 */
3887 if (gvisitor->m_lock_open_count++ == 0) {
3888 locked = true;
3889 table_cache_manager.lock_all_and_tdc();
3890 }
3891
3892 Table_cache_iterator tables_it(this);
3893
3894 /*
3895 In case of multiple searches running in parallel, avoid going
3896 over the same loop twice and shortcut the search.
3897 Do it after taking the lock to weed out unnecessary races.
3898 */
3899 if (src_ctx->m_wait.get_status() != MDL_wait::WS_EMPTY) {
3900 result = false;
3901 goto end;
3902 }
3903
3904 if (gvisitor->enter_node(src_ctx)) goto end;
3905
3906 while ((table = tables_it++)) {
3907 if (gvisitor->inspect_edge(&table->in_use->mdl_context)) {
3908 goto end_leave_node;
3909 }
3910 }
3911
3912 tables_it.rewind();
3913 while ((table = tables_it++)) {
3914 if (table->in_use->mdl_context.visit_subgraph(gvisitor)) {
3915 goto end_leave_node;
3916 }
3917 }
3918
3919 result = false;
3920
3921 end_leave_node:
3922 gvisitor->leave_node(src_ctx);
3923
3924 end:
3925 gvisitor->m_lock_open_count--;
3926 if (locked) {
3927 DBUG_ASSERT(gvisitor->m_lock_open_count == 0);
3928 table_cache_manager.unlock_all_and_tdc();
3929 }
3930
3931 return result;
3932 }
3933
find_histogram(uint field_index) const3934 const histograms::Histogram *TABLE_SHARE::find_histogram(
3935 uint field_index) const {
3936 if (m_histograms == nullptr) return nullptr;
3937
3938 const auto found = m_histograms->find(field_index);
3939 if (found == m_histograms->end()) return nullptr;
3940
3941 return found->second;
3942 }
3943
3944 /**
3945 Wait until the subject share is removed from the table
3946 definition cache and make sure it's destroyed.
3947
3948 @note This method may access the share concurrently with another
3949 thread if the share is in the process of being opened, i.e., that
3950 m_open_in_progress is true. In this case, close_cached_tables() may
3951 iterate over elements in the table definition cache, and call this
3952 method regardless of the share being opened or not. This works anyway
3953 since a new flush ticket is added below, and LOCK_open ensures
3954 that the share may not be destroyed by another thread in the time
3955 between finding this share (having an old version) and adding the flush
3956 ticket. Thus, after this thread has added the flush ticket, the thread
3957 opening the table will eventually call free_table_share (as a result of
3958 releasing the share after using it, or as a result of a failing
3959 open_table_def()), which will notify the owners of the flush tickets,
3960 and the last one being notified will actually destroy the share.
3961
3962 @param thd Session.
3963 @param abstime Timeout for waiting as absolute time value.
3964 @param deadlock_weight Weight of this wait for deadlock detector.
3965
3966 @pre LOCK_open is write locked, the share is used (has
3967 non-zero reference count), is marked for flush and
3968 this connection does not reference the share.
3969 LOCK_open will be unlocked temporarily during execution.
3970
3971 @retval false - Success.
3972 @retval true - Error (OOM, deadlock, timeout, etc...).
3973 */
3974
wait_for_old_version(THD * thd,struct timespec * abstime,uint deadlock_weight)3975 bool TABLE_SHARE::wait_for_old_version(THD *thd, struct timespec *abstime,
3976 uint deadlock_weight) {
3977 MDL_context *mdl_context = &thd->mdl_context;
3978 Wait_for_flush ticket(mdl_context, this, deadlock_weight);
3979 MDL_wait::enum_wait_status wait_status;
3980
3981 mysql_mutex_assert_owner(&LOCK_open);
3982 /*
3983 We should enter this method only when share's version is not
3984 up to date and the share is referenced. Otherwise our
3985 thread will never be woken up from wait.
3986 */
3987 DBUG_ASSERT(has_old_version() && ref_count() != 0);
3988
3989 m_flush_tickets.push_front(&ticket);
3990
3991 mdl_context->m_wait.reset_status();
3992
3993 mysql_mutex_unlock(&LOCK_open);
3994
3995 mdl_context->will_wait_for(&ticket);
3996
3997 mdl_context->find_deadlock();
3998
3999 DEBUG_SYNC(thd, "flush_complete");
4000
4001 wait_status = mdl_context->m_wait.timed_wait(thd, abstime, true,
4002 &stage_waiting_for_table_flush);
4003
4004 mdl_context->done_waiting_for();
4005
4006 mysql_mutex_lock(&LOCK_open);
4007
4008 m_flush_tickets.remove(&ticket);
4009
4010 if (m_flush_tickets.is_empty() && ref_count() == 0) {
4011 /*
4012 If our thread was the last one using the share,
4013 we must destroy it here.
4014 */
4015 destroy();
4016 }
4017
4018 DEBUG_SYNC(thd, "share_destroyed");
4019
4020 /*
4021 In cases when our wait was aborted by KILL statement,
4022 a deadlock or a timeout, the share might still be referenced,
4023 so we don't delete it. Note, that we can't determine this
4024 condition by checking wait_status alone, since, for example,
4025 a timeout can happen after all references to the table share
4026 were released, but before the share is removed from the
4027 cache and we receive the notification. This is why
4028 we first destroy the share, and then look at
4029 wait_status.
4030 */
4031 switch (wait_status) {
4032 case MDL_wait::GRANTED:
4033 return false;
4034 case MDL_wait::VICTIM:
4035 my_error(ER_LOCK_DEADLOCK, MYF(0));
4036 return true;
4037 case MDL_wait::TIMEOUT:
4038 my_error(ER_LOCK_WAIT_TIMEOUT, MYF(0));
4039 return true;
4040 case MDL_wait::KILLED:
4041 return true;
4042 default:
4043 DBUG_ASSERT(0);
4044 return true;
4045 }
4046 }
4047
get_table_ref_version() const4048 ulonglong TABLE_SHARE::get_table_ref_version() const {
4049 if (table_category == TABLE_CATEGORY_DICTIONARY ||
4050 tmp_table == SYSTEM_TMP_TABLE ||
4051 (is_view && view_object &&
4052 view_object->type() == dd::enum_table_type::SYSTEM_VIEW))
4053 return 0;
4054
4055 return table_map_id.id();
4056 }
4057
Blob_mem_storage()4058 Blob_mem_storage::Blob_mem_storage() : truncated_value(false) {
4059 init_alloc_root(key_memory_blob_mem_storage, &storage,
4060 MAX_FIELD_VARCHARLENGTH, 0);
4061 }
4062
~Blob_mem_storage()4063 Blob_mem_storage::~Blob_mem_storage() { free_root(&storage, MYF(0)); }
4064
4065 /**
4066 Initialize TABLE instance (newly created, or coming either from table
4067 cache or THD::temporary_tables list) and prepare it for further use
4068 during statement execution. Set the 'alias' attribute from the specified
4069 TABLE_LIST element. Remember the TABLE_LIST element in the
4070 TABLE::pos_in_table_list member.
4071
4072 @param thd Thread context.
4073 @param tl TABLE_LIST element.
4074 */
4075
init(THD * thd,TABLE_LIST * tl)4076 void TABLE::init(THD *thd, TABLE_LIST *tl) {
4077 #ifndef DBUG_OFF
4078 if (s->tmp_table == NO_TMP_TABLE) {
4079 mysql_mutex_lock(&LOCK_open);
4080 DBUG_ASSERT(s->ref_count() > 0);
4081 mysql_mutex_unlock(&LOCK_open);
4082 }
4083 #endif
4084
4085 if (thd->lex->need_correct_ident())
4086 alias_name_used =
4087 my_strcasecmp(table_alias_charset, s->table_name.str, tl->alias);
4088 /* Fix alias if table name changes. */
4089 if (strcmp(alias, tl->alias)) {
4090 size_t length = strlen(tl->alias) + 1;
4091 alias = static_cast<char *>(my_realloc(
4092 key_memory_TABLE, const_cast<char *>(alias), length, MYF(MY_WME)));
4093 memcpy(const_cast<char *>(alias), tl->alias, length);
4094 }
4095
4096 const_table = false;
4097 nullable = false;
4098 force_index = false;
4099 force_index_order = false;
4100 force_index_group = false;
4101 set_not_started();
4102 insert_values = nullptr;
4103 fulltext_searched = false;
4104 file->ft_handler = nullptr;
4105 reginfo.impossible_range = false;
4106
4107 /* Catch wrong handling of the autoinc_field_has_explicit_non_null_value. */
4108 DBUG_ASSERT(!autoinc_field_has_explicit_non_null_value);
4109 autoinc_field_has_explicit_non_null_value = false;
4110
4111 pos_in_table_list = tl;
4112
4113 clear_column_bitmaps();
4114
4115 DBUG_ASSERT(key_read == 0);
4116 no_keyread = false;
4117
4118 /* Tables may be reused in a sub statement. */
4119 DBUG_ASSERT(!db_stat || !file->ha_extra(HA_EXTRA_IS_ATTACHED_CHILDREN));
4120
4121 /*
4122 Do not call refix_value_generator_items() for tables which are not directly
4123 used by the statement (i.e. used by the substatements of routines or
4124 triggers to be invoked by the statement).
4125
4126 Firstly, there will be call to refix_value_generator_items() at the start
4127 of execution of substatement which directly uses this table anyway.
4128 Secondly, cleanup of generated column (call to
4129 cleanup_value_generator_items()) for the table will be done only at the
4130 end of execution of substatement which uses it. Because of this call to
4131 refix_value_generator_items() for prelocking
4132 placeholder will miss corresponding call to cleanup_value_generator_items()
4133 if substatement which uses the table is not executed for some reason.
4134 */
4135 if (!pos_in_table_list->prelocking_placeholder) {
4136 bool error MY_ATTRIBUTE((unused)) = refix_value_generator_items(thd);
4137 DBUG_ASSERT(!error);
4138 }
4139 }
4140
4141 /**
4142 Initialize table as internal tmp table
4143
4144 @param thd thread handle
4145 @param share table share
4146 @param m_root table's mem root
4147 @param charset table's charset
4148 @param alias_arg table's alias
4149 @param fld table's fields array
4150 @param blob_fld buffer for blob field index
4151 @param is_virtual true <=> it's a virtual tmp table
4152
4153 @returns
4154 true OOM
4155 false otherwise
4156 */
4157
init_tmp_table(THD * thd,TABLE_SHARE * share,MEM_ROOT * m_root,CHARSET_INFO * charset,const char * alias_arg,Field ** fld,uint * blob_fld,bool is_virtual)4158 bool TABLE::init_tmp_table(THD *thd, TABLE_SHARE *share, MEM_ROOT *m_root,
4159 CHARSET_INFO *charset, const char *alias_arg,
4160 Field **fld, uint *blob_fld, bool is_virtual) {
4161 if (!is_virtual) {
4162 char *name, path[FN_REFLEN];
4163 DBUG_ASSERT(sizeof(my_thread_id) == 4);
4164 sprintf(path, "%s%lx_%x_%x", tmp_file_prefix, current_pid, thd->thread_id(),
4165 thd->tmp_table++);
4166 fn_format(path, path, mysql_tmpdir, "",
4167 MY_REPLACE_EXT | MY_UNPACK_FILENAME);
4168 if (!(name = (char *)m_root->Alloc(strlen(path) + 1))) return true;
4169 my_stpcpy(name, path);
4170
4171 init_tmp_table_share(thd, share, "", 0, name, name, m_root);
4172 }
4173 s = share;
4174 in_use = thd;
4175
4176 share->blob_field = blob_fld;
4177 share->db_low_byte_first = true; // True for HEAP and MyISAM
4178 share->increment_ref_count();
4179 share->primary_key = MAX_KEY;
4180 share->visible_indexes.init();
4181 share->keys_for_keyread.init();
4182 share->keys_in_use.init();
4183 share->keys = 0;
4184 share->field = field = fld;
4185 share->table_charset = charset;
4186 set_not_started();
4187 alias = alias_arg;
4188 reginfo.lock_type = TL_WRITE; /* Will be updated */
4189 db_stat = HA_OPEN_KEYFILE + HA_OPEN_RNDFILE;
4190 copy_blobs = true;
4191 quick_keys.init();
4192 possible_quick_keys.init();
4193 covering_keys.init();
4194 merge_keys.init();
4195 keys_in_use_for_query.init();
4196 keys_in_use_for_group_by.init();
4197 keys_in_use_for_order_by.init();
4198 #ifndef DBUG_OFF
4199 set_tmp_table_seq_id(thd->get_tmp_table_seq_id());
4200 #endif
4201 return false;
4202 }
4203
refix_value_generator_items(THD * thd)4204 bool TABLE::refix_value_generator_items(THD *thd) {
4205 if (vfield) {
4206 for (Field **vfield_ptr = vfield; *vfield_ptr; vfield_ptr++) {
4207 Field *vfield = *vfield_ptr;
4208 DBUG_ASSERT(vfield->gcol_info && vfield->gcol_info->expr_item);
4209 refix_inner_value_generator_items(thd, vfield->gcol_info, vfield,
4210 vfield->table, VGS_GENERATED_COLUMN,
4211 vfield->field_name);
4212 }
4213 }
4214
4215 if (gen_def_fields_ptr)
4216 for (Field **gen_def_col = gen_def_fields_ptr; *gen_def_col;
4217 gen_def_col++) {
4218 Value_generator *gen_def_expr = (*gen_def_col)->m_default_val_expr;
4219 DBUG_ASSERT(gen_def_expr && gen_def_expr->expr_item);
4220 refix_inner_value_generator_items(
4221 thd, gen_def_expr, (*gen_def_col), (*gen_def_col)->table,
4222 VGS_DEFAULT_EXPRESSION, (*gen_def_col)->field_name);
4223 }
4224
4225 if (table_check_constraint_list != nullptr) {
4226 for (auto &table_cc : *table_check_constraint_list) {
4227 Value_generator *cc_expr = table_cc.value_generator();
4228 DBUG_ASSERT(cc_expr != nullptr && cc_expr->expr_item != nullptr);
4229 refix_inner_value_generator_items(thd, cc_expr, nullptr, table_cc.table(),
4230 VGS_CHECK_CONSTRAINT,
4231 table_cc.name().str);
4232 }
4233 }
4234
4235 return false;
4236 }
4237
refix_inner_value_generator_items(THD * thd,Value_generator * g_expr,Field * field,TABLE * table,Value_generator_source source,const char * source_name)4238 bool TABLE::refix_inner_value_generator_items(THD *thd, Value_generator *g_expr,
4239 Field *field, TABLE *table,
4240 Value_generator_source source,
4241 const char *source_name) {
4242 if (!g_expr->expr_item->fixed) {
4243 bool res = false;
4244 /*
4245 The call to fix_value_generators_fields() may create new item objects in
4246 the item tree for the val generation expression. If these are permanent
4247 changes to the item tree, the new items must have the same life-span
4248 as the ones created during parsing of the generated expression
4249 string. We achieve this by temporarily switching to use the TABLE's
4250 mem_root if the permanent changes to the item tree haven't been
4251 completed (by checking the status of
4252 gcol_info->permanent_changes_completed) and this call is not part of
4253 context analysis (like prepare or show create table).
4254 */
4255 Query_arena *backup_stmt_arena_ptr = thd->stmt_arena;
4256 Query_arena backup_arena;
4257 Query_arena gcol_arena(&mem_root, Query_arena::STMT_REGULAR_EXECUTION);
4258 if (!g_expr->permanent_changes_completed &&
4259 !thd->lex->is_ps_or_view_context_analysis()) {
4260 thd->swap_query_arena(gcol_arena, &backup_arena);
4261 thd->stmt_arena = &gcol_arena;
4262 }
4263
4264 /*
4265 Temporarily disable privileges check; already done when first fixed,
4266 and then based on definer's (owner's) rights: this thread has
4267 invoker's rights
4268 */
4269 ulong sav_want_priv = thd->want_privilege;
4270 thd->want_privilege = 0;
4271
4272 if (fix_value_generators_fields(thd, table, g_expr, source, source_name,
4273 field))
4274 res = true;
4275
4276 if (!g_expr->permanent_changes_completed &&
4277 !thd->lex->is_ps_or_view_context_analysis()) {
4278 // Switch back to the original stmt_arena.
4279 thd->stmt_arena = backup_stmt_arena_ptr;
4280 thd->swap_query_arena(backup_arena, &gcol_arena);
4281
4282 // Append the new items to the original item_free_list.
4283 Item *item = g_expr->item_list;
4284 while (item->next_free) item = item->next_free;
4285 item->next_free = gcol_arena.item_list();
4286
4287 // Permanent changes to the item_tree are completed.
4288 g_expr->permanent_changes_completed = true;
4289 }
4290
4291 // Restore any privileges check
4292 thd->want_privilege = sav_want_priv;
4293 get_fields_in_item_tree = false;
4294
4295 /* error occurs */
4296 if (res) return res;
4297 }
4298 return false;
4299 }
4300
cleanup_value_generator_items()4301 void TABLE::cleanup_value_generator_items() {
4302 if (gen_def_fields_ptr)
4303 for (Field **vfield_ptr = gen_def_fields_ptr; *vfield_ptr; vfield_ptr++)
4304 cleanup_items((*vfield_ptr)->m_default_val_expr->item_list);
4305
4306 if (table_check_constraint_list != nullptr) {
4307 for (auto &table_cc : *table_check_constraint_list)
4308 cleanup_items(table_cc.value_generator()->item_list);
4309 }
4310
4311 if (!has_gcol()) return;
4312
4313 for (Field **vfield_ptr = vfield; *vfield_ptr; vfield_ptr++)
4314 cleanup_items((*vfield_ptr)->gcol_info->item_list);
4315 }
4316
4317 /**
4318 Create Item_field for each column in the table.
4319
4320 SYNPOSIS
4321 TABLE::fill_item_list()
4322 item_list a pointer to an empty list used to store items
4323
4324 Create Item_field object for each column in the table and
4325 initialize it with the corresponding Field. New items are
4326 created in the current THD memory root.
4327
4328 @retval 0 success
4329 @retval 1 out of memory
4330 */
4331
fill_item_list(List<Item> * item_list) const4332 bool TABLE::fill_item_list(List<Item> *item_list) const {
4333 /*
4334 All Item_field's created using a direct pointer to a field
4335 are fixed in Item_field constructor.
4336 */
4337 uint i = 0;
4338 for (Field **ptr = visible_field_ptr(); *ptr; ptr++, i++) {
4339 Item_field *item = new Item_field(*ptr);
4340 if (!item || item_list->push_back(item)) return true;
4341 }
4342 return false;
4343 }
4344
4345 /**
4346 Reset an existing list of Item_field items to point to the
4347 Fields of this table.
4348
4349 SYNPOSIS
4350 TABLE::reset_item_list()
4351 item_list a non-empty list with Item_fields
4352
4353 This is a counterpart of fill_item_list used to redirect
4354 Item_fields to the fields of a newly created table.
4355 */
4356
reset_item_list(List<Item> * item_list) const4357 void TABLE::reset_item_list(List<Item> *item_list) const {
4358 List_iterator_fast<Item> it(*item_list);
4359 uint i = 0;
4360 for (Field **ptr = visible_field_ptr(); *ptr; ptr++, i++) {
4361 Item_field *item_field = (Item_field *)it++;
4362 DBUG_ASSERT(item_field != nullptr);
4363 item_field->reset_field(*ptr);
4364 }
4365 }
4366
4367 /**
4368 Create a TABLE_LIST object representing a nested join
4369
4370 @param allocator Mem root allocator that object is created from.
4371 @param alias Name of nested join object
4372 @param embedding Pointer to embedding join nest (or NULL if top-most)
4373 @param belongs_to List of tables this nest belongs to (never NULL).
4374 @param select The query block that this join nest belongs within.
4375
4376 @returns Pointer to created join nest object, or NULL if error.
4377 */
4378
new_nested_join(MEM_ROOT * allocator,const char * alias,TABLE_LIST * embedding,mem_root_deque<TABLE_LIST * > * belongs_to,SELECT_LEX * select)4379 TABLE_LIST *TABLE_LIST::new_nested_join(
4380 MEM_ROOT *allocator, const char *alias, TABLE_LIST *embedding,
4381 mem_root_deque<TABLE_LIST *> *belongs_to, SELECT_LEX *select) {
4382 DBUG_ASSERT(belongs_to && select);
4383
4384 TABLE_LIST *const join_nest = new (allocator) TABLE_LIST;
4385 if (join_nest == nullptr) return nullptr;
4386
4387 join_nest->nested_join = new (allocator) NESTED_JOIN;
4388 if (join_nest->nested_join == nullptr) return nullptr;
4389
4390 join_nest->db = "";
4391 join_nest->db_length = 0;
4392 join_nest->table_name = "";
4393 join_nest->table_name_length = 0;
4394 join_nest->alias = alias;
4395
4396 join_nest->embedding = embedding;
4397 join_nest->join_list = belongs_to;
4398 join_nest->select_lex = select;
4399 join_nest->nested_join->first_nested = NO_PLAN_IDX;
4400
4401 join_nest->nested_join->join_list.clear();
4402
4403 return join_nest;
4404 }
4405
4406 /**
4407 Merge tables from a query block into a nested join structure.
4408
4409 @param select Query block containing tables to be merged into nested join
4410
4411 @return false if success, true if error
4412 */
4413
merge_underlying_tables(SELECT_LEX * select)4414 bool TABLE_LIST::merge_underlying_tables(SELECT_LEX *select) {
4415 DBUG_ASSERT(nested_join->join_list.empty());
4416
4417 for (TABLE_LIST *tl : select->top_join_list) {
4418 tl->embedding = this;
4419 tl->join_list = &nested_join->join_list;
4420 nested_join->join_list.push_back(tl);
4421 }
4422
4423 return false;
4424 }
4425
4426 /**
4427 Reset a table before starting optimization
4428 */
reset()4429 void TABLE::reset() {
4430 const_table = false;
4431 nullable = false;
4432 set_not_started();
4433
4434 force_index = false;
4435 force_index_order = false;
4436 force_index_group = false;
4437 merge_keys.clear_all();
4438 quick_keys.clear_all();
4439 covering_keys = s->keys_for_keyread;
4440 possible_quick_keys.clear_all();
4441 set_keyread(false);
4442 no_keyread = false;
4443 reginfo.not_exists_optimize = false;
4444 reginfo.impossible_range = false;
4445 m_record_buffer = Record_buffer{0, 0, nullptr};
4446 memset(const_key_parts, 0, sizeof(key_part_map) * s->keys);
4447 insert_values = nullptr;
4448 autoinc_field_has_explicit_non_null_value = false;
4449
4450 file->ft_handler = nullptr;
4451
4452 pos_in_table_list = nullptr;
4453 }
4454
4455 /**
4456 Merge WHERE condition of view or derived table into outer query.
4457
4458 If the derived table is on the inner side of an outer join, its WHERE
4459 condition is merged into the respective join operation's join condition,
4460 otherwise the WHERE condition is merged with the derived table's
4461 join condition.
4462
4463 @param thd thread handler
4464
4465 @return false if success, true if error
4466 */
4467
merge_where(THD * thd)4468 bool TABLE_LIST::merge_where(THD *thd) {
4469 DBUG_TRACE;
4470
4471 DBUG_ASSERT(is_merged());
4472
4473 Item *const condition = derived_unit()->first_select()->where_cond();
4474
4475 if (!condition) return false;
4476
4477 /*
4478 Save the WHERE condition separately. This is needed because it is already
4479 resolved, so we need to explicitly update used tables information after
4480 merging this derived table into the outer query.
4481 */
4482 derived_where_cond = condition;
4483
4484 Prepared_stmt_arena_holder ps_arena_holder(thd);
4485
4486 /*
4487 Merge WHERE condition with the join condition of the outer join nest
4488 and attach it to join nest representing this derived table.
4489 */
4490 set_join_cond(and_conds(join_cond(), condition));
4491 if (!join_cond()) return true; /* purecov: inspected */
4492
4493 return false;
4494 }
4495
4496 /**
4497 Create field translation for merged derived table/view.
4498
4499 @param thd Thread handle
4500
4501 @return false if success, true if error.
4502 */
4503
create_field_translation(THD * thd)4504 bool TABLE_LIST::create_field_translation(THD *thd) {
4505 Item *item;
4506 SELECT_LEX *select = derived->first_select();
4507 List_iterator_fast<Item> it(select->fields_list);
4508 uint field_count = 0;
4509
4510 DBUG_ASSERT(derived->is_prepared());
4511
4512 DBUG_ASSERT(!field_translation);
4513
4514 Prepared_stmt_arena_holder ps_arena_holder(thd);
4515
4516 // Create view fields translation table
4517 Field_translator *transl = (Field_translator *)thd->stmt_arena->alloc(
4518 select->fields_list.elements * sizeof(Field_translator));
4519 if (!transl) return true; /* purecov: inspected */
4520
4521 while ((item = it++)) {
4522 /*
4523 Notice that all items keep their nullability here.
4524 All items are later wrapped within Item_direct_view objects.
4525 If the view is used on the inner side of an outer join, these
4526 objects will reflect the correct nullability of the selected
4527 expressions.
4528 The name is either explicitely specified in a list of column
4529 names, or is derived from the name of the expression in the SELECT
4530 list.
4531 */
4532 transl[field_count].name = m_derived_column_names
4533 ? (*m_derived_column_names)[field_count].str
4534 : item->item_name.ptr();
4535 transl[field_count++].item = item;
4536 }
4537 field_translation = transl;
4538 field_translation_end = transl + field_count;
4539
4540 return false;
4541 }
4542
4543 /**
4544 Return merged WHERE clause and join conditions for a view
4545
4546 @param thd thread handle
4547 @param table table for the VIEW
4548 @param[out] pcond Pointer to the built condition (NULL if none)
4549
4550 This function returns the result of ANDing the WHERE clause and the
4551 join conditions of the given view.
4552
4553 @returns false for success, true for error
4554 */
4555
merge_join_conditions(THD * thd,TABLE_LIST * table,Item ** pcond)4556 static bool merge_join_conditions(THD *thd, TABLE_LIST *table, Item **pcond) {
4557 DBUG_TRACE;
4558
4559 *pcond = nullptr;
4560 DBUG_PRINT("info", ("alias: %s", table->alias));
4561 if (table->join_cond()) {
4562 if (!(*pcond = table->join_cond()->copy_andor_structure(thd)))
4563 return true; /* purecov: inspected */
4564 }
4565 if (!table->nested_join) return false;
4566 for (TABLE_LIST *tbl : table->nested_join->join_list) {
4567 if (tbl->is_view()) continue;
4568 Item *cond;
4569 if (merge_join_conditions(thd, tbl, &cond))
4570 return true; /* purecov: inspected */
4571 if (cond && !(*pcond = and_conds(*pcond, cond)))
4572 return true; /* purecov: inspected */
4573 }
4574 return false;
4575 }
4576
4577 /**
4578 Prepare check option expression of table
4579
4580 @param thd thread handler
4581 @param is_cascaded True if parent view requests that this view's
4582 filtering condition be treated as WITH CASCADED CHECK OPTION; this is for
4583 recursive calls; user code should omit this argument.
4584
4585 This function builds check option condition for use in regular execution or
4586 subsequent SP/PS executions.
4587
4588 This function must be called after the WHERE clause and join condition
4589 of this and all underlying derived tables/views have been resolved.
4590
4591 The function will always call itself recursively for all underlying views
4592 and base tables.
4593
4594 On first invocation, the check option condition is built bottom-up in
4595 statement mem_root, and check_option_processed is set true.
4596
4597 On subsequent executions, check_option_processed is true and no
4598 expression building is necessary. However, the function needs to assure that
4599 the expression is resolved by calling fix_fields() on it.
4600
4601 @returns false if success, true if error
4602 */
4603
prepare_check_option(THD * thd,bool is_cascaded)4604 bool TABLE_LIST::prepare_check_option(THD *thd, bool is_cascaded) {
4605 DBUG_TRACE;
4606 DBUG_ASSERT(is_view());
4607
4608 /*
4609 True if conditions of underlying views should be treated as WITH CASCADED
4610 CHECK OPTION
4611 */
4612 is_cascaded |= (with_check == VIEW_CHECK_CASCADED);
4613
4614 for (TABLE_LIST *tbl = merge_underlying_list; tbl; tbl = tbl->next_local) {
4615 if (tbl->is_view() && tbl->prepare_check_option(thd, is_cascaded))
4616 return true; /* purecov: inspected */
4617 }
4618
4619 if (!check_option_processed) {
4620 Prepared_stmt_arena_holder ps_arena_holder(thd);
4621 if ((with_check || is_cascaded) &&
4622 merge_join_conditions(thd, this, &check_option))
4623 return true; /* purecov: inspected */
4624
4625 for (TABLE_LIST *tbl = merge_underlying_list; tbl; tbl = tbl->next_local) {
4626 if (tbl->check_option &&
4627 !(check_option = and_conds(check_option, tbl->check_option)))
4628 return true; /* purecov: inspected */
4629 }
4630
4631 check_option_processed = true;
4632 }
4633
4634 if (check_option && !check_option->fixed) {
4635 const char *save_where = thd->where;
4636 thd->where = "check option";
4637 if (check_option->fix_fields(thd, &check_option) ||
4638 check_option->check_cols(1))
4639 return true; /* purecov: inspected */
4640 thd->where = save_where;
4641 }
4642
4643 return false;
4644 }
4645
4646 /**
4647 Prepare replace filter for a table that is inserted into via a view.
4648
4649 Used with REPLACE command to filter out rows that should not be deleted.
4650 Concatenate WHERE clauses from multiple views into one permanent field:
4651 TABLE::replace_filter.
4652
4653 Since REPLACE is not possible against a join view, there is no need to
4654 process join conditions, only WHERE clause is needed. But we still call
4655 merge_join_conditions() since this is a general function that handles both
4656 join conditions (if any) and the original WHERE clause.
4657
4658 @param thd thread handler
4659
4660 @returns false if success, true if error
4661 */
4662
prepare_replace_filter(THD * thd)4663 bool TABLE_LIST::prepare_replace_filter(THD *thd) {
4664 DBUG_TRACE;
4665
4666 for (TABLE_LIST *tbl = merge_underlying_list; tbl; tbl = tbl->next_local) {
4667 if (tbl->is_view() && tbl->prepare_replace_filter(thd)) return true;
4668 }
4669
4670 if (!replace_filter_processed) {
4671 Prepared_stmt_arena_holder ps_arena_holder(thd);
4672
4673 if (merge_join_conditions(thd, this, &replace_filter))
4674 return true; /* purecov: inspected */
4675 for (TABLE_LIST *tbl = merge_underlying_list; tbl; tbl = tbl->next_local) {
4676 if (tbl->replace_filter) {
4677 if (!(replace_filter = and_conds(replace_filter, tbl->replace_filter)))
4678 return true;
4679 }
4680 }
4681 replace_filter_processed = true;
4682 }
4683
4684 if (replace_filter && !replace_filter->fixed) {
4685 const char *save_where = thd->where;
4686 thd->where = "replace filter";
4687 if (replace_filter->fix_fields(thd, &replace_filter) ||
4688 replace_filter->check_cols(1))
4689 return true;
4690 thd->where = save_where;
4691 }
4692
4693 return false;
4694 }
4695
4696 /**
4697 Cleanup items belonged to view fields translation table
4698 */
4699
cleanup_items()4700 void TABLE_LIST::cleanup_items() {
4701 if (!field_translation) return;
4702
4703 for (Field_translator *transl = field_translation;
4704 transl < field_translation_end; transl++)
4705 transl->item->walk(&Item::cleanup_processor, enum_walk::POSTFIX, nullptr);
4706 }
4707
4708 /**
4709 Check CHECK OPTION condition
4710
4711 @param thd thread handler
4712
4713 @retval VIEW_CHECK_OK OK
4714 @retval VIEW_CHECK_ERROR FAILED
4715 @retval VIEW_CHECK_SKIP FAILED, but continue
4716 */
4717
view_check_option(THD * thd) const4718 int TABLE_LIST::view_check_option(THD *thd) const {
4719 if (check_option && check_option->val_int() == 0) {
4720 const TABLE_LIST *main_view = top_table();
4721 my_error(ER_VIEW_CHECK_FAILED, MYF(0), main_view->view_db.str,
4722 main_view->view_name.str);
4723 if (thd->lex->is_ignore()) return (VIEW_CHECK_SKIP);
4724 return (VIEW_CHECK_ERROR);
4725 }
4726 return (VIEW_CHECK_OK);
4727 }
4728
4729 /**
4730 Find table in underlying tables by map and check that only this
4731 table belong to given map.
4732
4733 @param[out] table_ref reference to found table
4734 (must be set to NULL by caller)
4735 @param map bit mask of tables
4736
4737 @retval false table not found or found only one (table_ref is non-NULL)
4738 @retval true found several tables
4739 */
4740
check_single_table(TABLE_LIST ** table_ref,table_map map)4741 bool TABLE_LIST::check_single_table(TABLE_LIST **table_ref, table_map map) {
4742 for (TABLE_LIST *tbl = merge_underlying_list; tbl; tbl = tbl->next_local) {
4743 if (tbl->is_view_or_derived() && tbl->is_merged()) {
4744 if (tbl->check_single_table(table_ref, map)) return true;
4745 } else if (tbl->map() & map) {
4746 if (*table_ref) return true;
4747
4748 *table_ref = tbl;
4749 }
4750 }
4751 return false;
4752 }
4753
4754 /**
4755 Set insert_values buffer
4756
4757 @param mem_root memory pool for allocating
4758
4759 @returns false if success, true if error (out of memory)
4760 */
4761
set_insert_values(MEM_ROOT * mem_root)4762 bool TABLE_LIST::set_insert_values(MEM_ROOT *mem_root) {
4763 if (table) {
4764 DBUG_ASSERT(table->insert_values == nullptr);
4765 if (!table->insert_values &&
4766 !(table->insert_values =
4767 (uchar *)mem_root->Alloc(table->s->rec_buff_length)))
4768 return true; /* purecov: inspected */
4769 } else {
4770 DBUG_ASSERT(view && merge_underlying_list);
4771 for (TABLE_LIST *tbl = merge_underlying_list; tbl; tbl = tbl->next_local)
4772 if (tbl->set_insert_values(mem_root))
4773 return true; /* purecov: inspected */
4774 }
4775 return false;
4776 }
4777
4778 /**
4779 Test if this is a leaf with respect to name resolution.
4780
4781
4782 A table reference is a leaf with respect to name resolution if
4783 it is either a leaf node in a nested join tree (table, view,
4784 schema table, subquery), or an inner node that represents a
4785 NATURAL/USING join, or a nested join with materialized join
4786 columns.
4787
4788 @retval true if a leaf, false otherwise.
4789 */
is_leaf_for_name_resolution() const4790 bool TABLE_LIST::is_leaf_for_name_resolution() const {
4791 return (is_view_or_derived() || is_natural_join || is_join_columns_complete ||
4792 !nested_join);
4793 }
4794
4795 /**
4796 Retrieve the first (left-most) leaf in a nested join tree with
4797 respect to name resolution.
4798
4799
4800 Given that 'this' is a nested table reference, recursively walk
4801 down the left-most children of 'this' until we reach a leaf
4802 table reference with respect to name resolution.
4803
4804 The left-most child of a nested table reference is the last element
4805 in the list of children because the children are inserted in
4806 reverse order.
4807
4808 @retval If 'this' is a nested table reference - the left-most child of
4809 @retval the tree rooted in 'this',
4810 else return 'this'
4811 */
4812
first_leaf_for_name_resolution()4813 TABLE_LIST *TABLE_LIST::first_leaf_for_name_resolution() {
4814 TABLE_LIST *cur_table_ref = nullptr;
4815 NESTED_JOIN *cur_nested_join;
4816
4817 if (is_leaf_for_name_resolution()) return this;
4818 DBUG_ASSERT(nested_join);
4819
4820 for (cur_nested_join = nested_join; cur_nested_join;
4821 cur_nested_join = cur_table_ref->nested_join) {
4822 cur_table_ref = cur_nested_join->join_list.front();
4823
4824 /*
4825 If the current nested join is a RIGHT JOIN, the operands in
4826 'join_list' are in reverse order, thus the first operand is
4827 already at the front of the list. Otherwise the first operand
4828 is in the end of the list of join operands.
4829 */
4830 if (cur_table_ref->outer_join != JOIN_TYPE_RIGHT) {
4831 cur_table_ref = cur_nested_join->join_list.back();
4832 }
4833 if (cur_table_ref->is_leaf_for_name_resolution()) break;
4834 }
4835 return cur_table_ref;
4836 }
4837
4838 /**
4839 Retrieve the last (right-most) leaf in a nested join tree with
4840 respect to name resolution.
4841
4842
4843 Given that 'this' is a nested table reference, recursively walk
4844 down the right-most children of 'this' until we reach a leaf
4845 table reference with respect to name resolution.
4846
4847 The right-most child of a nested table reference is the first
4848 element in the list of children because the children are inserted
4849 in reverse order.
4850
4851 @retval - If 'this' is a nested table reference - the right-most child of
4852 @retval the tree rooted in 'this',
4853 @retval - else - 'this'
4854 */
4855
last_leaf_for_name_resolution()4856 TABLE_LIST *TABLE_LIST::last_leaf_for_name_resolution() {
4857 TABLE_LIST *cur_table_ref = this;
4858 NESTED_JOIN *cur_nested_join;
4859
4860 if (is_leaf_for_name_resolution()) return this;
4861 DBUG_ASSERT(nested_join);
4862
4863 for (cur_nested_join = nested_join; cur_nested_join;
4864 cur_nested_join = cur_table_ref->nested_join) {
4865 cur_table_ref = cur_nested_join->join_list.front();
4866 /*
4867 If the current nested is a RIGHT JOIN, the operands in
4868 'join_list' are in reverse order, thus the last operand is in the
4869 end of the list.
4870 */
4871 if (cur_table_ref->outer_join == JOIN_TYPE_RIGHT) {
4872 cur_table_ref = cur_nested_join->join_list.back();
4873 }
4874 if (cur_table_ref->is_leaf_for_name_resolution()) break;
4875 }
4876 return cur_table_ref;
4877 }
4878
4879 /**
4880 Load security context information for this view
4881
4882 @param thd thread handler
4883
4884 @retval false OK
4885 @retval true Error
4886 */
4887
prepare_view_security_context(THD * thd)4888 bool TABLE_LIST::prepare_view_security_context(THD *thd) {
4889 DBUG_TRACE;
4890 DBUG_PRINT("enter", ("table: %s", alias));
4891
4892 DBUG_ASSERT(!prelocking_placeholder && view);
4893 if (view_suid) {
4894 DBUG_PRINT("info", ("This table is suid view => load contest"));
4895 DBUG_ASSERT(view && view_sctx);
4896 if (acl_getroot(thd, view_sctx, definer.user.str, definer.host.str,
4897 definer.host.str, thd->db().str)) {
4898 if ((thd->lex->sql_command == SQLCOM_SHOW_CREATE) ||
4899 (thd->lex->sql_command == SQLCOM_SHOW_FIELDS)) {
4900 push_warning_printf(thd, Sql_condition::SL_NOTE, ER_NO_SUCH_USER,
4901 ER_THD(thd, ER_NO_SUCH_USER), definer.user.str,
4902 definer.host.str);
4903 } else {
4904 if (thd->security_context()->check_access(SUPER_ACL)) {
4905 my_error(ER_NO_SUCH_USER, MYF(0), definer.user.str, definer.host.str);
4906
4907 } else {
4908 if (thd->password == 2)
4909 my_error(ER_ACCESS_DENIED_NO_PASSWORD_ERROR, MYF(0),
4910 thd->security_context()->priv_user().str,
4911 thd->security_context()->priv_host().str);
4912 else
4913 my_error(
4914 ER_ACCESS_DENIED_ERROR, MYF(0),
4915 thd->security_context()->priv_user().str,
4916 thd->security_context()->priv_host().str,
4917 (thd->password ? ER_THD(thd, ER_YES) : ER_THD(thd, ER_NO)));
4918 }
4919 return true;
4920 }
4921 }
4922 }
4923 return false;
4924 }
4925
4926 /**
4927 Find security context of current view
4928
4929 @param thd thread handler
4930
4931 */
4932
find_view_security_context(THD * thd)4933 Security_context *TABLE_LIST::find_view_security_context(THD *thd) {
4934 Security_context *sctx;
4935 TABLE_LIST *upper_view = this;
4936 DBUG_TRACE;
4937
4938 DBUG_ASSERT(view);
4939 while (upper_view && !upper_view->view_suid) {
4940 DBUG_ASSERT(!upper_view->prelocking_placeholder);
4941 upper_view = upper_view->referencing_view;
4942 }
4943 if (upper_view) {
4944 DBUG_PRINT("info",
4945 ("Security context of view %s will be used", upper_view->alias));
4946 sctx = upper_view->view_sctx;
4947 DBUG_ASSERT(sctx);
4948 } else {
4949 DBUG_PRINT("info", ("Current global context will be used"));
4950 sctx = thd->security_context();
4951 }
4952 return sctx;
4953 }
4954
4955 /**
4956 Prepare security context and load underlying tables priveleges for view
4957
4958 @param thd thread handler
4959
4960 @retval false OK
4961 @retval true Error
4962 */
4963
prepare_security(THD * thd)4964 bool TABLE_LIST::prepare_security(THD *thd) {
4965 DBUG_TRACE;
4966 Security_context *save_security_ctx = thd->security_context();
4967
4968 DBUG_ASSERT(!prelocking_placeholder);
4969 if (prepare_view_security_context(thd)) return true;
4970 /* Acl_map was previously checked out by get_aclroot */
4971 thd->set_security_context(find_view_security_context(thd));
4972 opt_trace_disable_if_no_security_context_access(thd);
4973 for (TABLE_LIST *tbl : *view_tables) {
4974 DBUG_ASSERT(tbl->referencing_view);
4975 const char *local_db, *local_table_name;
4976 if (tbl->is_view()) {
4977 local_db = tbl->view_db.str;
4978 local_table_name = tbl->view_name.str;
4979 } else if (tbl->is_derived()) {
4980 /* Initialize privileges for derived tables */
4981 tbl->grant.privilege = SELECT_ACL;
4982 continue;
4983 } else {
4984 local_db = tbl->db;
4985 local_table_name = tbl->get_table_name();
4986 }
4987 fill_effective_table_privileges(thd, &tbl->grant, local_db,
4988 local_table_name);
4989 }
4990 thd->set_security_context(save_security_ctx);
4991 return false;
4992 }
4993
Natural_join_column(Field_translator * field_param,TABLE_LIST * tab)4994 Natural_join_column::Natural_join_column(Field_translator *field_param,
4995 TABLE_LIST *tab) {
4996 DBUG_ASSERT(tab->field_translation);
4997 view_field = field_param;
4998 table_field = nullptr;
4999 table_ref = tab;
5000 is_common = false;
5001 }
5002
Natural_join_column(Item_field * field_param,TABLE_LIST * tab)5003 Natural_join_column::Natural_join_column(Item_field *field_param,
5004 TABLE_LIST *tab) {
5005 DBUG_ASSERT(tab->table == field_param->field->table);
5006 table_field = field_param;
5007 /*
5008 Cache table, to have no resolution problem after natural join nests have
5009 been changed to ordinary join nests.
5010 */
5011 if (tab->cacheable_table) field_param->cached_table = tab;
5012 view_field = nullptr;
5013 table_ref = tab;
5014 is_common = false;
5015 }
5016
name()5017 const char *Natural_join_column::name() {
5018 if (view_field) {
5019 DBUG_ASSERT(table_field == nullptr);
5020 return view_field->name;
5021 }
5022
5023 return table_field->field_name;
5024 }
5025
create_item(THD * thd)5026 Item *Natural_join_column::create_item(THD *thd) {
5027 if (view_field) {
5028 DBUG_ASSERT(table_field == nullptr);
5029 SELECT_LEX *select = thd->lex->current_select();
5030 return create_view_field(thd, table_ref, &view_field->item,
5031 view_field->name, &select->context);
5032 }
5033 return table_field;
5034 }
5035
field()5036 Field *Natural_join_column::field() {
5037 if (view_field) {
5038 DBUG_ASSERT(table_field == nullptr);
5039 return nullptr;
5040 }
5041 return table_field->field;
5042 }
5043
table_name()5044 const char *Natural_join_column::table_name() {
5045 DBUG_ASSERT(table_ref);
5046 return table_ref->alias;
5047 }
5048
db_name()5049 const char *Natural_join_column::db_name() {
5050 if (view_field) return table_ref->view_db.str;
5051
5052 /*
5053 Test that TABLE_LIST::db is the same as TABLE_SHARE::db to
5054 ensure consistency. An exception are I_S schema tables, which
5055 are inconsistent in this respect.
5056 */
5057 DBUG_ASSERT(!strcmp(table_ref->db, table_ref->table->s->db.str) ||
5058 (table_ref->schema_table &&
5059 is_infoschema_db(table_ref->table->s->db.str,
5060 table_ref->table->s->db.length)));
5061 return table_ref->db;
5062 }
5063
grant()5064 GRANT_INFO *Natural_join_column::grant() { return &table_ref->grant; }
5065
set(TABLE_LIST * table)5066 void Field_iterator_view::set(TABLE_LIST *table) {
5067 DBUG_ASSERT(table->field_translation);
5068 view = table;
5069 ptr = table->field_translation;
5070 array_end = table->field_translation_end;
5071 }
5072
name()5073 const char *Field_iterator_table::name() { return (*ptr)->field_name; }
5074
create_item(THD * thd)5075 Item *Field_iterator_table::create_item(THD *thd) {
5076 SELECT_LEX *select = thd->lex->current_select();
5077
5078 Item_field *item = new Item_field(thd, &select->context, *ptr);
5079 if (!item) return nullptr;
5080 /*
5081 This function creates Item-s which don't go through fix_fields(); see same
5082 code in Item_field::fix_fields().
5083 */
5084 if (is_null_on_empty_table(thd, item)) {
5085 item->maybe_null = true;
5086 (*ptr)->table->set_nullable();
5087 }
5088
5089 return item;
5090 }
5091
name()5092 const char *Field_iterator_view::name() { return ptr->name; }
5093
create_item(THD * thd)5094 Item *Field_iterator_view::create_item(THD *thd) {
5095 SELECT_LEX *select = thd->lex->current_select();
5096 return create_view_field(thd, view, &ptr->item, ptr->name, &select->context);
5097 }
5098
create_view_field(THD * thd,TABLE_LIST * view,Item ** field_ref,const char * name,Name_resolution_context * context)5099 static Item *create_view_field(THD *thd, TABLE_LIST *view, Item **field_ref,
5100 const char *name,
5101 Name_resolution_context *context) {
5102 Item *field = *field_ref;
5103 const char *table_name;
5104 DBUG_TRACE;
5105
5106 if (view->schema_table_reformed) {
5107 /*
5108 Translation table items are always Item_fields and already fixed
5109 ('mysql_schema_table' function). So we can return directly the
5110 field. This case happens only for 'show & where' commands.
5111 */
5112 DBUG_ASSERT(field && field->fixed);
5113 return field;
5114 }
5115
5116 DBUG_ASSERT(field);
5117 if (!field->fixed) {
5118 if (field->fix_fields(thd, field_ref))
5119 return nullptr; /* purecov: inspected */
5120 field = *field_ref;
5121 }
5122
5123 /*
5124 Original table name of a field is calculated as follows:
5125 - For a view or base table, the view or base table name.
5126 - For a derived table, the base table name.
5127 - For an expression that is not a simple column reference, an empty string.
5128 */
5129 if (view->is_derived()) {
5130 while (field->type() == Item::REF_ITEM) {
5131 field = down_cast<Item_ref *>(field)->ref[0];
5132 }
5133 if (field->type() == Item::FIELD_ITEM)
5134 table_name = thd->mem_strdup(down_cast<Item_field *>(field)->table_name);
5135 else
5136 table_name = "";
5137 } else {
5138 table_name = view->table_name;
5139 }
5140 /*
5141 @note Creating an Item_view_ref object on top of an Item_field
5142 means that the underlying Item_field object may be shared by
5143 multiple occurrences of superior fields. This is a vulnerable
5144 practice, so special precaution must be taken to avoid programming
5145 mistakes, such as forgetting to mark the use of a field in both
5146 read_set and write_set (may happen e.g in an UPDATE statement).
5147 */
5148 Item *item = new Item_view_ref(context, field_ref, view->alias, table_name,
5149 name, view);
5150 if (item != nullptr && (*field_ref)->type() == Item::FIELD_ITEM &&
5151 down_cast<Item_field *>(*field_ref)->table_ref->m_was_scalar_subquery)
5152 // This logic can be removed after WL#6570
5153 thd->alias_rollback(field_ref);
5154 return item;
5155 }
5156
set(TABLE_LIST * table_ref)5157 void Field_iterator_natural_join::set(TABLE_LIST *table_ref) {
5158 DBUG_ASSERT(table_ref->join_columns);
5159 column_ref_it.init(*(table_ref->join_columns));
5160 cur_column_ref = column_ref_it++;
5161 }
5162
next()5163 void Field_iterator_natural_join::next() {
5164 cur_column_ref = column_ref_it++;
5165 DBUG_ASSERT(!cur_column_ref || !cur_column_ref->table_field ||
5166 cur_column_ref->table_ref->table ==
5167 cur_column_ref->table_field->field->table);
5168 }
5169
set_field_iterator()5170 void Field_iterator_table_ref::set_field_iterator() {
5171 DBUG_TRACE;
5172 /*
5173 If the table reference we are iterating over is a natural join, or it is
5174 an operand of a natural join, and TABLE_LIST::join_columns contains all
5175 the columns of the join operand, then we pick the columns from
5176 TABLE_LIST::join_columns, instead of the orginial container of the
5177 columns of the join operator.
5178 */
5179 if (table_ref->is_join_columns_complete) {
5180 /* Necesary, but insufficient conditions. */
5181 DBUG_ASSERT(
5182 table_ref->is_natural_join || table_ref->nested_join ||
5183 (table_ref->join_columns &&
5184 /* This is a merge view. */
5185 ((table_ref->field_translation &&
5186 table_ref->join_columns->elements ==
5187 (ulong)(table_ref->field_translation_end -
5188 table_ref->field_translation)) ||
5189 /* This is stored table or a tmptable view. */
5190 (!table_ref->field_translation &&
5191 table_ref->join_columns->elements == table_ref->table->s->fields))));
5192 field_it = &natural_join_it;
5193 DBUG_PRINT("info", ("field_it for '%s' is Field_iterator_natural_join",
5194 table_ref->alias));
5195 }
5196 /* This is a merge view, so use field_translation. */
5197 else if (table_ref->field_translation) {
5198 DBUG_ASSERT(table_ref->is_merged());
5199 field_it = &view_field_it;
5200 DBUG_PRINT("info",
5201 ("field_it for '%s' is Field_iterator_view", table_ref->alias));
5202 }
5203 /* This is a base table or stored view. */
5204 else {
5205 DBUG_ASSERT(table_ref->table || table_ref->is_view());
5206 field_it = &table_field_it;
5207 DBUG_PRINT("info",
5208 ("field_it for '%s' is Field_iterator_table", table_ref->alias));
5209 }
5210 field_it->set(table_ref);
5211 }
5212
set(TABLE_LIST * table)5213 void Field_iterator_table_ref::set(TABLE_LIST *table) {
5214 DBUG_ASSERT(table);
5215 first_leaf = table->first_leaf_for_name_resolution();
5216 last_leaf = table->last_leaf_for_name_resolution();
5217 DBUG_ASSERT(first_leaf && last_leaf);
5218 table_ref = first_leaf;
5219 set_field_iterator();
5220 }
5221
next()5222 void Field_iterator_table_ref::next() {
5223 /* Move to the next field in the current table reference. */
5224 field_it->next();
5225 /*
5226 If all fields of the current table reference are exhausted, move to
5227 the next leaf table reference.
5228 */
5229 if (field_it->end_of_fields() && table_ref != last_leaf) {
5230 table_ref = table_ref->next_name_resolution_table;
5231 DBUG_ASSERT(table_ref);
5232 set_field_iterator();
5233 }
5234 }
5235
get_table_name()5236 const char *Field_iterator_table_ref::get_table_name() {
5237 if (table_ref->is_view())
5238 return table_ref->view_name.str;
5239 else if (table_ref->is_natural_join)
5240 return natural_join_it.column_ref()->table_name();
5241
5242 DBUG_ASSERT(
5243 table_ref->is_table_function() ||
5244 !strcmp(table_ref->table_name, table_ref->table->s->table_name.str));
5245 return table_ref->table_name;
5246 }
5247
get_db_name()5248 const char *Field_iterator_table_ref::get_db_name() {
5249 if (table_ref->is_view())
5250 return table_ref->view_db.str;
5251 else if (table_ref->is_natural_join)
5252 return natural_join_it.column_ref()->db_name();
5253
5254 /*
5255 Test that TABLE_LIST::db is the same as TABLE_SHARE::db to
5256 ensure consistency. An exception are I_S schema tables, which
5257 are inconsistent in this respect and any_db (used in the handler
5258 interface to manage aliases).
5259 */
5260 DBUG_ASSERT(!strcmp(table_ref->db, table_ref->table->s->db.str) ||
5261 table_ref->db == any_db ||
5262 (table_ref->schema_table &&
5263 is_infoschema_db(table_ref->table->s->db.str,
5264 table_ref->table->s->db.length)));
5265
5266 return table_ref->db == any_db ? table_ref->table->s->db.str : table_ref->db;
5267 }
5268
grant()5269 GRANT_INFO *Field_iterator_table_ref::grant() {
5270 if (table_ref->is_natural_join)
5271 return natural_join_it.column_ref()->grant();
5272 else
5273 return &table_ref->grant;
5274 }
5275
5276 /**
5277 Create new or return existing column reference to a column of a
5278 natural/using join.
5279
5280 @param thd Session.
5281 @param parent_table_ref the parent table reference over which the
5282 iterator is iterating
5283
5284 Create a new natural join column for the current field of the
5285 iterator if no such column was created, or return an already
5286 created natural join column. The former happens for base tables or
5287 views, and the latter for natural/using joins. If a new field is
5288 created, then the field is added to 'parent_table_ref' if it is
5289 given, or to the original table referene of the field if
5290 parent_table_ref == NULL.
5291
5292 @note
5293 This method is designed so that when a Field_iterator_table_ref
5294 walks through the fields of a table reference, all its fields
5295 are created and stored as follows:
5296 - If the table reference being iterated is a stored table, view or
5297 natural/using join, store all natural join columns in a list
5298 attached to that table reference.
5299 - If the table reference being iterated is a nested join that is
5300 not natural/using join, then do not materialize its result
5301 fields. This is OK because for such table references
5302 Field_iterator_table_ref iterates over the fields of the nested
5303 table references (recursively). In this way we avoid the storage
5304 of unnecessay copies of result columns of nested joins.
5305
5306 @retval other Pointer to a column of a natural join (or its operand)
5307 @retval NULL No memory to allocate the column
5308 */
5309
get_or_create_column_ref(THD * thd,TABLE_LIST * parent_table_ref)5310 Natural_join_column *Field_iterator_table_ref::get_or_create_column_ref(
5311 THD *thd, TABLE_LIST *parent_table_ref) {
5312 Natural_join_column *nj_col;
5313 bool is_created = true;
5314 uint field_count = 0;
5315 TABLE_LIST *add_table_ref = parent_table_ref ? parent_table_ref : table_ref;
5316
5317 if (field_it == &table_field_it) {
5318 /* The field belongs to a stored table. */
5319 Field *tmp_field = table_field_it.field();
5320 Item_field *tmp_item =
5321 new Item_field(thd, &thd->lex->current_select()->context, tmp_field);
5322 if (!tmp_item) return nullptr;
5323 nj_col = new (thd->mem_root) Natural_join_column(tmp_item, table_ref);
5324 field_count = table_ref->table->s->fields;
5325 } else if (field_it == &view_field_it) {
5326 /* The field belongs to a merge view or information schema table. */
5327 Field_translator *translated_field = view_field_it.field_translator();
5328 nj_col =
5329 new (thd->mem_root) Natural_join_column(translated_field, table_ref);
5330 field_count =
5331 table_ref->field_translation_end - table_ref->field_translation;
5332 } else {
5333 /*
5334 The field belongs to a NATURAL join, therefore the column reference was
5335 already created via one of the two constructor calls above. In this case
5336 we just return the already created column reference.
5337 */
5338 DBUG_ASSERT(table_ref->is_join_columns_complete);
5339 is_created = false;
5340 nj_col = natural_join_it.column_ref();
5341 DBUG_ASSERT(nj_col);
5342 }
5343 DBUG_ASSERT(!nj_col->table_field ||
5344 nj_col->table_ref->table == nj_col->table_field->field->table);
5345
5346 /*
5347 If the natural join column was just created add it to the list of
5348 natural join columns of either 'parent_table_ref' or to the table
5349 reference that directly contains the original field.
5350 */
5351 if (is_created) {
5352 /* Make sure not all columns were materialized. */
5353 DBUG_ASSERT(!add_table_ref->is_join_columns_complete);
5354 if (!add_table_ref->join_columns) {
5355 /* Create a list of natural join columns on demand. */
5356 if (!(add_table_ref->join_columns =
5357 new (thd->mem_root) List<Natural_join_column>))
5358 return nullptr;
5359 add_table_ref->is_join_columns_complete = false;
5360 }
5361 add_table_ref->join_columns->push_back(nj_col);
5362 /*
5363 If new fields are added to their original table reference, mark if
5364 all fields were added. We do it here as the caller has no easy way
5365 of knowing when to do it.
5366 If the fields are being added to parent_table_ref, then the caller
5367 must take care to mark when all fields are created/added.
5368 */
5369 if (!parent_table_ref &&
5370 add_table_ref->join_columns->elements == field_count)
5371 add_table_ref->is_join_columns_complete = true;
5372 }
5373
5374 return nj_col;
5375 }
5376
5377 /**
5378 Return an existing reference to a column of a natural/using join.
5379
5380
5381 The method should be called in contexts where it is expected that
5382 all natural join columns are already created, and that the column
5383 being retrieved is a Natural_join_column.
5384
5385 @retval other Pointer to a column of a natural join (or its operand)
5386 @retval NULL No memory to allocate the column
5387 */
5388
get_natural_column_ref()5389 Natural_join_column *Field_iterator_table_ref::get_natural_column_ref() {
5390 Natural_join_column *nj_col;
5391
5392 DBUG_ASSERT(field_it == &natural_join_it);
5393 /*
5394 The field belongs to a NATURAL join, therefore the column reference was
5395 already created via one of the two constructor calls above. In this case
5396 we just return the already created column reference.
5397 */
5398 nj_col = natural_join_it.column_ref();
5399 DBUG_ASSERT(nj_col &&
5400 (!nj_col->table_field ||
5401 nj_col->table_ref->table == nj_col->table_field->field->table));
5402 return nj_col;
5403 }
5404
5405 /*****************************************************************************
5406 Functions to handle column usage bitmaps (read_set, write_set etc...)
5407 *****************************************************************************/
5408
5409 /* Reset all columns bitmaps */
5410
clear_column_bitmaps()5411 void TABLE::clear_column_bitmaps() {
5412 /*
5413 Reset column read/write usage. It's identical to:
5414 bitmap_clear_all(&table->def_read_set);
5415 bitmap_clear_all(&table->def_write_set);
5416 */
5417 memset(def_read_set.bitmap, 0, s->column_bitmap_size * 2);
5418 column_bitmaps_set(&def_read_set, &def_write_set);
5419
5420 bitmap_clear_all(&def_fields_set_during_insert);
5421 fields_set_during_insert = &def_fields_set_during_insert;
5422
5423 bitmap_clear_all(&tmp_set);
5424 bitmap_clear_all(&cond_set);
5425
5426 if (m_partial_update_columns != nullptr)
5427 bitmap_clear_all(m_partial_update_columns);
5428 }
5429
5430 /**
5431 Tell handler we are going to call position() and rnd_pos() later.
5432
5433 This is needed for handlers that uses the primary key to find the
5434 row. In this case we have to extend the read bitmap with the primary
5435 key fields.
5436
5437 @note: Calling this function does not initialize the table for
5438 reading using rnd_pos(). rnd_init() still has to be called before
5439 rnd_pos().
5440 */
5441
prepare_for_position()5442 void TABLE::prepare_for_position() {
5443 DBUG_TRACE;
5444
5445 if ((file->ha_table_flags() & HA_PRIMARY_KEY_REQUIRED_FOR_POSITION) &&
5446 s->primary_key < MAX_KEY) {
5447 mark_columns_used_by_index_no_reset(s->primary_key, read_set);
5448 /* signal change */
5449 file->column_bitmaps_signal();
5450 }
5451 }
5452
5453 /**
5454 Mark column as either read or written (or none) according to mark_used.
5455
5456 @note If marking a written field, set thd->dup_field if the column is
5457 already marked.
5458
5459 @note If TABLE::get_fields_in_item_tree is set, set the flag bit
5460 GET_FIXED_FIELDS_FLAG for the field.
5461
5462 @param field The column to be marked as used
5463 @param mark =MARK_COLUMNS_NONE: Only update flag field, if applicable
5464 =MARK_COLUMNS_READ: Mark column as read
5465 =MARK_COLUMNS_WRITE: Mark column as written
5466 =MARK_COLUMNS_TEMP: Mark column as read, used by filesort()
5467 and processing of generated columns
5468 */
5469
mark_column_used(Field * field,enum enum_mark_columns mark)5470 void TABLE::mark_column_used(Field *field, enum enum_mark_columns mark) {
5471 DBUG_TRACE;
5472
5473 switch (mark) {
5474 case MARK_COLUMNS_NONE:
5475 if (get_fields_in_item_tree) field->set_flag(GET_FIXED_FIELDS_FLAG);
5476 break;
5477
5478 case MARK_COLUMNS_READ: {
5479 Key_map part_of_key = field->part_of_key;
5480 bitmap_set_bit(read_set, field->field_index());
5481
5482 part_of_key.merge(field->part_of_prefixkey);
5483 covering_keys.intersect(part_of_key);
5484 merge_keys.merge(field->part_of_key);
5485 if (get_fields_in_item_tree) field->set_flag(GET_FIXED_FIELDS_FLAG);
5486 if (field->is_virtual_gcol()) mark_gcol_in_maps(field);
5487 break;
5488 }
5489 case MARK_COLUMNS_WRITE:
5490 bitmap_set_bit(write_set, field->field_index());
5491 DBUG_ASSERT(!get_fields_in_item_tree);
5492
5493 if (field->is_gcol()) mark_gcol_in_maps(field);
5494 break;
5495
5496 case MARK_COLUMNS_TEMP:
5497 bitmap_set_bit(read_set, field->field_index());
5498 if (field->is_virtual_gcol()) mark_gcol_in_maps(field);
5499 break;
5500 }
5501 }
5502
5503 /*
5504 Mark that only fields from one key is used
5505
5506 NOTE:
5507 This changes the bitmap to use the tmp bitmap
5508 After this, you can't access any other columns in the table until
5509 bitmaps are reset, for example with TABLE::clear_column_bitmaps().
5510 */
5511
mark_columns_used_by_index(uint index)5512 void TABLE::mark_columns_used_by_index(uint index) {
5513 MY_BITMAP *bitmap = &tmp_set;
5514 DBUG_TRACE;
5515
5516 set_keyread(true);
5517 bitmap_clear_all(bitmap);
5518 mark_columns_used_by_index_no_reset(index, bitmap);
5519 column_bitmaps_set(bitmap, bitmap);
5520 }
5521
5522 /**
5523 mark columns used by key, but don't reset other fields
5524
5525 The parameter key_parts is used for controlling how many of the
5526 key_parts that will be marked in the bitmap. It has the following
5527 interpretation:
5528
5529 = 0: Use all regular key parts from the key
5530 (user_defined_key_parts)
5531 >= actual_key_parts: Use all regular and extended columns
5532 < actual_key_parts: Use this exact number of key parts
5533
5534 To use all regular key parts, the caller can use the default value (0).
5535 To use all regular and extended key parts, use UINT_MAX.
5536
5537 @note The bit map is not cleared by this function. Only bits
5538 corresponding to a column used by the index will be set. Bits
5539 representing columns not used by the index will not be changed.
5540
5541 @param index index number
5542 @param bitmap bitmap to mark
5543 @param key_parts number of leading key parts to mark. Default is 0.
5544
5545 @todo consider using actual_key_parts(key_info[index]) instead of
5546 key_info[index].user_defined_key_parts: if the PK suffix of a secondary
5547 index is usable it should be marked.
5548 */
5549
mark_columns_used_by_index_no_reset(uint index,MY_BITMAP * bitmap,uint key_parts) const5550 void TABLE::mark_columns_used_by_index_no_reset(uint index, MY_BITMAP *bitmap,
5551 uint key_parts) const {
5552 // If key_parts has the default value, then include user defined key parts
5553 if (key_parts == 0)
5554 key_parts = key_info[index].user_defined_key_parts;
5555 else if (key_parts > key_info[index].actual_key_parts)
5556 key_parts = key_info[index].actual_key_parts;
5557
5558 KEY_PART_INFO *key_part = key_info[index].key_part;
5559 KEY_PART_INFO *key_part_end = key_part + key_parts;
5560 for (; key_part != key_part_end; key_part++)
5561 bitmap_set_bit(bitmap, key_part->fieldnr - 1);
5562 }
5563
5564 /**
5565 Mark auto-increment fields as used fields in both read and write maps
5566
5567 @note
5568 This is needed in insert & update as the auto-increment field is
5569 always set and sometimes read.
5570 */
5571
mark_auto_increment_column()5572 void TABLE::mark_auto_increment_column() {
5573 DBUG_ASSERT(found_next_number_field);
5574 /*
5575 We must set bit in read set as update_auto_increment() is using the
5576 store() to check overflow of auto_increment values
5577 */
5578 bitmap_set_bit(read_set, found_next_number_field->field_index());
5579 bitmap_set_bit(write_set, found_next_number_field->field_index());
5580 if (s->next_number_keypart)
5581 mark_columns_used_by_index_no_reset(s->next_number_index, read_set);
5582 file->column_bitmaps_signal();
5583 }
5584
5585 /*
5586 Mark columns needed for doing an delete of a row
5587
5588 DESCRIPTON
5589 Some table engines don't have a cursor on the retrieve rows
5590 so they need either to use the primary key or all columns to
5591 be able to delete a row.
5592
5593 If the engine needs this, the function works as follows:
5594 - If primary key exits, mark the primary key columns to be read.
5595 - If not, mark all columns to be read
5596
5597 If the engine has HA_REQUIRES_KEY_COLUMNS_FOR_DELETE, we will
5598 mark all key columns as 'to-be-read'. This allows the engine to
5599 loop over the given record to find all keys and doesn't have to
5600 retrieve the row again.
5601 */
5602
mark_columns_needed_for_delete(THD * thd)5603 void TABLE::mark_columns_needed_for_delete(THD *thd) {
5604 mark_columns_per_binlog_row_image(thd);
5605
5606 if (triggers && triggers->mark_fields(TRG_EVENT_DELETE)) return;
5607
5608 if (file->ha_table_flags() & HA_REQUIRES_KEY_COLUMNS_FOR_DELETE) {
5609 Field **reg_field;
5610 for (reg_field = field; *reg_field; reg_field++) {
5611 if ((*reg_field)->is_flag_set(PART_KEY_FLAG))
5612 bitmap_set_bit(read_set, (*reg_field)->field_index());
5613 }
5614 file->column_bitmaps_signal();
5615 }
5616 if (file->ha_table_flags() & HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) {
5617 /*
5618 If the handler has no cursor capabilites we have to read
5619 either the primary key, the hidden primary key or all columns to
5620 be able to do an delete
5621 */
5622 if (s->primary_key == MAX_KEY) {
5623 /*
5624 If in RBR, we have alreay marked the full before image
5625 in mark_columns_per_binlog_row_image, if not, then use
5626 the hidden primary key
5627 */
5628 if (!(mysql_bin_log.is_open() && in_use &&
5629 in_use->is_current_stmt_binlog_format_row()))
5630 file->use_hidden_primary_key();
5631 } else
5632 mark_columns_used_by_index_no_reset(s->primary_key, read_set);
5633
5634 file->column_bitmaps_signal();
5635 }
5636 if (vfield) {
5637 /*
5638 InnoDB's delete_row may need to log pre-image of the index entries to
5639 its UNDO log. Thus, indexed virtual generated column must be made ready
5640 for evaluation.
5641 */
5642 mark_generated_columns(true);
5643 }
5644 }
5645
5646 /**
5647 @brief
5648 Mark columns needed for doing an update of a row
5649
5650 @details
5651 Some engines needs to have all columns in an update (to be able to
5652 build a complete row). If this is the case, we mark all not
5653 updated columns to be read.
5654
5655 If this is not the case, we do like in the delete case and mark
5656 if neeed, either the primary key column or all columns to be read.
5657 (see mark_columns_needed_for_delete() for details)
5658
5659 If the engine has HA_REQUIRES_KEY_COLUMNS_FOR_DELETE, we will
5660 mark all USED key columns as 'to-be-read'. This allows the engine to
5661 loop over the given record to find all changed keys and doesn't have to
5662 retrieve the row again.
5663
5664 Unlike other similar methods, it doesn't mark fields used by triggers,
5665 that is the responsibility of the caller to do, by using
5666 Table_trigger_dispatcher::mark_used_fields(TRG_EVENT_UPDATE)!
5667
5668 Note: Marking additional columns as per binlog_row_image requirements will
5669 influence query execution plan. For example in the case of
5670 binlog_row_image=FULL the entire read_set and write_set needs to be flagged.
5671 This will influence update query to think that 'used key is being modified'
5672 and query will create a temporary table to process the update operation.
5673 Which will result in performance degradation. Hence callers who don't want
5674 their query execution to be influenced as per binlog_row_image requirements
5675 can skip marking binlog specific columns here and they should make an
5676 explicit call to 'mark_columns_per_binlog_row_image()' function to mark
5677 binlog_row_image specific columns.
5678 */
5679
mark_columns_needed_for_update(THD * thd,bool mark_binlog_columns)5680 void TABLE::mark_columns_needed_for_update(THD *thd, bool mark_binlog_columns) {
5681 DBUG_TRACE;
5682 if (mark_binlog_columns) mark_columns_per_binlog_row_image(thd);
5683 if (file->ha_table_flags() & HA_REQUIRES_KEY_COLUMNS_FOR_DELETE) {
5684 /* Mark all used key columns for read */
5685 Field **reg_field;
5686 for (reg_field = field; *reg_field; reg_field++) {
5687 /* Merge keys is all keys that had a column refered to in the query */
5688 if (merge_keys.is_overlapping((*reg_field)->part_of_key))
5689 bitmap_set_bit(read_set, (*reg_field)->field_index());
5690 }
5691 file->column_bitmaps_signal();
5692 }
5693
5694 if (file->ha_table_flags() & HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) {
5695 /*
5696 If the handler has no cursor capabilites we have to read either
5697 the primary key, the hidden primary key or all columns to be
5698 able to do an update
5699 */
5700 if (s->primary_key == MAX_KEY) {
5701 /*
5702 If in RBR, we have alreay marked the full before image
5703 in mark_columns_per_binlog_row_image, if not, then use
5704 the hidden primary key
5705 */
5706 if (!(mysql_bin_log.is_open() && in_use &&
5707 in_use->is_current_stmt_binlog_format_row()))
5708 file->use_hidden_primary_key();
5709 } else
5710 mark_columns_used_by_index_no_reset(s->primary_key, read_set);
5711
5712 file->column_bitmaps_signal();
5713 }
5714 /* Mark dependent generated columns as writable */
5715 if (vfield) mark_generated_columns(true);
5716 /* Mark columns needed for check constraints evaluation */
5717 if (table_check_constraint_list != nullptr)
5718 mark_check_constraint_columns(true);
5719 }
5720
5721 /*
5722 Mark columns according the binlog row image option.
5723
5724 When logging in RBR, the user can select whether to
5725 log partial or full rows, depending on the table
5726 definition, and the value of binlog_row_image.
5727
5728 Semantics of the binlog_row_image are the following
5729 (PKE - primary key equivalent, ie, PK fields if PK
5730 exists, all fields otherwise):
5731
5732 binlog_row_image= MINIMAL
5733 - This marks the PKE fields in the read_set
5734 - This marks all fields where a value was specified
5735 in the write_set
5736
5737 binlog_row_image= NOBLOB
5738 - This marks PKE + all non-blob fields in the read_set
5739 - This marks all fields where a value was specified
5740 and all non-blob fields in the write_set
5741
5742 binlog_row_image= FULL
5743 - all columns in the read_set
5744 - all columns in the write_set
5745
5746 This marking is done without resetting the original
5747 bitmaps. This means that we will strip extra fields in
5748 the read_set at binlogging time (for those cases that
5749 we only want to log a PK and we needed other fields for
5750 execution).
5751 */
mark_columns_per_binlog_row_image(THD * thd)5752 void TABLE::mark_columns_per_binlog_row_image(THD *thd) {
5753 DBUG_TRACE;
5754 DBUG_ASSERT(read_set->bitmap);
5755 DBUG_ASSERT(write_set->bitmap);
5756
5757 /**
5758 If in RBR we may need to mark some extra columns,
5759 depending on the binlog-row-image command line argument.
5760 */
5761 if ((mysql_bin_log.is_open() && in_use &&
5762 in_use->is_current_stmt_binlog_format_row() &&
5763 !ha_check_storage_engine_flag(s->db_type(), HTON_NO_BINLOG_ROW_OPT))) {
5764 /* if there is no PK, then mark all columns for the BI. */
5765 if (s->primary_key >= MAX_KEY) bitmap_set_all(read_set);
5766
5767 switch (thd->variables.binlog_row_image) {
5768 case BINLOG_ROW_IMAGE_FULL:
5769 if (s->primary_key < MAX_KEY) bitmap_set_all(read_set);
5770 bitmap_set_all(write_set);
5771 break;
5772 case BINLOG_ROW_IMAGE_NOBLOB:
5773 /* for every field that is not set, mark it unless it is a blob */
5774 for (Field **ptr = field; *ptr; ptr++) {
5775 Field *my_field = *ptr;
5776 /*
5777 bypass blob fields. These can be set or not set, we don't care.
5778 Later, at binlogging time, if we don't need them in the before
5779 image, we will discard them.
5780
5781 If set in the AI, then the blob is really needed, there is
5782 nothing we can do about it.
5783 */
5784 if ((s->primary_key < MAX_KEY) &&
5785 (my_field->is_flag_set(PRI_KEY_FLAG) ||
5786 (my_field->type() != MYSQL_TYPE_BLOB)))
5787 bitmap_set_bit(read_set, my_field->field_index());
5788
5789 if (my_field->type() != MYSQL_TYPE_BLOB)
5790 bitmap_set_bit(write_set, my_field->field_index());
5791 }
5792 break;
5793 case BINLOG_ROW_IMAGE_MINIMAL:
5794 /* mark the primary key if available in the read_set */
5795 if (s->primary_key < MAX_KEY)
5796 mark_columns_used_by_index_no_reset(s->primary_key, read_set);
5797 break;
5798
5799 default:
5800 DBUG_ASSERT(false);
5801 }
5802 file->column_bitmaps_signal();
5803 }
5804 }
5805
5806 /**
5807 Allocate space for keys, for a materialized derived table.
5808
5809 @param key_count Number of keys to allocate.
5810 @param modify_share Do modifications to TABLE_SHARE.
5811
5812 When modifying TABLE, modifications to TABLE_SHARE are needed, so that both
5813 objects remain consistent. Even if several TABLEs point to the same
5814 TABLE_SHARE, those modifications must be done only once (consider for
5815 example, incremementing TABLE_SHARE::keys). Should they be done when
5816 processing the first TABLE, or the second, or? In case this function, when
5817 updating TABLE, relies on TABLE_SHARE members which are the subject of
5818 modifications, we follow this rule: do those TABLE_SHARE member
5819 modifications first: thus, TABLE-modifying code can be identical for all
5820 TABLEs. So the _first_ TABLE calling this function, only, should pass
5821 'true': all next ones should not modify the TABLE_SHARE.
5822
5823 @returns true if error
5824 */
5825
alloc_tmp_keys(uint key_count,bool modify_share)5826 bool TABLE::alloc_tmp_keys(uint key_count, bool modify_share) {
5827 const size_t bytes = sizeof(KEY) * key_count;
5828
5829 if (modify_share) {
5830 s->max_tmp_keys = key_count;
5831 /*
5832 s->keyinfo may pre-exist, if keys have already been added to another
5833 reference to the same CTE in another query block.
5834 */
5835 KEY *old_ki = s->key_info;
5836 if (!(s->key_info = static_cast<KEY *>(s->mem_root.Alloc(bytes))))
5837 return true; /* purecov: inspected */
5838 memset(s->key_info, 0, bytes);
5839 if (old_ki) memcpy(s->key_info, old_ki, sizeof(KEY) * s->keys);
5840 }
5841
5842 // Catch if the caller didn't respect the rule for 'modify_share'
5843 DBUG_ASSERT(s->max_tmp_keys == key_count);
5844
5845 KEY *old_ki = key_info;
5846 if (!(key_info = static_cast<KEY *>(s->mem_root.Alloc(bytes))))
5847 return true; /* purecov: inspected */
5848 memset(key_info, 0, bytes);
5849 if (old_ki) memcpy(key_info, old_ki, sizeof(KEY) * s->keys);
5850
5851 return false;
5852 }
5853
5854 /**
5855 @brief Add one key to a materialized derived table.
5856
5857 @param key_parts bitmap of fields that take a part in the key.
5858 @param key_name name of the key
5859 @param invisible If true, set up bitmaps so the key is never used by
5860 this TABLE
5861 @param modify_share Do modifications to TABLE_SHARE. @see alloc_tmp_keys
5862
5863 @details
5864 Creates a key for this table from fields which corresponds the bits set to 1
5865 in the 'key_parts' bitmap. The 'key_name' name is given to the newly created
5866 key. In the key, columns are in the same order as in the table.
5867 @see add_derived_key
5868
5869 @todo somehow manage to create keys in tmp_table_param for unification
5870 purposes
5871
5872 @return true OOM error.
5873 @return false the key was created or ignored (too long key).
5874 */
5875
add_tmp_key(Field_map * key_parts,char * key_name,bool invisible,bool modify_share)5876 bool TABLE::add_tmp_key(Field_map *key_parts, char *key_name, bool invisible,
5877 bool modify_share) {
5878 DBUG_ASSERT(!created && key_parts);
5879
5880 Field **reg_field;
5881 uint i;
5882 bool key_start = true;
5883 uint field_count = 0;
5884 uint key_len = 0;
5885
5886 for (i = 0, reg_field = field; *reg_field; i++, reg_field++) {
5887 if (key_parts->is_set(i)) {
5888 KEY_PART_INFO tkp;
5889 // Ensure that we're not creating a key over a blob field.
5890 DBUG_ASSERT(!(*reg_field)->is_flag_set(BLOB_FLAG));
5891 /*
5892 Check if possible key is too long, ignore it if so.
5893 The reason to use MI_MAX_KEY_LENGTH (myisam's default) is that it is
5894 smaller than MAX_KEY_LENGTH (heap's default) and it's unknown whether
5895 myisam or heap will be used for tmp table.
5896 */
5897 tkp.init_from_field(*reg_field);
5898 key_len += tkp.store_length;
5899 if (key_len > MI_MAX_KEY_LENGTH) {
5900 return false;
5901 }
5902 }
5903 field_count++;
5904 }
5905 const uint key_part_count = key_parts->bits_set();
5906
5907 // Code above didn't change TABLE; start with changing TABLE_SHARE:
5908 if (modify_share) {
5909 s->max_key_length = std::max(s->max_key_length, key_len);
5910 s->key_parts += key_part_count;
5911 DBUG_ASSERT(s->keys < s->max_tmp_keys);
5912 s->keys++;
5913 }
5914
5915 const uint keyno = s->keys - 1;
5916 KEY *cur_key = key_info + keyno;
5917
5918 cur_key->usable_key_parts = cur_key->user_defined_key_parts = key_part_count;
5919 cur_key->actual_key_parts = cur_key->user_defined_key_parts;
5920 cur_key->key_length = key_len;
5921 cur_key->algorithm = HA_KEY_ALG_BTREE;
5922 cur_key->name = key_name;
5923 cur_key->actual_flags = cur_key->flags = HA_GENERATED_KEY;
5924 cur_key->set_in_memory_estimate(IN_MEMORY_ESTIMATE_UNKNOWN);
5925
5926 /*
5927 Allocate storage for the key part array and the two rec_per_key arrays in
5928 the tables' mem_root.
5929 */
5930 const size_t key_buf_size = sizeof(KEY_PART_INFO) * key_part_count;
5931 ulong *rec_per_key;
5932 rec_per_key_t *rec_per_key_float;
5933 uchar *key_buf;
5934 KEY_PART_INFO *key_part_info;
5935
5936 if (!multi_alloc_root(&s->mem_root, &key_buf, key_buf_size, &rec_per_key,
5937 sizeof(ulong) * key_part_count, &rec_per_key_float,
5938 sizeof(rec_per_key_t) * key_part_count, NULL))
5939 return true; /* purecov: inspected */
5940
5941 memset(key_buf, 0, key_buf_size);
5942 cur_key->key_part = key_part_info = (KEY_PART_INFO *)key_buf;
5943 cur_key->set_rec_per_key_array(rec_per_key, rec_per_key_float);
5944 cur_key->table = this;
5945
5946 /* Initialize rec_per_key and rec_per_key_float */
5947 for (uint kp = 0; kp < key_part_count; ++kp) {
5948 cur_key->rec_per_key[kp] = 0;
5949 cur_key->set_records_per_key(kp, REC_PER_KEY_UNKNOWN);
5950 }
5951
5952 if (!invisible) {
5953 if (field_count == key_part_count) covering_keys.set_bit(keyno);
5954 keys_in_use_for_group_by.set_bit(keyno);
5955 keys_in_use_for_order_by.set_bit(keyno);
5956 }
5957
5958 for (i = 0, reg_field = field; *reg_field; i++, reg_field++) {
5959 if (!(key_parts->is_set(i))) continue;
5960
5961 if (key_start) (*reg_field)->key_start.set_bit(keyno);
5962 key_start = false;
5963 (*reg_field)->part_of_key.set_bit(keyno);
5964 (*reg_field)->part_of_sortkey.set_bit(keyno);
5965 (*reg_field)->set_flag(PART_KEY_FLAG);
5966 key_part_info->init_from_field(*reg_field);
5967 key_part_info++;
5968 }
5969
5970 if (modify_share) {
5971 /*
5972 We copy the TABLE's key_info to the TABLE_SHARE's key_info. Some of the
5973 copied info is constant over all instances of TABLE,
5974 e.g. s->key_info[keyno].key_part[i].key_part_flag, so can be
5975 legally accessed from the share. On the other hand, TABLE-specific
5976 members (rec_per_key, field, etc) of the TABLE's key_info shouldn't be
5977 accessed from the share.
5978 */
5979 KEY &sk = s->key_info[keyno];
5980 sk = *cur_key;
5981 sk.table = nullptr; // catch any illegal access
5982 sk.set_rec_per_key_array(nullptr, nullptr);
5983 }
5984
5985 return false;
5986 }
5987
5988 /**
5989 For a materialized derived table: informs the share that certain
5990 not-yet-used keys are going to be used.
5991
5992 @param k Used keys
5993 @returns New position of first not-yet-used key.
5994 */
find_first_unused_tmp_key(const Key_map & k)5995 uint TABLE_SHARE::find_first_unused_tmp_key(const Key_map &k) {
5996 while (first_unused_tmp_key < MAX_INDEXES && k.is_set(first_unused_tmp_key))
5997 first_unused_tmp_key++; // locate the first free slot
5998 return first_unused_tmp_key;
5999 }
6000
6001 /**
6002 For a materialized derived table: copies a KEY definition from a position to
6003 the first not-yet-used position (which is lower).
6004
6005 @param old_idx source position
6006 @param modify_share Do modifications to TABLE_SHARE. @see alloc_tmp_keys
6007 */
copy_tmp_key(int old_idx,bool modify_share)6008 void TABLE::copy_tmp_key(int old_idx, bool modify_share) {
6009 if (modify_share)
6010 s->key_info[s->first_unused_tmp_key++] = s->key_info[old_idx];
6011 const int new_idx = s->first_unused_tmp_key - 1;
6012 DBUG_ASSERT(!created && new_idx < old_idx && old_idx < (int)s->keys);
6013 key_info[new_idx] = key_info[old_idx];
6014
6015 for (auto reg_field = field; *reg_field; reg_field++) {
6016 auto f = *reg_field;
6017 f->key_start.clear_bit(new_idx);
6018 if (f->key_start.is_set(old_idx)) f->key_start.set_bit(new_idx);
6019 f->part_of_key.clear_bit(new_idx);
6020 if (f->part_of_key.is_set(old_idx)) f->part_of_key.set_bit(new_idx);
6021 f->part_of_sortkey.clear_bit(new_idx);
6022 if (f->part_of_sortkey.is_set(old_idx)) f->part_of_sortkey.set_bit(new_idx);
6023 }
6024 covering_keys.clear_bit(new_idx);
6025 if (covering_keys.is_set(old_idx)) covering_keys.set_bit(new_idx);
6026 keys_in_use_for_group_by.clear_bit(new_idx);
6027 if (keys_in_use_for_group_by.is_set(old_idx))
6028 keys_in_use_for_group_by.set_bit(new_idx);
6029 keys_in_use_for_order_by.clear_bit(new_idx);
6030 if (keys_in_use_for_order_by.is_set(old_idx))
6031 keys_in_use_for_order_by.set_bit(new_idx);
6032 }
6033
6034 /**
6035 For a materialized derived table: after copy_tmp_key() has copied all
6036 definitions of used KEYs, in TABLE::key_info we have a head of used keys
6037 followed by a tail of unused keys; this function chops the tail.
6038
6039 @param modify_share Do modifications to TABLE_SHARE. @see alloc_tmp_keys
6040 */
drop_unused_tmp_keys(bool modify_share)6041 void TABLE::drop_unused_tmp_keys(bool modify_share) {
6042 if (modify_share) {
6043 DBUG_ASSERT(s->first_unused_tmp_key <= s->keys);
6044 s->keys = s->first_unused_tmp_key;
6045 s->key_parts = 0;
6046 for (uint i = 0; i < s->keys; i++)
6047 s->key_parts += s->key_info[i].user_defined_key_parts;
6048 if (s->first_unused_tmp_key == 0) s->key_info = nullptr;
6049 }
6050 if (!s->key_info) key_info = nullptr;
6051 const Key_map keys_to_keep(s->keys);
6052 for (auto reg_field = field; *reg_field; reg_field++) {
6053 auto f = *reg_field;
6054 f->key_start.intersect(keys_to_keep);
6055 f->part_of_key.intersect(keys_to_keep);
6056 if (f->part_of_key.is_clear_all()) f->clear_flag(PART_KEY_FLAG);
6057 f->part_of_sortkey.intersect(keys_to_keep);
6058 }
6059
6060 // Eliminate unused keys; make other keys visible
6061 covering_keys.intersect(keys_to_keep);
6062 for (uint keyno = 0; keyno < s->keys; keyno++)
6063 if (key_info[keyno].actual_key_parts == s->fields)
6064 covering_keys.set_bit(keyno);
6065 keys_in_use_for_group_by.set_prefix(s->keys);
6066 keys_in_use_for_order_by.set_prefix(s->keys);
6067 }
6068
set_keyread(bool flag)6069 void TABLE::set_keyread(bool flag) {
6070 DBUG_ASSERT(file);
6071 if (flag && !key_read) {
6072 key_read = true;
6073 if (is_created()) file->ha_extra(HA_EXTRA_KEYREAD);
6074 } else if (!flag && key_read) {
6075 key_read = false;
6076 if (is_created()) file->ha_extra(HA_EXTRA_NO_KEYREAD);
6077 }
6078 }
6079
set_created()6080 void TABLE::set_created() {
6081 if (created) return;
6082 if (key_read) file->ha_extra(HA_EXTRA_KEYREAD);
6083 created = true;
6084 }
6085
6086 /*
6087 Mark columns the handler needs for doing an insert
6088
6089 For now, this is used to mark fields used by the trigger
6090 as changed.
6091 */
6092
mark_columns_needed_for_insert(THD * thd)6093 void TABLE::mark_columns_needed_for_insert(THD *thd) {
6094 mark_columns_per_binlog_row_image(thd);
6095
6096 if (found_next_number_field) mark_auto_increment_column();
6097 /* Mark all generated columns as writable */
6098 if (vfield) mark_generated_columns(false);
6099 /* Mark columns needed for check constraints evaluation */
6100 if (table_check_constraint_list != nullptr)
6101 mark_check_constraint_columns(false);
6102 }
6103
6104 /**
6105 @brief Update the write/read_set for generated columns
6106 when doing update and insert operation.
6107
6108 @param is_update true means the operation is UPDATE.
6109 false means it's INSERT.
6110
6111
6112 Prerequisites for INSERT:
6113
6114 - write_map is filled with all base columns.
6115
6116 - read_map is filled with base columns and generated columns to be read.
6117 Otherwise, it is empty. covering_keys and merge_keys are adjusted according
6118 to read_map.
6119
6120 Actions for INSERT:
6121
6122 - Fill write_map with all generated columns.
6123 Stored columns are needed because their values will be stored.
6124 Virtual columns are needed because their values must be checked against
6125 constraints and it might be referenced by latter generated columns.
6126
6127 - Fill read_map with base columns for all generated columns.
6128 This has no technical reason, but is required because the function that
6129 evaluates generated functions asserts that base columns are in the read_map.
6130 covering_keys and merge_keys are adjusted according to read_map.
6131
6132 Prerequisites for UPDATE:
6133
6134 - write_map is filled with base columns to be updated.
6135
6136 - read_map is filled with base columns and generated columns to be read
6137 prior to the row update. covering_keys and merge_keys are adjusted
6138 according to read_map.
6139
6140 Actions for UPDATE:
6141
6142 - Fill write_map with generated columns that are dependent on updated base
6143 columns and all virtual generated columns. Stored columns are needed because
6144 their values will be stored. Virtual columns are needed because their values
6145 must be checked against constraints and might be referenced by latter
6146 generated columns.
6147 */
6148
mark_generated_columns(bool is_update)6149 void TABLE::mark_generated_columns(bool is_update) {
6150 Field **vfield_ptr, *tmp_vfield;
6151 bool bitmap_updated = false;
6152
6153 if (is_update) {
6154 MY_BITMAP dependent_fields;
6155 my_bitmap_map
6156 bitbuf[bitmap_buffer_size(MAX_FIELDS) / sizeof(my_bitmap_map)];
6157 bitmap_init(&dependent_fields, bitbuf, s->fields);
6158
6159 for (vfield_ptr = vfield; *vfield_ptr; vfield_ptr++) {
6160 tmp_vfield = *vfield_ptr;
6161 DBUG_ASSERT(tmp_vfield->gcol_info && tmp_vfield->gcol_info->expr_item);
6162
6163 /*
6164 We need to evaluate the GC if:
6165 - it depends on any updated column
6166 - or it is virtual indexed, for example:
6167 * UPDATE changes the primary key's value, and the virtual index
6168 is a secondary index which includes the pk's value
6169 * the gcol is in a multi-column index, and UPDATE changes another
6170 column of this index
6171 * in both cases the entry in the index needs to change, so needs to
6172 be located first, for that the GC's value is needed.
6173 */
6174 if ((!tmp_vfield->stored_in_db && tmp_vfield->m_indexed) ||
6175 bitmap_is_overlapping(write_set,
6176 &tmp_vfield->gcol_info->base_columns_map)) {
6177 // The GC needs to be updated
6178 tmp_vfield->table->mark_column_used(tmp_vfield, MARK_COLUMNS_WRITE);
6179 // In order to update the new value, we have to read the old value
6180 tmp_vfield->table->mark_column_used(tmp_vfield, MARK_COLUMNS_READ);
6181 bitmap_updated = true;
6182 }
6183 }
6184 } else // Insert needs to evaluate all generated columns
6185 {
6186 for (vfield_ptr = vfield; *vfield_ptr; vfield_ptr++) {
6187 tmp_vfield = *vfield_ptr;
6188 DBUG_ASSERT(tmp_vfield->gcol_info && tmp_vfield->gcol_info->expr_item);
6189 tmp_vfield->table->mark_column_used(tmp_vfield, MARK_COLUMNS_WRITE);
6190 bitmap_updated = true;
6191 }
6192 }
6193
6194 if (bitmap_updated) file->column_bitmaps_signal();
6195 }
6196
6197 /**
6198 Update the read_map with columns needed for check constraint evaluation when
6199 doing update and insert operations.
6200
6201 The read_map is filled with the base columns and generated columns to be read
6202 to evaluate check constraints. Prerequisites for UPDATE is, write_map is
6203 filled with the base columns to be updated and generated columns that are
6204 dependent on updated base columns.
6205
6206 @param is_update true means the operation is UPDATE.
6207 false means it's INSERT.
6208 */
mark_check_constraint_columns(bool is_update)6209 void TABLE::mark_check_constraint_columns(bool is_update) {
6210 DBUG_ASSERT(table_check_constraint_list != nullptr);
6211
6212 bool bitmap_updated = false;
6213 for (Sql_table_check_constraint &tbl_cc : *table_check_constraint_list) {
6214 if (tbl_cc.is_enforced()) {
6215 /*
6216 For update operation, check constraint should be evaluated if it is
6217 dependent on any of the updated column.
6218 */
6219 if (is_update &&
6220 !bitmap_is_overlapping(write_set,
6221 &tbl_cc.value_generator()->base_columns_map))
6222 continue;
6223
6224 // Mark all the columns used in the check constraint.
6225 const MY_BITMAP *columns_map =
6226 &tbl_cc.value_generator()->base_columns_map;
6227 for (uint i = bitmap_get_first_set(columns_map); i != MY_BIT_NONE;
6228 i = bitmap_get_next_set(columns_map, i)) {
6229 DBUG_ASSERT(i < s->fields);
6230 mark_column_used(field[i], MARK_COLUMNS_READ);
6231 }
6232 bitmap_updated = true;
6233 }
6234 }
6235
6236 if (bitmap_updated) file->column_bitmaps_signal();
6237 }
6238
6239 /**
6240 Cleanup this table for re-execution.
6241
6242 */
6243
reinit_before_use(THD * thd)6244 void TABLE_LIST::reinit_before_use(THD *thd) {
6245 /*
6246 Reset old pointers to TABLEs: they are not valid since the tables
6247 were closed in the end of previous prepare or execute call.
6248 */
6249 table = nullptr;
6250
6251 /*
6252 Reset table_name and table_name_length for schema table.
6253 They are not valid as TABLEs were closed in the end of previous prepare
6254 or execute call.
6255 */
6256 if (schema_table_name) {
6257 table_name = schema_table_name;
6258 table_name_length = strlen(schema_table_name);
6259 }
6260
6261 /* Reset is_schema_table_processed value(needed for I_S tables */
6262 schema_table_state = NOT_PROCESSED;
6263
6264 mdl_request.ticket = nullptr;
6265
6266 if (is_recursive_reference() && select_lex)
6267 set_derived_unit(select_lex->recursive_dummy_unit);
6268
6269 /*
6270 Is this table part of a SECURITY DEFINER VIEW?
6271 */
6272 if (!prelocking_placeholder && view && view_suid && view_sctx) {
6273 /*
6274 The suid view needs to "login" again at this stage before privilege
6275 precheck is done. The THD::m_view_ctx list is used to keep track of the
6276 new authorized security context life time. When the THD is reset or
6277 destroyed the security context is safely logged out and and any Acl_maps
6278 returned to the Acl cache.
6279 */
6280 prepare_view_security_context(thd);
6281 thd->m_view_ctx_list.push_back(view_sctx);
6282 }
6283 }
6284
query_block_id() const6285 uint TABLE_LIST::query_block_id() const {
6286 if (!derived) return 0;
6287 return derived->first_select()->select_number;
6288 }
6289
query_block_id_for_explain() const6290 uint TABLE_LIST::query_block_id_for_explain() const {
6291 if (!derived) return 0;
6292 if (!m_common_table_expr || !m_common_table_expr->tmp_tables.size())
6293 return derived->first_select()->select_number;
6294 return m_common_table_expr->tmp_tables[0]
6295 ->derived_unit()
6296 ->first_select()
6297 ->select_number;
6298 }
6299
6300 /**
6301 Compiles the tagged hints list and fills up the bitmasks.
6302
6303 @param thd The current session.
6304 @param tbl the TABLE to operate on.
6305
6306 The parser collects the index hints for each table in a "tagged list"
6307 (TABLE_LIST::index_hints). Using the information in this tagged list
6308 this function sets the members st_table::keys_in_use_for_query,
6309 st_table::keys_in_use_for_group_by, st_table::keys_in_use_for_order_by,
6310 st_table::force_index, st_table::force_index_order,
6311 st_table::force_index_group and st_table::covering_keys.
6312
6313 Current implementation of the runtime does not allow mixing FORCE INDEX
6314 and USE INDEX, so this is checked here. Then the FORCE INDEX list
6315 (if non-empty) is appended to the USE INDEX list and a flag is set.
6316
6317 Multiple hints of the same kind are processed so that each clause
6318 is applied to what is computed in the previous clause.
6319 For example:
6320 USE INDEX (i1) USE INDEX (i2)
6321 is equivalent to
6322 USE INDEX (i1,i2)
6323 and means "consider only i1 and i2".
6324
6325 Similarly
6326 USE INDEX () USE INDEX (i1)
6327 is equivalent to
6328 USE INDEX (i1)
6329 and means "consider only the index i1"
6330
6331 It is OK to have the same index several times, e.g. "USE INDEX (i1,i1)" is
6332 not an error.
6333
6334 Different kind of hints (USE/FORCE/IGNORE) are processed in the following
6335 order:
6336 1. All indexes in USE (or FORCE) INDEX are added to the mask.
6337 2. All IGNORE INDEX
6338
6339 e.g. "USE INDEX i1, IGNORE INDEX i1, USE INDEX i1" will not use i1 at all
6340 as if we had "USE INDEX i1, USE INDEX i1, IGNORE INDEX i1".
6341
6342 @retval false No errors found.
6343 @retval true Found and reported an error.
6344 */
process_index_hints(const THD * thd,TABLE * tbl)6345 bool TABLE_LIST::process_index_hints(const THD *thd, TABLE *tbl) {
6346 /* initialize the result variables */
6347 tbl->keys_in_use_for_query = tbl->keys_in_use_for_group_by =
6348 tbl->keys_in_use_for_order_by = tbl->s->usable_indexes(thd);
6349
6350 /* index hint list processing */
6351 if (index_hints) {
6352 /* Temporary variables used to collect hints of each kind. */
6353 Key_map index_join[INDEX_HINT_FORCE + 1];
6354 Key_map index_order[INDEX_HINT_FORCE + 1];
6355 Key_map index_group[INDEX_HINT_FORCE + 1];
6356 Index_hint *hint;
6357 bool have_empty_use_join = false, have_empty_use_order = false,
6358 have_empty_use_group = false;
6359 List_iterator<Index_hint> iter(*index_hints);
6360
6361 /* iterate over the hints list */
6362 while ((hint = iter++)) {
6363 uint pos;
6364
6365 /* process empty USE INDEX () */
6366 if (hint->type == INDEX_HINT_USE && !hint->key_name.str) {
6367 if (hint->clause & INDEX_HINT_MASK_JOIN) {
6368 index_join[hint->type].clear_all();
6369 have_empty_use_join = true;
6370 }
6371 if (hint->clause & INDEX_HINT_MASK_ORDER) {
6372 index_order[hint->type].clear_all();
6373 have_empty_use_order = true;
6374 }
6375 if (hint->clause & INDEX_HINT_MASK_GROUP) {
6376 index_group[hint->type].clear_all();
6377 have_empty_use_group = true;
6378 }
6379 continue;
6380 }
6381
6382 /*
6383 Check if an index with the given name exists and get his offset in
6384 the keys bitmask for the table
6385 */
6386 if (tbl->s->keynames.type_names == nullptr ||
6387 (pos = find_type(&tbl->s->keynames, hint->key_name.str,
6388 hint->key_name.length, true)) <= 0 ||
6389 !tbl->s->key_info[pos - 1].is_visible) {
6390 my_error(ER_KEY_DOES_NOT_EXITS, MYF(0), hint->key_name.str, alias);
6391 return true;
6392 }
6393
6394 pos--;
6395
6396 /* add to the appropriate clause mask */
6397 if (hint->clause & INDEX_HINT_MASK_JOIN)
6398 index_join[hint->type].set_bit(pos);
6399 if (hint->clause & INDEX_HINT_MASK_ORDER)
6400 index_order[hint->type].set_bit(pos);
6401 if (hint->clause & INDEX_HINT_MASK_GROUP)
6402 index_group[hint->type].set_bit(pos);
6403 }
6404
6405 /* cannot mix USE INDEX and FORCE INDEX */
6406 if ((!index_join[INDEX_HINT_FORCE].is_clear_all() ||
6407 !index_order[INDEX_HINT_FORCE].is_clear_all() ||
6408 !index_group[INDEX_HINT_FORCE].is_clear_all()) &&
6409 (!index_join[INDEX_HINT_USE].is_clear_all() || have_empty_use_join ||
6410 !index_order[INDEX_HINT_USE].is_clear_all() || have_empty_use_order ||
6411 !index_group[INDEX_HINT_USE].is_clear_all() || have_empty_use_group)) {
6412 my_error(ER_WRONG_USAGE, MYF(0), index_hint_type_name[INDEX_HINT_USE],
6413 index_hint_type_name[INDEX_HINT_FORCE]);
6414 return true;
6415 }
6416
6417 /* process FORCE INDEX as USE INDEX with a flag */
6418 if (!index_order[INDEX_HINT_FORCE].is_clear_all()) {
6419 tbl->force_index_order = true;
6420 index_order[INDEX_HINT_USE].merge(index_order[INDEX_HINT_FORCE]);
6421 }
6422
6423 if (!index_group[INDEX_HINT_FORCE].is_clear_all()) {
6424 tbl->force_index_group = true;
6425 index_group[INDEX_HINT_USE].merge(index_group[INDEX_HINT_FORCE]);
6426 }
6427
6428 /*
6429 TODO: get rid of tbl->force_index (on if any FORCE INDEX is specified) and
6430 create tbl->force_index_join instead.
6431 Then use the correct force_index_XX instead of the global one.
6432 */
6433 if (!index_join[INDEX_HINT_FORCE].is_clear_all() ||
6434 tbl->force_index_group || tbl->force_index_order) {
6435 tbl->force_index = true;
6436 index_join[INDEX_HINT_USE].merge(index_join[INDEX_HINT_FORCE]);
6437 }
6438
6439 /* apply USE INDEX */
6440 if (!index_join[INDEX_HINT_USE].is_clear_all() || have_empty_use_join)
6441 tbl->keys_in_use_for_query.intersect(index_join[INDEX_HINT_USE]);
6442 if (!index_order[INDEX_HINT_USE].is_clear_all() || have_empty_use_order)
6443 tbl->keys_in_use_for_order_by.intersect(index_order[INDEX_HINT_USE]);
6444 if (!index_group[INDEX_HINT_USE].is_clear_all() || have_empty_use_group)
6445 tbl->keys_in_use_for_group_by.intersect(index_group[INDEX_HINT_USE]);
6446
6447 /* apply IGNORE INDEX */
6448 tbl->keys_in_use_for_query.subtract(index_join[INDEX_HINT_IGNORE]);
6449 tbl->keys_in_use_for_order_by.subtract(index_order[INDEX_HINT_IGNORE]);
6450 tbl->keys_in_use_for_group_by.subtract(index_group[INDEX_HINT_IGNORE]);
6451 }
6452
6453 /* make sure covering_keys don't include indexes disabled with a hint */
6454 tbl->covering_keys.intersect(tbl->keys_in_use_for_query);
6455 return false;
6456 }
6457
6458 /**
6459 Helper function which allows to allocate metadata lock request
6460 objects for all elements of table list.
6461 */
6462
init_mdl_requests(TABLE_LIST * table_list)6463 void init_mdl_requests(TABLE_LIST *table_list) {
6464 for (; table_list; table_list = table_list->next_global)
6465 MDL_REQUEST_INIT(&table_list->mdl_request, MDL_key::TABLE, table_list->db,
6466 table_list->table_name,
6467 mdl_type_for_dml(table_list->lock_descriptor().type),
6468 MDL_TRANSACTION);
6469 }
6470
6471 /**
6472 @returns true if view or derived table is mergeable, based on
6473 technical constraints.
6474 */
is_mergeable() const6475 bool TABLE_LIST::is_mergeable() const {
6476 if (!is_view_or_derived() || algorithm == VIEW_ALGORITHM_TEMPTABLE)
6477 return false;
6478 /*
6479 If the table's content is non-deterministic and the query references it
6480 multiple times, merging it has the risk of creating different contents.
6481 */
6482 Common_table_expr *cte = common_table_expr();
6483 if (cte != nullptr && cte->references.size() >= 2 &&
6484 derived->uncacheable & UNCACHEABLE_RAND)
6485 return false;
6486 return derived->is_mergeable();
6487 }
6488
materializable_is_const() const6489 bool TABLE_LIST::materializable_is_const() const {
6490 DBUG_ASSERT(uses_materialization());
6491 const SELECT_LEX_UNIT *unit = derived_unit();
6492 return unit->query_result()->estimated_rowcount <= 1 &&
6493 (unit->first_select()->active_options() &
6494 OPTION_NO_SUBQUERY_DURING_OPTIMIZATION) == 0;
6495 }
6496
6497 /**
6498 Return the number of leaf tables for a merged view.
6499 */
6500
leaf_tables_count() const6501 uint TABLE_LIST::leaf_tables_count() const {
6502 // Join nests are not permissible, except as merged views
6503 DBUG_ASSERT(nested_join == nullptr || is_merged());
6504 if (!is_merged()) // Base table or materialized view
6505 return 1;
6506
6507 uint count = 0;
6508 for (TABLE_LIST *tbl = merge_underlying_list; tbl; tbl = tbl->next_local)
6509 count += tbl->leaf_tables_count();
6510
6511 return count;
6512 }
6513
6514 /**
6515 @brief
6516 Retrieve number of rows in the table
6517
6518 @details
6519 Retrieve number of rows in the table referred by this TABLE_LIST and
6520 store it in the table's stats.records variable. If this TABLE_LIST refers
6521 to a materialized derived table/view, then the estimated number of rows of
6522 the derived table/view is used instead.
6523
6524 @return 0 ok
6525 @return non zero error
6526 */
6527
fetch_number_of_rows()6528 int TABLE_LIST::fetch_number_of_rows() {
6529 int error = 0;
6530 if (is_table_function()) {
6531 // FIXME: open question - there's no estimate for table function.
6532 // return arbitrary, non-zero number;
6533 table->file->stats.records = PLACEHOLDER_TABLE_ROW_ESTIMATE;
6534 } else if (uses_materialization()) {
6535 /*
6536 @todo: CostModel: This updates the stats.record value to the
6537 estimated number of records. This number is used when estimating
6538 the cost of a table scan for a heap table (ie. it helps producing
6539 a reasonable good cost estimate for heap tables). If the materialized
6540 table is stored in MyISAM, this number is not used in the cost estimate
6541 for table scan. The table scan cost for MyISAM thus always becomes
6542 the estimate for an empty table.
6543 */
6544 table->file->stats.records = derived->query_result()->estimated_rowcount;
6545 } else if (is_recursive_reference()) {
6546 /*
6547 Use the estimated row count of all query blocks before this one, as the
6548 table will contain, at least, the rows produced by those blocks.
6549 */
6550 table->file->stats.records =
6551 std::max(select_lex->master_unit()->query_result()->estimated_rowcount,
6552 // Recursive reference is never a const table
6553 (ha_rows)PLACEHOLDER_TABLE_ROW_ESTIMATE);
6554 } else
6555 error = table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
6556 return error;
6557 }
6558
6559 /**
6560 A helper function to add a derived key to the list of possible keys
6561
6562 @param derived_key_list list of all possible derived keys
6563 @param field referenced field
6564 @param ref_by_tbl the table that refers to given field
6565
6566 @details The possible key to be used for join with table with ref_by_tbl
6567 table map is extended to include 'field'. If ref_by_tbl == 0 then the key
6568 that includes all referred fields is extended.
6569
6570 @note
6571 Procedure of keys generation for result tables of materialized derived
6572 tables/views for allowing ref access to them.
6573
6574 A key is generated for each equi-join pair (derived table, another table).
6575 Each generated key consists of fields of derived table used in equi-join.
6576 Example:
6577
6578 @code
6579 SELECT * FROM (SELECT f1, f2, count(*) FROM t1 GROUP BY f1) tt JOIN
6580 t1 ON tt.f1=t1.f3 and tt.f2=t1.f4;
6581 @endcode
6582
6583 In this case for the derived table tt one key will be generated. It will
6584 consist of two parts f1 and f2.
6585 Example:
6586
6587 @code
6588 SELECT * FROM (SELECT f1, f2, count(*) FROM t1 GROUP BY f1) tt JOIN
6589 t1 ON tt.f1=t1.f3 JOIN
6590 t2 ON tt.f2=t2.f4;
6591 @endcode
6592
6593 In this case for the derived table tt two keys will be generated.
6594 One key over f1 field, and another key over f2 field.
6595 Currently optimizer may choose to use only one such key, thus the second
6596 one will be dropped after the range optimizer is finished.
6597 See also JOIN::finalize_derived_keys function.
6598 Example:
6599
6600 @code
6601 SELECT * FROM (SELECT f1, f2, count(*) FROM t1 GROUP BY f1) tt JOIN
6602 t1 ON tt.f1=a_function(t1.f3);
6603 @endcode
6604
6605 In this case for the derived table tt one key will be generated. It will
6606 consist of one field - f1.
6607 In all cases beside one-per-table keys one additional key is generated.
6608 It includes all fields referenced by other tables.
6609
6610 Implementation is split in three steps:
6611
6612 1. gather information on all used fields of derived tables/view and
6613 store it in lists of possible keys, one per a derived table/view.
6614 2. add keys to result tables of derived tables/view using info from above
6615 lists.
6616 (...Planner selects best key...)
6617 3. drop unused keys from the table.
6618
6619 The above procedure is implemented in 4 functions:
6620 1. TABLE_LIST::update_derived_keys()
6621 Create/extend list of possible keys for one derived
6622 table/view based on given field/used tables info.
6623 (Step one)
6624 2. JOIN::generate_derived_keys()
6625 This function is called from update_ref_and_keys
6626 when all possible info on keys is gathered and it's
6627 safe to add keys - no keys or key parts would be
6628 missed. Walk over list of derived tables/views and
6629 call to TABLE_LIST::generate_keys to actually
6630 generate keys. (Step two)
6631 3. TABLE_LIST::generate_keys()
6632 Walks over list of possible keys for this derived
6633 table/view to add keys to the result table.
6634 Calls to TABLE::add_tmp_key() to actually add
6635 keys (i.e. KEY objects in TABLE::key_info). (Step
6636 two)
6637 4. TABLE::add_tmp_key() Creates one index description according to given
6638 bitmap of used fields. (Step two)
6639 [ Planner runs and possibly chooses one key, stored in Key_use->key ]
6640 JOIN::finalize_derived_keys Walk over list of derived tables/views to
6641 destroy unused keys. (Step three)
6642
6643 This design is used for derived tables, views and CTEs. As a CTE
6644 can be multi-referenced, some points are worth noting:
6645
6646 ## Definitions
6647
6648 - let's call the CTE 'X'
6649 - Key creation/deletion happens in a window between the start of
6650 update_derived_keys() and the end of finalize_derived_keys().
6651
6652 ## Key array locking
6653
6654 - Evaluation of constant subqueries (and thus their optimization)
6655 may happen either before, inside, or after the window above:
6656 * an example of "before": `WHERE 1=(subq))`, due to optimize_cond()
6657 * an example of "inside": `WHERE col<>(subq)`, as make_join_plan()
6658 calls estimate_rowcount() which calls the range optimizer for <>, which
6659 evaluates subq
6660 * an example of "after": `WHERE key_col=(subq)`, due to
6661 create_ref_for_key().
6662 - let's say that a being-optimized query block 'QB1' is entering that
6663 window; other query blocks are QB2, etc; let's say (subq) above is QB2, a
6664 subquery of QB1.
6665 - While QB1 is in this window, it is possible, as we saw above, that QB2
6666 gets optimized. Because it is not safe to have two query blocks
6667 reading/writing possible keys for a same table at the same time, a locking
6668 mechanism is in place: TABLE_SHARE::owner_of_possible_tmp_keys is a record
6669 of which query block entered first the window for this table and hasn't left
6670 it yet; only that query block is allowed to read/write possible keys for
6671 this table.
6672
6673 ## Key array growth
6674
6675 - let's say that a being-optimized query block 'QB1' is entering the
6676 window; other query blocks are QB2 (not necessarily the same QB2 as in
6677 previous paragraph), etc.
6678 - let's call "local" the references to X in QB1, let's call "nonlocal" the
6679 ones in other query blocks. For example,
6680
6681 @code
6682 with X(n) as (select 1)
6683 select /+ QB_NAME(QB2) *_/ n from X as X2
6684 where X2.n = (select /+* QB_NAME(QB1) *_/ X1.n from X as X1)
6685 union
6686 select n+2 from X as X3;
6687 @endcode
6688
6689 QB1 owns the window, then X1 is local, X2 and X3 are nonlocal.
6690 - when QB1 enters the window, update_derived_keys() starts for the local
6691 reference X1, other references to X may already have keys,
6692 defined by previously optimized query blocks on their
6693 references (e.g. QB2 on X2). At that stage the TABLE_SHARE::key_info array is
6694 of size TABLE_SHARE::keys, and the TABLE_SHARE::first_unused_tmp_key member
6695 points to 'where any new key should be added in this array', so it's equal
6696 to TABLE_SHARE::keys. Let's call the keys defined by QB2 the "existing
6697 keys": they exist at this point and will continue to do so. X2 in QB2 is
6698 already set up to read with such key. Here's the key_info array, with cell 0
6699 to the left, "E" meaning "an existing key, created by previous
6700 optimizations", "-" meaning "an empty cell created by alloc_keys()".
6701
6702 @verbatim
6703 EEEEEEEEEE-----------
6704 ^ s->first_unused_keys
6705 ^ s->keys
6706 @endverbatim
6707
6708 - generate_keys() extends the key_info array and adds "possible" keys to the
6709 end. "Possible" is defined as "not yet existing", "might be dropped in the
6710 end". Even if a possible key is a duplicate of an existing key, it is
6711 added. TABLE_SHARE::keys is increased to include existing and possible
6712 keys. All TABLEs referencing X, local or not, are kept in sync (i.e. any
6713 possible key is added to all key_info arrays). But possible keys are set to
6714 be unusable by nonlocal references, so that the decision to drop those keys
6715 can be left to the window's owner. Key_info array now is ("P" means
6716 "possible key"):
6717
6718 @verbatim
6719 EEEEEEEEEEPPPPPPP---
6720 ^ s->first_unused_keys
6721 ^ s->keys
6722 @endverbatim
6723
6724
6725 - All possible keys are unused, at this stage.
6726 - Planner selects the best key for each local reference, among existing and
6727 possible keys, it is recorded in Key_use.
6728 - finalize_derived_keys() looks at local references, and gathers the list
6729 of (existing and possible) keys which the Planner has chosen for them. We
6730 call this list the list of locally-used keys, marked below with "!":
6731
6732 @verbatim
6733 ! ! !
6734 EEEEEEEEEEPPPPPPP---
6735 ^ s->first_unused_keys
6736 ^ s->keys
6737 @endverbatim
6738
6739 - Any possible key which isn't locally-used is unnecessary.
6740
6741 - finalize_derived_keys() re-organizes the possible locally-used keys and
6742 unnecessary keys, and does needed updates to TABLEs' bitmaps.
6743
6744 @verbatim
6745 ! !!
6746 EEEEEEEEEEPPPPPPP---
6747 ^ s->first_unused_keys
6748 ^ s->keys
6749 @endverbatim
6750
6751 The locally-used keys become existing keys and are made visible to nonlocal
6752 references. The unnecessary keys are chopped.
6753
6754 @verbatim
6755 ! !!
6756 EEEEEEEEEEEE-----
6757 ^ s->first_unused_keys
6758 ^ s->keys
6759 @endverbatim
6760
6761 - After that, another query block can be optimized.
6762 - So, query block after query block, optimization phases grow the key_info
6763 array.
6764 - If a reference is considered constant in a query block and the Optimizer
6765 decides to evaluate it, this triggers materialization (creation in engine),
6766 which freezes the key definition: other query blocks will not be allowed to
6767 add keys.
6768
6769 @retval true OOM
6770 @retval false otherwise
6771 */
6772
add_derived_key(List<Derived_key> & derived_key_list,Field * field,table_map ref_by_tbl)6773 static bool add_derived_key(List<Derived_key> &derived_key_list, Field *field,
6774 table_map ref_by_tbl) {
6775 uint key = 0;
6776 Derived_key *entry = nullptr;
6777 List_iterator<Derived_key> ki(derived_key_list);
6778
6779 /* Search for already existing possible key. */
6780 while ((entry = ki++)) {
6781 key++;
6782 if (ref_by_tbl) {
6783 /* Search for the entry for the specified table.*/
6784 if (entry->referenced_by & ref_by_tbl) break;
6785 } else {
6786 /*
6787 Search for the special entry that should contain fields referred
6788 from any table.
6789 */
6790 if (!entry->referenced_by) break;
6791 }
6792 }
6793 /* Add new possible key if nothing is found. */
6794 if (!entry) {
6795 THD *thd = field->table->in_use;
6796 key++;
6797 entry = new (thd->mem_root) Derived_key();
6798 if (!entry) return true;
6799 entry->referenced_by = ref_by_tbl;
6800 entry->used_fields.clear_all();
6801 if (derived_key_list.push_back(entry, thd->mem_root)) return true;
6802 }
6803 /* Don't create keys longer than REF access can use. */
6804 if (entry->used_fields.bits_set() < MAX_REF_PARTS) {
6805 field->part_of_key.set_bit(key - 1);
6806 field->set_flag(PART_KEY_FLAG);
6807 entry->used_fields.set_bit(field->field_index());
6808 }
6809 return false;
6810 }
6811
6812 /**
6813 @brief
6814 Update derived table's list of possible keys
6815
6816 @param thd session context
6817 @param field derived table's field to take part in a key
6818 @param values array of values. Each value combined with "field"
6819 forms an equality predicate.
6820 @param num_values number of elements in the array values
6821 @param[out] allocated true if key was allocated, false if unsupported
6822
6823 @details
6824 This function creates/extends a list of possible keys for this derived
6825 table/view. For each table used by a value from the 'values' array the
6826 corresponding possible key is extended to include the 'field'.
6827 If there is no such possible key, then it is created. field's
6828 part_of_key bitmaps are updated accordingly.
6829 @see add_derived_key
6830
6831 @returns false if success, true if error
6832 */
6833
update_derived_keys(THD * thd,Field * field,Item ** values,uint num_values,bool * allocated)6834 bool TABLE_LIST::update_derived_keys(THD *thd, Field *field, Item **values,
6835 uint num_values, bool *allocated) {
6836 *allocated = false;
6837 /*
6838 Don't bother with keys for CREATE VIEW, BLOB fields and fields with
6839 zero length.
6840 */
6841 if (thd->lex->is_ps_or_view_context_analysis() ||
6842 field->is_flag_set(BLOB_FLAG) || field->field_length == 0)
6843 return false;
6844
6845 const Sql_cmd *const cmd = thd->lex->m_sql_cmd;
6846
6847 // Secondary storage engines do not support use of indexes on derived tables
6848 if (cmd != nullptr && cmd->using_secondary_storage_engine()) return false;
6849
6850 /* Allow all keys to be used. */
6851 if (derived_key_list.elements == 0) table->keys_in_use_for_query.set_all();
6852
6853 for (uint i = 0; i < num_values; i++) {
6854 table_map tables = values[i]->used_tables() & ~PSEUDO_TABLE_BITS;
6855 if (!tables || values[i]->real_item()->type() != Item::FIELD_ITEM) continue;
6856 for (table_map tbl = 1; tables >= tbl; tbl <<= 1) {
6857 if (!(tables & tbl)) continue;
6858 if (add_derived_key(derived_key_list, field, tbl)) return true;
6859 }
6860 }
6861 /* Extend key which includes all referenced fields. */
6862 if (add_derived_key(derived_key_list, field, (table_map)0)) return true;
6863 *allocated = true;
6864
6865 return false;
6866 }
6867
6868 /*
6869 Comparison function for Derived_key entries.
6870 See TABLE_LIST::generate_keys.
6871 */
6872
Derived_key_comp(Derived_key * e1,Derived_key * e2)6873 static int Derived_key_comp(Derived_key *e1, Derived_key *e2) {
6874 /* Move entries for tables with greater table bit to the end. */
6875 return ((e1->referenced_by < e2->referenced_by)
6876 ? -1
6877 : ((e1->referenced_by > e2->referenced_by) ? 1 : 0));
6878 }
6879
6880 /**
6881 @brief
6882 Generate keys for a materialized derived table/view.
6883 @details
6884 This function adds keys to the result table by walking over the list of
6885 possible keys for this derived table/view and calling the
6886 TABLE::add_tmp_key to actually add keys. A name @<auto_keyN@>, where N is a
6887 sequential number, is given to each key to ease debugging.
6888 @see add_derived_key
6889
6890 @return true an error occur.
6891 @return false all keys were successfully added.
6892 */
6893
generate_keys()6894 bool TABLE_LIST::generate_keys() {
6895 DBUG_ASSERT(uses_materialization());
6896
6897 if (!derived_key_list.elements) return false;
6898
6899 Derived_refs_iterator ref_it(this);
6900 while (TABLE *t = ref_it.get_next())
6901 if (t->is_created()) {
6902 /*
6903 The table may have been instantiated already, by another query
6904 block. Consider:
6905 with qn as (...) select * from qn where a=(select * from qn)
6906 union select * from qn where b=3;
6907 Then the scalar subquery is non-correlated, and cache-able, so the
6908 optimization phase of the first UNION member evaluates this subquery,
6909 which instantiates qn, then this phase may want to add an index on 'a'
6910 (for 'a=') but it's too late. Or the upcoming optimization phase for
6911 the second UNION member may want to add an index on 'b'.
6912 */
6913 return false;
6914 }
6915
6916 if (table->s->owner_of_possible_tmp_keys != nullptr &&
6917 table->s->owner_of_possible_tmp_keys != select_lex)
6918 return false;
6919
6920 // Extend the key array of every reference
6921
6922 const int new_key_count =
6923 std::min(table->s->keys + derived_key_list.elements, MAX_INDEXES);
6924 ref_it.rewind();
6925 while (TABLE *t = ref_it.get_next())
6926 if (t->alloc_tmp_keys(new_key_count, ref_it.is_first()))
6927 return true; /* purecov: inspected */
6928
6929 /* Sort entries to make key numbers sequence deterministic. */
6930 derived_key_list.sort(Derived_key_comp);
6931
6932 List_iterator<Derived_key> it(derived_key_list);
6933 Derived_key *entry;
6934 char buf[NAME_CHAR_LEN];
6935
6936 while ((entry = it++)) {
6937 if (table->s->keys == MAX_INDEXES)
6938 break; // Impossible to create more keys.
6939 sprintf(buf, "<auto_key%d>", table->s->keys);
6940 char *name_buf = table->in_use->mem_strdup(buf);
6941 ref_it.rewind();
6942 while (TABLE *t = ref_it.get_next()) {
6943 if (t->add_tmp_key(&entry->used_fields, name_buf,
6944 t->pos_in_table_list->select_lex != select_lex,
6945 ref_it.is_first()))
6946 return true; /* purecov: inspected */
6947 }
6948 }
6949
6950 if (table->s->keys)
6951 table->s->owner_of_possible_tmp_keys = select_lex; // Acquire lock
6952
6953 return false;
6954 }
6955
6956 /**
6957 Update TABLE::const_key_parts for single table UPDATE/DELETE query
6958
6959 @param conds WHERE clause expression
6960
6961 @retval true error (OOM)
6962 @retval false success
6963
6964 @note
6965 Set const_key_parts bits if key fields are equal to constants in
6966 the WHERE expression.
6967 */
6968
update_const_key_parts(Item * conds)6969 bool TABLE::update_const_key_parts(Item *conds) {
6970 memset(const_key_parts, 0, sizeof(key_part_map) * s->keys);
6971
6972 if (conds == nullptr) return false;
6973
6974 for (uint index = 0; index < s->keys; index++) {
6975 KEY_PART_INFO *keyinfo = key_info[index].key_part;
6976 KEY_PART_INFO *keyinfo_end =
6977 keyinfo + key_info[index].user_defined_key_parts;
6978
6979 for (key_part_map part_map = (key_part_map)1; keyinfo < keyinfo_end;
6980 keyinfo++, part_map <<= 1) {
6981 if (const_expression_in_where(conds, nullptr, keyinfo->field))
6982 const_key_parts[index] |= part_map;
6983 }
6984 }
6985
6986 /*
6987 Handle error for the whole function here instead of along with the call for
6988 const_expression_in_where() as the function does not return true for errors.
6989 */
6990 return this->in_use && this->in_use->is_error();
6991 }
6992
6993 /**
6994 Read removal is possible if the selected quick read
6995 method is using full unique index
6996
6997 @see HA_READ_BEFORE_WRITE_REMOVAL
6998
6999 @param index Number of the index used for read
7000
7001 @retval true success, read removal started
7002 @retval false read removal not started
7003 */
7004
check_read_removal(uint index)7005 bool TABLE::check_read_removal(uint index) {
7006 bool retval = false;
7007
7008 DBUG_TRACE;
7009 DBUG_ASSERT(file->ha_table_flags() & HA_READ_BEFORE_WRITE_REMOVAL);
7010 DBUG_ASSERT(index != MAX_KEY);
7011
7012 // Index must be unique
7013 if ((key_info[index].flags & HA_NOSAME) == 0) return false;
7014
7015 // Full index must be used
7016 bitmap_clear_all(&tmp_set);
7017 mark_columns_used_by_index_no_reset(index, &tmp_set);
7018
7019 if (bitmap_cmp(&tmp_set, read_set)) {
7020 // Start read removal in handler
7021 retval = file->start_read_removal();
7022 }
7023
7024 bitmap_clear_all(&tmp_set);
7025 return retval;
7026 }
7027
7028 /**
7029 Test if the order list consists of simple field expressions
7030
7031 @param order Linked list of ORDER BY arguments
7032
7033 @return true if @a order is empty or consist of simple field expressions
7034 */
7035
is_simple_order(ORDER * order)7036 bool is_simple_order(ORDER *order) {
7037 for (ORDER *ord = order; ord; ord = ord->next) {
7038 if (ord->item[0]->real_item()->type() != Item::FIELD_ITEM) return false;
7039 }
7040 return true;
7041 }
7042
7043 /**
7044 Repoint a table's fields from old_rec to new_rec
7045
7046 @param table the table of fields needed to be repointed
7047 @param old_rec the original record buffer fields point to
7048 @param new_rec the target record buff fields need to repoint
7049 */
7050
repoint_field_to_record(TABLE * table,uchar * old_rec,uchar * new_rec)7051 void repoint_field_to_record(TABLE *table, uchar *old_rec, uchar *new_rec) {
7052 Field **fields = table->field;
7053 ptrdiff_t ptrdiff = new_rec - old_rec;
7054 for (uint i = 0; i < table->s->fields; i++)
7055 fields[i]->move_field_offset(ptrdiff);
7056 }
7057
7058 /**
7059 Evaluate necessary virtual generated columns.
7060 This is used right after reading a row from the storage engine.
7061
7062 @note this is not necessary for stored generated columns, as they are
7063 provided by the storage engine.
7064
7065 @param [in,out] buf the buffer to store data
7066 @param table the TABLE object
7067 @param active_index the number of key for index scan (MAX_KEY is default)
7068
7069 @return true if error.
7070
7071 @todo see below for potential conflict with Bug#21815348 .
7072 */
update_generated_read_fields(uchar * buf,TABLE * table,uint active_index)7073 bool update_generated_read_fields(uchar *buf, TABLE *table, uint active_index) {
7074 DBUG_TRACE;
7075 DBUG_ASSERT(table && table->vfield);
7076 if (table->in_use->is_error()) return true;
7077 if (active_index != MAX_KEY && table->key_read) {
7078 /*
7079 The covering index is providing all necessary columns, including
7080 generated ones.
7081 Note that this logic may have to be reconsidered when we fix
7082 Bug#21815348; indeed, for that bug it could be possible to implement the
7083 following optimization: if A is an indexed base column, and B is a
7084 virtual generated column dependent on A, "select B from t" could choose
7085 an index-only scan over the index of A and calculate values of B on the
7086 fly. In that case, we would come here, however calculation of B would
7087 still be needed.
7088 Currently MySQL doesn't choose an index scan in that case because it
7089 considers B as independent from A, in its index-scan decision logic.
7090 */
7091 return false;
7092 }
7093
7094 int error = 0;
7095
7096 /*
7097 If the buffer storing the record data is not record[0], then the field
7098 objects must be temporarily changed to point into the supplied buffer.
7099 The field pointers are restored at the end of this function.
7100 */
7101 if (buf != table->record[0])
7102 repoint_field_to_record(table, table->record[0], buf);
7103
7104 for (Field **vfield_ptr = table->vfield; *vfield_ptr; vfield_ptr++) {
7105 Field *vfield = *vfield_ptr;
7106 DBUG_ASSERT(vfield->gcol_info && vfield->gcol_info->expr_item);
7107 /*
7108 Only calculate those virtual generated fields that are marked in the
7109 read_set bitmap.
7110 */
7111 if (vfield->is_virtual_gcol() &&
7112 bitmap_is_set(table->read_set, vfield->field_index())) {
7113 if (vfield->handle_old_value()) {
7114 (down_cast<Field_blob *>(vfield))->keep_old_value();
7115 (down_cast<Field_blob *>(vfield))->set_keep_old_value(true);
7116 }
7117
7118 error = vfield->gcol_info->expr_item->save_in_field(vfield, false);
7119 DBUG_PRINT("info", ("field '%s' - updated", vfield->field_name));
7120 if (error && !table->in_use->is_error()) {
7121 /*
7122 Most likely a calculation error which only triggered a warning, so
7123 let's not make the read fail.
7124 */
7125 error = 0;
7126 }
7127 } else {
7128 DBUG_PRINT("info", ("field '%s' - skipped", vfield->field_name));
7129 }
7130 }
7131
7132 if (buf != table->record[0])
7133 repoint_field_to_record(table, buf, table->record[0]);
7134
7135 return error != 0;
7136 /*
7137 @todo
7138 this function is used by ha_rnd/etc, those ha_* functions are expected to
7139 return 0 or a HA_ERR code (and such codes are picked up by
7140 handler::print_error), but update_generated_read_fields returns true/false
7141 (0/1), which is then returned by the ha_* functions. If it
7142 returns 1 we get:
7143 ERROR 1030 (HY000): Got error 1 from storage engine
7144 which isn't informative for the user.
7145 */
7146 }
7147
7148 /**
7149 Calculate data for each generated field marked for write in the
7150 corresponding column map.
7151
7152 @note We need calculate data for both virtual and stored generated
7153 fields.
7154
7155 @param bitmap Bitmap over fields to update
7156 @param table the TABLE object
7157
7158 @retval false Success
7159 @retval true Error occurred during the generation/calculation of a generated
7160 field value
7161 */
update_generated_write_fields(const MY_BITMAP * bitmap,TABLE * table)7162 bool update_generated_write_fields(const MY_BITMAP *bitmap, TABLE *table) {
7163 DBUG_TRACE;
7164 Field **field_ptr;
7165 int error = 0;
7166
7167 if (table->in_use->is_error()) return true;
7168
7169 if (table->vfield) {
7170 /* Iterate over generated fields in the table */
7171 for (field_ptr = table->vfield; *field_ptr; field_ptr++) {
7172 Field *vfield;
7173 vfield = (*field_ptr);
7174 DBUG_ASSERT(vfield->gcol_info && vfield->gcol_info->expr_item);
7175
7176 /* Only update those fields that are marked in the bitmap */
7177 if (bitmap_is_set(bitmap, vfield->field_index())) {
7178 /*
7179 For a virtual generated column of blob type, we have to keep
7180 the current blob value since this might be needed by the
7181 storage engine during updates.
7182 All arrays are BLOB fields.
7183 */
7184 if (vfield->handle_old_value()) {
7185 (down_cast<Field_blob *>(vfield))->keep_old_value();
7186 (down_cast<Field_blob *>(vfield))->set_keep_old_value(true);
7187 }
7188
7189 /* Generate the actual value of the generated fields */
7190 error = vfield->gcol_info->expr_item->save_in_field(vfield, false);
7191
7192 DBUG_PRINT("info", ("field '%s' - updated", vfield->field_name));
7193 if (error && !table->in_use->is_error()) error = 0;
7194 if (table->fields_set_during_insert)
7195 bitmap_set_bit(table->fields_set_during_insert,
7196 vfield->field_index());
7197 } else {
7198 DBUG_PRINT("info", ("field '%s' - skipped", vfield->field_name));
7199 }
7200 }
7201 }
7202
7203 if (error > 0) return true;
7204 return false;
7205 }
7206
7207 /**
7208 Adds a generated column and its dependencies to the read_set/write_set
7209 bitmaps.
7210
7211 If the value of a generated column (gcol) must be calculated, it needs to
7212 be in write_set (to satisfy the assertion in Field::store); the value of
7213 its underlying base columns is necessary to the calculation so those must
7214 be in read_set.
7215
7216 A gcol must be calculated in two cases:
7217 - we're sending the gcol to the engine
7218 - the gcol is virtual and we're reading it from the engine without using a
7219 covering index on it.
7220 */
mark_gcol_in_maps(const Field * field)7221 void TABLE::mark_gcol_in_maps(const Field *field) {
7222 bitmap_set_bit(write_set, field->field_index());
7223
7224 /*
7225 Typed array fields internally are using a conversion field, it needs to
7226 marked as readable in order to do conversions.
7227 */
7228 if (field->is_array()) bitmap_set_bit(read_set, field->field_index());
7229
7230 /*
7231 Note that underlying base columns are here added to read_set but not added
7232 to requirements for an index to be covering (covering_keys is not touched).
7233 So, if we have:
7234 SELECT gcol FROM t :
7235 - an index covering gcol only (not including base columns), can still be
7236 chosen by the optimizer; note that InnoDB's build_template_needs_field()
7237 properly ignores read_set when MySQL asks for "index only" reads
7238 (table->key_read == true); if it didn't, it would do useless reads.
7239 - but if gcol is not read from an index, we will read base columns because
7240 they are in read_set.
7241 - Note how this relies on InnoDB's behaviour.
7242 */
7243 for (uint i = 0; i < s->fields; i++) {
7244 if (bitmap_is_set(&field->gcol_info->base_columns_map, i)) {
7245 bitmap_set_bit(read_set, i);
7246 if (this->field[i]->is_virtual_gcol()) bitmap_set_bit(write_set, i);
7247 }
7248 }
7249 }
7250
column_bitmaps_set(MY_BITMAP * read_set_arg,MY_BITMAP * write_set_arg)7251 void TABLE::column_bitmaps_set(MY_BITMAP *read_set_arg,
7252 MY_BITMAP *write_set_arg) {
7253 read_set = read_set_arg;
7254 write_set = write_set_arg;
7255 if (file && created) file->column_bitmaps_signal();
7256 }
7257
set_recursive_reference()7258 bool TABLE_LIST::set_recursive_reference() {
7259 if (select_lex->recursive_reference != nullptr) return true;
7260 select_lex->recursive_reference = this;
7261 m_is_recursive_reference = true;
7262 return false;
7263 }
7264
7265 /**
7266 Propagate table map of a table up by nested join tree. Used to check
7267 dependencies for LATERAL JOIN of table functions.
7268 simplify_joins() calculates the same information but also does
7269 transformations, and we need this semantic check to be earlier than
7270 simplify_joins() and before transformations.
7271
7272 @param map_arg table map to propagate
7273 */
7274
propagate_table_maps(table_map map_arg)7275 void TABLE_LIST::propagate_table_maps(table_map map_arg) {
7276 table_map prop_map;
7277 if (nested_join) {
7278 nested_join->used_tables |= map_arg;
7279 prop_map = nested_join->used_tables;
7280 } else
7281 prop_map = map();
7282 if (embedding) embedding->propagate_table_maps(prop_map);
7283 }
7284
alloc(THD * thd,LEX_STRING * user_arg,LEX_STRING * host_arg)7285 LEX_USER *LEX_USER::alloc(THD *thd, LEX_STRING *user_arg,
7286 LEX_STRING *host_arg) {
7287 LEX_USER *ret = static_cast<LEX_USER *>(thd->alloc(sizeof(LEX_USER)));
7288 if (ret == nullptr) return nullptr;
7289 /*
7290 Trim whitespace as the values will go to a CHAR field
7291 when stored.
7292 */
7293 trim_whitespace(system_charset_info, user_arg);
7294 if (host_arg) trim_whitespace(system_charset_info, host_arg);
7295
7296 ret->user.str = user_arg->str;
7297 ret->user.length = user_arg->length;
7298 ret->host.str = host_arg ? host_arg->str : "%";
7299 ret->host.length = host_arg ? host_arg->length : 1;
7300 ret->plugin = EMPTY_CSTR;
7301 ret->auth = NULL_CSTR;
7302 ret->current_auth = NULL_CSTR;
7303 ret->uses_replace_clause = false;
7304 ret->uses_identified_by_clause = false;
7305 ret->uses_identified_with_clause = false;
7306 ret->uses_authentication_string_clause = false;
7307 ret->has_password_generator = false;
7308 ret->retain_current_password = false;
7309 ret->discard_old_password = false;
7310 ret->alter_status.account_locked = false;
7311 ret->alter_status.expire_after_days = 0;
7312 ret->alter_status.update_account_locked_column = false;
7313 ret->alter_status.update_password_expired_column = false;
7314 ret->alter_status.update_password_expired_fields = false;
7315 ret->alter_status.use_default_password_lifetime = true;
7316 ret->alter_status.use_default_password_history = true;
7317 ret->alter_status.update_password_require_current =
7318 Lex_acl_attrib_udyn::UNCHANGED;
7319 ret->alter_status.password_history_length = 0;
7320 ret->alter_status.password_reuse_interval = 0;
7321 ret->alter_status.failed_login_attempts = 0;
7322 ret->alter_status.password_lock_time = 0;
7323 ret->alter_status.update_failed_login_attempts = false;
7324 ret->alter_status.update_password_lock_time = false;
7325 if (check_string_char_length(ret->user, ER_THD(thd, ER_USERNAME),
7326 USERNAME_CHAR_LENGTH, system_charset_info,
7327 false) ||
7328 (host_arg && check_host_name(ret->host)))
7329 return nullptr;
7330 if (host_arg) {
7331 /*
7332 Convert hostname part of username to lowercase.
7333 It's OK to use in-place lowercase as long as
7334 the character set is utf8.
7335 */
7336 my_casedn_str(system_charset_info, host_arg->str);
7337 ret->host.str = host_arg->str;
7338 }
7339 return ret;
7340 }
7341
7342 /**
7343 A struct that contains execution time state used for partial update of JSON
7344 columns.
7345 */
7346 struct Partial_update_info {
Partial_update_infoPartial_update_info7347 Partial_update_info(const TABLE *table, const MY_BITMAP *columns,
7348 bool logical_diffs)
7349 : m_binary_diff_vectors(table->in_use->mem_root, table->s->fields,
7350 nullptr),
7351 m_logical_diff_vectors(table->in_use->mem_root,
7352 logical_diffs ? table->s->fields : 0, nullptr) {
7353 MEM_ROOT *const mem_root = table->in_use->mem_root;
7354 const size_t bitmap_size = table->s->column_bitmap_size;
7355
7356 auto buffer = static_cast<my_bitmap_map *>(mem_root->Alloc(bitmap_size));
7357 if (buffer != nullptr) {
7358 bitmap_init(&m_enabled_binary_diff_columns, buffer, table->s->fields);
7359 bitmap_copy(&m_enabled_binary_diff_columns, columns);
7360 }
7361
7362 buffer = static_cast<my_bitmap_map *>(mem_root->Alloc(bitmap_size));
7363 if (buffer != nullptr) {
7364 bitmap_init(&m_enabled_logical_diff_columns, buffer, table->s->fields);
7365 if (logical_diffs)
7366 bitmap_copy(&m_enabled_logical_diff_columns, columns);
7367 else
7368 bitmap_clear_all(&m_enabled_logical_diff_columns);
7369 }
7370
7371 for (uint i = bitmap_get_first_set(columns); i != MY_BIT_NONE;
7372 i = bitmap_get_next_set(columns, i)) {
7373 m_binary_diff_vectors[i] = new (mem_root) Binary_diff_vector(mem_root);
7374
7375 if (logical_diffs) {
7376 Json_diff_vector::allocator_type alloc(mem_root);
7377 m_logical_diff_vectors[i] = new (mem_root) Json_diff_vector(alloc);
7378 }
7379 }
7380 }
7381
~Partial_update_infoPartial_update_info7382 ~Partial_update_info() {
7383 for (auto v : m_logical_diff_vectors) destroy(v);
7384 }
7385
7386 /**
7387 The columns for which partial update using binary diffs is enabled
7388 in the current row.
7389 */
7390 MY_BITMAP m_enabled_binary_diff_columns;
7391
7392 /**
7393 The columns for which partial update using logical JSON diffs is
7394 enabled in the current row.
7395 */
7396 MY_BITMAP m_enabled_logical_diff_columns;
7397
7398 /**
7399 The binary diffs that have been collected for the current row.
7400
7401 The Binary_diff_vector objects live entirely in a MEM_ROOT, so
7402 there is no need to destroy them when this object is destroyed.
7403 */
7404 Mem_root_array<Binary_diff_vector *> m_binary_diff_vectors;
7405
7406 /**
7407 The logical diffs that have been collected for JSON operations in
7408 the current row.
7409
7410 Whereas the Json_diff_vector objects live in a MEM_ROOT and their
7411 memory will be reclaimed automatically, the Json_diff objects
7412 within them can own memory allocated on the heap, so they will
7413 have to be destroyed when this object is destroyed.
7414 */
7415 Mem_root_array<Json_diff_vector *> m_logical_diff_vectors;
7416
7417 /**
7418 A buffer that can be used to hold the partially updated column value while
7419 performing the update in memory.
7420 */
7421 String m_buffer;
7422
7423 /// Should logical JSON diffs be collected in addition to binary diffs?
collect_logical_diffsPartial_update_info7424 bool collect_logical_diffs() const {
7425 /*
7426 We only allocate logical diff vectors when we want logical diffs
7427 to be collected, so check if we have any.
7428 */
7429 return !m_logical_diff_vectors.empty();
7430 }
7431 };
7432
mark_column_for_partial_update(const Field * field)7433 bool TABLE::mark_column_for_partial_update(const Field *field) {
7434 DBUG_ASSERT(field->table == this);
7435 if (m_partial_update_columns == nullptr) {
7436 MY_BITMAP *map = new (&mem_root) MY_BITMAP;
7437 my_bitmap_map *buf =
7438 static_cast<my_bitmap_map *>(mem_root.Alloc(s->column_bitmap_size));
7439 if (map == nullptr || buf == nullptr || bitmap_init(map, buf, s->fields))
7440 return true; /* purecov: inspected */
7441 m_partial_update_columns = map;
7442 }
7443
7444 bitmap_set_bit(m_partial_update_columns, field->field_index());
7445 return false;
7446 }
7447
disable_binary_diffs_for_current_row(const Field * field)7448 void TABLE::disable_binary_diffs_for_current_row(const Field *field) {
7449 DBUG_ASSERT(field->table == this);
7450 DBUG_ASSERT(is_binary_diff_enabled(field));
7451
7452 // Remove the diffs collected for the column.
7453 m_partial_update_info->m_binary_diff_vectors[field->field_index()]->clear();
7454
7455 // Mark the column as disabled.
7456 bitmap_clear_bit(&m_partial_update_info->m_enabled_binary_diff_columns,
7457 field->field_index());
7458 }
7459
is_marked_for_partial_update(const Field * field) const7460 bool TABLE::is_marked_for_partial_update(const Field *field) const {
7461 DBUG_ASSERT(field->table == this);
7462 return m_partial_update_columns != nullptr &&
7463 bitmap_is_set(m_partial_update_columns, field->field_index());
7464 }
7465
has_binary_diff_columns() const7466 bool TABLE::has_binary_diff_columns() const {
7467 return m_partial_update_info != nullptr &&
7468 !bitmap_is_clear_all(
7469 &m_partial_update_info->m_enabled_binary_diff_columns);
7470 }
7471
setup_partial_update(bool logical_diffs)7472 bool TABLE::setup_partial_update(bool logical_diffs) {
7473 DBUG_TRACE;
7474 DBUG_ASSERT(m_partial_update_info == nullptr);
7475
7476 if (!has_columns_marked_for_partial_update()) return false;
7477
7478 Opt_trace_context *trace = &in_use->opt_trace;
7479 if (trace->is_started()) {
7480 Opt_trace_object trace_wrapper(trace);
7481 Opt_trace_object trace_partial_update(trace, "json_partial_update");
7482 trace_partial_update.add_utf8_table(pos_in_table_list);
7483 Opt_trace_array columns(trace, "eligible_columns");
7484 for (uint i = bitmap_get_first_set(m_partial_update_columns);
7485 i != MY_BIT_NONE;
7486 i = bitmap_get_next_set(m_partial_update_columns, i)) {
7487 columns.add_utf8(s->field[i]->field_name);
7488 }
7489 }
7490
7491 m_partial_update_info = new (in_use->mem_root)
7492 Partial_update_info(this, m_partial_update_columns, logical_diffs);
7493 return in_use->is_error();
7494 }
7495
setup_partial_update()7496 bool TABLE::setup_partial_update() {
7497 bool logical_diffs = (in_use->variables.binlog_row_value_options &
7498 PARTIAL_JSON_UPDATES) != 0 &&
7499 mysql_bin_log.is_open() &&
7500 (in_use->variables.option_bits & OPTION_BIN_LOG) != 0 &&
7501 log_bin_use_v1_row_events == 0 &&
7502 in_use->is_current_stmt_binlog_format_row();
7503 DBUG_PRINT(
7504 "info",
7505 ("TABLE::setup_partial_update(): logical_diffs=%d "
7506 "because binlog_row_value_options=%d binlog.is_open=%d "
7507 "sql_log_bin=%d use_v1_row_events=%d rbr=%d",
7508 logical_diffs,
7509 (in_use->variables.binlog_row_value_options & PARTIAL_JSON_UPDATES) != 0,
7510 mysql_bin_log.is_open(),
7511 (in_use->variables.option_bits & OPTION_BIN_LOG) != 0,
7512 log_bin_use_v1_row_events, in_use->is_current_stmt_binlog_format_row()));
7513 return setup_partial_update(logical_diffs);
7514 }
7515
has_columns_marked_for_partial_update() const7516 bool TABLE::has_columns_marked_for_partial_update() const {
7517 /*
7518 Do we have any columns that satisfy the syntactical requirements for
7519 partial update?
7520 */
7521 return m_partial_update_columns != nullptr &&
7522 !bitmap_is_clear_all(m_partial_update_columns);
7523 }
7524
cleanup_partial_update()7525 void TABLE::cleanup_partial_update() {
7526 DBUG_TRACE;
7527 destroy(m_partial_update_info);
7528 m_partial_update_info = nullptr;
7529 }
7530
get_partial_update_buffer()7531 String *TABLE::get_partial_update_buffer() {
7532 DBUG_ASSERT(m_partial_update_info != nullptr);
7533 return &m_partial_update_info->m_buffer;
7534 }
7535
clear_partial_update_diffs()7536 void TABLE::clear_partial_update_diffs() {
7537 DBUG_TRACE;
7538 if (m_partial_update_info != nullptr) {
7539 for (auto v : m_partial_update_info->m_binary_diff_vectors)
7540 if (v != nullptr) v->clear();
7541
7542 bitmap_copy(&m_partial_update_info->m_enabled_binary_diff_columns,
7543 m_partial_update_columns);
7544
7545 if (m_partial_update_info->collect_logical_diffs()) {
7546 for (auto v : m_partial_update_info->m_logical_diff_vectors)
7547 if (v != nullptr) v->clear();
7548
7549 bitmap_copy(&m_partial_update_info->m_enabled_logical_diff_columns,
7550 m_partial_update_columns);
7551 }
7552 }
7553 }
7554
get_binary_diffs(const Field * field) const7555 const Binary_diff_vector *TABLE::get_binary_diffs(const Field *field) const {
7556 if (!is_binary_diff_enabled(field)) return nullptr;
7557 return m_partial_update_info->m_binary_diff_vectors[field->field_index()];
7558 }
7559
add_binary_diff(const Field * field,size_t offset,size_t length)7560 bool TABLE::add_binary_diff(const Field *field, size_t offset, size_t length) {
7561 DBUG_ASSERT(is_binary_diff_enabled(field));
7562
7563 Binary_diff_vector *diffs =
7564 m_partial_update_info->m_binary_diff_vectors[field->field_index()];
7565
7566 /*
7567 Find the first diff that does not end before the diff we want to insert.
7568 That is, we find the first diff that is either overlapping with the diff we
7569 want to insert, adjacent to the diff we want to insert, or comes after the
7570 diff that we want to insert.
7571
7572 In the case of overlapping or adjacent diffs, we want to merge the diffs
7573 rather than insert a new one.
7574 */
7575 Binary_diff_vector::iterator first_it =
7576 std::lower_bound(diffs->begin(), diffs->end(), offset,
7577 [](const Binary_diff &diff, size_t start_offset) {
7578 return diff.offset() + diff.length() < start_offset;
7579 });
7580
7581 if (first_it != diffs->end() && first_it->offset() <= offset + length) {
7582 /*
7583 The diff we found was overlapping or adjacent, so we want to merge the
7584 new diff with it. Find out if the new diff overlaps with or borders to
7585 some of the diffs behind it. The call below finds the first diff after
7586 first_it that is not overlapping with or adjacent to the new diff.
7587 */
7588 Binary_diff_vector::const_iterator last_it =
7589 std::upper_bound(first_it, diffs->end(), offset + length,
7590 [](size_t end_offset, const Binary_diff &diff) {
7591 return end_offset < diff.offset();
7592 });
7593
7594 // First and last adjacent or overlapping diff. They can be the same one.
7595 const Binary_diff &first_diff = *first_it;
7596 const Binary_diff &last_diff = *(last_it - 1);
7597
7598 // Calculate the boundaries of the merged diff.
7599 size_t beg = std::min(offset, first_diff.offset());
7600 size_t end =
7601 std::max(offset + length, last_diff.offset() + last_diff.length());
7602
7603 /*
7604 Replace the first overlapping/adjacent diff with the merged diff, and
7605 erase any subsequent diffs that are covered by the merged diff.
7606 */
7607 *first_it = Binary_diff(beg, end - beg);
7608 diffs->erase(first_it + 1, last_it);
7609 return false;
7610 }
7611
7612 /*
7613 The new diff isn't overlapping with or adjacent to any of the existing
7614 diffs. Just insert it.
7615 */
7616 diffs->insert(first_it, Binary_diff(offset, length));
7617 return false;
7618 }
7619
new_data(const Field * field) const7620 const char *Binary_diff::new_data(const Field *field) const {
7621 /*
7622 Currently, partial update is only supported for JSON columns, so it's
7623 safe to assume that the Field is in fact a Field_json.
7624 */
7625 auto fld = down_cast<const Field_json *>(field);
7626 return fld->get_binary() + m_offset;
7627 }
7628
old_data(const Field * field) const7629 const char *Binary_diff::old_data(const Field *field) const {
7630 ptrdiff_t ptrdiff = field->table->record[1] - field->table->record[0];
7631 auto fld = down_cast<const Field_json *>(field);
7632 return fld->get_binary(ptrdiff) + m_offset;
7633 }
7634
add_logical_diff(const Field_json * field,const Json_seekable_path & path,enum_json_diff_operation operation,const Json_wrapper * new_value)7635 void TABLE::add_logical_diff(const Field_json *field,
7636 const Json_seekable_path &path,
7637 enum_json_diff_operation operation,
7638 const Json_wrapper *new_value) {
7639 DBUG_ASSERT(is_logical_diff_enabled(field));
7640 Json_diff_vector *diffs =
7641 m_partial_update_info->m_logical_diff_vectors[field->field_index()];
7642 if (new_value == nullptr)
7643 diffs->add_diff(path, operation);
7644 else {
7645 diffs->add_diff(path, operation,
7646 new_value->clone_dom(field->table->in_use));
7647 }
7648 #ifndef DBUG_OFF
7649 StringBuffer<STRING_BUFFER_USUAL_SIZE> path_str;
7650 StringBuffer<STRING_BUFFER_USUAL_SIZE> value_str;
7651 if (diffs->at(diffs->size() - 1).path().to_string(&path_str))
7652 path_str.length(0); /* purecov: inspected */
7653 if (new_value == nullptr || new_value->type() == enum_json_type::J_ERROR)
7654 value_str.set_ascii("<none>", 6);
7655 else {
7656 if (new_value->to_string(&value_str, false, "add_logical_diff"))
7657 value_str.length(0); /* purecov: inspected */
7658 }
7659 DBUG_PRINT("info", ("add_logical_diff(operation=%d, path=%.*s, value=%.*s)",
7660 (int)operation, (int)path_str.length(), path_str.ptr(),
7661 (int)value_str.length(), value_str.ptr()));
7662 #endif
7663 }
7664
get_logical_diffs(const Field_json * field) const7665 const Json_diff_vector *TABLE::get_logical_diffs(
7666 const Field_json *field) const {
7667 if (!is_logical_diff_enabled(field)) return nullptr;
7668 return m_partial_update_info->m_logical_diff_vectors[field->field_index()];
7669 }
7670
is_binary_diff_enabled(const Field * field) const7671 bool TABLE::is_binary_diff_enabled(const Field *field) const {
7672 return m_partial_update_info != nullptr &&
7673 bitmap_is_set(&m_partial_update_info->m_enabled_binary_diff_columns,
7674 field->field_index());
7675 }
7676
is_logical_diff_enabled(const Field * field) const7677 bool TABLE::is_logical_diff_enabled(const Field *field) const {
7678 DBUG_TRACE;
7679 bool ret =
7680 m_partial_update_info != nullptr &&
7681 bitmap_is_set(&m_partial_update_info->m_enabled_logical_diff_columns,
7682 field->field_index());
7683 DBUG_PRINT("info",
7684 ("field=%s "
7685 "is_logical_diff_enabled returns=%d "
7686 "(m_partial_update_info!=NULL)=%d "
7687 "m_enabled_logical_diff_columns[column]=%s",
7688 field->field_name, ret, m_partial_update_info != nullptr,
7689 m_partial_update_info != nullptr
7690 ? (bitmap_is_set(
7691 &m_partial_update_info->m_enabled_logical_diff_columns,
7692 field->field_index())
7693 ? "1"
7694 : "0")
7695 : "unknown"));
7696 return ret;
7697 }
7698
disable_logical_diffs_for_current_row(const Field * field) const7699 void TABLE::disable_logical_diffs_for_current_row(const Field *field) const {
7700 DBUG_ASSERT(field->table == this);
7701 DBUG_ASSERT(is_logical_diff_enabled(field));
7702
7703 // Remove the diffs collected for the column.
7704 m_partial_update_info->m_logical_diff_vectors[field->field_index()]->clear();
7705
7706 // Mark the column as disabled.
7707 bitmap_clear_bit(&m_partial_update_info->m_enabled_logical_diff_columns,
7708 field->field_index());
7709 }
7710
7711 //////////////////////////////////////////////////////////////////////////
7712
7713 /*
7714 NOTE:
7715
7716 The functions in this block are used to read .frm file.
7717 They should not be used any where else in the code. They are only used
7718 in upgrade scenario for migrating old data directory to be compatible
7719 with current server. They will be removed in future release.
7720
7721 Any new code should not be added in this section.
7722 */
7723
7724 /**
7725 Open and Read .frm file.
7726 Based on header, it is decided if its a table or view.
7727 Prepare TABLE_SHARE if its a table.
7728 Prepare File_parser if its a view.
7729
7730 @param thd thread handle
7731 @param share TABLE_SHARE object to be filled.
7732 @param frm_context FRM_context for structures removed from
7733 TABLE_SHARE
7734 @param table table name
7735 @param is_fix_view_cols_and_deps Flag to indicate that we are recreating view
7736 to create view dependency entry in DD tables
7737
7738 @retval true Error
7739 @retval false Success
7740 */
read_frm_file(THD * thd,TABLE_SHARE * share,FRM_context * frm_context,const std::string & table,bool is_fix_view_cols_and_deps)7741 static bool read_frm_file(THD *thd, TABLE_SHARE *share,
7742 FRM_context *frm_context, const std::string &table,
7743 bool is_fix_view_cols_and_deps) {
7744 File file;
7745 uchar head[64];
7746 char path[FN_REFLEN + 1];
7747 MEM_ROOT **root_ptr, *old_root;
7748
7749 strxnmov(path, sizeof(path) - 1, share->normalized_path.str, reg_ext, NullS);
7750 LEX_STRING pathstr = {path, strlen(path)};
7751
7752 if ((file = mysql_file_open(key_file_frm, path, O_RDONLY, MYF(0))) < 0) {
7753 LogErr(ERROR_LEVEL, ER_CANT_OPEN_FRM_FILE, path);
7754 return true;
7755 }
7756
7757 if (mysql_file_read(file, head, 64, MYF(MY_NABP))) {
7758 LogErr(ERROR_LEVEL, ER_CANT_READ_FRM_FILE, path);
7759 goto err;
7760 }
7761
7762 /*
7763 Checking if the given .frm file is TABLE or VIEW.
7764 */
7765 if (head[0] == (uchar)254 && head[1] == 1) {
7766 if (head[2] == FRM_VER || head[2] == FRM_VER + 1 ||
7767 (head[2] >= FRM_VER + 3 && head[2] <= FRM_VER + 4)) {
7768 /*
7769 This means this is a BASE_TABLE.
7770 Don't read .frm file for tables if we are recreating views
7771 to resolve dependency. At this time, all tables are already upgraded.
7772 .frm file should be only read for views.
7773 */
7774 if (is_fix_view_cols_and_deps) {
7775 mysql_file_close(file, MYF(MY_WME));
7776 return false;
7777 }
7778 int error;
7779 root_ptr = THR_MALLOC;
7780 old_root = *root_ptr;
7781 *root_ptr = &share->mem_root;
7782
7783 error = open_binary_frm(thd, share, frm_context, head, file);
7784
7785 *root_ptr = old_root;
7786 if (error) {
7787 LogErr(ERROR_LEVEL, ER_CANT_READ_FRM_FILE, path);
7788 goto err;
7789 }
7790 } else {
7791 LogErr(ERROR_LEVEL, ER_TABLE_CREATED_WITH_DIFFERENT_VERSION,
7792 table.c_str());
7793 goto err;
7794 }
7795 } else if (memcmp(head, STRING_WITH_LEN("TYPE=")) == 0) {
7796 if (memcmp(head + 5, "VIEW", 4) == 0) {
7797 // View found
7798 share->is_view = true;
7799
7800 /*
7801 Create view file parser and hold it in
7802 FRM_context member view_def.
7803 */
7804 frm_context->view_def =
7805 sql_parse_prepare(&pathstr, &share->mem_root, true);
7806 if (!frm_context->view_def) {
7807 LogErr(ERROR_LEVEL, ER_VIEW_UNPARSABLE, pathstr.str);
7808 goto err;
7809 }
7810 } else {
7811 LogErr(ERROR_LEVEL, ER_FILE_TYPE_UNKNOWN, pathstr.str);
7812 goto err;
7813 }
7814 } else {
7815 LogErr(ERROR_LEVEL, ER_INVALID_INFO_IN_FRM, pathstr.str);
7816 goto err;
7817 }
7818
7819 // Close file and return
7820 mysql_file_close(file, MYF(MY_WME));
7821 return false;
7822
7823 err:
7824 mysql_file_close(file, MYF(MY_WME));
7825 return true;
7826 }
7827
create_table_share_for_upgrade(THD * thd,const char * path,TABLE_SHARE * share,FRM_context * frm_context,const char * db_name,const char * table_name,bool is_fix_view_cols_and_deps)7828 bool create_table_share_for_upgrade(THD *thd, const char *path,
7829 TABLE_SHARE *share,
7830 FRM_context *frm_context,
7831 const char *db_name, const char *table_name,
7832 bool is_fix_view_cols_and_deps) {
7833 DBUG_TRACE;
7834
7835 init_tmp_table_share(thd, share, db_name, 0, table_name, path, nullptr);
7836
7837 // Fix table categories set by init_tmp_table_share
7838 share->table_category = TABLE_UNKNOWN_CATEGORY;
7839 share->tmp_table = NO_TMP_TABLE;
7840 mysql_mutex_init(key_TABLE_SHARE_LOCK_ha_data, &share->LOCK_ha_data,
7841 MY_MUTEX_INIT_FAST);
7842
7843 if (read_frm_file(thd, share, frm_context, table_name,
7844 is_fix_view_cols_and_deps)) {
7845 free_table_share(share);
7846 return true;
7847 }
7848 return false;
7849 }
7850
blobs_need_not_keep_old_value()7851 void TABLE::blobs_need_not_keep_old_value() {
7852 for (Field **vfield_ptr = vfield; *vfield_ptr; vfield_ptr++) {
7853 Field *vfield = *vfield_ptr;
7854 /*
7855 Set this flag so that all blob columns can keep the old value.
7856 */
7857 if (vfield->handle_old_value())
7858 (down_cast<Field_blob *>(vfield))->set_keep_old_value(false);
7859 }
7860 }
7861
set_binlog_drop_if_temp(bool should_binlog)7862 void TABLE::set_binlog_drop_if_temp(bool should_binlog) {
7863 should_binlog_drop_if_temp_flag = should_binlog;
7864 }
7865
should_binlog_drop_if_temp(void) const7866 bool TABLE::should_binlog_drop_if_temp(void) const {
7867 return should_binlog_drop_if_temp_flag;
7868 }
7869
empty_result_table()7870 bool TABLE::empty_result_table() {
7871 materialized = false;
7872 set_not_started();
7873 if (!is_created()) return false;
7874 if (file->ha_index_or_rnd_end() || file->ha_extra(HA_EXTRA_RESET_STATE) ||
7875 file->ha_delete_all_rows())
7876 return true;
7877 free_io_cache(this);
7878 filesort_free_buffers(this, false);
7879 return false;
7880 }
7881
update_covering_prefix_keys(Field * field,uint16 key_read_length,Key_map * covering_prefix_keys)7882 void TABLE::update_covering_prefix_keys(Field *field, uint16 key_read_length,
7883 Key_map *covering_prefix_keys) {
7884 for (uint keyno = 0; keyno < s->keys; keyno++)
7885 if (covering_prefix_keys->is_set(keyno)) {
7886 KEY *key_info = &this->key_info[keyno];
7887 for (KEY_PART_INFO *part = key_info->key_part,
7888 *part_end = part + actual_key_parts(key_info);
7889 part != part_end; ++part)
7890 if ((part->key_part_flag & HA_PART_KEY_SEG) && field->eq(part->field)) {
7891 uint16 key_part_length = part->length / field->charset()->mbmaxlen;
7892 if (key_part_length < key_read_length) covering_keys.clear_bit(keyno);
7893 }
7894 }
7895 }
7896
7897 //////////////////////////////////////////////////////////////////////////
7898