1 /* Copyright (c) 2000, 2017, Oracle and/or its affiliates.
2    Copyright (c) 2008, 2021, MariaDB
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 as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA */
16 
17 
18 /* Some general useful functions */
19 
20 #include "mariadb.h"                 /* NO_EMBEDDED_ACCESS_CHECKS */
21 #include "sql_priv.h"
22 #include "table.h"
23 #include "key.h"                                // find_ref_key
24 #include "sql_table.h"                          // build_table_filename,
25                                                 // primary_key_name
26 #include "sql_parse.h"                          // free_items
27 #include "strfunc.h"                            // unhex_type2
28 #include "ha_partition.h"        // PART_EXT
29                                  // mysql_unpack_partition,
30                                  // fix_partition_func, partition_info
31 #include "sql_base.h"
32 #include "create_options.h"
33 #include "sql_trigger.h"
34 #include <m_ctype.h>
35 #include "my_md5.h"
36 #include "my_bit.h"
37 #include "sql_select.h"
38 #include "sql_derived.h"
39 #include "sql_statistics.h"
40 #include "discover.h"
41 #include "mdl.h"                 // MDL_wait_for_graph_visitor
42 #include "sql_view.h"
43 #include "rpl_filter.h"
44 #include "sql_cte.h"
45 #include "ha_sequence.h"
46 #include "sql_show.h"
47 #include "opt_trace.h"
48 
49 /* For MySQL 5.7 virtual fields */
50 #define MYSQL57_GENERATED_FIELD 128
51 #define MYSQL57_GCOL_HEADER_SIZE 4
52 
53 class Table_arena: public Query_arena
54 {
55 public:
Table_arena(MEM_ROOT * mem_root,enum enum_state state_arg)56   Table_arena(MEM_ROOT *mem_root, enum enum_state state_arg) :
57           Query_arena(mem_root, state_arg){}
type() const58   virtual Type type() const
59   {
60     return TABLE_ARENA;
61   }
62 };
63 
64 struct extra2_fields
65 {
66   LEX_CUSTRING version;
67   LEX_CUSTRING options;
68   Lex_ident engine;
69   LEX_CUSTRING gis;
70   LEX_CUSTRING field_flags;
71   LEX_CUSTRING system_period;
72   LEX_CUSTRING application_period;
73   LEX_CUSTRING field_data_type_info;
74   LEX_CUSTRING without_overlaps;
resetextra2_fields75   void reset()
76   { bzero((void*)this, sizeof(*this)); }
77 };
78 
79 static Virtual_column_info * unpack_vcol_info_from_frm(THD *, MEM_ROOT *,
80               TABLE *, String *, Virtual_column_info **, bool *);
81 static bool check_vcol_forward_refs(Field *, Virtual_column_info *,
82                                     bool check_constraint);
83 
84 /* INFORMATION_SCHEMA name */
85 LEX_CSTRING INFORMATION_SCHEMA_NAME= {STRING_WITH_LEN("information_schema")};
86 
87 /* PERFORMANCE_SCHEMA name */
88 LEX_CSTRING PERFORMANCE_SCHEMA_DB_NAME= {STRING_WITH_LEN("performance_schema")};
89 
90 /* MYSQL_SCHEMA name */
91 LEX_CSTRING MYSQL_SCHEMA_NAME= {STRING_WITH_LEN("mysql")};
92 
93 /* GENERAL_LOG name */
94 LEX_CSTRING GENERAL_LOG_NAME= {STRING_WITH_LEN("general_log")};
95 
96 /* SLOW_LOG name */
97 LEX_CSTRING SLOW_LOG_NAME= {STRING_WITH_LEN("slow_log")};
98 
99 LEX_CSTRING TRANSACTION_REG_NAME= {STRING_WITH_LEN("transaction_registry")};
100 LEX_CSTRING MYSQL_PROC_NAME= {STRING_WITH_LEN("proc")};
101 
102 /*
103   Keyword added as a prefix when parsing the defining expression for a
104   virtual column read from the column definition saved in the frm file
105 */
106 static LEX_CSTRING parse_vcol_keyword= { STRING_WITH_LEN("PARSE_VCOL_EXPR ") };
107 
108 static std::atomic<ulong> last_table_id;
109 
110 	/* Functions defined in this file */
111 
112 static bool fix_type_pointers(const char ***typelib_value_names,
113                               uint **typelib_value_lengths,
114                               TYPELIB *point_to_type, uint types,
115                               char *names, size_t names_length);
116 
117 static uint find_field(Field **fields, uchar *record, uint start, uint length);
118 
119 inline bool is_system_table_name(const char *name, size_t length);
120 
121 /**************************************************************************
122   Object_creation_ctx implementation.
123 **************************************************************************/
124 
set_n_backup(THD * thd)125 Object_creation_ctx *Object_creation_ctx::set_n_backup(THD *thd)
126 {
127   Object_creation_ctx *backup_ctx;
128   DBUG_ENTER("Object_creation_ctx::set_n_backup");
129 
130   backup_ctx= create_backup_ctx(thd);
131   change_env(thd);
132 
133   DBUG_RETURN(backup_ctx);
134 }
135 
restore_env(THD * thd,Object_creation_ctx * backup_ctx)136 void Object_creation_ctx::restore_env(THD *thd, Object_creation_ctx *backup_ctx)
137 {
138   if (!backup_ctx)
139     return;
140 
141   backup_ctx->change_env(thd);
142 
143   delete backup_ctx;
144 }
145 
146 /**************************************************************************
147   Default_object_creation_ctx implementation.
148 **************************************************************************/
149 
Default_object_creation_ctx(THD * thd)150 Default_object_creation_ctx::Default_object_creation_ctx(THD *thd)
151   : m_client_cs(thd->variables.character_set_client),
152     m_connection_cl(thd->variables.collation_connection)
153 { }
154 
Default_object_creation_ctx(CHARSET_INFO * client_cs,CHARSET_INFO * connection_cl)155 Default_object_creation_ctx::Default_object_creation_ctx(
156   CHARSET_INFO *client_cs, CHARSET_INFO *connection_cl)
157   : m_client_cs(client_cs),
158     m_connection_cl(connection_cl)
159 { }
160 
161 Object_creation_ctx *
create_backup_ctx(THD * thd) const162 Default_object_creation_ctx::create_backup_ctx(THD *thd) const
163 {
164   return new Default_object_creation_ctx(thd);
165 }
166 
change_env(THD * thd) const167 void Default_object_creation_ctx::change_env(THD *thd) const
168 {
169   thd->update_charset(m_client_cs, m_connection_cl);
170 }
171 
172 /**************************************************************************
173   View_creation_ctx implementation.
174 **************************************************************************/
175 
create(THD * thd)176 View_creation_ctx *View_creation_ctx::create(THD *thd)
177 {
178   View_creation_ctx *ctx= new (thd->mem_root) View_creation_ctx(thd);
179 
180   return ctx;
181 }
182 
183 /*************************************************************************/
184 
create(THD * thd,TABLE_LIST * view)185 View_creation_ctx * View_creation_ctx::create(THD *thd,
186                                               TABLE_LIST *view)
187 {
188   View_creation_ctx *ctx= new (thd->mem_root) View_creation_ctx(thd);
189 
190   /* Throw a warning if there is NULL cs name. */
191 
192   if (!view->view_client_cs_name.str ||
193       !view->view_connection_cl_name.str)
194   {
195     push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
196                         ER_VIEW_NO_CREATION_CTX,
197                         ER_THD(thd, ER_VIEW_NO_CREATION_CTX),
198                         view->db.str,
199                         view->table_name.str);
200 
201     ctx->m_client_cs= system_charset_info;
202     ctx->m_connection_cl= system_charset_info;
203 
204     return ctx;
205   }
206 
207   /* Resolve cs names. Throw a warning if there is unknown cs name. */
208 
209   bool invalid_creation_ctx;
210 
211   invalid_creation_ctx= resolve_charset(view->view_client_cs_name.str,
212                                         system_charset_info,
213                                         &ctx->m_client_cs);
214 
215   invalid_creation_ctx= resolve_collation(view->view_connection_cl_name.str,
216                                           system_charset_info,
217                                           &ctx->m_connection_cl) ||
218                         invalid_creation_ctx;
219 
220   if (invalid_creation_ctx)
221   {
222     sql_print_warning("View '%s'.'%s': there is unknown charset/collation "
223                       "names (client: '%s'; connection: '%s').",
224                       view->db.str,
225                       view->table_name.str,
226                       (const char *) view->view_client_cs_name.str,
227                       (const char *) view->view_connection_cl_name.str);
228 
229     push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
230                         ER_VIEW_INVALID_CREATION_CTX,
231                         ER_THD(thd, ER_VIEW_INVALID_CREATION_CTX),
232                         view->db.str,
233                         view->table_name.str);
234   }
235 
236   return ctx;
237 }
238 
239 /*************************************************************************/
240 
241 /* Get column name from column hash */
242 
get_field_name(Field ** buff,size_t * length,my_bool not_used)243 static uchar *get_field_name(Field **buff, size_t *length,
244                              my_bool not_used __attribute__((unused)))
245 {
246   *length= (uint) (*buff)->field_name.length;
247   return (uchar*) (*buff)->field_name.str;
248 }
249 
250 
251 /*
252   Returns pointer to '.frm' extension of the file name.
253 
254   SYNOPSIS
255     fn_frm_ext()
256     name       file name
257 
258   DESCRIPTION
259     Checks file name part starting with the rightmost '.' character,
260     and returns it if it is equal to '.frm'.
261 
262   RETURN VALUES
263     Pointer to the '.frm' extension or NULL if not a .frm file
264 */
265 
fn_frm_ext(const char * name)266 const char *fn_frm_ext(const char *name)
267 {
268   const char *res= strrchr(name, '.');
269   if (res && !strcmp(res, reg_ext))
270     return res;
271   return 0;
272 }
273 
274 
get_table_category(const LEX_CSTRING * db,const LEX_CSTRING * name)275 TABLE_CATEGORY get_table_category(const LEX_CSTRING *db,
276                                   const LEX_CSTRING *name)
277 {
278   DBUG_ASSERT(db != NULL);
279   DBUG_ASSERT(name != NULL);
280 
281 #ifdef WITH_WSREP
282   if (db->str &&
283       my_strcasecmp(system_charset_info, db->str, "mysql") == 0 &&
284       my_strcasecmp(system_charset_info, name->str, "wsrep_streaming_log") == 0)
285   {
286     return TABLE_CATEGORY_INFORMATION;
287   }
288 #endif /* WITH_WSREP */
289   if (is_infoschema_db(db))
290     return TABLE_CATEGORY_INFORMATION;
291 
292   if (is_perfschema_db(db))
293     return TABLE_CATEGORY_PERFORMANCE;
294 
295   if (lex_string_eq(&MYSQL_SCHEMA_NAME, db))
296   {
297     if (is_system_table_name(name->str, name->length))
298       return TABLE_CATEGORY_SYSTEM;
299 
300     if (lex_string_eq(&GENERAL_LOG_NAME, name))
301       return TABLE_CATEGORY_LOG;
302 
303     if (lex_string_eq(&SLOW_LOG_NAME, name))
304       return TABLE_CATEGORY_LOG;
305 
306     if (lex_string_eq(&TRANSACTION_REG_NAME, name))
307       return TABLE_CATEGORY_LOG;
308   }
309 
310   return TABLE_CATEGORY_USER;
311 }
312 
313 
314 /*
315   Allocate and setup a TABLE_SHARE structure
316 
317   SYNOPSIS
318     alloc_table_share()
319     db                  Database name
320     table_name          Table name
321     key			Table cache key (db \0 table_name \0...)
322     key_length		Length of key
323 
324   RETURN
325     0  Error (out of memory)
326     #  Share
327 */
328 
alloc_table_share(const char * db,const char * table_name,const char * key,uint key_length)329 TABLE_SHARE *alloc_table_share(const char *db, const char *table_name,
330                                const char *key, uint key_length)
331 {
332   MEM_ROOT mem_root;
333   TABLE_SHARE *share;
334   char *key_buff, *path_buff;
335   char path[FN_REFLEN];
336   uint path_length;
337   DBUG_ENTER("alloc_table_share");
338   DBUG_PRINT("enter", ("table: '%s'.'%s'", db, table_name));
339 
340   path_length= build_table_filename(path, sizeof(path) - 1,
341                                     db, table_name, "", 0);
342   init_sql_alloc(key_memory_table_share, &mem_root, TABLE_ALLOC_BLOCK_SIZE, 0,
343                  MYF(0));
344   if (multi_alloc_root(&mem_root,
345                        &share, sizeof(*share),
346                        &key_buff, key_length,
347                        &path_buff, path_length + 1,
348                        NULL))
349   {
350     bzero((char*) share, sizeof(*share));
351 
352     share->set_table_cache_key(key_buff, key, key_length);
353 
354     share->path.str= path_buff;
355     share->path.length= path_length;
356     strmov(path_buff, path);
357     share->normalized_path.str=    share->path.str;
358     share->normalized_path.length= path_length;
359     share->table_category= get_table_category(& share->db, & share->table_name);
360     share->open_errno= ENOENT;
361     /* The following will be updated in open_table_from_share */
362     share->can_do_row_logging= 1;
363     if (share->table_category == TABLE_CATEGORY_LOG)
364       share->no_replicate= 1;
365     if (key_length > 6 &&
366         table_alias_charset->strnncoll(key, 6, "mysql", 6) == 0)
367       share->not_usable_by_query_cache= 1;
368 
369     init_sql_alloc(PSI_INSTRUMENT_ME, &share->stats_cb.mem_root,
370                    TABLE_ALLOC_BLOCK_SIZE, 0, MYF(0));
371 
372     memcpy((char*) &share->mem_root, (char*) &mem_root, sizeof(mem_root));
373     mysql_mutex_init(key_TABLE_SHARE_LOCK_share,
374                      &share->LOCK_share, MY_MUTEX_INIT_SLOW);
375     mysql_mutex_init(key_TABLE_SHARE_LOCK_ha_data,
376                      &share->LOCK_ha_data, MY_MUTEX_INIT_FAST);
377 
378     DBUG_EXECUTE_IF("simulate_big_table_id",
379                     if (last_table_id < UINT_MAX32)
380                       last_table_id= UINT_MAX32 - 1;);
381     /*
382       There is one reserved number that cannot be used. Remember to
383       change this when 6-byte global table id's are introduced.
384     */
385     do
386     {
387       share->table_map_id=
388         last_table_id.fetch_add(1, std::memory_order_relaxed);
389     } while (unlikely(share->table_map_id == ~0UL ||
390                       share->table_map_id == 0));
391   }
392   DBUG_RETURN(share);
393 }
394 
395 
396 /*
397   Initialize share for temporary tables
398 
399   SYNOPSIS
400     init_tmp_table_share()
401     thd         thread handle
402     share	Share to fill
403     key		Table_cache_key, as generated from tdc_create_key.
404 		must start with db name.
405     key_length	Length of key
406     table_name	Table name
407     path	Path to file (possible in lower case) without .frm
408 
409   NOTES
410     This is different from alloc_table_share() because temporary tables
411     don't have to be shared between threads or put into the table def
412     cache, so we can do some things notable simpler and faster
413 
414     If table is not put in thd->temporary_tables (happens only when
415     one uses OPEN TEMPORARY) then one can specify 'db' as key and
416     use key_length= 0 as neither table_cache_key or key_length will be used).
417 */
418 
init_tmp_table_share(THD * thd,TABLE_SHARE * share,const char * key,uint key_length,const char * table_name,const char * path)419 void init_tmp_table_share(THD *thd, TABLE_SHARE *share, const char *key,
420                           uint key_length, const char *table_name,
421                           const char *path)
422 {
423   DBUG_ENTER("init_tmp_table_share");
424   DBUG_PRINT("enter", ("table: '%s'.'%s'", key, table_name));
425 
426   bzero((char*) share, sizeof(*share));
427   /*
428     This can't be MY_THREAD_SPECIFIC for slaves as they are freed
429     during cleanup() from Relay_log_info::close_temporary_tables()
430   */
431   init_sql_alloc(key_memory_table_share, &share->mem_root,
432                  TABLE_ALLOC_BLOCK_SIZE, 0,
433                  MYF(thd->slave_thread ? 0 : MY_THREAD_SPECIFIC));
434   share->table_category=         TABLE_CATEGORY_TEMPORARY;
435   share->tmp_table=              INTERNAL_TMP_TABLE;
436   share->db.str=                 (char*) key;
437   share->db.length=		 strlen(key);
438   share->table_cache_key.str=    (char*) key;
439   share->table_cache_key.length= key_length;
440   share->table_name.str=         (char*) table_name;
441   share->table_name.length=      strlen(table_name);
442   share->path.str=               (char*) path;
443   share->normalized_path.str=    (char*) path;
444   share->path.length= share->normalized_path.length= strlen(path);
445   share->frm_version= 		 FRM_VER_CURRENT;
446   share->not_usable_by_query_cache= 1;
447   share->can_do_row_logging= 0;           // No row logging
448 
449   /*
450     table_map_id is also used for MERGE tables to suppress repeated
451     compatibility checks.
452   */
453   share->table_map_id= (ulong) thd->query_id;
454   DBUG_VOID_RETURN;
455 }
456 
457 
458 /**
459   Release resources (plugins) used by the share and free its memory.
460   TABLE_SHARE is self-contained -- it's stored in its own MEM_ROOT.
461   Free this MEM_ROOT.
462 */
463 
destroy()464 void TABLE_SHARE::destroy()
465 {
466   uint idx;
467   KEY *info_it;
468   DBUG_ENTER("TABLE_SHARE::destroy");
469   DBUG_PRINT("info", ("db: %s table: %s", db.str, table_name.str));
470 
471   if (ha_share)
472   {
473     delete ha_share;
474     ha_share= NULL;                             // Safety
475   }
476 
477   delete_stat_values_for_table_share(this);
478   delete sequence;
479   free_root(&stats_cb.mem_root, MYF(0));
480 
481   /* The mutexes are initialized only for shares that are part of the TDC */
482   if (tmp_table == NO_TMP_TABLE)
483   {
484     mysql_mutex_destroy(&LOCK_share);
485     mysql_mutex_destroy(&LOCK_ha_data);
486   }
487   my_hash_free(&name_hash);
488 
489   plugin_unlock(NULL, db_plugin);
490   db_plugin= NULL;
491 
492   /* Release fulltext parsers */
493   info_it= key_info;
494   for (idx= keys; idx; idx--, info_it++)
495   {
496     if (info_it->flags & HA_USES_PARSER)
497     {
498       plugin_unlock(NULL, info_it->parser);
499       info_it->flags= 0;
500     }
501   }
502 
503 #ifdef WITH_PARTITION_STORAGE_ENGINE
504   plugin_unlock(NULL, default_part_plugin);
505 #endif /* WITH_PARTITION_STORAGE_ENGINE */
506 
507   PSI_CALL_release_table_share(m_psi);
508 
509   /*
510     Make a copy since the share is allocated in its own root,
511     and free_root() updates its argument after freeing the memory.
512   */
513   MEM_ROOT own_root= mem_root;
514   free_root(&own_root, MYF(0));
515   DBUG_VOID_RETURN;
516 }
517 
518 /*
519   Free table share and memory used by it
520 
521   SYNOPSIS
522     free_table_share()
523     share		Table share
524 */
525 
free_table_share(TABLE_SHARE * share)526 void free_table_share(TABLE_SHARE *share)
527 {
528   DBUG_ENTER("free_table_share");
529   DBUG_PRINT("enter", ("table: %s.%s", share->db.str, share->table_name.str));
530   share->destroy();
531   DBUG_VOID_RETURN;
532 }
533 
534 
535 /**
536   Return TRUE if a table name matches one of the system table names.
537   Currently these are:
538 
539   help_category, help_keyword, help_relation, help_topic,
540   proc, event
541   time_zone, time_zone_leap_second, time_zone_name, time_zone_transition,
542   time_zone_transition_type
543 
544   This function trades accuracy for speed, so may return false
545   positives. Presumably mysql.* database is for internal purposes only
546   and should not contain user tables.
547 */
548 
is_system_table_name(const char * name,size_t length)549 inline bool is_system_table_name(const char *name, size_t length)
550 {
551   CHARSET_INFO *ci= system_charset_info;
552 
553   return (
554           /* mysql.proc table */
555           (length == 4 &&
556            my_tolower(ci, name[0]) == 'p' &&
557            my_tolower(ci, name[1]) == 'r' &&
558            my_tolower(ci, name[2]) == 'o' &&
559            my_tolower(ci, name[3]) == 'c') ||
560 
561           (length > 4 &&
562            (
563             /* one of mysql.help* tables */
564             (my_tolower(ci, name[0]) == 'h' &&
565              my_tolower(ci, name[1]) == 'e' &&
566              my_tolower(ci, name[2]) == 'l' &&
567              my_tolower(ci, name[3]) == 'p') ||
568 
569             /* one of mysql.time_zone* tables */
570             (my_tolower(ci, name[0]) == 't' &&
571              my_tolower(ci, name[1]) == 'i' &&
572              my_tolower(ci, name[2]) == 'm' &&
573              my_tolower(ci, name[3]) == 'e') ||
574 
575             /* one of mysql.*_stat tables, but not mysql.innodb* tables*/
576             ((my_tolower(ci, name[length-5]) == 's' &&
577               my_tolower(ci, name[length-4]) == 't' &&
578               my_tolower(ci, name[length-3]) == 'a' &&
579               my_tolower(ci, name[length-2]) == 't' &&
580               my_tolower(ci, name[length-1]) == 's') &&
581              !(my_tolower(ci, name[0]) == 'i' &&
582                my_tolower(ci, name[1]) == 'n' &&
583                my_tolower(ci, name[2]) == 'n' &&
584                my_tolower(ci, name[3]) == 'o')) ||
585 
586             /* mysql.event table */
587             (my_tolower(ci, name[0]) == 'e' &&
588              my_tolower(ci, name[1]) == 'v' &&
589              my_tolower(ci, name[2]) == 'e' &&
590              my_tolower(ci, name[3]) == 'n' &&
591              my_tolower(ci, name[4]) == 't')
592             )
593            )
594          );
595 }
596 
597 
598 /*
599   Read table definition from a binary / text based .frm file
600 
601   SYNOPSIS
602   open_table_def()
603   thd		  Thread handler
604   share		Fill this with table definition
605   flags	  Bit mask of the following flags: OPEN_VIEW
606 
607   NOTES
608     This function is called when the table definition is not cached in
609     table definition cache
610     The data is returned in 'share', which is allocated by
611     alloc_table_share().. The code assumes that share is initialized.
612 */
613 
open_table_def(THD * thd,TABLE_SHARE * share,uint flags)614 enum open_frm_error open_table_def(THD *thd, TABLE_SHARE *share, uint flags)
615 {
616   bool error_given= false;
617   File file;
618   uchar *buf;
619   uchar head[FRM_HEADER_SIZE];
620   char	path[FN_REFLEN];
621   size_t frmlen, read_length;
622   uint length;
623   DBUG_ENTER("open_table_def");
624   DBUG_PRINT("enter", ("table: '%s'.'%s'  path: '%s'", share->db.str,
625                        share->table_name.str, share->normalized_path.str));
626 
627   share->error= OPEN_FRM_OPEN_ERROR;
628 
629   length=(uint) (strxmov(path, share->normalized_path.str, reg_ext, NullS) -
630                  path);
631   if (flags & GTS_FORCE_DISCOVERY)
632   {
633     const char *path2= share->normalized_path.str;
634     DBUG_ASSERT(flags & GTS_TABLE);
635     DBUG_ASSERT(flags & GTS_USE_DISCOVERY);
636     /* Delete .frm and .par files */
637     mysql_file_delete_with_symlink(key_file_frm, path2, reg_ext, MYF(0));
638     mysql_file_delete_with_symlink(key_file_partition_ddl_log, path2, PAR_EXT,
639                                    MYF(0));
640     file= -1;
641   }
642   else
643     file= mysql_file_open(key_file_frm, path, O_RDONLY | O_SHARE, MYF(0));
644 
645   if (file < 0)
646   {
647     if ((flags & GTS_TABLE) && (flags & GTS_USE_DISCOVERY))
648     {
649       ha_discover_table(thd, share);
650       error_given= true;
651     }
652     goto err_not_open;
653   }
654 
655   if (mysql_file_read(file, head, sizeof(head), MYF(MY_NABP)))
656   {
657     share->error = my_errno == HA_ERR_FILE_TOO_SHORT
658                       ? OPEN_FRM_CORRUPTED : OPEN_FRM_READ_ERROR;
659     goto err;
660   }
661 
662   if (memcmp(head, STRING_WITH_LEN("TYPE=VIEW\n")) == 0)
663   {
664     share->is_view= 1;
665     if (flags & GTS_VIEW)
666     {
667       LEX_CSTRING pathstr= { path, length };
668       /*
669         Create view file parser and hold it in TABLE_SHARE member
670         view_def.
671       */
672       share->view_def= sql_parse_prepare(&pathstr, &share->mem_root, true);
673       if (!share->view_def)
674         share->error= OPEN_FRM_ERROR_ALREADY_ISSUED;
675       else
676         share->error= OPEN_FRM_OK;
677     }
678     else
679       share->error= OPEN_FRM_NOT_A_TABLE;
680     goto err;
681   }
682   if (!is_binary_frm_header(head))
683   {
684     /* No handling of text based files yet */
685     share->error = OPEN_FRM_CORRUPTED;
686     goto err;
687   }
688   if (!(flags & GTS_TABLE))
689   {
690     share->error = OPEN_FRM_NOT_A_VIEW;
691     goto err;
692   }
693 
694   frmlen= uint4korr(head+10);
695   set_if_smaller(frmlen, FRM_MAX_SIZE); // safety
696 
697   if (!(buf= (uchar*)my_malloc(PSI_INSTRUMENT_ME, frmlen,
698                                MYF(MY_THREAD_SPECIFIC|MY_WME))))
699     goto err;
700 
701   memcpy(buf, head, sizeof(head));
702 
703   read_length= mysql_file_read(file, buf + sizeof(head),
704                                frmlen - sizeof(head), MYF(MY_WME));
705   if (read_length == 0 || read_length == (size_t)-1)
706   {
707     share->error = OPEN_FRM_READ_ERROR;
708     my_free(buf);
709     goto err;
710   }
711   mysql_file_close(file, MYF(MY_WME));
712 
713   frmlen= read_length + sizeof(head);
714 
715   share->init_from_binary_frm_image(thd, false, buf, frmlen);
716   /*
717     Don't give any additional errors. If there would be a problem,
718     init_from_binary_frm_image would call my_error() itself.
719   */
720   error_given= true;
721   my_free(buf);
722 
723   goto err_not_open;
724 
725 err:
726   mysql_file_close(file, MYF(MY_WME));
727 
728 err_not_open:
729   /* Mark that table was created earlier and thus should have been logged */
730   share->table_creation_was_logged= 1;
731 
732   if (unlikely(share->error && !error_given))
733   {
734     share->open_errno= my_errno;
735     open_table_error(share, share->error, share->open_errno);
736   }
737 
738   DBUG_RETURN(share->error);
739 }
740 
create_key_infos(const uchar * strpos,const uchar * frm_image_end,uint keys,KEY * keyinfo,uint new_frm_ver,uint * ext_key_parts,TABLE_SHARE * share,uint len,KEY * first_keyinfo,LEX_STRING * keynames)741 static bool create_key_infos(const uchar *strpos, const uchar *frm_image_end,
742                              uint keys, KEY *keyinfo,
743                              uint new_frm_ver, uint *ext_key_parts,
744                              TABLE_SHARE *share, uint len,
745                              KEY *first_keyinfo,
746                              LEX_STRING *keynames)
747 {
748   uint i, j, n_length;
749   KEY_PART_INFO *key_part= NULL;
750   ulong *rec_per_key= NULL;
751   KEY_PART_INFO *first_key_part= NULL;
752   uint first_key_parts= 0;
753 
754   if (!keys)
755   {
756     if (!(keyinfo = (KEY*) alloc_root(&share->mem_root, len)))
757       return 1;
758     bzero((char*) keyinfo, len);
759     key_part= reinterpret_cast<KEY_PART_INFO*> (keyinfo);
760   }
761 
762   /*
763     If share->use_ext_keys is set to TRUE we assume that any key
764     can be extended by the components of the primary key whose
765     definition is read first from the frm file.
766     For each key only those fields of the assumed primary key are
767     added that are not included in the proper key definition.
768     If after all it turns out that there is no primary key the
769     added components are removed from each key.
770 
771     When in the future we support others schemes of extending of
772     secondary keys with components of the primary key we'll have
773     to change the type of this flag for an enumeration type.
774   */
775 
776   for (i=0 ; i < keys ; i++, keyinfo++)
777   {
778     if (new_frm_ver >= 3)
779     {
780       if (strpos + 8 >= frm_image_end)
781         return 1;
782       keyinfo->flags=	   (uint) uint2korr(strpos) ^ HA_NOSAME;
783       keyinfo->key_length= (uint) uint2korr(strpos+2);
784       keyinfo->user_defined_key_parts=  (uint) strpos[4];
785       keyinfo->algorithm=  (enum ha_key_alg) strpos[5];
786       keyinfo->block_size= uint2korr(strpos+6);
787       strpos+=8;
788     }
789     else
790     {
791       if (strpos + 4 >= frm_image_end)
792         return 1;
793       keyinfo->flags=	 ((uint) strpos[0]) ^ HA_NOSAME;
794       keyinfo->key_length= (uint) uint2korr(strpos+1);
795       keyinfo->user_defined_key_parts=  (uint) strpos[3];
796       keyinfo->algorithm= HA_KEY_ALG_UNDEF;
797       strpos+=4;
798     }
799 
800     if (i == 0)
801     {
802       (*ext_key_parts)+= (share->use_ext_keys ? first_keyinfo->user_defined_key_parts*(keys-1) : 0);
803       n_length=keys * sizeof(KEY) + *ext_key_parts * sizeof(KEY_PART_INFO);
804       if (!(keyinfo= (KEY*) alloc_root(&share->mem_root,
805 				       n_length + len)))
806         return 1;
807       bzero((char*) keyinfo,n_length);
808       share->key_info= keyinfo;
809       key_part= reinterpret_cast<KEY_PART_INFO*> (keyinfo + keys);
810 
811       if (!(rec_per_key= (ulong*) alloc_root(&share->mem_root,
812                                              sizeof(ulong) * *ext_key_parts)))
813         return 1;
814       first_key_part= key_part;
815       first_key_parts= first_keyinfo->user_defined_key_parts;
816       keyinfo->flags= first_keyinfo->flags;
817       keyinfo->key_length= first_keyinfo->key_length;
818       keyinfo->user_defined_key_parts= first_keyinfo->user_defined_key_parts;
819       keyinfo->algorithm= first_keyinfo->algorithm;
820       if (new_frm_ver >= 3)
821         keyinfo->block_size= first_keyinfo->block_size;
822     }
823 
824     keyinfo->key_part=	 key_part;
825     keyinfo->rec_per_key= rec_per_key;
826     for (j=keyinfo->user_defined_key_parts ; j-- ; key_part++)
827     {
828       if (strpos + (new_frm_ver >= 1 ? 9 : 7) >= frm_image_end)
829         return 1;
830       if (!(keyinfo->algorithm == HA_KEY_ALG_LONG_HASH))
831         *rec_per_key++=0;
832       key_part->fieldnr=	(uint16) (uint2korr(strpos) & FIELD_NR_MASK);
833       key_part->offset= (uint) uint2korr(strpos+2)-1;
834       key_part->key_type=	(uint) uint2korr(strpos+5);
835       // key_part->field=	(Field*) 0;	// Will be fixed later
836       if (new_frm_ver >= 1)
837       {
838 	key_part->key_part_flag= *(strpos+4);
839 	key_part->length=	(uint) uint2korr(strpos+7);
840 	strpos+=9;
841       }
842       else
843       {
844 	key_part->length=	*(strpos+4);
845 	key_part->key_part_flag=0;
846 	if (key_part->length > 128)
847 	{
848 	  key_part->length&=127;		/* purecov: inspected */
849 	  key_part->key_part_flag=HA_REVERSE_SORT; /* purecov: inspected */
850 	}
851 	strpos+=7;
852       }
853       key_part->store_length=key_part->length;
854     }
855     if (keyinfo->algorithm == HA_KEY_ALG_LONG_HASH)
856     {
857       keyinfo->key_length= HA_HASH_KEY_LENGTH_WITHOUT_NULL;
858       key_part++; // reserved for the hash value
859       *rec_per_key++=0;
860     }
861 
862     /*
863       Add primary key to end of extended keys for non unique keys for
864       storage engines that supports it.
865     */
866     keyinfo->ext_key_parts= keyinfo->user_defined_key_parts;
867     keyinfo->ext_key_flags= keyinfo->flags;
868     keyinfo->ext_key_part_map= 0;
869     if (share->use_ext_keys && i && !(keyinfo->flags & HA_NOSAME))
870     {
871       for (j= 0;
872            j < first_key_parts && keyinfo->ext_key_parts < MAX_REF_PARTS;
873            j++)
874       {
875         uint key_parts= keyinfo->user_defined_key_parts;
876         KEY_PART_INFO* curr_key_part= keyinfo->key_part;
877         KEY_PART_INFO* curr_key_part_end= curr_key_part+key_parts;
878         for ( ; curr_key_part < curr_key_part_end; curr_key_part++)
879         {
880           if (curr_key_part->fieldnr == first_key_part[j].fieldnr)
881             break;
882         }
883         if (curr_key_part == curr_key_part_end)
884         {
885           *key_part++= first_key_part[j];
886           *rec_per_key++= 0;
887           keyinfo->ext_key_parts++;
888           keyinfo->ext_key_part_map|= 1 << j;
889         }
890       }
891       if (j == first_key_parts)
892         keyinfo->ext_key_flags= keyinfo->flags | HA_EXT_NOSAME;
893     }
894     if (keyinfo->algorithm == HA_KEY_ALG_LONG_HASH)
895       share->ext_key_parts++;
896     share->ext_key_parts+= keyinfo->ext_key_parts;
897   }
898   keynames->str= (char*) key_part;
899   keynames->length= strnmov(keynames->str, (char *) strpos,
900                             frm_image_end - strpos) - keynames->str;
901   strpos+= keynames->length;
902   if (*strpos++) // key names are \0-terminated
903     return 1;
904   keynames->length++; // Include '\0', to make fix_type_pointers() happy.
905 
906   //reading index comments
907   for (keyinfo= share->key_info, i=0; i < keys; i++, keyinfo++)
908   {
909     if (keyinfo->flags & HA_USES_COMMENT)
910     {
911       if (strpos + 2 >= frm_image_end)
912         return 1;
913       keyinfo->comment.length= uint2korr(strpos);
914       strpos+= 2;
915 
916       if (strpos + keyinfo->comment.length >= frm_image_end)
917         return 1;
918       keyinfo->comment.str= strmake_root(&share->mem_root, (char*) strpos,
919                                          keyinfo->comment.length);
920       strpos+= keyinfo->comment.length;
921     }
922     DBUG_ASSERT(MY_TEST(keyinfo->flags & HA_USES_COMMENT) ==
923                 (keyinfo->comment.length > 0));
924   }
925 
926   share->keys= keys; // do it *after* all key_info's are initialized
927 
928   return 0;
929 }
930 
931 
932 /** ensures that the enum value (read from frm) is within limits
933 
934     if not - issues a warning and resets the value to 0
935     (that is, 0 is assumed to be a default value)
936 */
937 
enum_value_with_check(THD * thd,TABLE_SHARE * share,const char * name,uint value,uint limit)938 static uint enum_value_with_check(THD *thd, TABLE_SHARE *share,
939                                   const char *name, uint value, uint limit)
940 {
941   if (value < limit)
942     return value;
943 
944   sql_print_warning("%s.frm: invalid value %d for the field %s",
945                 share->normalized_path.str, value, name);
946   return 0;
947 }
948 
949 
950 /**
951    Check if a collation has changed number
952 
953    @param mysql_version
954    @param current collation number
955 
956    @retval new collation number (same as current collation number of no change)
957 */
958 
upgrade_collation(ulong mysql_version,uint cs_number)959 static uint upgrade_collation(ulong mysql_version, uint cs_number)
960 {
961   if (mysql_version >= 50300 && mysql_version <= 50399)
962   {
963     switch (cs_number) {
964     case 149: return MY_PAGE2_COLLATION_ID_UCS2;   // ucs2_crotian_ci
965     case 213: return MY_PAGE2_COLLATION_ID_UTF8;   // utf8_crotian_ci
966     }
967   }
968   if ((mysql_version >= 50500 && mysql_version <= 50599) ||
969       (mysql_version >= 100000 && mysql_version <= 100005))
970   {
971     switch (cs_number) {
972     case 149: return MY_PAGE2_COLLATION_ID_UCS2;   // ucs2_crotian_ci
973     case 213: return MY_PAGE2_COLLATION_ID_UTF8;   // utf8_crotian_ci
974     case 214: return MY_PAGE2_COLLATION_ID_UTF32;  // utf32_croatian_ci
975     case 215: return MY_PAGE2_COLLATION_ID_UTF16;  // utf16_croatian_ci
976     case 245: return MY_PAGE2_COLLATION_ID_UTF8MB4;// utf8mb4_croatian_ci
977     }
978   }
979   return cs_number;
980 }
981 
982 
frm_pack_basic(uchar * buff) const983 void Column_definition_attributes::frm_pack_basic(uchar *buff) const
984 {
985   int2store(buff + 3, length);
986   int2store(buff + 8, pack_flag);
987   buff[10]= (uchar) unireg_check;
988 }
989 
990 
frm_unpack_basic(const uchar * buff)991 void Column_definition_attributes::frm_unpack_basic(const uchar *buff)
992 {
993   length=       uint2korr(buff + 3);
994   pack_flag=    uint2korr(buff + 8);
995   unireg_check= (Field::utype) MTYP_TYPENR((uint) buff[10]);
996 }
997 
998 
frm_pack_numeric_with_dec(uchar * buff) const999 void Column_definition_attributes::frm_pack_numeric_with_dec(uchar *buff) const
1000 {
1001   DBUG_ASSERT(f_decimals(pack_flag) == 0);
1002   uint tmp_pack_flag= pack_flag | (decimals << FIELDFLAG_DEC_SHIFT);
1003   int2store(buff + 3, length);
1004   int2store(buff + 8, tmp_pack_flag);
1005   buff[10]= (uchar) unireg_check;
1006 }
1007 
1008 
1009 bool
frm_unpack_numeric_with_dec(TABLE_SHARE * share,const uchar * buff)1010 Column_definition_attributes::frm_unpack_numeric_with_dec(TABLE_SHARE *share,
1011                                                           const uchar *buff)
1012 {
1013   frm_unpack_basic(buff);
1014   decimals= f_decimals(pack_flag);
1015   pack_flag&= ~FIELDFLAG_DEC_MASK;
1016   return frm_unpack_charset(share, buff);
1017 }
1018 
1019 
1020 bool
frm_unpack_temporal_with_dec(TABLE_SHARE * share,uint intlen,const uchar * buff)1021 Column_definition_attributes::frm_unpack_temporal_with_dec(TABLE_SHARE *share,
1022                                                            uint intlen,
1023                                                            const uchar *buff)
1024 {
1025   frm_unpack_basic(buff);
1026   decimals= temporal_dec(intlen);
1027   return frm_unpack_charset(share, buff);
1028 }
1029 
1030 
frm_pack_charset(uchar * buff) const1031 void Column_definition_attributes::frm_pack_charset(uchar *buff) const
1032 {
1033   buff[11]= (uchar) (charset->number >> 8);
1034   buff[14]= (uchar) charset->number;
1035 }
1036 
1037 
frm_unpack_charset(TABLE_SHARE * share,const uchar * buff)1038 bool Column_definition_attributes::frm_unpack_charset(TABLE_SHARE *share,
1039                                                       const uchar *buff)
1040 {
1041   uint cs_org= buff[14] + (((uint) buff[11]) << 8);
1042   uint cs_new= upgrade_collation(share->mysql_version, cs_org);
1043   if (cs_org != cs_new)
1044     share->incompatible_version|= HA_CREATE_USED_CHARSET;
1045   if (cs_new && !(charset= get_charset(cs_new, MYF(0))))
1046   {
1047     const char *csname= get_charset_name((uint) cs_new);
1048     char tmp[10];
1049     if (!csname || csname[0] =='?')
1050     {
1051       my_snprintf(tmp, sizeof(tmp), "#%u", cs_new);
1052       csname= tmp;
1053     }
1054     my_printf_error(ER_UNKNOWN_COLLATION,
1055                     "Unknown collation '%s' in table '%-.64s' definition",
1056                     MYF(0), csname, share->table_name.str);
1057     return true;
1058   }
1059   return false;
1060 }
1061 
1062 
1063 /*
1064   In MySQL 5.7 the null bits for not stored virtual fields are last.
1065   Calculate the position for these bits
1066 */
1067 
mysql57_calculate_null_position(TABLE_SHARE * share,uchar ** null_pos,uint * null_bit_pos,const uchar * strpos,const uchar * vcol_screen_pos)1068 static void mysql57_calculate_null_position(TABLE_SHARE *share,
1069                                             uchar **null_pos,
1070                                             uint *null_bit_pos,
1071                                             const uchar *strpos,
1072                                             const uchar *vcol_screen_pos)
1073 {
1074   uint field_pack_length= 17;
1075 
1076   for (uint i=0 ; i < share->fields; i++, strpos+= field_pack_length)
1077   {
1078     uint field_length, pack_flag;
1079     enum_field_types field_type;
1080 
1081     if ((strpos[10] & MYSQL57_GENERATED_FIELD))
1082     {
1083       /* Skip virtual (not stored) generated field */
1084       bool stored_in_db= vcol_screen_pos[3];
1085       vcol_screen_pos+= (uint2korr(vcol_screen_pos + 1) +
1086                          MYSQL57_GCOL_HEADER_SIZE);
1087       if (! stored_in_db)
1088         continue;
1089     }
1090     field_length= uint2korr(strpos+3);
1091     pack_flag=    uint2korr(strpos+8);
1092     field_type=   (enum_field_types) (uint) strpos[13];
1093     if (field_type == MYSQL_TYPE_BIT && !f_bit_as_char(pack_flag))
1094     {
1095       if (((*null_bit_pos)+= field_length & 7) > 7)
1096       {
1097         (*null_pos)++;
1098         (*null_bit_pos)-= 8;
1099       }
1100     }
1101     if (f_maybe_null(pack_flag))
1102     {
1103       if (!((*null_bit_pos)= ((*null_bit_pos) + 1) & 7))
1104         (*null_pos)++;
1105     }
1106   }
1107 }
1108 
1109 static bool fix_and_check_vcol_expr(THD *thd, TABLE *table,
1110                                     Virtual_column_info *vcol);
1111 
1112 /** Parse TABLE_SHARE::vcol_defs
1113 
1114   unpack_vcol_info_from_frm
1115   5.7
1116     byte 1      = 1
1117     byte 2,3    = expr length
1118     byte 4      = stored_in_db
1119     expression
1120   10.1-
1121     byte 1     = 1 | 2
1122     byte 2     = sql_type       ; but  TABLE::init_from_binary_frm_image()
1123     byte 3     = stored_in_db   ; has put expr_length here
1124     [byte 4]   = optional interval_id for sql_type (if byte 1 == 2)
1125     expression
1126   10.2+
1127     byte 1     = type
1128     byte 2,3   = field_number
1129     byte 4,5   = length of expression
1130     byte 6     = length of name
1131     name
1132     expression
1133 */
parse_vcol_defs(THD * thd,MEM_ROOT * mem_root,TABLE * table,bool * error_reported,vcol_init_mode mode)1134 bool parse_vcol_defs(THD *thd, MEM_ROOT *mem_root, TABLE *table,
1135                      bool *error_reported, vcol_init_mode mode)
1136 {
1137   CHARSET_INFO *save_character_set_client= thd->variables.character_set_client;
1138   CHARSET_INFO *save_collation= thd->variables.collation_connection;
1139   Query_arena  *backup_stmt_arena_ptr= thd->stmt_arena;
1140   const uchar *pos= table->s->vcol_defs.str;
1141   const uchar *end= pos + table->s->vcol_defs.length;
1142   Field **field_ptr= table->field - 1;
1143   Field **vfield_ptr= table->vfield;
1144   Field **dfield_ptr= table->default_field;
1145   Virtual_column_info **check_constraint_ptr= table->check_constraints;
1146   Query_arena backup_arena;
1147   Virtual_column_info *vcol= 0;
1148   StringBuffer<MAX_FIELD_WIDTH> expr_str;
1149   bool res= 1;
1150   DBUG_ENTER("parse_vcol_defs");
1151 
1152   if (check_constraint_ptr)
1153     memcpy(table->check_constraints + table->s->field_check_constraints,
1154            table->s->check_constraints,
1155            table->s->table_check_constraints * sizeof(Virtual_column_info*));
1156 
1157   DBUG_ASSERT(table->expr_arena == NULL);
1158   /*
1159     We need to use CONVENTIONAL_EXECUTION here to ensure that
1160     any new items created by fix_fields() are not reverted.
1161   */
1162   table->expr_arena= new (alloc_root(mem_root, sizeof(Table_arena)))
1163                         Table_arena(mem_root,
1164                                     Query_arena::STMT_CONVENTIONAL_EXECUTION);
1165   if (!table->expr_arena)
1166     DBUG_RETURN(1);
1167 
1168   thd->set_n_backup_active_arena(table->expr_arena, &backup_arena);
1169   thd->stmt_arena= table->expr_arena;
1170   thd->update_charset(&my_charset_utf8mb4_general_ci, table->s->table_charset);
1171   expr_str.append(&parse_vcol_keyword);
1172   Sql_mode_instant_remove sms(thd, MODE_NO_BACKSLASH_ESCAPES |
1173                               MODE_EMPTY_STRING_IS_NULL);
1174 
1175   while (pos < end)
1176   {
1177     uint type, expr_length;
1178     if (table->s->frm_version >= FRM_VER_EXPRESSSIONS)
1179     {
1180       uint field_nr, name_length;
1181       /* see pack_expression() for how data is stored */
1182       type= pos[0];
1183       field_nr= uint2korr(pos+1);
1184       expr_length= uint2korr(pos+3);
1185       name_length= pos[5];
1186       pos+= FRM_VCOL_NEW_HEADER_SIZE + name_length;
1187       field_ptr= table->field + field_nr;
1188     }
1189     else
1190     {
1191       /*
1192         see below in ::init_from_binary_frm_image for how data is stored
1193         in versions below 10.2 (that includes 5.7 too)
1194       */
1195       while (*++field_ptr && !(*field_ptr)->vcol_info) /* no-op */;
1196       if (!*field_ptr)
1197       {
1198         open_table_error(table->s, OPEN_FRM_CORRUPTED, 1);
1199         goto end;
1200       }
1201       type= (*field_ptr)->vcol_info->stored_in_db
1202             ? VCOL_GENERATED_STORED : VCOL_GENERATED_VIRTUAL;
1203       expr_length= uint2korr(pos+1);
1204       if (table->s->mysql_version > 50700 && table->s->mysql_version < 100000)
1205         pos+= 4;                        // MySQL from 5.7
1206       else
1207         pos+= pos[0] == 2 ? 4 : 3;      // MariaDB from 5.2 to 10.1
1208     }
1209 
1210     expr_str.length(parse_vcol_keyword.length);
1211     expr_str.append((char*)pos, expr_length);
1212     thd->where= vcol_type_name(static_cast<enum_vcol_info_type>(type));
1213 
1214     switch (type) {
1215     case VCOL_GENERATED_VIRTUAL:
1216     case VCOL_GENERATED_STORED:
1217       vcol= unpack_vcol_info_from_frm(thd, mem_root, table, &expr_str,
1218                                     &((*field_ptr)->vcol_info), error_reported);
1219       *(vfield_ptr++)= *field_ptr;
1220       DBUG_ASSERT(table->map == 0);
1221       /*
1222         We need Item_field::const_item() to return false, so
1223         datetime_precision() and time_precision() do not try to calculate
1224         field values, e.g. val_str().
1225         Set table->map to non-zero temporarily.
1226       */
1227       table->map= 1;
1228       if (vcol && field_ptr[0]->check_vcol_sql_mode_dependency(thd, mode))
1229       {
1230         DBUG_ASSERT(thd->is_error());
1231         *error_reported= true;
1232         goto end;
1233       }
1234       table->map= 0;
1235       break;
1236     case VCOL_DEFAULT:
1237       vcol= unpack_vcol_info_from_frm(thd, mem_root, table, &expr_str,
1238                                       &((*field_ptr)->default_value),
1239                                       error_reported);
1240       *(dfield_ptr++)= *field_ptr;
1241       if (vcol && (vcol->flags & (VCOL_NON_DETERMINISTIC | VCOL_SESSION_FUNC)))
1242         table->s->non_determinstic_insert= true;
1243       break;
1244     case VCOL_CHECK_FIELD:
1245       vcol= unpack_vcol_info_from_frm(thd, mem_root, table, &expr_str,
1246                                       &((*field_ptr)->check_constraint),
1247                                       error_reported);
1248       *check_constraint_ptr++= (*field_ptr)->check_constraint;
1249       break;
1250     case VCOL_CHECK_TABLE:
1251       vcol= unpack_vcol_info_from_frm(thd, mem_root, table, &expr_str,
1252                                       check_constraint_ptr, error_reported);
1253       check_constraint_ptr++;
1254       break;
1255     }
1256     if (!vcol)
1257       goto end;
1258     pos+= expr_length;
1259   }
1260 
1261   /* Now, initialize CURRENT_TIMESTAMP and UNIQUE_INDEX_HASH_FIELD fields */
1262   for (field_ptr= table->field; *field_ptr; field_ptr++)
1263   {
1264     Field *field= *field_ptr;
1265     if (field->flags & LONG_UNIQUE_HASH_FIELD)
1266     {
1267       List<Item> *field_list= new (mem_root) List<Item>();
1268       Item *list_item;
1269       KEY *key= 0;
1270       uint key_index, parts= 0;
1271       for (key_index= 0; key_index < table->s->keys; key_index++)
1272       {
1273         key=table->key_info + key_index;
1274           parts= key->user_defined_key_parts;
1275         if (key->key_part[parts].fieldnr == field->field_index + 1)
1276             break;
1277         }
1278       if (!key || key->algorithm != HA_KEY_ALG_LONG_HASH)
1279         goto end;
1280       KEY_PART_INFO *keypart;
1281       for (uint i=0; i < parts; i++)
1282       {
1283         keypart= key->key_part + i;
1284         if (keypart->key_part_flag & HA_PART_KEY_SEG)
1285         {
1286           int length= keypart->length/keypart->field->charset()->mbmaxlen;
1287           list_item= new (mem_root) Item_func_left(thd,
1288                        new (mem_root) Item_field(thd, keypart->field),
1289                        new (mem_root) Item_int(thd, length));
1290           list_item->fix_fields(thd, NULL);
1291           keypart->field->vcol_info=
1292             table->field[keypart->field->field_index]->vcol_info;
1293         }
1294         else
1295           list_item= new (mem_root) Item_field(thd, keypart->field);
1296         field_list->push_back(list_item, mem_root);
1297       }
1298       Item_func_hash *hash_item= new(mem_root)Item_func_hash(thd, *field_list);
1299       Virtual_column_info *v= new (mem_root) Virtual_column_info();
1300       field->vcol_info= v;
1301       field->vcol_info->expr= hash_item;
1302       field->vcol_info->set_vcol_type(VCOL_USING_HASH);
1303       if (fix_and_check_vcol_expr(thd, table, v))
1304         goto end;
1305       key->user_defined_key_parts= key->ext_key_parts= key->usable_key_parts= 1;
1306       key->key_part+= parts;
1307 
1308       if (key->flags & HA_NULL_PART_KEY)
1309         key->key_length= HA_HASH_KEY_LENGTH_WITH_NULL;
1310       else
1311         key->key_length= HA_HASH_KEY_LENGTH_WITHOUT_NULL;
1312 
1313       *(vfield_ptr++)= *field_ptr;
1314     }
1315     if (field->has_default_now_unireg_check())
1316     {
1317       expr_str.length(parse_vcol_keyword.length);
1318       expr_str.append(STRING_WITH_LEN("current_timestamp("));
1319       expr_str.append_ulonglong(field->decimals());
1320       expr_str.append(')');
1321       vcol= unpack_vcol_info_from_frm(thd, mem_root, table, &expr_str,
1322                                       &((*field_ptr)->default_value),
1323                                       error_reported);
1324       *(dfield_ptr++)= *field_ptr;
1325       if (!field->default_value->expr)
1326         goto end;
1327     }
1328     else if (field->has_update_default_function() && !field->default_value)
1329       *(dfield_ptr++)= *field_ptr;
1330   }
1331 
1332   if (vfield_ptr)
1333     *vfield_ptr= 0;
1334 
1335   if (dfield_ptr)
1336     *dfield_ptr= 0;
1337 
1338   if (check_constraint_ptr)
1339     *check_constraint_ptr= 0;
1340 
1341   /* Check that expressions aren't referring to not yet initialized fields */
1342   for (field_ptr= table->field; *field_ptr; field_ptr++)
1343   {
1344     Field *field= *field_ptr;
1345     if (check_vcol_forward_refs(field, field->vcol_info, 0) ||
1346         check_vcol_forward_refs(field, field->check_constraint, 1) ||
1347         check_vcol_forward_refs(field, field->default_value, 0))
1348     {
1349       *error_reported= true;
1350       goto end;
1351     }
1352   }
1353 
1354   table->find_constraint_correlated_indexes();
1355 
1356   res=0;
1357 end:
1358   thd->restore_active_arena(table->expr_arena, &backup_arena);
1359   thd->stmt_arena= backup_stmt_arena_ptr;
1360   if (save_character_set_client)
1361     thd->update_charset(save_character_set_client, save_collation);
1362   DBUG_RETURN(res);
1363 }
1364 
1365 
old_frm_type_handler(uint pack_flag,uint interval_nr)1366 static const Type_handler *old_frm_type_handler(uint pack_flag,
1367                                                 uint interval_nr)
1368 {
1369   enum_field_types field_type= (enum_field_types) f_packtype(pack_flag);
1370   DBUG_ASSERT(field_type < 16);
1371 
1372   if (!f_is_alpha(pack_flag))
1373     return Type_handler::get_handler_by_real_type(field_type);
1374 
1375   if (!f_is_packed(pack_flag))
1376   {
1377     if (field_type == MYSQL_TYPE_DECIMAL)  // 3.23 or 4.0 string
1378       return &type_handler_string;
1379     if (field_type == MYSQL_TYPE_VARCHAR)  // Since mysql-5.0
1380       return &type_handler_varchar;
1381     return NULL;  // Error (bad frm?)
1382   }
1383 
1384   if (f_is_blob(pack_flag))
1385     return &type_handler_blob; // QQ: exact type??
1386 
1387   if (interval_nr)
1388   {
1389     if (f_is_enum(pack_flag))
1390       return &type_handler_enum;
1391     return &type_handler_set;
1392   }
1393   return Type_handler::get_handler_by_real_type(field_type);
1394 }
1395 
1396 /* Set overlapped bitmaps for each index */
1397 
set_overlapped_keys()1398 void TABLE_SHARE::set_overlapped_keys()
1399 {
1400   KEY *key1= key_info;
1401   for (uint i= 0; i < keys; i++, key1++)
1402   {
1403     key1->overlapped.clear_all();
1404     key1->overlapped.set_bit(i);
1405   }
1406   key1= key_info;
1407   for (uint i= 0; i < keys; i++, key1++)
1408   {
1409     KEY *key2= key1 + 1;
1410     for (uint j= i+1; j < keys; j++, key2++)
1411     {
1412       KEY_PART_INFO *key_part1= key1->key_part;
1413       uint n1= key1->user_defined_key_parts;
1414       uint n2= key2->user_defined_key_parts;
1415       for (uint k= 0; k < n1; k++, key_part1++)
1416       {
1417         KEY_PART_INFO *key_part2= key2->key_part;
1418         for (uint l= 0; l < n2; l++, key_part2++)
1419 	{
1420           if (key_part1->fieldnr == key_part2->fieldnr)
1421 	  {
1422             key1->overlapped.set_bit(j);
1423             key2->overlapped.set_bit(i);
1424             goto end_checking_overlap;
1425           }
1426         }
1427       }
1428     end_checking_overlap:
1429       ;
1430     }
1431   }
1432 }
1433 
1434 
check_index_dependence(void * arg)1435 bool Item_field::check_index_dependence(void *arg)
1436 {
1437   TABLE *table= (TABLE *)arg;
1438 
1439   KEY *key= table->key_info;
1440   for (uint j= 0; j < table->s->keys; j++, key++)
1441   {
1442     if (table->constraint_dependent_keys.is_set(j))
1443       continue;
1444 
1445     KEY_PART_INFO *key_part= key->key_part;
1446     uint n= key->user_defined_key_parts;
1447 
1448     for (uint k= 0; k < n; k++, key_part++)
1449     {
1450       if (this->field == key_part->field)
1451       {
1452         table->constraint_dependent_keys.set_bit(j);
1453         break;
1454       }
1455     }
1456   }
1457   return false;
1458 }
1459 
1460 
1461 /**
1462   @brief
1463     Find keys that occur in the same constraint on this table
1464 
1465   @details
1466     Constraints on this table are checked only.
1467 
1468     The method goes through constraints list trying to find at
1469     least two keys which parts participate in some constraint.
1470     These keys are called constraint correlated.
1471 
1472     Each key has its own key map with the information about with
1473     which keys it is constraint correlated. Bit in this map is set
1474     only if keys are constraint correlated.
1475     This method fills each keys constraint correlated key map.
1476 */
1477 
find_constraint_correlated_indexes()1478 void TABLE::find_constraint_correlated_indexes()
1479 {
1480   if (s->keys == 0)
1481     return;
1482 
1483   KEY *key= key_info;
1484   for (uint i= 0; i < s->keys; i++, key++)
1485   {
1486     key->constraint_correlated.clear_all();
1487     key->constraint_correlated.set_bit(i);
1488   }
1489 
1490   if (!check_constraints)
1491     return;
1492 
1493   for (Virtual_column_info **chk= check_constraints ; *chk ; chk++)
1494   {
1495     constraint_dependent_keys.clear_all();
1496     (*chk)->expr->walk(&Item::check_index_dependence, 0, this);
1497 
1498     if (constraint_dependent_keys.bits_set() <= 1)
1499       continue;
1500 
1501     uint key_no= 0;
1502     key_map::Iterator ki(constraint_dependent_keys);
1503     while ((key_no= ki++) != key_map::Iterator::BITMAP_END)
1504       key_info[key_no].constraint_correlated.merge(constraint_dependent_keys);
1505   }
1506 }
1507 
1508 
init_period_from_extra2(period_info_t * period,const uchar * data,const uchar * end)1509 bool TABLE_SHARE::init_period_from_extra2(period_info_t *period,
1510                                           const uchar *data, const uchar *end)
1511 {
1512   if (data + 2*frm_fieldno_size > end)
1513     return 1;
1514   period->start_fieldno= read_frm_fieldno(data);
1515   period->end_fieldno= read_frm_fieldno(data + frm_fieldno_size);
1516   return period->start_fieldno >= fields || period->end_fieldno >= fields;
1517 }
1518 
1519 
extra2_read_len(const uchar ** extra2,const uchar * extra2_end)1520 static size_t extra2_read_len(const uchar **extra2, const uchar *extra2_end)
1521 {
1522   size_t length= *(*extra2)++;
1523   if (length)
1524     return length;
1525 
1526   if ((*extra2) + 2 >= extra2_end)
1527     return 0;
1528   length= uint2korr(*extra2);
1529   (*extra2)+= 2;
1530   if (length < 256 || *extra2 + length > extra2_end)
1531     return 0;
1532   return length;
1533 }
1534 
1535 static
read_extra2_section_once(const uchar * extra2,size_t len,LEX_CUSTRING * section)1536 bool read_extra2_section_once(const uchar *extra2, size_t len, LEX_CUSTRING *section)
1537 {
1538   if (section->str)
1539     return true;
1540   *section= {extra2, len};
1541   return false;
1542 }
1543 
1544 static
read_extra2(const uchar * frm_image,size_t len,extra2_fields * fields)1545 bool read_extra2(const uchar *frm_image, size_t len, extra2_fields *fields)
1546 {
1547   const uchar *extra2= frm_image + 64;
1548 
1549   DBUG_ENTER("read_extra2");
1550 
1551   fields->reset();
1552 
1553   if (*extra2 != '/')   // old frm had '/' there
1554   {
1555     const uchar *e2end= extra2 + len;
1556     while (extra2 + 3 <= e2end)
1557     {
1558       extra2_frm_value_type type= (extra2_frm_value_type)*extra2++;
1559       size_t length= extra2_read_len(&extra2, e2end);
1560       if (!length)
1561         DBUG_RETURN(true);
1562 
1563       bool fail= false;
1564       switch (type) {
1565         case EXTRA2_TABLEDEF_VERSION:
1566           if (fields->version.str) // see init_from_sql_statement_string()
1567           {
1568             if (length != fields->version.length)
1569               DBUG_RETURN(true);
1570           }
1571           else
1572           {
1573             fields->version.str= extra2;
1574             fields->version.length= length;
1575           }
1576           break;
1577         case EXTRA2_ENGINE_TABLEOPTS:
1578           fail= read_extra2_section_once(extra2, length, &fields->options);
1579           break;
1580         case EXTRA2_DEFAULT_PART_ENGINE:
1581           fields->engine.set((const char*)extra2, length);
1582           break;
1583         case EXTRA2_GIS:
1584           fail= read_extra2_section_once(extra2, length, &fields->gis);
1585           break;
1586         case EXTRA2_PERIOD_FOR_SYSTEM_TIME:
1587           fail= read_extra2_section_once(extra2, length, &fields->system_period)
1588                   || length != 2 * frm_fieldno_size;
1589           break;
1590         case EXTRA2_FIELD_FLAGS:
1591           fail= read_extra2_section_once(extra2, length, &fields->field_flags);
1592           break;
1593         case EXTRA2_APPLICATION_TIME_PERIOD:
1594           fail= read_extra2_section_once(extra2, length, &fields->application_period);
1595           break;
1596         case EXTRA2_PERIOD_WITHOUT_OVERLAPS:
1597           fail= read_extra2_section_once(extra2, length, &fields->without_overlaps);
1598           break;
1599         case EXTRA2_FIELD_DATA_TYPE_INFO:
1600           fail= read_extra2_section_once(extra2, length, &fields->field_data_type_info);
1601           break;
1602         default:
1603           /* abort frm parsing if it's an unknown but important extra2 value */
1604           if (type >= EXTRA2_ENGINE_IMPORTANT)
1605             DBUG_RETURN(true);
1606       }
1607       if (fail)
1608         DBUG_RETURN(true);
1609 
1610       extra2+= length;
1611     }
1612     if (extra2 != e2end)
1613       DBUG_RETURN(true);
1614   }
1615   DBUG_RETURN(false);
1616 }
1617 
1618 
1619 class Field_data_type_info_array
1620 {
1621 public:
1622   class Elem
1623   {
1624     LEX_CSTRING m_type_info;
1625   public:
set(const LEX_CSTRING & type_info)1626     void set(const LEX_CSTRING &type_info)
1627     {
1628       m_type_info= type_info;
1629     }
type_info() const1630     const LEX_CSTRING &type_info() const
1631     {
1632       return m_type_info;
1633     }
1634   };
1635 private:
1636   Elem *m_array;
1637   uint m_count;
alloc(MEM_ROOT * root,uint count)1638   bool alloc(MEM_ROOT *root, uint count)
1639   {
1640     DBUG_ASSERT(!m_array);
1641     DBUG_ASSERT(!m_count);
1642     size_t nbytes= sizeof(Elem) * count;
1643     if (!(m_array= (Elem*) alloc_root(root, nbytes)))
1644       return true;
1645     m_count= count;
1646     bzero((void*) m_array, nbytes);
1647     return false;
1648   }
read_length(uchar ** pos,const uchar * end)1649   static uint32 read_length(uchar **pos, const uchar *end)
1650   {
1651     ulonglong num= safe_net_field_length_ll(pos, end - *pos);
1652     if (num > UINT_MAX32)
1653       return 0;
1654     return (uint32) num;
1655   }
read_string(LEX_CSTRING * to,uchar ** pos,const uchar * end)1656   static bool read_string(LEX_CSTRING *to, uchar **pos, const uchar *end)
1657   {
1658     to->length= read_length(pos, end);
1659     if (*pos + to->length > end)
1660       return true; // Not enough data
1661     to->str= (const char *) *pos;
1662     *pos+= to->length;
1663     return false;
1664   }
1665 public:
Field_data_type_info_array()1666   Field_data_type_info_array()
1667    :m_array(NULL), m_count(0)
1668   { }
count() const1669   uint count() const
1670   {
1671     return m_count;
1672   }
element(uint i) const1673   const Elem& element(uint i) const
1674   {
1675     DBUG_ASSERT(i < m_count);
1676     return m_array[i];
1677   }
parse(MEM_ROOT * root,uint count,LEX_CUSTRING & image)1678   bool parse(MEM_ROOT *root, uint count, LEX_CUSTRING &image)
1679   {
1680     const uchar *pos= image.str;
1681     const uchar *end= pos + image.length;
1682     if (alloc(root, count))
1683       return true;
1684     for (uint i= 0; i < count && pos < end; i++)
1685     {
1686       LEX_CSTRING type_info;
1687       uint fieldnr= read_length((uchar**) &pos, end);
1688       if ((fieldnr == 0 && i > 0) || fieldnr >= count)
1689         return true; // Bad data
1690       if (read_string(&type_info, (uchar**) &pos, end) || type_info.length == 0)
1691         return true; // Bad data
1692       m_array[fieldnr].set(type_info);
1693     }
1694     return pos < end; // Error if some data is still left
1695   }
1696 };
1697 
1698 
1699 /**
1700   Read data from a binary .frm file image into a TABLE_SHARE
1701 
1702   @param write   Write the .frm and .par file.  These are not created if
1703                  the function returns an error.
1704 
1705   @note
1706   frm bytes at the following offsets are unused in MariaDB 10.0:
1707 
1708   8..9    (used to be the number of "form names")
1709   28..29  (used to be key_info_length)
1710 
1711   They're still set, for compatibility reasons, but never read.
1712 
1713   42..46 are unused since 5.0 (were for RAID support)
1714   Also, there're few unused bytes in forminfo.
1715 */
1716 
init_from_binary_frm_image(THD * thd,bool write,const uchar * frm_image,size_t frm_length,const uchar * par_image,size_t par_length)1717 int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write,
1718                                             const uchar *frm_image,
1719                                             size_t frm_length,
1720                                             const uchar *par_image,
1721                                             size_t par_length)
1722 {
1723   TABLE_SHARE *share= this;
1724   uint new_frm_ver, field_pack_length, new_field_pack_flag;
1725   uint interval_count, interval_parts, read_length, int_length;
1726   uint total_typelib_value_count;
1727   uint db_create_options, keys, key_parts, n_length;
1728   uint com_length, null_bit_pos, UNINIT_VAR(mysql57_vcol_null_bit_pos), bitmap_count;
1729   uint i, hash_fields= 0;
1730   bool use_hash, mysql57_null_bits= 0;
1731   LEX_STRING keynames= {NULL, 0};
1732   char *names, *comment_pos;
1733   const uchar *forminfo;
1734   const uchar *frm_image_end = frm_image + frm_length;
1735   uchar *record, *null_flags, *null_pos, *UNINIT_VAR(mysql57_vcol_null_pos);
1736   const uchar *disk_buff, *strpos;
1737   ulong pos, record_offset;
1738   ulong rec_buff_length;
1739   handler *handler_file= 0;
1740   KEY	*keyinfo;
1741   KEY_PART_INFO *key_part= NULL;
1742   Field  **field_ptr, *reg_field;
1743   const char **interval_array;
1744   uint *typelib_value_lengths= NULL;
1745   enum legacy_db_type legacy_db_type;
1746   my_bitmap_map *bitmaps;
1747   bool null_bits_are_used;
1748   uint vcol_screen_length;
1749   uchar *vcol_screen_pos;
1750   LEX_CUSTRING options;
1751   KEY first_keyinfo;
1752   uint len;
1753   uint ext_key_parts= 0;
1754   plugin_ref se_plugin= 0;
1755   bool vers_can_native= false, frm_created= 0;
1756   Field_data_type_info_array field_data_type_info_array;
1757   MEM_ROOT *old_root= thd->mem_root;
1758   Virtual_column_info **table_check_constraints;
1759   extra2_fields extra2;
1760   DBUG_ENTER("TABLE_SHARE::init_from_binary_frm_image");
1761 
1762   keyinfo= &first_keyinfo;
1763   thd->mem_root= &share->mem_root;
1764 
1765   if (frm_length < FRM_HEADER_SIZE + FRM_FORMINFO_SIZE)
1766     goto err;
1767 
1768   if (write)
1769   {
1770     frm_created= 1;
1771     if (write_frm_image(frm_image, frm_length))
1772       goto err;
1773 #ifdef WITH_PARTITION_STORAGE_ENGINE
1774     if (par_image)
1775       if (write_par_image(par_image, par_length))
1776         goto err;
1777 #endif
1778   }
1779 
1780   share->frm_version= frm_image[2];
1781   /*
1782     Check if .frm file created by MySQL 5.0. In this case we want to
1783     display CHAR fields as CHAR and not as VARCHAR.
1784     We do it this way as we want to keep the old frm version to enable
1785     MySQL 4.1 to read these files.
1786   */
1787   if (share->frm_version == FRM_VER_TRUE_VARCHAR -1 && frm_image[33] == 5)
1788     share->frm_version= FRM_VER_TRUE_VARCHAR;
1789 
1790   new_field_pack_flag= frm_image[27];
1791   new_frm_ver= (frm_image[2] - FRM_VER);
1792   field_pack_length= new_frm_ver < 2 ? 11 : 17;
1793 
1794   /* Length of the MariaDB extra2 segment in the form file. */
1795   len = uint2korr(frm_image+4);
1796 
1797   if (read_extra2(frm_image, len, &extra2))
1798     goto err;
1799 
1800   tabledef_version.length= extra2.version.length;
1801   tabledef_version.str= (uchar*)memdup_root(&mem_root, extra2.version.str,
1802                                                        extra2.version.length);
1803   if (!tabledef_version.str)
1804     goto err;
1805 
1806   /* remember but delay parsing until we have read fields and keys */
1807   options= extra2.options;
1808 
1809 #ifdef WITH_PARTITION_STORAGE_ENGINE
1810   if (extra2.engine)
1811   {
1812     share->default_part_plugin= ha_resolve_by_name(NULL, &extra2.engine, false);
1813     if (!share->default_part_plugin)
1814       goto err;
1815   }
1816 #endif
1817 
1818   if (frm_length < FRM_HEADER_SIZE + len ||
1819       !(pos= uint4korr(frm_image + FRM_HEADER_SIZE + len)))
1820     goto err;
1821 
1822   forminfo= frm_image + pos;
1823   if (forminfo + FRM_FORMINFO_SIZE >= frm_image_end)
1824     goto err;
1825 
1826 #ifdef WITH_PARTITION_STORAGE_ENGINE
1827   if (frm_image[61] && !share->default_part_plugin)
1828   {
1829     enum legacy_db_type db_type= (enum legacy_db_type) (uint) frm_image[61];
1830     share->default_part_plugin= ha_lock_engine(NULL, ha_checktype(thd, db_type));
1831     if (!share->default_part_plugin)
1832       goto err;
1833   }
1834 #endif
1835   legacy_db_type= (enum legacy_db_type) (uint) frm_image[3];
1836   /*
1837     if the storage engine is dynamic, no point in resolving it by its
1838     dynamically allocated legacy_db_type. We will resolve it later by name.
1839   */
1840   if (legacy_db_type > DB_TYPE_UNKNOWN &&
1841       legacy_db_type < DB_TYPE_FIRST_DYNAMIC)
1842     se_plugin= ha_lock_engine(NULL, ha_checktype(thd, legacy_db_type));
1843   share->db_create_options= db_create_options= uint2korr(frm_image+30);
1844   share->db_options_in_use= share->db_create_options;
1845   share->mysql_version= uint4korr(frm_image+51);
1846   share->table_type= TABLE_TYPE_NORMAL;
1847   share->null_field_first= 0;
1848   if (!frm_image[32])				// New frm file in 3.23
1849   {
1850     uint cs_org= (((uint) frm_image[41]) << 8) + (uint) frm_image[38];
1851     uint cs_new= upgrade_collation(share->mysql_version, cs_org);
1852     if (cs_org != cs_new)
1853       share->incompatible_version|= HA_CREATE_USED_CHARSET;
1854 
1855     share->avg_row_length= uint4korr(frm_image+34);
1856     share->transactional= (ha_choice)
1857       enum_value_with_check(thd, share, "transactional", frm_image[39] & 3, HA_CHOICE_MAX);
1858     share->page_checksum= (ha_choice)
1859       enum_value_with_check(thd, share, "page_checksum", (frm_image[39] >> 2) & 3, HA_CHOICE_MAX);
1860     if (((ha_choice) enum_value_with_check(thd, share, "sequence",
1861                                            (frm_image[39] >> 4) & 3,
1862                                            HA_CHOICE_MAX)) == HA_CHOICE_YES)
1863     {
1864       share->table_type= TABLE_TYPE_SEQUENCE;
1865       share->sequence= new (&share->mem_root) SEQUENCE();
1866       share->non_determinstic_insert= true;
1867     }
1868     share->row_type= (enum row_type)
1869       enum_value_with_check(thd, share, "row_format", frm_image[40], ROW_TYPE_MAX);
1870 
1871     if (cs_new && !(share->table_charset= get_charset(cs_new, MYF(MY_WME))))
1872       goto err;
1873     share->null_field_first= 1;
1874     share->stats_sample_pages= uint2korr(frm_image+42);
1875     share->stats_auto_recalc= (enum_stats_auto_recalc)(frm_image[44]);
1876     share->table_check_constraints= uint2korr(frm_image+45);
1877   }
1878   if (!share->table_charset)
1879   {
1880     const CHARSET_INFO *cs= thd->variables.collation_database;
1881     /* unknown charset in frm_image[38] or pre-3.23 frm */
1882     if (cs->use_mb())
1883     {
1884       /* Warn that we may be changing the size of character columns */
1885       sql_print_warning("'%s' had no or invalid character set, "
1886                         "and default character set is multi-byte, "
1887                         "so character column sizes may have changed",
1888                         share->path.str);
1889     }
1890     share->table_charset= cs;
1891   }
1892 
1893   share->db_record_offset= 1;
1894   share->max_rows= uint4korr(frm_image+18);
1895   share->min_rows= uint4korr(frm_image+22);
1896 
1897   /* Read keyinformation */
1898   disk_buff= frm_image + uint2korr(frm_image+6);
1899 
1900   if (disk_buff + 6 >= frm_image_end)
1901     goto err;
1902 
1903   if (disk_buff[0] & 0x80)
1904   {
1905     keys=      (disk_buff[1] << 7) | (disk_buff[0] & 0x7f);
1906     share->key_parts= key_parts= uint2korr(disk_buff+2);
1907   }
1908   else
1909   {
1910     keys=      disk_buff[0];
1911     share->key_parts= key_parts= disk_buff[1];
1912   }
1913   share->keys_for_keyread.init(0);
1914   share->keys_in_use.init(keys);
1915   ext_key_parts= key_parts;
1916 
1917   len= (uint) uint2korr(disk_buff+4);
1918 
1919   share->reclength = uint2korr(frm_image+16);
1920   share->stored_rec_length= share->reclength;
1921   if (frm_image[26] == 1)
1922     share->system= 1;				/* one-record-database */
1923 
1924   record_offset= (ulong) (uint2korr(frm_image+6)+
1925                           ((uint2korr(frm_image+14) == 0xffff ?
1926                             uint4korr(frm_image+47) : uint2korr(frm_image+14))));
1927 
1928   if (record_offset + share->reclength >= frm_length)
1929     goto err;
1930 
1931   if ((n_length= uint4korr(frm_image+55)))
1932   {
1933     /* Read extra data segment */
1934     const uchar *next_chunk, *buff_end;
1935     DBUG_PRINT("info", ("extra segment size is %u bytes", n_length));
1936     next_chunk= frm_image + record_offset + share->reclength;
1937     buff_end= next_chunk + n_length;
1938 
1939     if (buff_end >= frm_image_end)
1940       goto err;
1941 
1942     share->connect_string.length= uint2korr(next_chunk);
1943     if (!(share->connect_string.str= strmake_root(&share->mem_root,
1944                                                   (char*) next_chunk + 2,
1945                                                   share->connect_string.
1946                                                   length)))
1947     {
1948       goto err;
1949     }
1950     next_chunk+= share->connect_string.length + 2;
1951     if (next_chunk + 2 < buff_end)
1952     {
1953       uint str_db_type_length= uint2korr(next_chunk);
1954       LEX_CSTRING name;
1955       name.str= (char*) next_chunk + 2;
1956       name.length= str_db_type_length;
1957 
1958       plugin_ref tmp_plugin= ha_resolve_by_name(thd, &name, false);
1959       if (tmp_plugin != NULL && !plugin_equals(tmp_plugin, se_plugin) &&
1960           legacy_db_type != DB_TYPE_S3)
1961       {
1962         if (se_plugin)
1963         {
1964           /* bad file, legacy_db_type did not match the name */
1965           sql_print_warning("%s.frm is inconsistent: engine typecode %d, engine name %s (%d)",
1966                         share->normalized_path.str, legacy_db_type,
1967                         plugin_name(tmp_plugin)->str,
1968                         ha_legacy_type(plugin_data(tmp_plugin, handlerton *)));
1969         }
1970         /*
1971           tmp_plugin is locked with a local lock.
1972           we unlock the old value of se_plugin before
1973           replacing it with a globally locked version of tmp_plugin
1974         */
1975         plugin_unlock(NULL, se_plugin);
1976         se_plugin= plugin_lock(NULL, tmp_plugin);
1977       }
1978 #ifdef WITH_PARTITION_STORAGE_ENGINE
1979       else if (str_db_type_length == 9 &&
1980                !strncmp((char *) next_chunk + 2, "partition", 9))
1981       {
1982         /*
1983           Use partition handler
1984           tmp_plugin is locked with a local lock.
1985           we unlock the old value of se_plugin before
1986           replacing it with a globally locked version of tmp_plugin
1987         */
1988         /* Check if the partitioning engine is ready */
1989         if (!plugin_is_ready(&name, MYSQL_STORAGE_ENGINE_PLUGIN))
1990         {
1991           my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0),
1992                    "--skip-partition");
1993           goto err;
1994         }
1995         plugin_unlock(NULL, se_plugin);
1996         se_plugin= ha_lock_engine(NULL, partition_hton);
1997       }
1998 #endif
1999       else if (!tmp_plugin)
2000       {
2001         /* purecov: begin inspected */
2002         ((char*) name.str)[name.length]=0;
2003         my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), name.str);
2004         goto err;
2005         /* purecov: end */
2006       }
2007       next_chunk+= str_db_type_length + 2;
2008     }
2009 
2010     share->set_use_ext_keys_flag(plugin_hton(se_plugin)->flags & HTON_SUPPORTS_EXTENDED_KEYS);
2011 
2012     if (create_key_infos(disk_buff + 6, frm_image_end, keys, keyinfo,
2013                          new_frm_ver, &ext_key_parts,
2014                          share, len, &first_keyinfo, &keynames))
2015       goto err;
2016 
2017     if (next_chunk + 5 < buff_end)
2018     {
2019       uint32 partition_info_str_len = uint4korr(next_chunk);
2020 #ifdef WITH_PARTITION_STORAGE_ENGINE
2021       if ((share->partition_info_buffer_size=
2022              share->partition_info_str_len= partition_info_str_len))
2023       {
2024         if (!(share->partition_info_str= (char*)
2025               memdup_root(&share->mem_root, next_chunk + 4,
2026                           partition_info_str_len + 1)))
2027         {
2028           goto err;
2029         }
2030       }
2031 #else
2032       if (partition_info_str_len)
2033       {
2034         DBUG_PRINT("info", ("WITH_PARTITION_STORAGE_ENGINE is not defined"));
2035         goto err;
2036       }
2037 #endif
2038       next_chunk+= 5 + partition_info_str_len;
2039     }
2040     if (share->mysql_version >= 50110 && next_chunk < buff_end)
2041     {
2042       /* New auto_partitioned indicator introduced in 5.1.11 */
2043 #ifdef WITH_PARTITION_STORAGE_ENGINE
2044       share->auto_partitioned= *next_chunk;
2045 #endif
2046       next_chunk++;
2047     }
2048     keyinfo= share->key_info;
2049     for (i= 0; i < keys; i++, keyinfo++)
2050     {
2051       if (keyinfo->flags & HA_USES_PARSER)
2052       {
2053         LEX_CSTRING parser_name;
2054         if (next_chunk >= buff_end)
2055         {
2056           DBUG_PRINT("error",
2057                      ("fulltext key uses parser that is not defined in .frm"));
2058           goto err;
2059         }
2060         parser_name.str= (char*) next_chunk;
2061         parser_name.length= strlen((char*) next_chunk);
2062         next_chunk+= parser_name.length + 1;
2063         keyinfo->parser= my_plugin_lock_by_name(NULL, &parser_name,
2064                                                 MYSQL_FTPARSER_PLUGIN);
2065         if (! keyinfo->parser)
2066         {
2067           my_error(ER_PLUGIN_IS_NOT_LOADED, MYF(0), parser_name.str);
2068           goto err;
2069         }
2070       }
2071     }
2072 
2073     if (forminfo[46] == (uchar)255)
2074     {
2075       //reading long table comment
2076       if (next_chunk + 2 > buff_end)
2077       {
2078           DBUG_PRINT("error",
2079                      ("long table comment is not defined in .frm"));
2080           goto err;
2081       }
2082       share->comment.length = uint2korr(next_chunk);
2083       if (! (share->comment.str= strmake_root(&share->mem_root,
2084              (char*)next_chunk + 2, share->comment.length)))
2085       {
2086           goto err;
2087       }
2088       next_chunk+= 2 + share->comment.length;
2089     }
2090 
2091     DBUG_ASSERT(next_chunk <= buff_end);
2092 
2093     if (share->db_create_options & HA_OPTION_TEXT_CREATE_OPTIONS_legacy)
2094     {
2095       if (options.str)
2096         goto err;
2097       options.length= uint4korr(next_chunk);
2098       options.str= next_chunk + 4;
2099       next_chunk+= options.length + 4;
2100     }
2101     DBUG_ASSERT(next_chunk <= buff_end);
2102   }
2103   else
2104   {
2105     if (create_key_infos(disk_buff + 6, frm_image_end, keys, keyinfo,
2106                          new_frm_ver, &ext_key_parts,
2107                          share, len, &first_keyinfo, &keynames))
2108       goto err;
2109   }
2110   share->key_block_size= uint2korr(frm_image+62);
2111   keyinfo= share->key_info;
2112   for (uint i= 0; i < share->keys; i++, keyinfo++)
2113     if (keyinfo->algorithm == HA_KEY_ALG_LONG_HASH)
2114       hash_fields++;
2115 
2116 #ifdef WITH_PARTITION_STORAGE_ENGINE
2117   if (par_image && plugin_data(se_plugin, handlerton*) == partition_hton)
2118   {
2119     /*
2120       Discovery returned a partition plugin. Change to use it. The partition
2121       engine will then use discovery to find the rest of the plugin tables,
2122       which may be in the original engine used for discovery
2123     */
2124     share->db_plugin= se_plugin;
2125   }
2126 #endif
2127   if (share->db_plugin && !plugin_equals(share->db_plugin, se_plugin))
2128     goto err; // wrong engine (someone changed the frm under our feet?)
2129 
2130   rec_buff_length= ALIGN_SIZE(share->reclength + 1);
2131   share->rec_buff_length= rec_buff_length;
2132   if (!(record= (uchar *) alloc_root(&share->mem_root, rec_buff_length)))
2133     goto err;                          /* purecov: inspected */
2134   /* Mark bytes after record as not accessable to catch overrun bugs */
2135   MEM_NOACCESS(record + share->reclength, rec_buff_length - share->reclength);
2136   share->default_values= record;
2137   memcpy(record, frm_image + record_offset, share->reclength);
2138 
2139   disk_buff= frm_image + pos + FRM_FORMINFO_SIZE;
2140   share->fields= uint2korr(forminfo+258);
2141   if (extra2.field_flags.str && extra2.field_flags.length != share->fields)
2142     goto err;
2143   pos= uint2korr(forminfo+260);   /* Length of all screens */
2144   n_length= uint2korr(forminfo+268);
2145   interval_count= uint2korr(forminfo+270);
2146   interval_parts= uint2korr(forminfo+272);
2147   int_length= uint2korr(forminfo+274);
2148   share->null_fields= uint2korr(forminfo+282);
2149   com_length= uint2korr(forminfo+284);
2150   vcol_screen_length= uint2korr(forminfo+286);
2151   share->virtual_fields= share->default_expressions=
2152     share->field_check_constraints= share->default_fields= 0;
2153   share->visible_fields= 0;
2154   share->stored_fields= share->fields;
2155   if (forminfo[46] != (uchar)255)
2156   {
2157     share->comment.length=  (int) (forminfo[46]);
2158     share->comment.str= strmake_root(&share->mem_root, (char*) forminfo+47,
2159                                      share->comment.length);
2160   }
2161 
2162   DBUG_PRINT("info",("i_count: %d  i_parts: %d  index: %d  n_length: %d  int_length: %d  com_length: %d  vcol_screen_length: %d", interval_count,interval_parts, keys,n_length,int_length, com_length, vcol_screen_length));
2163 
2164   /*
2165     We load the following things into TYPELIBs:
2166     - One TYPELIB for field names
2167     - interval_count TYPELIBs for ENUM/SET values
2168     - One TYPELIB for key names
2169     Every TYPELIB requires one extra value with a NULL pointer and zero length,
2170     which is the end-of-values marker.
2171     TODO-10.5+:
2172     Note, we should eventually reuse this total_typelib_value_count
2173     to allocate interval_array. The below code reserves less space
2174     than total_typelib_value_count pointers. So it seems `interval_array`
2175     and `names` overlap in the memory. Too dangerous to fix in 10.1.
2176   */
2177   total_typelib_value_count=
2178     (share->fields  +              1/*end-of-values marker*/) +
2179     (interval_parts + interval_count/*end-of-values markers*/) +
2180     (keys           +              1/*end-of-values marker*/);
2181 
2182   if (!multi_alloc_root(&share->mem_root,
2183                         &share->field, (uint)(share->fields+1)*sizeof(Field*),
2184                         &share->intervals, (uint)interval_count*sizeof(TYPELIB),
2185                         &share->check_constraints, (uint) share->table_check_constraints * sizeof(Virtual_column_info*),
2186                         /*
2187                            This looks wrong: shouldn't it be (+2+interval_count)
2188                            instread of (+3) ?
2189                         */
2190                         &interval_array, (uint) (share->fields+interval_parts+ keys+3)*sizeof(char *),
2191                         &typelib_value_lengths, total_typelib_value_count * sizeof(uint *),
2192                         &names, (uint) (n_length+int_length),
2193                         &comment_pos, (uint) com_length,
2194                         &vcol_screen_pos, vcol_screen_length,
2195                         NullS))
2196 
2197     goto err;
2198 
2199   field_ptr= share->field;
2200   table_check_constraints= share->check_constraints;
2201   read_length=(uint) (share->fields * field_pack_length +
2202 		      pos+ (uint) (n_length+int_length+com_length+
2203 		                   vcol_screen_length));
2204   strpos= disk_buff+pos;
2205 
2206   if (!interval_count)
2207     share->intervals= 0;			// For better debugging
2208 
2209   share->vcol_defs.str= vcol_screen_pos;
2210   share->vcol_defs.length= vcol_screen_length;
2211 
2212   memcpy(names, strpos+(share->fields*field_pack_length), n_length+int_length);
2213   memcpy(comment_pos, disk_buff+read_length-com_length-vcol_screen_length,
2214          com_length);
2215   memcpy(vcol_screen_pos, disk_buff+read_length-vcol_screen_length,
2216          vcol_screen_length);
2217 
2218   if (fix_type_pointers(&interval_array, &typelib_value_lengths,
2219                         &share->fieldnames, 1, names, n_length) ||
2220       share->fieldnames.count != share->fields)
2221     goto err;
2222 
2223   if (fix_type_pointers(&interval_array, &typelib_value_lengths,
2224                         share->intervals, interval_count,
2225                         names + n_length, int_length))
2226     goto err;
2227 
2228   if (keynames.length &&
2229       (fix_type_pointers(&interval_array, &typelib_value_lengths,
2230                          &share->keynames, 1, keynames.str, keynames.length) ||
2231       share->keynames.count != keys))
2232     goto err;
2233 
2234  /* Allocate handler */
2235   if (!(handler_file= get_new_handler(share, thd->mem_root,
2236                                       plugin_hton(se_plugin))))
2237     goto err;
2238 
2239   if (handler_file->set_ha_share_ref(&share->ha_share))
2240     goto err;
2241 
2242   record= share->default_values-1;              /* Fieldstart = 1 */
2243   null_bits_are_used= share->null_fields != 0;
2244   if (share->null_field_first)
2245   {
2246     null_flags= null_pos= record+1;
2247     null_bit_pos= (db_create_options & HA_OPTION_PACK_RECORD) ? 0 : 1;
2248     /*
2249       null_bytes below is only correct under the condition that
2250       there are no bit fields.  Correct values is set below after the
2251       table struct is initialized
2252     */
2253     share->null_bytes= (share->null_fields + null_bit_pos + 7) / 8;
2254   }
2255 #ifndef WE_WANT_TO_SUPPORT_VERY_OLD_FRM_FILES
2256   else
2257   {
2258     share->null_bytes= (share->null_fields+7)/8;
2259     null_flags= null_pos= record + 1 + share->reclength - share->null_bytes;
2260     null_bit_pos= 0;
2261   }
2262 #endif
2263 
2264   use_hash= share->fields >= MAX_FIELDS_BEFORE_HASH;
2265   if (use_hash)
2266     use_hash= !my_hash_init(PSI_INSTRUMENT_ME, &share->name_hash,
2267                             system_charset_info, share->fields, 0, 0,
2268                             (my_hash_get_key) get_field_name, 0, 0);
2269 
2270   if (share->mysql_version >= 50700 && share->mysql_version < 100000 &&
2271       vcol_screen_length)
2272   {
2273     /*
2274       MySQL 5.7 stores the null bits for not stored fields last.
2275       Calculate the position for them.
2276     */
2277     mysql57_null_bits= 1;
2278     mysql57_vcol_null_pos= null_pos;
2279     mysql57_vcol_null_bit_pos= null_bit_pos;
2280     mysql57_calculate_null_position(share, &mysql57_vcol_null_pos,
2281                                     &mysql57_vcol_null_bit_pos,
2282                                     strpos, vcol_screen_pos);
2283   }
2284 
2285   /* Set system versioning information. */
2286   vers.name= Lex_ident(STRING_WITH_LEN("SYSTEM_TIME"));
2287   if (extra2.system_period.str == NULL)
2288   {
2289     versioned= VERS_UNDEFINED;
2290     vers.start_fieldno= 0;
2291     vers.end_fieldno= 0;
2292   }
2293   else
2294   {
2295     DBUG_PRINT("info", ("Setting system versioning information"));
2296     if (init_period_from_extra2(&vers, extra2.system_period.str,
2297                   extra2.system_period.str + extra2.system_period.length))
2298       goto err;
2299     DBUG_PRINT("info", ("Columns with system versioning: [%d, %d]",
2300                         vers.start_fieldno, vers.end_fieldno));
2301     versioned= VERS_TIMESTAMP;
2302     vers_can_native= handler_file->vers_can_native(thd);
2303     status_var_increment(thd->status_var.feature_system_versioning);
2304   } // if (system_period == NULL)
2305 
2306   if (extra2.application_period.str)
2307   {
2308     const uchar *pos= extra2.application_period.str;
2309     const uchar *end= pos + extra2.application_period.length;
2310     period.name.length= extra2_read_len(&pos, end);
2311     period.name.str= strmake_root(&mem_root, (char*)pos, period.name.length);
2312     pos+= period.name.length;
2313 
2314     period.constr_name.length= extra2_read_len(&pos, end);
2315     period.constr_name.str= strmake_root(&mem_root, (char*)pos,
2316                                          period.constr_name.length);
2317     pos+= period.constr_name.length;
2318 
2319     if (init_period_from_extra2(&period, pos, end))
2320       goto err;
2321     if (extra2_str_size(period.name.length)
2322          + extra2_str_size(period.constr_name.length)
2323          + 2 * frm_fieldno_size
2324         != extra2.application_period.length)
2325       goto err;
2326     status_var_increment(thd->status_var.feature_application_time_periods);
2327   }
2328 
2329   if (extra2.without_overlaps.str)
2330   {
2331     if (extra2.application_period.str == NULL)
2332       goto err;
2333     const uchar *key_pos= extra2.without_overlaps.str;
2334     period.unique_keys= read_frm_keyno(key_pos);
2335     for (uint k= 0; k < period.unique_keys; k++)
2336     {
2337       key_pos+= frm_keyno_size;
2338       uint key_nr= read_frm_keyno(key_pos);
2339       key_info[key_nr].without_overlaps= true;
2340     }
2341 
2342     if ((period.unique_keys + 1) * frm_keyno_size
2343         != extra2.without_overlaps.length)
2344       goto err;
2345   }
2346 
2347   if (extra2.field_data_type_info.length &&
2348       field_data_type_info_array.parse(old_root, share->fields,
2349                                        extra2.field_data_type_info))
2350     goto err;
2351 
2352   for (i=0 ; i < share->fields; i++, strpos+=field_pack_length, field_ptr++)
2353   {
2354     uint interval_nr= 0, recpos;
2355     LEX_CSTRING comment;
2356     LEX_CSTRING name;
2357     Virtual_column_info *vcol_info= 0;
2358     const Type_handler *handler;
2359     uint32 flags= 0;
2360     Column_definition_attributes attr;
2361 
2362     if (new_frm_ver >= 3)
2363     {
2364       /* new frm file in 4.1 */
2365       recpos=	    uint3korr(strpos+5);
2366       uint comment_length=uint2korr(strpos+15);
2367 
2368       if (!comment_length)
2369       {
2370 	comment.str= (char*) "";
2371 	comment.length=0;
2372       }
2373       else
2374       {
2375 	comment.str=    (char*) comment_pos;
2376 	comment.length= comment_length;
2377 	comment_pos+=   comment_length;
2378       }
2379 
2380       if ((uchar) strpos[13] == (uchar) MYSQL_TYPE_VIRTUAL
2381           && likely(share->mysql_version >= 100000))
2382       {
2383         /*
2384           MariaDB version 10.0 version.
2385           The interval_id byte in the .frm file stores the length of the
2386           expression statement for a virtual column.
2387         */
2388         uint vcol_info_length= (uint) strpos[12];
2389 
2390         if (!vcol_info_length) // Expect non-null expression
2391           goto err;
2392 
2393         attr.frm_unpack_basic(strpos);
2394         if (attr.frm_unpack_charset(share, strpos))
2395           goto err;
2396         /*
2397           Old virtual field information before 10.2
2398 
2399           Get virtual column data stored in the .frm file as follows:
2400           byte 1      = 1 | 2
2401           byte 2      = sql_type
2402           byte 3      = flags. 1 for stored_in_db
2403           [byte 4]    = optional interval_id for sql_type (if byte 1 == 2)
2404           next byte ...  = virtual column expression (text data)
2405         */
2406 
2407         vcol_info= new (&share->mem_root) Virtual_column_info();
2408         bool opt_interval_id= (uint)vcol_screen_pos[0] == 2;
2409         enum_field_types ftype= (enum_field_types) (uchar) vcol_screen_pos[1];
2410         if (!(handler= Type_handler::get_handler_by_real_type(ftype)))
2411           goto err;
2412         if (opt_interval_id)
2413           interval_nr= (uint)vcol_screen_pos[3];
2414         else if ((uint)vcol_screen_pos[0] != 1)
2415           goto err;
2416         bool stored= vcol_screen_pos[2] & 1;
2417         vcol_info->stored_in_db= stored;
2418         vcol_info->set_vcol_type(stored ? VCOL_GENERATED_STORED : VCOL_GENERATED_VIRTUAL);
2419         uint vcol_expr_length= vcol_info_length -
2420                               (uint)(FRM_VCOL_OLD_HEADER_SIZE(opt_interval_id));
2421         vcol_info->utf8= 0; // before 10.2.1 the charset was unknown
2422         int2store(vcol_screen_pos+1, vcol_expr_length); // for parse_vcol_defs()
2423         vcol_screen_pos+= vcol_info_length;
2424         share->virtual_fields++;
2425       }
2426       else
2427       {
2428         interval_nr=  (uint) strpos[12];
2429         enum_field_types field_type= (enum_field_types) strpos[13];
2430         if (!(handler= Type_handler::get_handler_by_real_type(field_type)))
2431         {
2432           if (field_type == 245 &&
2433               share->mysql_version >= 50700) // a.k.a MySQL 5.7 JSON
2434           {
2435             share->incompatible_version|= HA_CREATE_USED_ENGINE;
2436             const LEX_CSTRING mysql_json{STRING_WITH_LEN("MYSQL_JSON")};
2437             handler= Type_handler::handler_by_name_or_error(thd, mysql_json);
2438           }
2439 
2440           if (!handler)
2441             goto err; // Not supported field type
2442         }
2443         handler= handler->type_handler_frm_unpack(strpos);
2444         if (handler->Column_definition_attributes_frm_unpack(&attr, share,
2445                                                              strpos,
2446                                                              &extra2.gis))
2447           goto err;
2448 
2449         if (field_data_type_info_array.count())
2450         {
2451           const LEX_CSTRING &info= field_data_type_info_array.
2452                                      element(i).type_info();
2453           DBUG_EXECUTE_IF("frm_data_type_info",
2454             push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
2455             ER_UNKNOWN_ERROR, "DBUG: [%u] name='%s' type_info='%.*s'",
2456             i, share->fieldnames.type_names[i],
2457             (uint) info.length, info.str););
2458 
2459           if (info.length)
2460           {
2461             const Type_handler *h= Type_handler::handler_by_name_or_error(thd,
2462                                                                           info);
2463             /*
2464               This code will eventually be extended here:
2465               - If the handler was not found by name, we could
2466                 still open the table using the fallback type handler "handler",
2467                 at least for a limited set of commands.
2468               - If the handler was found by name, we could check
2469                 that "h" and "handler" have the same type code
2470                 (and maybe some other properties) to make sure
2471                 that the FRM data is consistent.
2472             */
2473             if (!h)
2474               goto err;
2475             handler= h;
2476           }
2477         }
2478       }
2479 
2480       if (((uint) strpos[10]) & MYSQL57_GENERATED_FIELD)
2481       {
2482         attr.unireg_check= Field::NONE;
2483 
2484         /*
2485           MySQL 5.7 generated fields
2486 
2487           byte 1        = 1
2488           byte 2,3      = expr length
2489           byte 4        = stored_in_db
2490           byte 5..      = expr
2491         */
2492         if ((uint)(vcol_screen_pos)[0] != 1)
2493           goto err;
2494         vcol_info= new (&share->mem_root) Virtual_column_info();
2495         uint vcol_info_length= uint2korr(vcol_screen_pos + 1);
2496         if (!vcol_info_length) // Expect non-empty expression
2497           goto err;
2498         vcol_info->stored_in_db= vcol_screen_pos[3];
2499         vcol_info->utf8= 0;
2500         vcol_screen_pos+= vcol_info_length + MYSQL57_GCOL_HEADER_SIZE;;
2501         share->virtual_fields++;
2502       }
2503     }
2504     else
2505     {
2506       attr.length= (uint) strpos[3];
2507       recpos=	    uint2korr(strpos+4),
2508       attr.pack_flag=    uint2korr(strpos+6);
2509       if (f_is_num(attr.pack_flag))
2510       {
2511         attr.decimals= f_decimals(attr.pack_flag);
2512         attr.pack_flag&= ~FIELDFLAG_DEC_MASK;
2513       }
2514       attr.pack_flag&=   ~FIELDFLAG_NO_DEFAULT;     // Safety for old files
2515       attr.unireg_check=  (Field::utype) MTYP_TYPENR((uint) strpos[8]);
2516       interval_nr=  (uint) strpos[10];
2517 
2518       /* old frm file */
2519       enum_field_types ftype= (enum_field_types) f_packtype(attr.pack_flag);
2520       if (!(handler= Type_handler::get_handler_by_real_type(ftype)))
2521         goto err; // Not supported field type
2522 
2523       if (f_is_binary(attr.pack_flag))
2524       {
2525         /*
2526           Try to choose the best 4.1 type:
2527           - for 4.0 "CHAR(N) BINARY" or "VARCHAR(N) BINARY"
2528            try to find a binary collation for character set.
2529           - for other types (e.g. BLOB) just use my_charset_bin.
2530         */
2531         if (!f_is_blob(attr.pack_flag))
2532         {
2533           // 3.23 or 4.0 string
2534           if (!(attr.charset= get_charset_by_csname(share->table_charset->csname,
2535                                                     MY_CS_BINSORT, MYF(0))))
2536             attr.charset= &my_charset_bin;
2537         }
2538       }
2539       else
2540         attr.charset= share->table_charset;
2541       bzero((char*) &comment, sizeof(comment));
2542       if ((!(handler= old_frm_type_handler(attr.pack_flag, interval_nr))))
2543         goto err; // Not supported field type
2544     }
2545 
2546     /* Remove >32 decimals from old files */
2547     if (share->mysql_version < 100200)
2548       attr.pack_flag&= ~FIELDFLAG_LONG_DECIMAL;
2549 
2550     if (interval_nr && attr.charset->mbminlen > 1)
2551     {
2552       /* Unescape UCS2 intervals from HEX notation */
2553       TYPELIB *interval= share->intervals + interval_nr - 1;
2554       unhex_type2(interval);
2555     }
2556 
2557 #ifndef TO_BE_DELETED_ON_PRODUCTION
2558     if (handler->real_field_type() == MYSQL_TYPE_NEWDECIMAL &&
2559         !share->mysql_version)
2560     {
2561       /*
2562         Fix pack length of old decimal values from 5.0.3 -> 5.0.4
2563         The difference is that in the old version we stored precision
2564         in the .frm table while we now store the display_length
2565       */
2566       uint decimals= f_decimals(attr.pack_flag);
2567       attr.length=
2568         my_decimal_precision_to_length((uint) attr.length, decimals,
2569                                        f_is_dec(attr.pack_flag) == 0);
2570       sql_print_error("Found incompatible DECIMAL field '%s' in %s; "
2571                       "Please do \"ALTER TABLE '%s' FORCE\" to fix it!",
2572                       share->fieldnames.type_names[i], share->table_name.str,
2573                       share->table_name.str);
2574       push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
2575                           ER_CRASHED_ON_USAGE,
2576                           "Found incompatible DECIMAL field '%s' in %s; "
2577                           "Please do \"ALTER TABLE '%s' FORCE\" to fix it!",
2578                           share->fieldnames.type_names[i],
2579                           share->table_name.str,
2580                           share->table_name.str);
2581       share->crashed= 1;                        // Marker for CHECK TABLE
2582     }
2583 #endif
2584 
2585     if (mysql57_null_bits && vcol_info && !vcol_info->stored_in_db)
2586     {
2587       swap_variables(uchar*, null_pos, mysql57_vcol_null_pos);
2588       swap_variables(uint, null_bit_pos, mysql57_vcol_null_bit_pos);
2589     }
2590 
2591     if (versioned)
2592     {
2593       if (i == vers.start_fieldno)
2594         flags|= VERS_ROW_START;
2595       else if (i == vers.end_fieldno)
2596         flags|= VERS_ROW_END;
2597 
2598       if (flags & VERS_SYSTEM_FIELD)
2599       {
2600         switch (handler->real_field_type())
2601         {
2602         case MYSQL_TYPE_TIMESTAMP2:
2603           break;
2604         case MYSQL_TYPE_LONGLONG:
2605           if (vers_can_native)
2606           {
2607             versioned= VERS_TRX_ID;
2608             break;
2609           }
2610           /* Fallthrough */
2611         default:
2612           my_error(ER_VERS_FIELD_WRONG_TYPE, MYF(0), fieldnames.type_names[i],
2613             versioned == VERS_TIMESTAMP ? "TIMESTAMP(6)" : "BIGINT(20) UNSIGNED",
2614             table_name.str);
2615           goto err;
2616         }
2617       }
2618     }
2619 
2620     /* Convert pre-10.2.2 timestamps to use Field::default_value */
2621     name.str= fieldnames.type_names[i];
2622     name.length= strlen(name.str);
2623     attr.interval= interval_nr ? share->intervals + interval_nr - 1 : NULL;
2624     Record_addr addr(record + recpos, null_pos, null_bit_pos);
2625     *field_ptr= reg_field=
2626       attr.make_field(share, &share->mem_root, &addr, handler, &name, flags);
2627     if (!reg_field)				// Not supported field type
2628       goto err;
2629 
2630     if (attr.unireg_check == Field::TIMESTAMP_DNUN_FIELD ||
2631         attr.unireg_check == Field::TIMESTAMP_DN_FIELD)
2632     {
2633       reg_field->default_value= new (&share->mem_root) Virtual_column_info();
2634       reg_field->default_value->set_vcol_type(VCOL_DEFAULT);
2635       reg_field->default_value->stored_in_db= 1;
2636       share->default_expressions++;
2637     }
2638 
2639     reg_field->field_index= i;
2640     reg_field->comment=comment;
2641     reg_field->vcol_info= vcol_info;
2642     reg_field->flags|= flags;
2643     if (extra2.field_flags.str)
2644     {
2645       uchar flags= *extra2.field_flags.str++;
2646       if (flags & VERS_OPTIMIZED_UPDATE)
2647         reg_field->flags|= VERS_UPDATE_UNVERSIONED_FLAG;
2648 
2649       reg_field->invisible= f_visibility(flags);
2650     }
2651     if (reg_field->invisible == INVISIBLE_USER)
2652       status_var_increment(thd->status_var.feature_invisible_columns);
2653     if (!reg_field->invisible)
2654       share->visible_fields++;
2655     if (handler->real_field_type() == MYSQL_TYPE_BIT &&
2656         !f_bit_as_char(attr.pack_flag))
2657     {
2658       null_bits_are_used= 1;
2659       if ((null_bit_pos+= (uint) (attr.length & 7)) > 7)
2660       {
2661         null_pos++;
2662         null_bit_pos-= 8;
2663       }
2664     }
2665     if (!(reg_field->flags & NOT_NULL_FLAG))
2666     {
2667       if (!(null_bit_pos= (null_bit_pos + 1) & 7))
2668         null_pos++;
2669     }
2670 
2671     if (vcol_info)
2672     {
2673       vcol_info->name= reg_field->field_name;
2674       if (mysql57_null_bits && !vcol_info->stored_in_db)
2675       {
2676         /* MySQL 5.7 has null bits last */
2677         swap_variables(uchar*, null_pos, mysql57_vcol_null_pos);
2678         swap_variables(uint, null_bit_pos, mysql57_vcol_null_bit_pos);
2679       }
2680     }
2681 
2682     if (f_no_default(attr.pack_flag))
2683       reg_field->flags|= NO_DEFAULT_VALUE_FLAG;
2684 
2685     if (reg_field->unireg_check == Field::NEXT_NUMBER)
2686       share->found_next_number_field= field_ptr;
2687 
2688     if (use_hash && my_hash_insert(&share->name_hash, (uchar*) field_ptr))
2689       goto err;
2690     if (!reg_field->stored_in_db())
2691     {
2692       share->stored_fields--;
2693       if (share->stored_rec_length>=recpos)
2694         share->stored_rec_length= recpos-1;
2695     }
2696     if (reg_field->has_update_default_function())
2697     {
2698       has_update_default_function= 1;
2699       if (!reg_field->default_value)
2700         share->default_fields++;
2701     }
2702   }
2703   *field_ptr=0;					// End marker
2704   /* Sanity checks: */
2705   DBUG_ASSERT(share->fields>=share->stored_fields);
2706   DBUG_ASSERT(share->reclength>=share->stored_rec_length);
2707 
2708   if (mysql57_null_bits)
2709   {
2710     /* We want to store the value for the last bits */
2711     swap_variables(uchar*, null_pos, mysql57_vcol_null_pos);
2712     swap_variables(uint, null_bit_pos, mysql57_vcol_null_bit_pos);
2713     DBUG_ASSERT((null_pos + (null_bit_pos + 7) / 8) <= share->field[0]->ptr);
2714   }
2715 
2716   /* Fix key->name and key_part->field */
2717   if (key_parts)
2718   {
2719     keyinfo= share->key_info;
2720     uint hash_field_used_no= share->fields - hash_fields;
2721     KEY_PART_INFO *hash_keypart;
2722     Field *hash_field;
2723     uint offset= share->reclength - HA_HASH_FIELD_LENGTH * hash_fields;
2724     for (uint i= 0; i < share->keys; i++, keyinfo++)
2725     {
2726       /* We need set value in hash key_part */
2727       if (keyinfo->algorithm == HA_KEY_ALG_LONG_HASH)
2728       {
2729         share->long_unique_table= 1;
2730         hash_keypart= keyinfo->key_part + keyinfo->user_defined_key_parts;
2731         hash_keypart->length= HA_HASH_KEY_LENGTH_WITHOUT_NULL;
2732         hash_keypart->store_length= hash_keypart->length;
2733         hash_keypart->type= HA_KEYTYPE_ULONGLONG;
2734         hash_keypart->key_part_flag= 0;
2735         hash_keypart->key_type= 32834;
2736         /* Last n fields are unique_index_hash fields*/
2737         hash_keypart->offset= offset;
2738         hash_keypart->fieldnr= hash_field_used_no + 1;
2739         hash_field= share->field[hash_field_used_no];
2740         hash_field->flags|= LONG_UNIQUE_HASH_FIELD;//Used in parse_vcol_defs
2741         keyinfo->flags|= HA_NOSAME;
2742         share->virtual_fields++;
2743         share->stored_fields--;
2744         if (record + share->stored_rec_length >= hash_field->ptr)
2745           share->stored_rec_length= (ulong)(hash_field->ptr - record - 1);
2746         hash_field_used_no++;
2747         offset+= HA_HASH_FIELD_LENGTH;
2748       }
2749     }
2750     uint add_first_key_parts= 0;
2751     longlong ha_option= handler_file->ha_table_flags();
2752     keyinfo= share->key_info;
2753     uint primary_key= my_strcasecmp(system_charset_info, share->keynames.type_names[0],
2754                                     primary_key_name) ? MAX_KEY : 0;
2755     KEY* key_first_info= NULL;
2756 
2757     if (primary_key >= MAX_KEY && keyinfo->flags & HA_NOSAME &&
2758         keyinfo->algorithm != HA_KEY_ALG_LONG_HASH)
2759     {
2760       /*
2761         If the UNIQUE key doesn't have NULL columns and is not a part key
2762         declare this as a primary key.
2763       */
2764       primary_key= 0;
2765       key_part= keyinfo->key_part;
2766       for (i=0 ; i < keyinfo->user_defined_key_parts ;i++)
2767       {
2768         DBUG_ASSERT(key_part[i].fieldnr > 0);
2769         // Table field corresponding to the i'th key part.
2770         Field *table_field= share->field[key_part[i].fieldnr - 1];
2771 
2772         /*
2773           If the key column is of NOT NULL BLOB type, then it
2774           will definitly have key prefix. And if key part prefix size
2775           is equal to the BLOB column max size, then we can promote
2776           it to primary key.
2777         */
2778         if (!table_field->real_maybe_null() &&
2779             table_field->type() == MYSQL_TYPE_BLOB &&
2780             table_field->field_length == key_part[i].length)
2781           continue;
2782 
2783         if (table_field->real_maybe_null() ||
2784             table_field->key_length() != key_part[i].length)
2785         {
2786           primary_key= MAX_KEY;		// Can't be used
2787           break;
2788         }
2789       }
2790     }
2791 
2792     if (share->use_ext_keys)
2793     {
2794       if (primary_key >= MAX_KEY)
2795       {
2796         add_first_key_parts= 0;
2797         share->set_use_ext_keys_flag(FALSE);
2798       }
2799       else
2800       {
2801         add_first_key_parts= first_keyinfo.user_defined_key_parts;
2802         /*
2803           Do not add components of the primary key starting from
2804           the major component defined over the beginning of a field.
2805 	*/
2806 	for (i= 0; i < first_keyinfo.user_defined_key_parts; i++)
2807 	{
2808           uint fieldnr= keyinfo[0].key_part[i].fieldnr;
2809           if (share->field[fieldnr-1]->key_length() !=
2810               keyinfo[0].key_part[i].length)
2811 	  {
2812             add_first_key_parts= i;
2813             break;
2814           }
2815         }
2816       }
2817     }
2818 
2819     key_first_info= keyinfo;
2820     for (uint key=0 ; key < keys ; key++,keyinfo++)
2821     {
2822       uint usable_parts= 0;
2823       keyinfo->name.str=    share->keynames.type_names[key];
2824       keyinfo->name.length= strlen(keyinfo->name.str);
2825       keyinfo->cache_name=
2826         (uchar*) alloc_root(&share->mem_root,
2827                             share->table_cache_key.length+
2828                             keyinfo->name.length + 1);
2829       if (keyinfo->cache_name)           // If not out of memory
2830       {
2831         uchar *pos= keyinfo->cache_name;
2832         memcpy(pos, share->table_cache_key.str, share->table_cache_key.length);
2833         memcpy(pos + share->table_cache_key.length, keyinfo->name.str,
2834                keyinfo->name.length+1);
2835       }
2836 
2837       if (ext_key_parts > share->key_parts && key)
2838       {
2839         KEY_PART_INFO *new_key_part= (keyinfo-1)->key_part +
2840                                      (keyinfo-1)->ext_key_parts;
2841         uint add_keyparts_for_this_key= add_first_key_parts;
2842         uint len_null_byte= 0, ext_key_length= 0;
2843         Field *field;
2844 
2845         if ((keyinfo-1)->algorithm == HA_KEY_ALG_LONG_HASH)
2846           new_key_part++; // reserved for the hash value
2847 
2848         /*
2849           Do not extend the key that contains a component
2850           defined over the beginning of a field.
2851 	*/
2852         for (i= 0; i < keyinfo->user_defined_key_parts; i++)
2853         {
2854           uint length_bytes= 0;
2855           uint fieldnr= keyinfo->key_part[i].fieldnr;
2856           field= share->field[fieldnr-1];
2857 
2858           if (field->null_ptr)
2859             len_null_byte= HA_KEY_NULL_LENGTH;
2860 
2861           if (keyinfo->algorithm != HA_KEY_ALG_LONG_HASH)
2862             length_bytes= field->key_part_length_bytes();
2863 
2864           ext_key_length+= keyinfo->key_part[i].length + len_null_byte
2865                             + length_bytes;
2866           if (field->key_length() != keyinfo->key_part[i].length)
2867 	  {
2868             add_keyparts_for_this_key= 0;
2869             break;
2870           }
2871         }
2872 
2873         if (add_keyparts_for_this_key)
2874         {
2875           for (i= 0; i < add_keyparts_for_this_key; i++)
2876           {
2877             uint pk_part_length= key_first_info->key_part[i].store_length;
2878             if (keyinfo->ext_key_part_map & 1<<i)
2879             {
2880               if (ext_key_length + pk_part_length > MAX_DATA_LENGTH_FOR_KEY)
2881               {
2882                 add_keyparts_for_this_key= i;
2883                 break;
2884               }
2885               ext_key_length+= pk_part_length;
2886             }
2887           }
2888         }
2889 
2890         if (add_keyparts_for_this_key < keyinfo->ext_key_parts -
2891                                         keyinfo->user_defined_key_parts)
2892         {
2893           share->ext_key_parts-= keyinfo->ext_key_parts;
2894           key_part_map ext_key_part_map= keyinfo->ext_key_part_map;
2895           keyinfo->ext_key_parts= keyinfo->user_defined_key_parts;
2896           keyinfo->ext_key_flags= keyinfo->flags;
2897 	  keyinfo->ext_key_part_map= 0;
2898           for (i= 0; i < add_keyparts_for_this_key; i++)
2899 	  {
2900             if (ext_key_part_map & 1<<i)
2901 	    {
2902               keyinfo->ext_key_part_map|= 1<<i;
2903 	      keyinfo->ext_key_parts++;
2904             }
2905           }
2906           share->ext_key_parts+= keyinfo->ext_key_parts;
2907         }
2908         if (new_key_part != keyinfo->key_part)
2909 	{
2910           memmove(new_key_part, keyinfo->key_part,
2911                   sizeof(KEY_PART_INFO) * keyinfo->ext_key_parts);
2912           keyinfo->key_part= new_key_part;
2913         }
2914       }
2915 
2916       /* Fix fulltext keys for old .frm files */
2917       if (share->key_info[key].flags & HA_FULLTEXT)
2918 	share->key_info[key].algorithm= HA_KEY_ALG_FULLTEXT;
2919 
2920       key_part= keyinfo->key_part;
2921       uint key_parts= share->use_ext_keys ? keyinfo->ext_key_parts :
2922 	                                    keyinfo->user_defined_key_parts;
2923       if (keyinfo->algorithm == HA_KEY_ALG_LONG_HASH)
2924         key_parts++;
2925       for (i=0; i < key_parts; key_part++, i++)
2926       {
2927         Field *field;
2928 	if (new_field_pack_flag <= 1)
2929 	  key_part->fieldnr= (uint16) find_field(share->field,
2930                                                  share->default_values,
2931                                                  (uint) key_part->offset,
2932                                                  (uint) key_part->length);
2933 	if (!key_part->fieldnr)
2934           goto err;
2935 
2936         field= key_part->field= share->field[key_part->fieldnr-1];
2937         key_part->type= field->key_type();
2938 
2939         if (field->invisible > INVISIBLE_USER && !field->vers_sys_field())
2940           if (keyinfo->algorithm != HA_KEY_ALG_LONG_HASH)
2941             keyinfo->flags |= HA_INVISIBLE_KEY;
2942         if (field->null_ptr)
2943         {
2944           key_part->null_offset=(uint) ((uchar*) field->null_ptr -
2945                                         share->default_values);
2946           key_part->null_bit= field->null_bit;
2947           key_part->store_length+=HA_KEY_NULL_LENGTH;
2948           keyinfo->flags|=HA_NULL_PART_KEY;
2949           keyinfo->key_length+= HA_KEY_NULL_LENGTH;
2950         }
2951 
2952         key_part->key_part_flag|= field->key_part_flag();
2953         uint16 key_part_length_bytes= field->key_part_length_bytes();
2954         key_part->store_length+= key_part_length_bytes;
2955         if (i < keyinfo->user_defined_key_parts)
2956           keyinfo->key_length+= key_part_length_bytes;
2957 
2958         if (i == 0 && key != primary_key)
2959           field->flags |= (((keyinfo->flags & HA_NOSAME ||
2960                             keyinfo->algorithm == HA_KEY_ALG_LONG_HASH) &&
2961                            (keyinfo->user_defined_key_parts == 1)) ?
2962                            UNIQUE_KEY_FLAG : MULTIPLE_KEY_FLAG);
2963         if (i == 0)
2964           field->key_start.set_bit(key);
2965         if (field->key_length() == key_part->length &&
2966             !(field->flags & BLOB_FLAG) &&
2967             keyinfo->algorithm != HA_KEY_ALG_LONG_HASH)
2968         {
2969           if (handler_file->index_flags(key, i, 0) & HA_KEYREAD_ONLY)
2970           {
2971             share->keys_for_keyread.set_bit(key);
2972             field->part_of_key.set_bit(key);
2973             if (i < keyinfo->user_defined_key_parts)
2974               field->part_of_key_not_clustered.set_bit(key);
2975           }
2976           if (handler_file->index_flags(key, i, 1) & HA_READ_ORDER)
2977             field->part_of_sortkey.set_bit(key);
2978         }
2979         if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
2980             usable_parts == i)
2981           usable_parts++;			// For FILESORT
2982         field->flags|= PART_KEY_FLAG;
2983         if (key == primary_key)
2984         {
2985           field->flags|= PRI_KEY_FLAG;
2986           /*
2987             If this field is part of the primary key and all keys contains
2988             the primary key, then we can use any key to find this column
2989           */
2990           if (ha_option & HA_PRIMARY_KEY_IN_READ_INDEX)
2991           {
2992             if (field->key_length() == key_part->length &&
2993                 !(field->flags & BLOB_FLAG))
2994               field->part_of_key= share->keys_in_use;
2995             if (field->part_of_sortkey.is_set(key))
2996               field->part_of_sortkey= share->keys_in_use;
2997           }
2998         }
2999         if (field->key_length() != key_part->length)
3000         {
3001 #ifndef TO_BE_DELETED_ON_PRODUCTION
3002           if (field->type() == MYSQL_TYPE_NEWDECIMAL &&
3003               keyinfo->algorithm != HA_KEY_ALG_LONG_HASH)
3004           {
3005             /*
3006               Fix a fatal error in decimal key handling that causes crashes
3007               on Innodb. We fix it by reducing the key length so that
3008               InnoDB never gets a too big key when searching.
3009               This allows the end user to do an ALTER TABLE to fix the
3010               error.
3011             */
3012             keyinfo->key_length-= (key_part->length - field->key_length());
3013             key_part->store_length-= (uint16)(key_part->length -
3014                                               field->key_length());
3015             key_part->length= (uint16)field->key_length();
3016             sql_print_error("Found wrong key definition in %s; "
3017                             "Please do \"ALTER TABLE '%s' FORCE \" to fix it!",
3018                             share->table_name.str,
3019                             share->table_name.str);
3020             push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
3021                                 ER_CRASHED_ON_USAGE,
3022                                 "Found wrong key definition in %s; "
3023                                 "Please do \"ALTER TABLE '%s' FORCE\" to fix "
3024                                 "it!",
3025                                 share->table_name.str,
3026                                 share->table_name.str);
3027             share->crashed= 1;                // Marker for CHECK TABLE
3028             continue;
3029           }
3030 #endif
3031           key_part->key_part_flag|= HA_PART_KEY_SEG;
3032         }
3033         if (field->real_maybe_null())
3034           key_part->key_part_flag|= HA_NULL_PART;
3035         /*
3036           Sometimes we can compare key parts for equality with memcmp.
3037           But not always.
3038         */
3039         if (!(key_part->key_part_flag & (HA_BLOB_PART | HA_VAR_LENGTH_PART |
3040                                          HA_BIT_PART)) &&
3041             key_part->type != HA_KEYTYPE_FLOAT &&
3042             key_part->type == HA_KEYTYPE_DOUBLE &&
3043             keyinfo->algorithm != HA_KEY_ALG_LONG_HASH)
3044           key_part->key_part_flag|= HA_CAN_MEMCMP;
3045       }
3046       keyinfo->usable_key_parts= usable_parts; // Filesort
3047 
3048       set_if_bigger(share->max_key_length,keyinfo->key_length+
3049                     keyinfo->user_defined_key_parts);
3050       /*
3051         MERGE tables do not have unique indexes. But every key could be
3052         an unique index on the underlying MyISAM table. (Bug #10400)
3053       */
3054       if ((keyinfo->flags & HA_NOSAME) ||
3055           (ha_option & HA_ANY_INDEX_MAY_BE_UNIQUE))
3056         set_if_bigger(share->max_unique_length,keyinfo->key_length);
3057     }
3058     if (primary_key < MAX_KEY &&
3059 	(share->keys_in_use.is_set(primary_key)))
3060     {
3061       share->primary_key= primary_key;
3062       /*
3063 	If we are using an integer as the primary key then allow the user to
3064 	refer to it as '_rowid'
3065       */
3066       if (share->key_info[primary_key].user_defined_key_parts == 1)
3067       {
3068 	Field *field= share->key_info[primary_key].key_part[0].field;
3069 	if (field && field->result_type() == INT_RESULT)
3070         {
3071           /* note that fieldnr here (and rowid_field_offset) starts from 1 */
3072 	  share->rowid_field_offset= (share->key_info[primary_key].key_part[0].
3073                                       fieldnr);
3074         }
3075       }
3076     }
3077     else
3078       share->primary_key = MAX_KEY; // we do not have a primary key
3079   }
3080   else
3081     share->primary_key= MAX_KEY;
3082   if (new_field_pack_flag <= 1)
3083   {
3084     /* Old file format with default as not null */
3085     uint null_length= (share->null_fields+7)/8;
3086     bfill(share->default_values + (null_flags - (uchar*) record),
3087           null_length, 255);
3088   }
3089 
3090   set_overlapped_keys();
3091 
3092   /* Handle virtual expressions */
3093   if (vcol_screen_length && share->frm_version >= FRM_VER_EXPRESSSIONS)
3094   {
3095     uchar *vcol_screen_end= vcol_screen_pos + vcol_screen_length;
3096 
3097     /* Skip header */
3098     vcol_screen_pos+= FRM_VCOL_NEW_BASE_SIZE;
3099     share->vcol_defs.str+= FRM_VCOL_NEW_BASE_SIZE;
3100     share->vcol_defs.length-= FRM_VCOL_NEW_BASE_SIZE;
3101 
3102     /*
3103       Read virtual columns, default values and check constraints
3104       See pack_expression() for how data is stored
3105     */
3106     while (vcol_screen_pos < vcol_screen_end)
3107     {
3108       Virtual_column_info *vcol_info;
3109       uint type=         (uint) vcol_screen_pos[0];
3110       uint field_nr=     uint2korr(vcol_screen_pos+1);
3111       uint expr_length=  uint2korr(vcol_screen_pos+3);
3112       uint name_length=  (uint) vcol_screen_pos[5];
3113 
3114       if (!(vcol_info=   new (&share->mem_root) Virtual_column_info()))
3115         goto err;
3116 
3117       /* The following can only be true for check_constraints */
3118 
3119       if (field_nr != UINT_MAX16)
3120       {
3121         DBUG_ASSERT(field_nr < share->fields);
3122         reg_field= share->field[field_nr];
3123       }
3124       else
3125       {
3126         reg_field= 0;
3127         DBUG_ASSERT(name_length);
3128       }
3129 
3130       vcol_screen_pos+= FRM_VCOL_NEW_HEADER_SIZE;
3131       vcol_info->set_vcol_type((enum_vcol_info_type) type);
3132       if (name_length)
3133       {
3134         vcol_info->name.str= strmake_root(&share->mem_root,
3135                                           (char*)vcol_screen_pos, name_length);
3136         vcol_info->name.length= name_length;
3137       }
3138       else
3139         vcol_info->name= reg_field->field_name;
3140       vcol_screen_pos+= name_length + expr_length;
3141 
3142       switch (type) {
3143       case VCOL_GENERATED_VIRTUAL:
3144       {
3145         uint recpos;
3146         reg_field->vcol_info= vcol_info;
3147         share->virtual_fields++;
3148         share->stored_fields--;
3149         if (reg_field->flags & BLOB_FLAG)
3150           share->virtual_not_stored_blob_fields++;
3151         /* Correct stored_rec_length as non stored fields are last */
3152         recpos= (uint) (reg_field->ptr - record);
3153         if (share->stored_rec_length >= recpos)
3154           share->stored_rec_length= recpos-1;
3155         break;
3156       }
3157       case VCOL_GENERATED_STORED:
3158         vcol_info->stored_in_db= 1;
3159         DBUG_ASSERT(!reg_field->vcol_info);
3160         reg_field->vcol_info= vcol_info;
3161         share->virtual_fields++;
3162         break;
3163       case VCOL_DEFAULT:
3164         vcol_info->stored_in_db= 1;
3165         DBUG_ASSERT(!reg_field->default_value);
3166         reg_field->default_value=    vcol_info;
3167         share->default_expressions++;
3168         break;
3169       case VCOL_CHECK_FIELD:
3170         DBUG_ASSERT(!reg_field->check_constraint);
3171         reg_field->check_constraint= vcol_info;
3172         share->field_check_constraints++;
3173         break;
3174       case VCOL_CHECK_TABLE:
3175         *(table_check_constraints++)= vcol_info;
3176         break;
3177       }
3178     }
3179   }
3180   DBUG_ASSERT((uint) (table_check_constraints - share->check_constraints) ==
3181               (uint) (share->table_check_constraints -
3182                       share->field_check_constraints));
3183 
3184   if (options.str)
3185   {
3186     DBUG_ASSERT(options.length);
3187     if (engine_table_options_frm_read(options.str, options.length, share))
3188       goto err;
3189   }
3190   if (parse_engine_table_options(thd, handler_file->partition_ht(), share))
3191     goto err;
3192 
3193   if (share->found_next_number_field)
3194   {
3195     reg_field= *share->found_next_number_field;
3196     if ((int) (share->next_number_index= (uint)
3197 	       find_ref_key(share->key_info, keys,
3198                             share->default_values, reg_field,
3199 			    &share->next_number_key_offset,
3200                             &share->next_number_keypart)) < 0)
3201       goto err; // Wrong field definition
3202     reg_field->flags |= AUTO_INCREMENT_FLAG;
3203   }
3204 
3205   if (share->blob_fields)
3206   {
3207     Field **ptr;
3208     uint k, *save;
3209 
3210     /* Store offsets to blob fields to find them fast */
3211     if (!(share->blob_field= save=
3212 	  (uint*) alloc_root(&share->mem_root,
3213                              (uint) (share->blob_fields* sizeof(uint)))))
3214       goto err;
3215     for (k=0, ptr= share->field ; *ptr ; ptr++, k++)
3216     {
3217       if ((*ptr)->flags & BLOB_FLAG)
3218 	(*save++)= k;
3219     }
3220   }
3221 
3222   /*
3223     the correct null_bytes can now be set, since bitfields have been taken
3224     into account
3225   */
3226   share->null_bytes= (uint)(null_pos - (uchar*) null_flags +
3227                       (null_bit_pos + 7) / 8);
3228   share->last_null_bit_pos= null_bit_pos;
3229   share->null_bytes_for_compare= null_bits_are_used ? share->null_bytes : 0;
3230   share->can_cmp_whole_record= (share->blob_fields == 0 &&
3231                                 share->varchar_fields == 0);
3232 
3233   share->column_bitmap_size= bitmap_buffer_size(share->fields);
3234 
3235   bitmap_count= 1;
3236   if (share->table_check_constraints)
3237   {
3238     feature_check_constraint++;
3239     if (!(share->check_set= (MY_BITMAP*)
3240           alloc_root(&share->mem_root, sizeof(*share->check_set))))
3241       goto err;
3242     bitmap_count++;
3243   }
3244   if (!(bitmaps= (my_bitmap_map*) alloc_root(&share->mem_root,
3245                                              share->column_bitmap_size *
3246                                              bitmap_count)))
3247     goto err;
3248   my_bitmap_init(&share->all_set, bitmaps, share->fields, FALSE);
3249   bitmap_set_all(&share->all_set);
3250   if (share->check_set)
3251   {
3252     /*
3253       Bitmap for fields used by CHECK constraint. Will be filled up
3254       at first usage of table.
3255     */
3256     my_bitmap_init(share->check_set,
3257                    (my_bitmap_map*) ((uchar*) bitmaps +
3258                                      share->column_bitmap_size),
3259                    share->fields, FALSE);
3260     bitmap_clear_all(share->check_set);
3261   }
3262 
3263 #ifndef DBUG_OFF
3264   if (use_hash)
3265     (void) my_hash_check(&share->name_hash);
3266 #endif
3267 
3268   share->db_plugin= se_plugin;
3269   delete handler_file;
3270 
3271   share->error= OPEN_FRM_OK;
3272   thd->status_var.opened_shares++;
3273   thd->mem_root= old_root;
3274   DBUG_RETURN(0);
3275 
3276 err:
3277   if (frm_created)
3278   {
3279     char path[FN_REFLEN+1];
3280     strxnmov(path, FN_REFLEN, normalized_path.str, reg_ext, NullS);
3281     my_delete(path, MYF(0));
3282 #ifdef WITH_PARTITION_STORAGE_ENGINE
3283     if (par_image)
3284     {
3285       strxnmov(path, FN_REFLEN, normalized_path.str, PAR_EXT, NullS);
3286       my_delete(path, MYF(0));
3287     }
3288 #endif
3289   }
3290   share->db_plugin= NULL;
3291   share->error= OPEN_FRM_CORRUPTED;
3292   share->open_errno= my_errno;
3293   delete handler_file;
3294   plugin_unlock(0, se_plugin);
3295   my_hash_free(&share->name_hash);
3296 
3297   if (!thd->is_error())
3298     open_table_error(share, OPEN_FRM_CORRUPTED, share->open_errno);
3299 
3300   thd->mem_root= old_root;
3301   DBUG_RETURN(HA_ERR_NOT_A_TABLE);
3302 }
3303 
3304 
sql_unusable_for_discovery(THD * thd,handlerton * engine,const char * sql)3305 static bool sql_unusable_for_discovery(THD *thd, handlerton *engine,
3306                                        const char *sql)
3307 {
3308   LEX *lex= thd->lex;
3309   HA_CREATE_INFO *create_info= &lex->create_info;
3310 
3311   // ... not CREATE TABLE
3312   if (lex->sql_command != SQLCOM_CREATE_TABLE &&
3313       lex->sql_command != SQLCOM_CREATE_SEQUENCE)
3314     return 1;
3315   // ... create like
3316   if (lex->create_info.like())
3317     return 1;
3318   // ... create select
3319   if (lex->first_select_lex()->item_list.elements)
3320     return 1;
3321   // ... temporary
3322   if (create_info->tmp_table())
3323     return 1;
3324   // ... if exists
3325   if (lex->create_info.if_not_exists())
3326     return 1;
3327 
3328   // XXX error out or rather ignore the following:
3329   // ... partitioning
3330   if (lex->part_info)
3331     return 1;
3332   // ... union
3333   if (create_info->used_fields & HA_CREATE_USED_UNION)
3334     return 1;
3335   // ... index/data directory
3336   if (create_info->data_file_name || create_info->index_file_name)
3337     return 1;
3338   // ... engine
3339   DBUG_ASSERT(lex->m_sql_cmd);
3340   if (lex->create_info.used_fields & HA_CREATE_USED_ENGINE)
3341   {
3342     /*
3343       TODO: we could just compare engine names here, without resolving.
3344       But this optimization is too late for 10.1.
3345     */
3346     Storage_engine_name *opt= lex->m_sql_cmd->option_storage_engine_name();
3347     DBUG_ASSERT(opt); // lex->m_sql_cmd must be an Sql_cmd_create_table instance
3348     if (opt->resolve_storage_engine_with_error(thd, &create_info->db_type,
3349                                                false) ||
3350         (create_info->db_type && create_info->db_type != engine))
3351       return 1;
3352   }
3353   // ... WITH SYSTEM VERSIONING
3354   if (create_info->versioned())
3355     return 1;
3356 
3357   return 0;
3358 }
3359 
init_from_sql_statement_string(THD * thd,bool write,const char * sql,size_t sql_length)3360 int TABLE_SHARE::init_from_sql_statement_string(THD *thd, bool write,
3361                                         const char *sql, size_t sql_length)
3362 {
3363   CHARSET_INFO *old_cs= thd->variables.character_set_client;
3364   Parser_state parser_state;
3365   bool error;
3366   char *sql_copy;
3367   handler *file;
3368   LEX *old_lex;
3369   Query_arena *arena, backup;
3370   LEX tmp_lex;
3371   KEY *unused1;
3372   uint unused2;
3373   handlerton *hton= plugin_hton(db_plugin);
3374   LEX_CUSTRING frm= {0,0};
3375   LEX_CSTRING db_backup= thd->db;
3376   DBUG_ENTER("TABLE_SHARE::init_from_sql_statement_string");
3377 
3378   /*
3379     Ouch. Parser may *change* the string it's working on.
3380     Currently (2013-02-26) it is used to permanently disable
3381     conditional comments.
3382     Anyway, let's copy the caller's string...
3383   */
3384   if (!(sql_copy= thd->strmake(sql, sql_length)))
3385     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3386 
3387   if (parser_state.init(thd, sql_copy, sql_length))
3388     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3389 
3390   Sql_mode_instant_set sms(thd, MODE_NO_ENGINE_SUBSTITUTION | MODE_NO_DIR_IN_CREATE);
3391   thd->variables.character_set_client= system_charset_info;
3392   tmp_disable_binlog(thd);
3393   old_lex= thd->lex;
3394   thd->lex= &tmp_lex;
3395 
3396   arena= thd->stmt_arena;
3397   if (arena->is_conventional())
3398     arena= 0;
3399   else
3400     thd->set_n_backup_active_arena(arena, &backup);
3401 
3402   thd->reset_db(&db);
3403   lex_start(thd);
3404 
3405   if (unlikely((error= parse_sql(thd, & parser_state, NULL) ||
3406                 sql_unusable_for_discovery(thd, hton, sql_copy))))
3407     goto ret;
3408 
3409   thd->lex->create_info.db_type= hton;
3410 #ifdef WITH_PARTITION_STORAGE_ENGINE
3411   thd->work_part_info= 0;                       // For partitioning
3412 #endif
3413 
3414   if (tabledef_version.str)
3415     thd->lex->create_info.tabledef_version= tabledef_version;
3416 
3417   promote_first_timestamp_column(&thd->lex->alter_info.create_list);
3418   file= mysql_create_frm_image(thd, db, table_name,
3419                                &thd->lex->create_info, &thd->lex->alter_info,
3420                                C_ORDINARY_CREATE, &unused1, &unused2, &frm);
3421   error|= file == 0;
3422   delete file;
3423 
3424   if (frm.str)
3425   {
3426     option_list= 0;             // cleanup existing options ...
3427     option_struct= 0;           // ... if it's an assisted discovery
3428     error= init_from_binary_frm_image(thd, write, frm.str, frm.length);
3429   }
3430 
3431 ret:
3432   my_free(const_cast<uchar*>(frm.str));
3433   lex_end(thd->lex);
3434   thd->reset_db(&db_backup);
3435   thd->lex= old_lex;
3436   if (arena)
3437     thd->restore_active_arena(arena, &backup);
3438   reenable_binlog(thd);
3439   thd->variables.character_set_client= old_cs;
3440   if (unlikely(thd->is_error() || error))
3441   {
3442     thd->clear_error();
3443     my_error(ER_SQL_DISCOVER_ERROR, MYF(0), hton_name(hton)->str,
3444              db.str, table_name.str, sql_copy);
3445     DBUG_RETURN(HA_ERR_GENERIC);
3446   }
3447   /* Treat the table as normal table from binary logging point of view */
3448   table_creation_was_logged= 1;
3449   DBUG_RETURN(0);
3450 }
3451 
write_frm_image(const uchar * frm,size_t len)3452 bool TABLE_SHARE::write_frm_image(const uchar *frm, size_t len)
3453 {
3454   char file_name[FN_REFLEN+1];
3455   strxnmov(file_name, sizeof(file_name)-1, normalized_path.str, reg_ext,
3456            NullS);
3457   return writefile(file_name, db.str, table_name.str, false,
3458                    frm, len);
3459 }
3460 
write_par_image(const uchar * par,size_t len)3461 bool TABLE_SHARE::write_par_image(const uchar *par, size_t len)
3462 {
3463   char file_name[FN_REFLEN+1];
3464   strxnmov(file_name, sizeof(file_name)-1, normalized_path.str, PAR_EXT,
3465            NullS);
3466   return writefile(file_name, db.str, table_name.str, false, par, len);
3467 }
3468 
3469 
read_frm_image(const uchar ** frm,size_t * len)3470 bool TABLE_SHARE::read_frm_image(const uchar **frm, size_t *len)
3471 {
3472   if (IF_PARTITIONING(partition_info_str, 0))   // cannot discover a partition
3473   {
3474     DBUG_ASSERT(db_type()->discover_table == 0);
3475     return 1;
3476   }
3477 
3478   if (frm_image)
3479   {
3480     *frm= frm_image->str;
3481     *len= frm_image->length;
3482     frm_image->str= 0; // pass the ownership to the caller
3483     frm_image= 0;
3484     return 0;
3485   }
3486   return readfrm(normalized_path.str, frm, len);
3487 }
3488 
3489 
free_frm_image(const uchar * frm)3490 void TABLE_SHARE::free_frm_image(const uchar *frm)
3491 {
3492   if (frm)
3493     my_free(const_cast<uchar*>(frm));
3494 }
3495 
3496 
fix_vcol_expr(THD * thd,Virtual_column_info * vcol)3497 static bool fix_vcol_expr(THD *thd, Virtual_column_info *vcol)
3498 {
3499   DBUG_ENTER("fix_vcol_expr");
3500 
3501   const enum enum_column_usage saved_column_usage= thd->column_usage;
3502   thd->column_usage= COLUMNS_WRITE;
3503 
3504   int error= vcol->expr->fix_fields(thd, &vcol->expr);
3505 
3506   thd->column_usage= saved_column_usage;
3507 
3508   if (unlikely(error))
3509   {
3510     StringBuffer<MAX_FIELD_WIDTH> str;
3511     vcol->print(&str);
3512     my_error(ER_ERROR_EVALUATING_EXPRESSION, MYF(0), str.c_ptr_safe());
3513     DBUG_RETURN(1);
3514   }
3515 
3516   DBUG_RETURN(0);
3517 }
3518 
3519 /** rerun fix_fields for vcols that returns time- or session- dependent values
3520 
3521     @note this is done for all vcols for INSERT/UPDATE/DELETE,
3522     and only as needed for SELECTs.
3523 */
fix_session_vcol_expr(THD * thd,Virtual_column_info * vcol)3524 bool fix_session_vcol_expr(THD *thd, Virtual_column_info *vcol)
3525 {
3526   DBUG_ENTER("fix_session_vcol_expr");
3527   if (!(vcol->flags & (VCOL_TIME_FUNC|VCOL_SESSION_FUNC)))
3528     DBUG_RETURN(0);
3529 
3530   vcol->expr->walk(&Item::cleanup_excluding_fields_processor, 0, 0);
3531   DBUG_ASSERT(!vcol->expr->is_fixed());
3532   DBUG_RETURN(fix_vcol_expr(thd, vcol));
3533 }
3534 
3535 
3536 /** invoke fix_session_vcol_expr for a vcol
3537 
3538     @note this is called for generated column or a DEFAULT expression from
3539     their corresponding fix_fields on SELECT.
3540 */
fix_session_vcol_expr_for_read(THD * thd,Field * field,Virtual_column_info * vcol)3541 bool fix_session_vcol_expr_for_read(THD *thd, Field *field,
3542                                     Virtual_column_info *vcol)
3543 {
3544   DBUG_ENTER("fix_session_vcol_expr_for_read");
3545   TABLE_LIST *tl= field->table->pos_in_table_list;
3546   if (!tl || tl->lock_type >= TL_WRITE_ALLOW_WRITE)
3547     DBUG_RETURN(0);
3548   Security_context *save_security_ctx= thd->security_ctx;
3549   if (tl->security_ctx)
3550     thd->security_ctx= tl->security_ctx;
3551   bool res= fix_session_vcol_expr(thd, vcol);
3552   thd->security_ctx= save_security_ctx;
3553   DBUG_RETURN(res);
3554 }
3555 
3556 
3557 /*
3558   @brief
3559     Perform semantic analysis of the defining expression for a virtual column
3560 
3561   @param thd        The thread object
3562   @param table      The table containing the virtual column
3563   @param field	    Field if this is a DEFAULT or AS, otherwise NULL
3564   @param vcol       The Virtual_column object
3565 
3566 
3567   @details
3568     The function performs semantic analysis of the defining expression for
3569     the virtual column vcol_field. The expression is used to compute the
3570     values of this column.
3571 
3572   @retval
3573     TRUE           An error occurred, something was wrong with the function
3574   @retval
3575     FALSE          Otherwise
3576 */
3577 
fix_and_check_vcol_expr(THD * thd,TABLE * table,Virtual_column_info * vcol)3578 static bool fix_and_check_vcol_expr(THD *thd, TABLE *table,
3579                                     Virtual_column_info *vcol)
3580 {
3581   Item* func_expr= vcol->expr;
3582   DBUG_ENTER("fix_and_check_vcol_expr");
3583   DBUG_PRINT("info", ("vcol: %p", vcol));
3584   DBUG_ASSERT(func_expr);
3585 
3586   if (func_expr->is_fixed())
3587     DBUG_RETURN(0); // nothing to do
3588 
3589   if (fix_vcol_expr(thd, vcol))
3590     DBUG_RETURN(1);
3591 
3592   if (vcol->flags)
3593     DBUG_RETURN(0); // already checked, no need to do it again
3594 
3595   /* fix_fields could've changed the expression */
3596   func_expr= vcol->expr;
3597 
3598   /* this was checked in check_expression(), but the frm could be mangled... */
3599   if (unlikely(func_expr->result_type() == ROW_RESULT))
3600   {
3601     my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
3602     DBUG_RETURN(1);
3603   }
3604 
3605   /*
3606     Walk through the Item tree checking if all items are valid
3607     to be part of the virtual column
3608   */
3609   Item::vcol_func_processor_result res;
3610 
3611   int error= func_expr->walk(&Item::check_vcol_func_processor, 0, &res);
3612   if (unlikely(error || (res.errors & VCOL_IMPOSSIBLE)))
3613   {
3614     // this can only happen if the frm was corrupted
3615     my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), res.name,
3616              vcol->get_vcol_type_name(), vcol->name.str);
3617     DBUG_RETURN(1);
3618   }
3619   else if (unlikely(res.errors & VCOL_AUTO_INC))
3620   {
3621     /*
3622       An auto_increment field may not be used in an expression for
3623       a check constraint, a default value or a generated column
3624 
3625       Note that this error condition is not detected during parsing
3626       of the statement because the field item does not have a field
3627       pointer at that time
3628     */
3629     myf warn= table->s->frm_version < FRM_VER_EXPRESSSIONS ? ME_WARNING : 0;
3630     my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(warn),
3631              "AUTO_INCREMENT", vcol->get_vcol_type_name(), res.name);
3632     if (!warn)
3633       DBUG_RETURN(1);
3634   }
3635   vcol->flags= res.errors;
3636 
3637   if (vcol->flags & VCOL_SESSION_FUNC)
3638     table->s->vcols_need_refixing= true;
3639 
3640   DBUG_RETURN(0);
3641 }
3642 
3643 
3644 /*
3645   @brief
3646     Unpack the definition of a virtual column from its linear representation
3647 
3648   @param thd             The thread object
3649   @param mem_root        Where to allocate memory
3650   @param table           The table containing the virtual column
3651   @param field           Field if this is a DEFAULT or AS, otherwise NULL
3652   @param vcol            The Virtual_column object
3653   @param[out] error_reported   Flag to inform the caller that no
3654                                other error messages are to be generated
3655 
3656   @details
3657 
3658     The function takes string expression from the 'vcol' object of the
3659     table 'table' and parses it, building an item object for it. The
3660     pointer to this item is placed into in a Virtual_column_info object
3661     that is created. After this the function performs
3662     semantic analysis of the item by calling the the function
3663     fix_and_check_vcol_expr().  Since the defining expression is part of the table
3664     definition the item for it is created in table->memroot within the
3665     special arena TABLE::expr_arena or in the thd memroot for INSERT DELAYED
3666 
3667   @note
3668     Before passing 'vcol_expr' to the parser the function wraps it in
3669     parentheses and prepends a special keyword.
3670 
3671    @retval Virtual_column_info*   Success
3672    @retval NULL                   Error
3673 */
3674 
3675 static Virtual_column_info *
unpack_vcol_info_from_frm(THD * thd,MEM_ROOT * mem_root,TABLE * table,String * expr_str,Virtual_column_info ** vcol_ptr,bool * error_reported)3676 unpack_vcol_info_from_frm(THD *thd, MEM_ROOT *mem_root, TABLE *table,
3677                           String *expr_str, Virtual_column_info **vcol_ptr,
3678                           bool *error_reported)
3679 {
3680   Create_field vcol_storage; // placeholder for vcol_info
3681   Parser_state parser_state;
3682   Virtual_column_info *vcol= *vcol_ptr, *vcol_info= 0;
3683   LEX *old_lex= thd->lex;
3684   LEX lex;
3685   bool error;
3686   DBUG_ENTER("unpack_vcol_info_from_frm");
3687 
3688   DBUG_ASSERT(vcol->expr == NULL);
3689 
3690   if (parser_state.init(thd, expr_str->c_ptr_safe(), expr_str->length()))
3691     goto end;
3692 
3693   if (init_lex_with_single_table(thd, table, &lex))
3694     goto end;
3695 
3696   lex.parse_vcol_expr= true;
3697   lex.last_field= &vcol_storage;
3698 
3699   error= parse_sql(thd, &parser_state, NULL);
3700   if (unlikely(error))
3701     goto end;
3702 
3703   if (lex.current_select->table_list.first[0].next_global)
3704   {
3705     /* We are using NEXT VALUE FOR sequence. Remember table name for open */
3706     TABLE_LIST *sequence= lex.current_select->table_list.first[0].next_global;
3707     sequence->next_global= table->internal_tables;
3708     table->internal_tables= sequence;
3709   }
3710 
3711   vcol_storage.vcol_info->set_vcol_type(vcol->get_vcol_type());
3712   vcol_storage.vcol_info->stored_in_db=      vcol->stored_in_db;
3713   vcol_storage.vcol_info->name=              vcol->name;
3714   vcol_storage.vcol_info->utf8=              vcol->utf8;
3715   if (!fix_and_check_vcol_expr(thd, table, vcol_storage.vcol_info))
3716   {
3717     *vcol_ptr= vcol_info= vcol_storage.vcol_info;   // Expression ok
3718     DBUG_ASSERT(vcol_info->expr);
3719     goto end;
3720   }
3721   *error_reported= TRUE;
3722 
3723 end:
3724   end_lex_with_single_table(thd, table, old_lex);
3725 
3726   DBUG_RETURN(vcol_info);
3727 }
3728 
check_vcol_forward_refs(Field * field,Virtual_column_info * vcol,bool check_constraint)3729 static bool check_vcol_forward_refs(Field *field, Virtual_column_info *vcol,
3730                                     bool check_constraint)
3731 {
3732   bool res;
3733   uint32 flags= field->flags;
3734   if (check_constraint)
3735   {
3736     /* Check constraints can refer it itself */
3737     field->flags|= NO_DEFAULT_VALUE_FLAG;
3738   }
3739   res= (vcol &&
3740         vcol->expr->walk(&Item::check_field_expression_processor, 0, field));
3741   field->flags= flags;
3742   return res;
3743 }
3744 
3745 #ifndef DBUG_OFF
print_long_unique_table(TABLE * table)3746 static void print_long_unique_table(TABLE *table)
3747 {
3748   char buff[256];
3749   String str;
3750   KEY *key_info_table, *key_info_share;
3751   KEY_PART_INFO *key_part;
3752   Field *field;
3753   my_snprintf(buff, sizeof(buff), "Printing Table state, It will print table fields,"
3754           " fields->offset,field->null_bit, field->null_pos and key_info ... \n"
3755           "\nPrinting  Table  keyinfo\n");
3756   str.append(buff, strlen(buff));
3757   my_snprintf(buff, sizeof(buff), "\ntable->s->reclength %d\n"
3758           "table->s->fields %d\n",
3759           table->s->reclength, table->s->fields);
3760   str.append(buff, strlen(buff));
3761   for (uint i= 0; i < table->s->keys; i++)
3762   {
3763     key_info_table= table->key_info + i;
3764     key_info_share= table->s->key_info + i;
3765     my_snprintf(buff, sizeof(buff), "\ntable->key_info[%d] user_defined_key_parts = %d\n"
3766                                     "table->key_info[%d] algorithm == HA_KEY_ALG_LONG_HASH = %d\n"
3767                                     "table->key_info[%d] flags & HA_NOSAME = %d\n",
3768                                    i, key_info_table->user_defined_key_parts,
3769                                    i, key_info_table->algorithm == HA_KEY_ALG_LONG_HASH,
3770                                    i, key_info_table->flags & HA_NOSAME);
3771     str.append(buff, strlen(buff));
3772     my_snprintf(buff, sizeof(buff), "\ntable->s->key_info[%d] user_defined_key_parts = %d\n"
3773                                     "table->s->key_info[%d] algorithm == HA_KEY_ALG_LONG_HASH = %d\n"
3774                                     "table->s->key_info[%d] flags & HA_NOSAME = %d\n",
3775                                    i, key_info_share->user_defined_key_parts,
3776                                    i, key_info_share->algorithm == HA_KEY_ALG_LONG_HASH,
3777                                    i, key_info_share->flags & HA_NOSAME);
3778     str.append(buff, strlen(buff));
3779     key_part = key_info_table->key_part;
3780     my_snprintf(buff, sizeof(buff), "\nPrinting table->key_info[%d].key_part[0] info\n"
3781             "key_part->offset = %d\n"
3782             "key_part->field_name = %s\n"
3783             "key_part->length = %d\n"
3784             "key_part->null_bit = %d\n"
3785             "key_part->null_offset = %d\n",
3786             i, key_part->offset, key_part->field->field_name.str, key_part->length,
3787             key_part->null_bit, key_part->null_offset);
3788     str.append(buff, strlen(buff));
3789 
3790     for (uint j= 0; j < key_info_share->user_defined_key_parts; j++)
3791     {
3792       key_part= key_info_share->key_part + j;
3793       my_snprintf(buff, sizeof(buff), "\nPrinting share->key_info[%d].key_part[%d] info\n"
3794             "key_part->offset = %d\n"
3795             "key_part->field_name = %s\n"
3796             "key_part->length = %d\n"
3797             "key_part->null_bit = %d\n"
3798             "key_part->null_offset = %d\n",
3799             i,j,key_part->offset, key_part->field->field_name.str, key_part->length,
3800             key_part->null_bit, key_part->null_offset);
3801       str.append(buff, strlen(buff));
3802     }
3803   }
3804   my_snprintf(buff, sizeof(buff), "\nPrinting table->fields\n");
3805   str.append(buff, strlen(buff));
3806   for(uint i= 0; i < table->s->fields; i++)
3807   {
3808     field= table->field[i];
3809     my_snprintf(buff, sizeof(buff), "\ntable->field[%d]->field_name %s\n"
3810             "table->field[%d]->offset = %d\n"
3811             "table->field[%d]->field_length = %d\n"
3812             "table->field[%d]->null_pos wrt to record 0 = %d\n"
3813             "table->field[%d]->null_bit_pos = %d\n",
3814             i, field->field_name.str,
3815             i, field->ptr- table->record[0],
3816             i, field->pack_length(),
3817             i, field->null_bit ? field->null_ptr - table->record[0] : -1,
3818             i, field->null_bit);
3819     str.append(buff, strlen(buff));
3820   }
3821   (*error_handler_hook)(1, str.ptr(), ME_NOTE);
3822 }
3823 #endif
3824 
copy_keys_from_share(TABLE * outparam,MEM_ROOT * root)3825 bool copy_keys_from_share(TABLE *outparam, MEM_ROOT *root)
3826 {
3827   TABLE_SHARE *share= outparam->s;
3828   if (share->key_parts)
3829   {
3830     KEY	*key_info, *key_info_end;
3831     KEY_PART_INFO *key_part;
3832 
3833     if (!multi_alloc_root(root, &key_info, share->keys*sizeof(KEY),
3834                           &key_part, share->ext_key_parts*sizeof(KEY_PART_INFO),
3835                           NullS))
3836       return 1;
3837 
3838     outparam->key_info= key_info;
3839 
3840     memcpy(key_info, share->key_info, sizeof(*key_info)*share->keys);
3841     memcpy(key_part, key_info->key_part, sizeof(*key_part)*share->ext_key_parts);
3842 
3843     my_ptrdiff_t adjust_ptrs= PTR_BYTE_DIFF(key_part, key_info->key_part);
3844     for (key_info_end= key_info + share->keys ;
3845          key_info < key_info_end ;
3846          key_info++)
3847     {
3848       key_info->table= outparam;
3849       key_info->key_part= reinterpret_cast<KEY_PART_INFO*>
3850         (reinterpret_cast<char*>(key_info->key_part) + adjust_ptrs);
3851       if (key_info->algorithm == HA_KEY_ALG_LONG_HASH)
3852         key_info->flags&= ~HA_NOSAME;
3853     }
3854     for (KEY_PART_INFO *key_part_end= key_part+share->ext_key_parts;
3855          key_part < key_part_end;
3856          key_part++)
3857     {
3858       Field *field= key_part->field= outparam->field[key_part->fieldnr - 1];
3859       if (field->key_length() != key_part->length &&
3860           !(field->flags & BLOB_FLAG))
3861       {
3862         /*
3863           We are using only a prefix of the column as a key:
3864           Create a new field for the key part that matches the index
3865         */
3866         field= key_part->field=field->make_new_field(root, outparam, 0);
3867         field->field_length= key_part->length;
3868       }
3869     }
3870   }
3871   return 0;
3872 }
3873 
3874 /*
3875   Open a table based on a TABLE_SHARE
3876 
3877   SYNOPSIS
3878     open_table_from_share()
3879     thd			Thread handler
3880     share		Table definition
3881     alias       	Alias for table
3882     db_stat		open flags (for example HA_OPEN_KEYFILE|
3883     			HA_OPEN_RNDFILE..) can be 0 (example in
3884                         ha_example_table)
3885     prgflag   		READ_ALL etc..
3886     ha_open_flags	HA_OPEN_ABORT_IF_LOCKED etc..
3887     outparam       	result table
3888     partitions_to_open  open only these partitions.
3889 
3890   RETURN VALUES
3891    0	ok
3892    1	Error (see open_table_error)
3893    2    Error (see open_table_error)
3894    3    Wrong data in .frm file
3895    4    Error (see open_table_error)
3896    5    Error (see open_table_error: charset unavailable)
3897    7    Table definition has changed in engine
3898 */
3899 
open_table_from_share(THD * thd,TABLE_SHARE * share,const LEX_CSTRING * alias,uint db_stat,uint prgflag,uint ha_open_flags,TABLE * outparam,bool is_create_table,List<String> * partitions_to_open)3900 enum open_frm_error open_table_from_share(THD *thd, TABLE_SHARE *share,
3901                        const LEX_CSTRING *alias, uint db_stat, uint prgflag,
3902                        uint ha_open_flags, TABLE *outparam,
3903                        bool is_create_table, List<String> *partitions_to_open)
3904 {
3905   enum open_frm_error error;
3906   uint records, i, bitmap_size, bitmap_count;
3907   const char *tmp_alias;
3908   bool error_reported= FALSE;
3909   uchar *record, *bitmaps;
3910   Field **field_ptr;
3911   uint8 save_context_analysis_only= thd->lex->context_analysis_only;
3912   TABLE_SHARE::enum_v_keys check_set_initialized= share->check_set_initialized;
3913   DBUG_ENTER("open_table_from_share");
3914   DBUG_PRINT("enter",("name: '%s.%s'  form: %p", share->db.str,
3915                       share->table_name.str, outparam));
3916 
3917   thd->lex->context_analysis_only&= ~CONTEXT_ANALYSIS_ONLY_VIEW; // not a view
3918 
3919   error= OPEN_FRM_ERROR_ALREADY_ISSUED; // for OOM errors below
3920   bzero((char*) outparam, sizeof(*outparam));
3921   outparam->in_use= thd;
3922   outparam->s= share;
3923   outparam->db_stat= db_stat;
3924   outparam->write_row_record= NULL;
3925 
3926   if (share->incompatible_version &&
3927       !(ha_open_flags & (HA_OPEN_FOR_ALTER | HA_OPEN_FOR_REPAIR |
3928                          HA_OPEN_FOR_FLUSH)))
3929   {
3930     /* one needs to run mysql_upgrade on the table */
3931     error= OPEN_FRM_NEEDS_REBUILD;
3932     goto err;
3933   }
3934   init_sql_alloc(key_memory_TABLE, &outparam->mem_root, TABLE_ALLOC_BLOCK_SIZE,
3935                  0, MYF(0));
3936 
3937   /*
3938     We have to store the original alias in mem_root as constraints and virtual
3939     functions may store pointers to it
3940   */
3941   if (!(tmp_alias= strmake_root(&outparam->mem_root, alias->str, alias->length)))
3942     goto err;
3943 
3944   outparam->alias.set(tmp_alias, alias->length, table_alias_charset);
3945 
3946   /* Allocate handler */
3947   outparam->file= 0;
3948   if (!(prgflag & OPEN_FRM_FILE_ONLY))
3949   {
3950     if (!(outparam->file= get_new_handler(share, &outparam->mem_root,
3951                                           share->db_type())))
3952       goto err;
3953 
3954     if (outparam->file->set_ha_share_ref(&share->ha_share))
3955       goto err;
3956   }
3957   else
3958   {
3959     DBUG_ASSERT(!db_stat);
3960   }
3961 
3962   if (share->sequence && outparam->file)
3963   {
3964     ha_sequence *file;
3965     /* SEQUENCE table. Create a sequence handler over the original handler */
3966     if (!(file= (ha_sequence*) sql_sequence_hton->create(sql_sequence_hton, share,
3967                                                      &outparam->mem_root)))
3968       goto err;
3969     file->register_original_handler(outparam->file);
3970     outparam->file= file;
3971   }
3972 
3973   outparam->reginfo.lock_type= TL_UNLOCK;
3974   outparam->current_lock= F_UNLCK;
3975   records=0;
3976   if ((db_stat & HA_OPEN_KEYFILE) || (prgflag & DELAYED_OPEN))
3977     records=1;
3978   if (prgflag & (READ_ALL + EXTRA_RECORD))
3979   {
3980     records++;
3981     if (share->versioned || share->period.name)
3982       records++;
3983   }
3984 
3985   if (records == 0)
3986   {
3987     /* We are probably in hard repair, and the buffers should not be used */
3988     record= share->default_values;
3989   }
3990   else
3991   {
3992     if (!(record= (uchar*) alloc_root(&outparam->mem_root,
3993                                       share->rec_buff_length * records)))
3994       goto err;                                   /* purecov: inspected */
3995   }
3996 
3997   for (i= 0; i < 3;)
3998   {
3999     outparam->record[i]= record;
4000     if (++i < records)
4001       record+= share->rec_buff_length;
4002   }
4003   /* Mark bytes between records as not accessable to catch overrun bugs */
4004   for (i= 0; i < records; i++)
4005     MEM_NOACCESS(outparam->record[i] + share->reclength,
4006                  share->rec_buff_length - share->reclength);
4007 
4008   if (!(field_ptr = (Field **) alloc_root(&outparam->mem_root,
4009                                           (uint) ((share->fields+1)*
4010                                                   sizeof(Field*)))))
4011     goto err;                                   /* purecov: inspected */
4012 
4013   /* Allocate storage for range optimizer */
4014   if (!multi_alloc_root(&outparam->mem_root,
4015                         &outparam->opt_range,
4016                         share->keys * sizeof(TABLE::OPT_RANGE),
4017                         &outparam->const_key_parts,
4018                         share->keys * sizeof(key_part_map),
4019                         NullS))
4020     goto err;
4021 
4022   outparam->field= field_ptr;
4023 
4024   record= (uchar*) outparam->record[0]-1;	/* Fieldstart = 1 */
4025   if (share->null_field_first)
4026     outparam->null_flags= (uchar*) record+1;
4027   else
4028     outparam->null_flags= (uchar*) (record+ 1+ share->reclength -
4029                                     share->null_bytes);
4030 
4031   /* Setup copy of fields from share, but use the right alias and record */
4032   for (i=0 ; i < share->fields; i++, field_ptr++)
4033   {
4034     if (!((*field_ptr)= share->field[i]->clone(&outparam->mem_root, outparam)))
4035       goto err;
4036   }
4037   (*field_ptr)= 0;                              // End marker
4038 
4039   DEBUG_SYNC(thd, "TABLE_after_field_clone");
4040 
4041   outparam->vers_write= share->versioned;
4042 
4043   if (share->found_next_number_field)
4044     outparam->found_next_number_field=
4045       outparam->field[(uint) (share->found_next_number_field - share->field)];
4046 
4047   if (copy_keys_from_share(outparam, &outparam->mem_root))
4048     goto err;
4049 
4050   /*
4051     Process virtual and default columns, if any.
4052   */
4053   if (share->virtual_fields || share->default_fields ||
4054       share->default_expressions || share->table_check_constraints)
4055   {
4056     Field **vfield_ptr, **dfield_ptr;
4057     Virtual_column_info **check_constraint_ptr;
4058 
4059     if (!multi_alloc_root(&outparam->mem_root,
4060                           &vfield_ptr, (uint) ((share->virtual_fields + 1)*
4061                                                sizeof(Field*)),
4062                           &dfield_ptr, (uint) ((share->default_fields +
4063                                                 share->default_expressions +1)*
4064                                                sizeof(Field*)),
4065                           &check_constraint_ptr,
4066                           (uint) ((share->table_check_constraints +
4067                                    share->field_check_constraints + 1)*
4068                                   sizeof(Virtual_column_info*)),
4069                           NullS))
4070       goto err;
4071     if (share->virtual_fields)
4072       outparam->vfield= vfield_ptr;
4073     if (share->default_fields + share->default_expressions)
4074       outparam->default_field= dfield_ptr;
4075     if (share->table_check_constraints || share->field_check_constraints)
4076       outparam->check_constraints= check_constraint_ptr;
4077 
4078     vcol_init_mode mode= VCOL_INIT_DEPENDENCY_FAILURE_IS_WARNING;
4079     switch (thd->lex->sql_command)
4080     {
4081     case SQLCOM_CREATE_TABLE:
4082       mode= VCOL_INIT_DEPENDENCY_FAILURE_IS_ERROR;
4083       break;
4084     case SQLCOM_ALTER_TABLE:
4085     case SQLCOM_CREATE_INDEX:
4086     case SQLCOM_DROP_INDEX:
4087       if ((ha_open_flags & HA_OPEN_FOR_ALTER) == 0)
4088         mode= VCOL_INIT_DEPENDENCY_FAILURE_IS_ERROR;
4089       break;
4090     default:
4091       break;
4092     }
4093 
4094     if (parse_vcol_defs(thd, &outparam->mem_root, outparam,
4095                         &error_reported, mode))
4096     {
4097       error= OPEN_FRM_CORRUPTED;
4098       goto err;
4099     }
4100 
4101     /* Update to use trigger fields */
4102     switch_defaults_to_nullable_trigger_fields(outparam);
4103 
4104     for (uint k= 0; k < share->keys; k++)
4105     {
4106       KEY &key_info= outparam->key_info[k];
4107       uint parts = (share->use_ext_keys ? key_info.ext_key_parts :
4108                     key_info.user_defined_key_parts);
4109       for (uint p= 0; p < parts; p++)
4110       {
4111         KEY_PART_INFO &kp= key_info.key_part[p];
4112         if (kp.field != outparam->field[kp.fieldnr - 1])
4113         {
4114           kp.field->vcol_info = outparam->field[kp.fieldnr - 1]->vcol_info;
4115         }
4116       }
4117     }
4118   }
4119 
4120 #ifdef WITH_PARTITION_STORAGE_ENGINE
4121   bool work_part_info_used;
4122   if (share->partition_info_str_len && outparam->file)
4123   {
4124   /*
4125     In this execution we must avoid calling thd->change_item_tree since
4126     we might release memory before statement is completed. We do this
4127     by changing to a new statement arena. As part of this arena we also
4128     set the memory root to be the memory root of the table since we
4129     call the parser and fix_fields which both can allocate memory for
4130     item objects. We keep the arena to ensure that we can release the
4131     free_list when closing the table object.
4132     SEE Bug #21658
4133   */
4134 
4135     Query_arena *backup_stmt_arena_ptr= thd->stmt_arena;
4136     Query_arena backup_arena;
4137     Query_arena part_func_arena(&outparam->mem_root,
4138                                 Query_arena::STMT_INITIALIZED);
4139     thd->set_n_backup_active_arena(&part_func_arena, &backup_arena);
4140     thd->stmt_arena= &part_func_arena;
4141     bool tmp;
4142 
4143     tmp= mysql_unpack_partition(thd, share->partition_info_str,
4144                                 share->partition_info_str_len,
4145                                 outparam, is_create_table,
4146                                 plugin_hton(share->default_part_plugin),
4147                                 &work_part_info_used);
4148     if (tmp)
4149     {
4150       thd->stmt_arena= backup_stmt_arena_ptr;
4151       thd->restore_active_arena(&part_func_arena, &backup_arena);
4152       goto partititon_err;
4153     }
4154     outparam->part_info->is_auto_partitioned= share->auto_partitioned;
4155     DBUG_PRINT("info", ("autopartitioned: %u", share->auto_partitioned));
4156     /*
4157       We should perform the fix_partition_func in either local or
4158       caller's arena depending on work_part_info_used value.
4159     */
4160     if (!work_part_info_used)
4161       tmp= fix_partition_func(thd, outparam, is_create_table);
4162     thd->stmt_arena= backup_stmt_arena_ptr;
4163     thd->restore_active_arena(&part_func_arena, &backup_arena);
4164     if (!tmp)
4165     {
4166       if (work_part_info_used)
4167         tmp= fix_partition_func(thd, outparam, is_create_table);
4168     }
4169     outparam->part_info->item_free_list= part_func_arena.free_list;
4170 partititon_err:
4171     if (tmp)
4172     {
4173       if (is_create_table)
4174       {
4175         /*
4176           During CREATE/ALTER TABLE it is ok to receive errors here.
4177           It is not ok if it happens during the opening of an frm
4178           file as part of a normal query.
4179         */
4180         error_reported= TRUE;
4181       }
4182       goto err;
4183     }
4184   }
4185 #endif
4186 
4187   /* Check virtual columns against table's storage engine. */
4188   if (share->virtual_fields &&
4189         (outparam->file &&
4190           !(outparam->file->ha_table_flags() & HA_CAN_VIRTUAL_COLUMNS)))
4191   {
4192     my_error(ER_UNSUPPORTED_ENGINE_FOR_VIRTUAL_COLUMNS, MYF(0),
4193              plugin_name(share->db_plugin)->str);
4194     error_reported= TRUE;
4195     goto err;
4196   }
4197 
4198   /* Allocate bitmaps */
4199 
4200   bitmap_size= share->column_bitmap_size;
4201   bitmap_count= 7;
4202   if (share->virtual_fields)
4203     bitmap_count++;
4204 
4205   if (!(bitmaps= (uchar*) alloc_root(&outparam->mem_root,
4206                                      bitmap_size * bitmap_count)))
4207     goto err;
4208 
4209   my_bitmap_init(&outparam->def_read_set,
4210                  (my_bitmap_map*) bitmaps, share->fields, FALSE);
4211   bitmaps+= bitmap_size;
4212   my_bitmap_init(&outparam->def_write_set,
4213                  (my_bitmap_map*) bitmaps, share->fields, FALSE);
4214   bitmaps+= bitmap_size;
4215 
4216   my_bitmap_init(&outparam->has_value_set,
4217                  (my_bitmap_map*) bitmaps, share->fields, FALSE);
4218   bitmaps+= bitmap_size;
4219   my_bitmap_init(&outparam->tmp_set,
4220                  (my_bitmap_map*) bitmaps, share->fields, FALSE);
4221   bitmaps+= bitmap_size;
4222   my_bitmap_init(&outparam->eq_join_set,
4223                  (my_bitmap_map*) bitmaps, share->fields, FALSE);
4224   bitmaps+= bitmap_size;
4225   my_bitmap_init(&outparam->cond_set,
4226                  (my_bitmap_map*) bitmaps, share->fields, FALSE);
4227   bitmaps+= bitmap_size;
4228   my_bitmap_init(&outparam->def_rpl_write_set,
4229                  (my_bitmap_map*) bitmaps, share->fields, FALSE);
4230   outparam->default_column_bitmaps();
4231 
4232   outparam->cond_selectivity= 1.0;
4233 
4234   /* The table struct is now initialized;  Open the table */
4235   if (db_stat)
4236   {
4237     if (specialflag & SPECIAL_WAIT_IF_LOCKED)
4238       ha_open_flags|= HA_OPEN_WAIT_IF_LOCKED;
4239     else
4240       ha_open_flags|= HA_OPEN_IGNORE_IF_LOCKED;
4241 
4242     int ha_err= outparam->file->ha_open(outparam, share->normalized_path.str,
4243                                  (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
4244                                  ha_open_flags, 0, partitions_to_open);
4245     if (ha_err)
4246     {
4247       share->open_errno= ha_err;
4248       /* Set a flag if the table is crashed and it can be auto. repaired */
4249       share->crashed= (outparam->file->auto_repair(ha_err) &&
4250                        !(ha_open_flags & HA_OPEN_FOR_REPAIR));
4251       if (!thd->is_error())
4252         outparam->file->print_error(ha_err, MYF(0));
4253       error_reported= TRUE;
4254 
4255       if (ha_err == HA_ERR_TABLE_DEF_CHANGED)
4256         error= OPEN_FRM_DISCOVER;
4257 
4258       /*
4259         We're here, because .frm file was successfully opened.
4260 
4261         But if the table doesn't exist in the engine and the engine
4262         supports discovery, we force rediscover to discover
4263         the fact that table doesn't in fact exist and remove
4264         the stray .frm file.
4265       */
4266       if (outparam->file->partition_ht()->discover_table &&
4267           (ha_err == ENOENT || ha_err == HA_ERR_NO_SUCH_TABLE))
4268         error= OPEN_FRM_DISCOVER;
4269 
4270       goto err;
4271     }
4272   }
4273 
4274   outparam->mark_columns_used_by_virtual_fields();
4275   if (!check_set_initialized &&
4276       share->check_set_initialized == TABLE_SHARE::V_KEYS)
4277   {
4278     // copy PART_INDIRECT_KEY_FLAG that was set meanwhile by *some* thread
4279     for (uint i= 0 ; i < share->fields ; i++)
4280     {
4281       if (share->field[i]->flags & PART_INDIRECT_KEY_FLAG)
4282         outparam->field[i]->flags|= PART_INDIRECT_KEY_FLAG;
4283     }
4284   }
4285 
4286   if (db_stat)
4287   {
4288     /* Set some flags in share on first open of the table */
4289     handler::Table_flags flags= outparam->file->ha_table_flags();
4290     if (! MY_TEST(flags & (HA_BINLOG_STMT_CAPABLE |
4291                            HA_BINLOG_ROW_CAPABLE)) ||
4292         MY_TEST(flags & HA_HAS_OWN_BINLOGGING))
4293       share->no_replicate= TRUE;
4294     if (outparam->file->table_cache_type() & HA_CACHE_TBL_NOCACHE)
4295       share->not_usable_by_query_cache= TRUE;
4296     if (outparam->file->ha_table_flags() & HA_CAN_ONLINE_BACKUPS)
4297       share->online_backup= 1;
4298   }
4299 
4300   if (share->no_replicate || !binlog_filter->db_ok(share->db.str))
4301     share->can_do_row_logging= 0;   // No row based replication
4302 
4303   /* Increment the opened_tables counter, only when open flags set. */
4304   if (db_stat)
4305     thd->status_var.opened_tables++;
4306 
4307   thd->lex->context_analysis_only= save_context_analysis_only;
4308   DBUG_EXECUTE_IF("print_long_unique_internal_state",
4309    print_long_unique_table(outparam););
4310   DBUG_RETURN (OPEN_FRM_OK);
4311 
4312  err:
4313   if (! error_reported)
4314     open_table_error(share, error, my_errno);
4315   delete outparam->file;
4316 #ifdef WITH_PARTITION_STORAGE_ENGINE
4317   if (outparam->part_info)
4318     free_items(outparam->part_info->item_free_list);
4319 #endif
4320   outparam->file= 0;				// For easier error checking
4321   outparam->db_stat=0;
4322   thd->lex->context_analysis_only= save_context_analysis_only;
4323   if (outparam->expr_arena)
4324     outparam->expr_arena->free_items();
4325   free_root(&outparam->mem_root, MYF(0));       // Safe to call on bzero'd root
4326   outparam->alias.free();
4327   DBUG_RETURN (error);
4328 }
4329 
4330 
4331 /*
4332   Free information allocated by openfrm
4333 
4334   SYNOPSIS
4335     closefrm()
4336     table		TABLE object to free
4337 */
4338 
closefrm(TABLE * table)4339 int closefrm(TABLE *table)
4340 {
4341   int error=0;
4342   DBUG_ENTER("closefrm");
4343   DBUG_PRINT("enter", ("table: %p", table));
4344 
4345   if (table->db_stat)
4346     error=table->file->ha_close();
4347   table->alias.free();
4348   if (table->expr_arena)
4349     table->expr_arena->free_items();
4350   if (table->field)
4351   {
4352     for (Field **ptr=table->field ; *ptr ; ptr++)
4353     {
4354       delete *ptr;
4355     }
4356     table->field= 0;
4357   }
4358   delete table->file;
4359   table->file= 0;				/* For easier errorchecking */
4360 #ifdef WITH_PARTITION_STORAGE_ENGINE
4361   if (table->part_info)
4362   {
4363     /* Allocated through table->mem_root, freed below */
4364     free_items(table->part_info->item_free_list);
4365     table->part_info->item_free_list= 0;
4366     table->part_info= 0;
4367   }
4368 #endif
4369   free_root(&table->mem_root, MYF(0));
4370   DBUG_RETURN(error);
4371 }
4372 
4373 
4374 /* Deallocate temporary blob storage */
4375 
free_blobs(TABLE * table)4376 void free_blobs(TABLE *table)
4377 {
4378   uint *ptr, *end;
4379   for (ptr= table->s->blob_field, end=ptr + table->s->blob_fields ;
4380        ptr != end ;
4381        ptr++)
4382   {
4383     /*
4384       Reduced TABLE objects which are used by row-based replication for
4385       type conversion might have some fields missing. Skip freeing BLOB
4386       buffers for such missing fields.
4387     */
4388     if (table->field[*ptr])
4389       ((Field_blob*) table->field[*ptr])->free();
4390   }
4391 }
4392 
4393 
4394 /**
4395   Reclaim temporary blob storage which is bigger than
4396   a threshold.
4397 
4398   @param table A handle to the TABLE object containing blob fields
4399   @param size The threshold value.
4400 
4401 */
4402 
free_field_buffers_larger_than(TABLE * table,uint32 size)4403 void free_field_buffers_larger_than(TABLE *table, uint32 size)
4404 {
4405   uint *ptr, *end;
4406   for (ptr= table->s->blob_field, end=ptr + table->s->blob_fields ;
4407        ptr != end ;
4408        ptr++)
4409   {
4410     Field_blob *blob= (Field_blob*) table->field[*ptr];
4411     if (blob->get_field_buffer_size() > size)
4412         blob->free();
4413   }
4414 }
4415 
4416 /* error message when opening a form file */
4417 
open_table_error(TABLE_SHARE * share,enum open_frm_error error,int db_errno)4418 void open_table_error(TABLE_SHARE *share, enum open_frm_error error,
4419                       int db_errno)
4420 {
4421   char buff[FN_REFLEN];
4422   const myf errortype= ME_ERROR_LOG;  // Write fatals error to log
4423   DBUG_ENTER("open_table_error");
4424   DBUG_PRINT("info", ("error: %d  db_errno: %d", error, db_errno));
4425 
4426   switch (error) {
4427   case OPEN_FRM_OPEN_ERROR:
4428     /*
4429       Test if file didn't exists. We have to also test for EINVAL as this
4430       may happen on windows when opening a file with a not legal file name
4431     */
4432     if (db_errno == ENOENT || db_errno == EINVAL)
4433       my_error(ER_NO_SUCH_TABLE, MYF(0), share->db.str, share->table_name.str);
4434     else
4435     {
4436       strxmov(buff, share->normalized_path.str, reg_ext, NullS);
4437       my_error((db_errno == EMFILE) ? ER_CANT_OPEN_FILE : ER_FILE_NOT_FOUND,
4438                errortype, buff, db_errno);
4439     }
4440     break;
4441   case OPEN_FRM_OK:
4442     DBUG_ASSERT(0); // open_table_error() is never called for this one
4443     break;
4444   case OPEN_FRM_ERROR_ALREADY_ISSUED:
4445     break;
4446   case OPEN_FRM_NOT_A_VIEW:
4447     my_error(ER_WRONG_OBJECT, MYF(0), share->db.str,
4448              share->table_name.str, "VIEW");
4449     break;
4450   case OPEN_FRM_NOT_A_TABLE:
4451     my_error(ER_WRONG_OBJECT, MYF(0), share->db.str,
4452              share->table_name.str, "TABLE");
4453     break;
4454   case OPEN_FRM_DISCOVER:
4455     DBUG_ASSERT(0); // open_table_error() is never called for this one
4456     break;
4457   case OPEN_FRM_CORRUPTED:
4458     strxmov(buff, share->normalized_path.str, reg_ext, NullS);
4459     my_error(ER_NOT_FORM_FILE, errortype, buff);
4460     break;
4461   case OPEN_FRM_READ_ERROR:
4462     strxmov(buff, share->normalized_path.str, reg_ext, NullS);
4463     my_error(ER_ERROR_ON_READ, errortype, buff, db_errno);
4464     break;
4465   case OPEN_FRM_NEEDS_REBUILD:
4466     strxnmov(buff, sizeof(buff)-1,
4467              share->db.str, ".", share->table_name.str, NullS);
4468     my_error(ER_TABLE_NEEDS_REBUILD, errortype, buff);
4469     break;
4470   }
4471   DBUG_VOID_RETURN;
4472 } /* open_table_error */
4473 
4474 
4475 	/*
4476 	** fix a str_type to a array type
4477 	** typeparts separated with some char. differents types are separated
4478 	** with a '\0'
4479 	*/
4480 
4481 static bool
fix_type_pointers(const char *** typelib_value_names,uint ** typelib_value_lengths,TYPELIB * point_to_type,uint types,char * ptr,size_t length)4482 fix_type_pointers(const char ***typelib_value_names,
4483                   uint **typelib_value_lengths,
4484                   TYPELIB *point_to_type, uint types,
4485                   char *ptr, size_t length)
4486 {
4487   const char *end= ptr + length;
4488 
4489   while (types--)
4490   {
4491     char sep;
4492     point_to_type->name=0;
4493     point_to_type->type_names= *typelib_value_names;
4494     point_to_type->type_lengths= *typelib_value_lengths;
4495 
4496     /*
4497       Typelib can be encoded as:
4498       1) 0x00                     - empty typelib
4499       2) 0xFF 0x00                - empty typelib (index names)
4500       3) sep (value sep)... 0x00  - non-empty typelib (where sep is a separator)
4501     */
4502     if (length == 2 && ptr[0] == (char) 0xFF && ptr[1] == '\0')
4503     {
4504       /*
4505         This is a special case #2.
4506         If there are no indexes at all, index names can be encoded
4507         as a two byte sequence: 0xFF 0x00
4508         TODO: Check if it's a bug in the FRM packing routine.
4509         It should probably write just 0x00 instead of 0xFF00.
4510       */
4511       ptr+= 2;
4512     }
4513     else if ((sep= *ptr++))            // A non-empty typelib
4514     {
4515       for ( ; ptr < end; )
4516       {
4517         // Now scan the next value+sep pair
4518         char *vend= (char*) memchr(ptr, sep, end - ptr);
4519         if (!vend)
4520           return true;            // Bad format
4521         *((*typelib_value_names)++)= ptr;
4522         *((*typelib_value_lengths)++)= (uint) (vend - ptr);
4523         *vend= '\0';              // Change sep to '\0'
4524         ptr= vend + 1;            // Shift from sep to the next byte
4525         /*
4526           Now we can have either:
4527           - the end-of-typelib marker (0x00)
4528           - more value+sep pairs
4529         */
4530         if (!*ptr)
4531         {
4532           /*
4533             We have an ambiguity here. 0x00 can be an end-of-typelib marker,
4534             but it can also be a part of the next value:
4535               CREATE TABLE t1 (a ENUM(0x61, 0x0062) CHARACTER SET BINARY);
4536             If this is the last ENUM/SET in the table and there is still more
4537             packed data left after 0x00, then we know for sure that 0x00
4538             is a part of the next value.
4539             TODO-10.5+: we should eventually introduce a new unambiguous
4540             typelib encoding for FRM.
4541           */
4542           if (!types && ptr + 1 < end)
4543             continue;           // A binary value starting with 0x00
4544           ptr++;                // Consume the end-of-typelib marker
4545           break;                // End of the current typelib
4546         }
4547       }
4548     }
4549     point_to_type->count= (uint) (*typelib_value_names -
4550                                   point_to_type->type_names);
4551     point_to_type++;
4552     *((*typelib_value_names)++)= NullS; /* End of type */
4553     *((*typelib_value_lengths)++)= 0;   /* End of type */
4554   }
4555   return ptr != end;
4556 } /* fix_type_pointers */
4557 
4558 
4559 /*
4560  Search after a field with given start & length
4561  If an exact field isn't found, return longest field with starts
4562  at right position.
4563 
4564  NOTES
4565    This is needed because in some .frm fields 'fieldnr' was saved wrong
4566 
4567  RETURN
4568    0  error
4569    #  field number +1
4570 */
4571 
find_field(Field ** fields,uchar * record,uint start,uint length)4572 static uint find_field(Field **fields, uchar *record, uint start, uint length)
4573 {
4574   Field **field;
4575   uint i, pos;
4576 
4577   pos= 0;
4578   for (field= fields, i=1 ; *field ; i++,field++)
4579   {
4580     if ((*field)->offset(record) == start)
4581     {
4582       if ((*field)->key_length() == length)
4583 	return (i);
4584       if (!pos || fields[pos-1]->pack_length() <
4585 	  (*field)->pack_length())
4586 	pos= i;
4587     }
4588   }
4589   return (pos);
4590 }
4591 
4592 
4593 /*
4594   Store an SQL quoted string.
4595 
4596   SYNOPSIS
4597     append_unescaped()
4598     res		result String
4599     pos		string to be quoted
4600     length	it's length
4601 
4602   NOTE
4603     This function works correctly with utf8 or single-byte charset strings.
4604     May fail with some multibyte charsets though.
4605 */
4606 
append_unescaped(String * res,const char * pos,size_t length)4607 void append_unescaped(String *res, const char *pos, size_t length)
4608 {
4609   const char *end= pos+length;
4610   res->append('\'');
4611 
4612   for (; pos != end ; pos++)
4613   {
4614     switch (*pos) {
4615     case 0:				/* Must be escaped for 'mysql' */
4616       res->append('\\');
4617       res->append('0');
4618       break;
4619     case '\n':				/* Must be escaped for logs */
4620       res->append('\\');
4621       res->append('n');
4622       break;
4623     case '\r':
4624       res->append('\\');		/* This gives better readability */
4625       res->append('r');
4626       break;
4627     case '\\':
4628       res->append('\\');		/* Because of the sql syntax */
4629       res->append('\\');
4630       break;
4631     case '\'':
4632       res->append('\'');		/* Because of the sql syntax */
4633       res->append('\'');
4634       break;
4635     default:
4636       res->append(*pos);
4637       break;
4638     }
4639   }
4640   res->append('\'');
4641 }
4642 
4643 
prepare_frm_header(THD * thd,uint reclength,uchar * fileinfo,HA_CREATE_INFO * create_info,uint keys,KEY * key_info)4644 void prepare_frm_header(THD *thd, uint reclength, uchar *fileinfo,
4645                         HA_CREATE_INFO *create_info, uint keys, KEY *key_info)
4646 {
4647   size_t key_comment_total_bytes= 0;
4648   uint i;
4649   uchar frm_format= create_info->expression_length ? FRM_VER_EXPRESSSIONS
4650                                                    : FRM_VER_TRUE_VARCHAR;
4651   DBUG_ENTER("prepare_frm_header");
4652 
4653   /* Fix this when we have new .frm files;  Current limit is 4G rows (TODO) */
4654   if (create_info->max_rows > UINT_MAX32)
4655     create_info->max_rows= UINT_MAX32;
4656   if (create_info->min_rows > UINT_MAX32)
4657     create_info->min_rows= UINT_MAX32;
4658 
4659   /*
4660     Keep in sync with pack_keys() in unireg.cc
4661     For each key:
4662     8 bytes for the key header
4663     9 bytes for each key-part (MAX_REF_PARTS)
4664     NAME_LEN bytes for the name
4665     1 byte for the NAMES_SEP_CHAR (before the name)
4666     For all keys:
4667     6 bytes for the header
4668     1 byte for the NAMES_SEP_CHAR (after the last name)
4669     9 extra bytes (padding for safety? alignment?)
4670   */
4671   for (i= 0; i < keys; i++)
4672   {
4673     DBUG_ASSERT(MY_TEST(key_info[i].flags & HA_USES_COMMENT) ==
4674                 (key_info[i].comment.length > 0));
4675     if (key_info[i].flags & HA_USES_COMMENT)
4676       key_comment_total_bytes += 2 + key_info[i].comment.length;
4677     if (key_info[i].algorithm == HA_KEY_ALG_LONG_HASH)
4678       frm_format= FRM_VER_EXPRESSSIONS;
4679   }
4680 
4681   size_t key_length, tmp_key_length, tmp, csid;
4682   bzero((char*) fileinfo, FRM_HEADER_SIZE);
4683   /* header */
4684   fileinfo[0]=(uchar) 254;
4685   fileinfo[1]= 1;
4686   fileinfo[2]= frm_format;
4687 
4688   DBUG_ASSERT(ha_storage_engine_is_enabled(create_info->db_type));
4689   fileinfo[3]= (uchar) ha_legacy_type(create_info->db_type);
4690 
4691   key_length= keys * (8 + MAX_REF_PARTS * 9 + NAME_LEN + 1) + 16
4692               + key_comment_total_bytes;
4693 
4694   int2store(fileinfo+8,1);
4695   tmp_key_length= (key_length < 0xffff) ? key_length : 0xffff;
4696   int2store(fileinfo+14,tmp_key_length);
4697   int2store(fileinfo+16,reclength);
4698   int4store(fileinfo+18,create_info->max_rows);
4699   int4store(fileinfo+22,create_info->min_rows);
4700   /* fileinfo[26] is set in mysql_create_frm() */
4701   fileinfo[27]=2;				// Use long pack-fields
4702   /* fileinfo[28 & 29] is set to key_info_length in mysql_create_frm() */
4703   create_info->table_options|=HA_OPTION_LONG_BLOB_PTR; // Use portable blob pointers
4704   int2store(fileinfo+30,create_info->table_options);
4705   fileinfo[32]=0;				// No filename anymore
4706   fileinfo[33]=5;                             // Mark for 5.0 frm file
4707   int4store(fileinfo+34,create_info->avg_row_length);
4708   csid= (create_info->default_table_charset ?
4709          create_info->default_table_charset->number : 0);
4710   fileinfo[38]= (uchar) csid;
4711   fileinfo[39]= (uchar) ((uint) create_info->transactional |
4712                          ((uint) create_info->page_checksum << 2) |
4713                          ((create_info->sequence ? HA_CHOICE_YES : 0) << 4));
4714   fileinfo[40]= (uchar) create_info->row_type;
4715   /* Bytes 41-46 were for RAID support; now reused for other purposes */
4716   fileinfo[41]= (uchar) (csid >> 8);
4717   int2store(fileinfo+42, create_info->stats_sample_pages & 0xffff);
4718   fileinfo[44]= (uchar)  create_info->stats_auto_recalc;
4719   int2store(fileinfo+45, (create_info->check_constraint_list->elements+
4720                           create_info->field_check_constraints));
4721   int4store(fileinfo+47, key_length);
4722   tmp= MYSQL_VERSION_ID;          // Store to avoid warning from int4store
4723   int4store(fileinfo+51, tmp);
4724   int4store(fileinfo+55, create_info->extra_size);
4725   /*
4726     59-60 is unused since 10.2.4
4727     61 for default_part_db_type
4728   */
4729   int2store(fileinfo+62, create_info->key_block_size);
4730   DBUG_VOID_RETURN;
4731 } /* prepare_fileinfo */
4732 
4733 
update_create_info_from_table(HA_CREATE_INFO * create_info,TABLE * table)4734 void update_create_info_from_table(HA_CREATE_INFO *create_info, TABLE *table)
4735 {
4736   TABLE_SHARE *share= table->s;
4737   DBUG_ENTER("update_create_info_from_table");
4738 
4739   create_info->max_rows= share->max_rows;
4740   create_info->min_rows= share->min_rows;
4741   create_info->table_options= share->db_create_options;
4742   create_info->avg_row_length= share->avg_row_length;
4743   create_info->row_type= share->row_type;
4744   create_info->key_block_size= share->key_block_size;
4745   create_info->default_table_charset= share->table_charset;
4746   create_info->alter_table_convert_to_charset= 0;
4747   create_info->comment= share->comment;
4748   create_info->transactional= share->transactional;
4749   create_info->page_checksum= share->page_checksum;
4750   create_info->option_list= share->option_list;
4751   create_info->sequence= MY_TEST(share->sequence);
4752 
4753   DBUG_VOID_RETURN;
4754 }
4755 
4756 int
rename_file_ext(const char * from,const char * to,const char * ext)4757 rename_file_ext(const char * from,const char * to,const char * ext)
4758 {
4759   char from_b[FN_REFLEN],to_b[FN_REFLEN];
4760   (void) strxmov(from_b,from,ext,NullS);
4761   (void) strxmov(to_b,to,ext,NullS);
4762   return mysql_file_rename(key_file_frm, from_b, to_b, MYF(0));
4763 }
4764 
4765 
4766 /*
4767   Allocate string field in MEM_ROOT and return it as String
4768 
4769   SYNOPSIS
4770     get_field()
4771     mem   	MEM_ROOT for allocating
4772     field 	Field for retrieving of string
4773     res         result String
4774 
4775   RETURN VALUES
4776     1   string is empty
4777     0	all ok
4778 */
4779 
get_field(MEM_ROOT * mem,Field * field,String * res)4780 bool get_field(MEM_ROOT *mem, Field *field, String *res)
4781 {
4782   char *to;
4783   StringBuffer<MAX_FIELD_WIDTH> str;
4784   bool rc;
4785   THD *thd= field->get_thd();
4786   Sql_mode_instant_remove sms(thd, MODE_PAD_CHAR_TO_FULL_LENGTH);
4787 
4788   field->val_str(&str);
4789   if ((rc= !str.length() ||
4790            !(to= strmake_root(mem, str.ptr(), str.length()))))
4791   {
4792     res->length(0);
4793     return rc;
4794   }
4795   res->set(to, str.length(), field->charset());
4796   return false;
4797 }
4798 
4799 
4800 /*
4801   Allocate string field in MEM_ROOT and return it as NULL-terminated string
4802 
4803   SYNOPSIS
4804     get_field()
4805     mem   	MEM_ROOT for allocating
4806     field 	Field for retrieving of string
4807 
4808   RETURN VALUES
4809     NullS  string is empty
4810     #      pointer to NULL-terminated string value of field
4811 */
4812 
get_field(MEM_ROOT * mem,Field * field)4813 char *get_field(MEM_ROOT *mem, Field *field)
4814 {
4815   String str;
4816   bool rc= get_field(mem, field, &str);
4817   DBUG_ASSERT(rc || str.ptr()[str.length()] == '\0');
4818   return  rc ? NullS : (char *) str.ptr();
4819 }
4820 
4821 /*
4822   DESCRIPTION
4823     given a buffer with a key value, and a map of keyparts
4824     that are present in this value, returns the length of the value
4825 */
calculate_key_len(TABLE * table,uint key,const uchar * buf,key_part_map keypart_map)4826 uint calculate_key_len(TABLE *table, uint key, const uchar *buf,
4827                        key_part_map keypart_map)
4828 {
4829   /* works only with key prefixes */
4830   DBUG_ASSERT(((keypart_map + 1) & keypart_map) == 0);
4831 
4832   KEY *key_info= table->key_info+key;
4833   KEY_PART_INFO *key_part= key_info->key_part;
4834   KEY_PART_INFO *end_key_part= key_part + table->actual_n_key_parts(key_info);
4835   uint length= 0;
4836 
4837   while (key_part < end_key_part && keypart_map)
4838   {
4839     length+= key_part->store_length;
4840     keypart_map >>= 1;
4841     key_part++;
4842   }
4843   return length;
4844 }
4845 
4846 #ifndef DBUG_OFF
4847 /**
4848   Verifies that database/table name is in lowercase, when it should be
4849 
4850   This is supposed to be used only inside DBUG_ASSERT()
4851 */
ok_for_lower_case_names(const char * name)4852 bool ok_for_lower_case_names(const char *name)
4853 {
4854   if (!lower_case_table_names || !name)
4855     return true;
4856 
4857   char buf[SAFE_NAME_LEN];
4858   strmake_buf(buf, name);
4859   my_casedn_str(files_charset_info, buf);
4860   return strcmp(name, buf) == 0;
4861 }
4862 #endif
4863 
4864 /*
4865   Check if database name is valid
4866 
4867   SYNPOSIS
4868     check_db_name()
4869     org_name		Name of database
4870 
4871   NOTES
4872     If lower_case_table_names is set to 1 then database name is converted
4873     to lower case
4874 
4875   RETURN
4876     0	ok
4877     1   error
4878 */
4879 
check_db_name(LEX_STRING * org_name)4880 bool check_db_name(LEX_STRING *org_name)
4881 {
4882   char *name= org_name->str;
4883   size_t name_length= org_name->length;
4884   bool check_for_path_chars;
4885 
4886   if ((check_for_path_chars= check_mysql50_prefix(name)))
4887   {
4888     name+= MYSQL50_TABLE_NAME_PREFIX_LENGTH;
4889     name_length-= MYSQL50_TABLE_NAME_PREFIX_LENGTH;
4890   }
4891 
4892   if (!name_length || name_length > NAME_LEN)
4893     return 1;
4894 
4895   if (lower_case_table_names == 1 && name != any_db)
4896   {
4897     org_name->length= name_length= my_casedn_str(files_charset_info, name);
4898     if (check_for_path_chars)
4899       org_name->length+= MYSQL50_TABLE_NAME_PREFIX_LENGTH;
4900   }
4901   if (db_name_is_in_ignore_db_dirs_list(name))
4902     return 1;
4903 
4904   return check_table_name(name, name_length, check_for_path_chars);
4905 }
4906 
4907 
4908 /*
4909   Allow anything as a table name, as long as it doesn't contain an
4910   ' ' at the end
4911   returns 1 on error
4912 */
4913 
check_table_name(const char * name,size_t length,bool check_for_path_chars)4914 bool check_table_name(const char *name, size_t length, bool check_for_path_chars)
4915 {
4916   // name length in symbols
4917   size_t name_length= 0;
4918   const char *end= name+length;
4919 
4920   if (!check_for_path_chars &&
4921       (check_for_path_chars= check_mysql50_prefix(name)))
4922   {
4923     name+= MYSQL50_TABLE_NAME_PREFIX_LENGTH;
4924     length-= MYSQL50_TABLE_NAME_PREFIX_LENGTH;
4925   }
4926 
4927   if (!length || length > NAME_LEN)
4928     return 1;
4929 #if defined(USE_MB) && defined(USE_MB_IDENT)
4930   bool last_char_is_space= FALSE;
4931 #else
4932   if (name[length-1]==' ')
4933     return 1;
4934 #endif
4935 
4936   while (name != end)
4937   {
4938 #if defined(USE_MB) && defined(USE_MB_IDENT)
4939     last_char_is_space= my_isspace(system_charset_info, *name);
4940     if (system_charset_info->use_mb())
4941     {
4942       int len=my_ismbchar(system_charset_info, name, end);
4943       if (len)
4944       {
4945         name+= len;
4946         name_length++;
4947         continue;
4948       }
4949     }
4950 #endif
4951     if (check_for_path_chars &&
4952         (*name == '/' || *name == '\\' || *name == '~' || *name == FN_EXTCHAR))
4953       return 1;
4954     /*
4955       We don't allow zero byte in table/schema names:
4956       - Some code still uses NULL-terminated strings.
4957         Zero bytes will confuse this code.
4958       - There is a little practical use of zero bytes in names anyway.
4959       Note, if the string passed as "name" comes here
4960       from the parser as an identifier, it does not contain zero bytes,
4961       as the parser rejects zero bytes in identifiers.
4962       But "name" can also come here from queries like this:
4963         SELECT * FROM I_S.TABLES WHERE TABLE_NAME='str';
4964       In this case "name" is a general string expression
4965       and it can have any arbitrary bytes, including zero bytes.
4966     */
4967     if (*name == 0x00)
4968       return 1;
4969     name++;
4970     name_length++;
4971   }
4972 #if defined(USE_MB) && defined(USE_MB_IDENT)
4973   return last_char_is_space || (name_length > NAME_CHAR_LEN);
4974 #else
4975   return FALSE;
4976 #endif
4977 }
4978 
4979 
check_column_name(const char * name)4980 bool check_column_name(const char *name)
4981 {
4982   // name length in symbols
4983   size_t name_length= 0;
4984   bool last_char_is_space= TRUE;
4985 
4986   while (*name)
4987   {
4988 #if defined(USE_MB) && defined(USE_MB_IDENT)
4989     last_char_is_space= my_isspace(system_charset_info, *name);
4990     if (system_charset_info->use_mb())
4991     {
4992       int len=my_ismbchar(system_charset_info, name,
4993                           name+system_charset_info->mbmaxlen);
4994       if (len)
4995       {
4996         name += len;
4997         name_length++;
4998         continue;
4999       }
5000     }
5001 #else
5002     last_char_is_space= *name==' ';
5003     if (*name == '\377')
5004       return 1;
5005 #endif
5006     name++;
5007     name_length++;
5008   }
5009   /* Error if empty or too long column name */
5010   return last_char_is_space || (name_length > NAME_CHAR_LEN);
5011 }
5012 
5013 
5014 /**
5015   Checks whether a table is intact. Should be done *just* after the table has
5016   been opened.
5017 
5018   @param[in] table             The table to check
5019   @param[in] table_def         Expected structure of the table (column name
5020                                and type)
5021 
5022   @retval  FALSE  OK
5023   @retval  TRUE   There was an error. An error message is output
5024                   to the error log.  We do not push an error
5025                   message into the error stack because this
5026                   function is currently only called at start up,
5027                   and such errors never reach the user.
5028 */
5029 
5030 bool
check(TABLE * table,const TABLE_FIELD_DEF * table_def)5031 Table_check_intact::check(TABLE *table, const TABLE_FIELD_DEF *table_def)
5032 {
5033   uint i;
5034   my_bool error= FALSE;
5035   const TABLE_FIELD_TYPE *field_def= table_def->field;
5036   DBUG_ENTER("table_check_intact");
5037   DBUG_PRINT("info",("table: %s  expected_count: %d",
5038                      table->alias.c_ptr(), table_def->count));
5039 
5040   /* Whether the table definition has already been validated. */
5041   if (table->s->table_field_def_cache == table_def)
5042     goto end;
5043 
5044   if (table->s->fields != table_def->count)
5045   {
5046     THD *thd= current_thd;
5047     DBUG_PRINT("info", ("Column count has changed, checking the definition"));
5048 
5049     /* previous MySQL version */
5050     if (MYSQL_VERSION_ID > table->s->mysql_version)
5051     {
5052       report_error(ER_COL_COUNT_DOESNT_MATCH_PLEASE_UPDATE,
5053                    ER_THD(thd, ER_COL_COUNT_DOESNT_MATCH_PLEASE_UPDATE),
5054                    table->alias.c_ptr(), table_def->count, table->s->fields,
5055                    static_cast<int>(table->s->mysql_version),
5056                    MYSQL_VERSION_ID);
5057       DBUG_RETURN(TRUE);
5058     }
5059     else if (MYSQL_VERSION_ID == table->s->mysql_version)
5060     {
5061       report_error(ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2,
5062                    ER_THD(thd, ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2),
5063                    table->s->db.str, table->s->table_name.str,
5064                    table_def->count, table->s->fields);
5065       DBUG_RETURN(TRUE);
5066     }
5067     /*
5068       Something has definitely changed, but we're running an older
5069       version of MySQL with new system tables.
5070       Let's check column definitions. If a column was added at
5071       the end of the table, then we don't care much since such change
5072       is backward compatible.
5073     */
5074   }
5075   else
5076   {
5077   StringBuffer<1024> sql_type(system_charset_info);
5078   sql_type.extra_allocation(256); // Allocate min 256 characters at once
5079   for (i=0 ; i < table_def->count; i++, field_def++)
5080   {
5081     sql_type.length(0);
5082     if (i < table->s->fields)
5083     {
5084       Field *field= table->field[i];
5085 
5086       if (strncmp(field->field_name.str, field_def->name.str,
5087                   field_def->name.length))
5088       {
5089         /*
5090           Name changes are not fatal, we use ordinal numbers to access columns.
5091           Still this can be a sign of a tampered table, output an error
5092           to the error log.
5093         */
5094         report_error(0, "Incorrect definition of table %s.%s: "
5095                      "expected column '%s' at position %d, found '%s'.",
5096                      table->s->db.str, table->alias.c_ptr(),
5097                      field_def->name.str, i,
5098                      field->field_name.str);
5099       }
5100       field->sql_type(sql_type);
5101       /*
5102         Generally, if column types don't match, then something is
5103         wrong.
5104 
5105         However, we only compare column definitions up to the
5106         length of the original definition, since we consider the
5107         following definitions compatible:
5108 
5109         1. DATETIME and DATETIM
5110         2. INT(11) and INT(11
5111         3. SET('one', 'two') and SET('one', 'two', 'more')
5112 
5113         For SETs or ENUMs, if the same prefix is there it's OK to
5114         add more elements - they will get higher ordinal numbers and
5115         the new table definition is backward compatible with the
5116         original one.
5117        */
5118       if (strncmp(sql_type.c_ptr_safe(), field_def->type.str,
5119                   field_def->type.length - 1))
5120       {
5121         report_error(0, "Incorrect definition of table %s.%s: "
5122                      "expected column '%s' at position %d to have type "
5123                      "%s, found type %s.", table->s->db.str,
5124                      table->alias.c_ptr(),
5125                      field_def->name.str, i, field_def->type.str,
5126                      sql_type.c_ptr_safe());
5127         error= TRUE;
5128       }
5129       else if (field_def->cset.str && !field->has_charset())
5130       {
5131         report_error(0, "Incorrect definition of table %s.%s: "
5132                      "expected the type of column '%s' at position %d "
5133                      "to have character set '%s' but the type has no "
5134                      "character set.", table->s->db.str,
5135                      table->alias.c_ptr(),
5136                      field_def->name.str, i, field_def->cset.str);
5137         error= TRUE;
5138       }
5139       else if (field_def->cset.str &&
5140                strcmp(field->charset()->csname, field_def->cset.str))
5141       {
5142         report_error(0, "Incorrect definition of table %s.%s: "
5143                      "expected the type of column '%s' at position %d "
5144                      "to have character set '%s' but found "
5145                      "character set '%s'.", table->s->db.str,
5146                      table->alias.c_ptr(),
5147                      field_def->name.str, i, field_def->cset.str,
5148                      field->charset()->csname);
5149         error= TRUE;
5150       }
5151     }
5152     else
5153     {
5154       report_error(0, "Incorrect definition of table %s.%s: "
5155                    "expected column '%s' at position %d to have type %s "
5156                    " but the column is not found.",
5157                    table->s->db.str, table->alias.c_ptr(),
5158                    field_def->name.str, i, field_def->type.str);
5159       error= TRUE;
5160     }
5161   }
5162   }
5163 
5164   if (table_def->primary_key_parts)
5165   {
5166     if (table->s->primary_key == MAX_KEY)
5167     {
5168       report_error(0, "Incorrect definition of table %s.%s: "
5169                    "missing primary key.", table->s->db.str,
5170                    table->alias.c_ptr());
5171       error= TRUE;
5172     }
5173     else
5174     {
5175       KEY *pk= &table->s->key_info[table->s->primary_key];
5176       if (pk->user_defined_key_parts != table_def->primary_key_parts)
5177       {
5178         report_error(0, "Incorrect definition of table %s.%s: "
5179                      "Expected primary key to have %u columns, but instead "
5180                      "found %u columns.", table->s->db.str,
5181                      table->alias.c_ptr(), table_def->primary_key_parts,
5182                      pk->user_defined_key_parts);
5183         error= TRUE;
5184       }
5185       else
5186       {
5187         for (i= 0; i < pk->user_defined_key_parts; ++i)
5188         {
5189           if (table_def->primary_key_columns[i] + 1 != pk->key_part[i].fieldnr)
5190           {
5191             report_error(0, "Incorrect definition of table %s.%s: Expected "
5192                          "primary key part %u to refer to column %u, but "
5193                          "instead found column %u.", table->s->db.str,
5194                          table->alias.c_ptr(), i + 1,
5195                          table_def->primary_key_columns[i] + 1,
5196                          pk->key_part[i].fieldnr);
5197             error= TRUE;
5198           }
5199         }
5200       }
5201     }
5202   }
5203 
5204   if (likely(! error))
5205     table->s->table_field_def_cache= table_def;
5206 
5207 end:
5208 
5209   if (has_keys && !error && !table->key_info)
5210   {
5211     report_error(0, "Incorrect definition of table %s.%s: "
5212                  "indexes are missing",
5213                  table->s->db.str, table->alias.c_ptr());
5214     error= TRUE;
5215   }
5216 
5217   DBUG_RETURN(error);
5218 }
5219 
5220 
report_error(uint,const char * fmt,...)5221 void Table_check_intact_log_error::report_error(uint, const char *fmt, ...)
5222 {
5223   va_list args;
5224   va_start(args, fmt);
5225   error_log_print(ERROR_LEVEL, fmt, args);
5226   va_end(args);
5227 }
5228 
5229 
5230 /**
5231   Traverse portion of wait-for graph which is reachable through edge
5232   represented by this flush ticket in search for deadlocks.
5233 
5234   @retval TRUE  A deadlock is found. A victim is remembered
5235                 by the visitor.
5236   @retval FALSE Success, no deadlocks.
5237 */
5238 
accept_visitor(MDL_wait_for_graph_visitor * gvisitor)5239 bool Wait_for_flush::accept_visitor(MDL_wait_for_graph_visitor *gvisitor)
5240 {
5241   return m_share->visit_subgraph(this, gvisitor);
5242 }
5243 
5244 
get_deadlock_weight() const5245 uint Wait_for_flush::get_deadlock_weight() const
5246 {
5247   return m_deadlock_weight;
5248 }
5249 
5250 
5251 /**
5252   Traverse portion of wait-for graph which is reachable through this
5253   table share in search for deadlocks.
5254 
5255   @param waiting_ticket  Ticket representing wait for this share.
5256   @param dvisitor        Deadlock detection visitor.
5257 
5258   @retval TRUE  A deadlock is found. A victim is remembered
5259                 by the visitor.
5260   @retval FALSE No deadlocks, it's OK to begin wait.
5261 */
5262 
visit_subgraph(Wait_for_flush * wait_for_flush,MDL_wait_for_graph_visitor * gvisitor)5263 bool TABLE_SHARE::visit_subgraph(Wait_for_flush *wait_for_flush,
5264                                  MDL_wait_for_graph_visitor *gvisitor)
5265 {
5266   TABLE *table;
5267   MDL_context *src_ctx= wait_for_flush->get_ctx();
5268   bool result= TRUE;
5269 
5270   /*
5271     To protect all_tables list from being concurrently modified
5272     while we are iterating through it we increment tdc.all_tables_refs.
5273     This does not introduce deadlocks in the deadlock detector
5274     because we won't try to acquire tdc.LOCK_table_share while
5275     holding a write-lock on MDL_lock::m_rwlock.
5276   */
5277   mysql_mutex_lock(&tdc->LOCK_table_share);
5278   tdc->all_tables_refs++;
5279   mysql_mutex_unlock(&tdc->LOCK_table_share);
5280 
5281   All_share_tables_list::Iterator tables_it(tdc->all_tables);
5282 
5283   /*
5284     In case of multiple searches running in parallel, avoid going
5285     over the same loop twice and shortcut the search.
5286     Do it after taking the lock to weed out unnecessary races.
5287   */
5288   if (src_ctx->m_wait.get_status() != MDL_wait::EMPTY)
5289   {
5290     result= FALSE;
5291     goto end;
5292   }
5293 
5294   if (gvisitor->enter_node(src_ctx))
5295     goto end;
5296 
5297   while ((table= tables_it++))
5298   {
5299     DBUG_ASSERT(table->in_use && tdc->flushed);
5300     if (gvisitor->inspect_edge(&table->in_use->mdl_context))
5301     {
5302       goto end_leave_node;
5303     }
5304   }
5305 
5306   tables_it.rewind();
5307   while ((table= tables_it++))
5308   {
5309     DBUG_ASSERT(table->in_use && tdc->flushed);
5310     if (table->in_use->mdl_context.visit_subgraph(gvisitor))
5311     {
5312       goto end_leave_node;
5313     }
5314   }
5315 
5316   result= FALSE;
5317 
5318 end_leave_node:
5319   gvisitor->leave_node(src_ctx);
5320 
5321 end:
5322   mysql_mutex_lock(&tdc->LOCK_table_share);
5323   if (!--tdc->all_tables_refs)
5324     mysql_cond_broadcast(&tdc->COND_release);
5325   mysql_mutex_unlock(&tdc->LOCK_table_share);
5326 
5327   return result;
5328 }
5329 
5330 
5331 /**
5332   Wait until the subject share is removed from the table
5333   definition cache and make sure it's destroyed.
5334 
5335   @param mdl_context     MDL context for thread which is going to wait.
5336   @param abstime         Timeout for waiting as absolute time value.
5337   @param deadlock_weight Weight of this wait for deadlock detector.
5338 
5339   @pre LOCK_table_share is locked, the share is marked for flush and
5340        this connection does not reference the share.
5341        LOCK_table_share will be unlocked temporarily during execution.
5342 
5343   It may happen that another FLUSH TABLES thread marked this share
5344   for flush, but didn't yet purge it from table definition cache.
5345   In this case we may start waiting for a table share that has no
5346   references (ref_count == 0). We do this with assumption that this
5347   another FLUSH TABLES thread is about to purge this share.
5348 
5349   @retval FALSE - Success.
5350   @retval TRUE  - Error (OOM, deadlock, timeout, etc...).
5351 */
5352 
wait_for_old_version(THD * thd,struct timespec * abstime,uint deadlock_weight)5353 bool TABLE_SHARE::wait_for_old_version(THD *thd, struct timespec *abstime,
5354                                        uint deadlock_weight)
5355 {
5356   MDL_context *mdl_context= &thd->mdl_context;
5357   Wait_for_flush ticket(mdl_context, this, deadlock_weight);
5358   MDL_wait::enum_wait_status wait_status;
5359 
5360   mysql_mutex_assert_owner(&tdc->LOCK_table_share);
5361   DBUG_ASSERT(tdc->flushed);
5362 
5363   tdc->m_flush_tickets.push_front(&ticket);
5364 
5365   mdl_context->m_wait.reset_status();
5366 
5367   mysql_mutex_unlock(&tdc->LOCK_table_share);
5368 
5369   mdl_context->will_wait_for(&ticket);
5370 
5371   mdl_context->find_deadlock();
5372 
5373   wait_status= mdl_context->m_wait.timed_wait(thd, abstime, TRUE,
5374                                               &stage_waiting_for_table_flush);
5375 
5376   mdl_context->done_waiting_for();
5377 
5378   mysql_mutex_lock(&tdc->LOCK_table_share);
5379   tdc->m_flush_tickets.remove(&ticket);
5380   mysql_cond_broadcast(&tdc->COND_release);
5381   mysql_mutex_unlock(&tdc->LOCK_table_share);
5382 
5383 
5384   /*
5385     In cases when our wait was aborted by KILL statement,
5386     a deadlock or a timeout, the share might still be referenced,
5387     so we don't delete it. Note, that we can't determine this
5388     condition by checking wait_status alone, since, for example,
5389     a timeout can happen after all references to the table share
5390     were released, but before the share is removed from the
5391     cache and we receive the notification. This is why
5392     we first destroy the share, and then look at
5393     wait_status.
5394   */
5395   switch (wait_status)
5396   {
5397   case MDL_wait::GRANTED:
5398     return FALSE;
5399   case MDL_wait::VICTIM:
5400     my_error(ER_LOCK_DEADLOCK, MYF(0));
5401     return TRUE;
5402   case MDL_wait::TIMEOUT:
5403     my_error(ER_LOCK_WAIT_TIMEOUT, MYF(0));
5404     return TRUE;
5405   case MDL_wait::KILLED:
5406     return TRUE;
5407   default:
5408     DBUG_ASSERT(0);
5409     return TRUE;
5410   }
5411 }
5412 
5413 
5414 /**
5415   Initialize TABLE instance (newly created, or coming either from table
5416   cache or THD::temporary_tables list) and prepare it for further use
5417   during statement execution. Set the 'alias' attribute from the specified
5418   TABLE_LIST element. Remember the TABLE_LIST element in the
5419   TABLE::pos_in_table_list member.
5420 
5421   @param thd  Thread context.
5422   @param tl   TABLE_LIST element.
5423 */
5424 
init(THD * thd,TABLE_LIST * tl)5425 void TABLE::init(THD *thd, TABLE_LIST *tl)
5426 {
5427   DBUG_ASSERT(s->tmp_table != NO_TMP_TABLE || s->tdc->ref_count > 0);
5428 
5429   if (thd->lex->need_correct_ident())
5430     alias_name_used= my_strcasecmp(table_alias_charset,
5431                                    s->table_name.str,
5432                                    tl->alias.str);
5433   /* Fix alias if table name changes. */
5434   if (!alias.alloced_length() || strcmp(alias.c_ptr(), tl->alias.str))
5435     alias.copy(tl->alias.str, tl->alias.length, alias.charset());
5436 
5437   tablenr= thd->current_tablenr++;
5438   used_fields= 0;
5439   const_table= 0;
5440   null_row= 0;
5441   maybe_null= 0;
5442   force_index= 0;
5443   force_index_order= 0;
5444   force_index_group= 0;
5445   status= STATUS_NO_RECORD;
5446   insert_values= 0;
5447   fulltext_searched= 0;
5448   file->ft_handler= 0;
5449   reginfo.impossible_range= 0;
5450   reginfo.join_tab= NULL;
5451   reginfo.not_exists_optimize= FALSE;
5452   created= TRUE;
5453   cond_selectivity= 1.0;
5454   cond_selectivity_sampling_explain= NULL;
5455   range_rowid_filter_cost_info_elems= 0;
5456   range_rowid_filter_cost_info_ptr= NULL;
5457   range_rowid_filter_cost_info= NULL;
5458   vers_write= s->versioned;
5459   opt_range_condition_rows=0;
5460   no_cache= false;
5461   initialize_opt_range_structures();
5462 #ifdef HAVE_REPLICATION
5463   /* used in RBR Triggers */
5464   master_had_triggers= 0;
5465 #endif
5466   /* Catch wrong handling of the auto_increment_field_not_null. */
5467   DBUG_ASSERT(!auto_increment_field_not_null);
5468   auto_increment_field_not_null= FALSE;
5469 
5470   pos_in_table_list= tl;
5471 
5472   clear_column_bitmaps();
5473   for (Field **f_ptr= field ; *f_ptr ; f_ptr++)
5474   {
5475     (*f_ptr)->next_equal_field= NULL;
5476     (*f_ptr)->cond_selectivity= 1.0;
5477   }
5478 
5479   notnull_cond= 0;
5480   DBUG_ASSERT(!file->keyread_enabled());
5481 
5482   restore_record(this, s->default_values);
5483 
5484   /* Tables may be reused in a sub statement. */
5485   DBUG_ASSERT(!file->extra(HA_EXTRA_IS_ATTACHED_CHILDREN));
5486 }
5487 
5488 
5489 /*
5490   Create Item_field for each column in the table.
5491 
5492   SYNPOSIS
5493     TABLE::fill_item_list()
5494       item_list          a pointer to an empty list used to store items
5495 
5496   DESCRIPTION
5497     Create Item_field object for each column in the table and
5498     initialize it with the corresponding Field. New items are
5499     created in the current THD memory root.
5500 
5501   RETURN VALUE
5502     0                    success
5503     1                    out of memory
5504 */
5505 
fill_item_list(List<Item> * item_list) const5506 bool TABLE::fill_item_list(List<Item> *item_list) const
5507 {
5508   /*
5509     All Item_field's created using a direct pointer to a field
5510     are fixed in Item_field constructor.
5511   */
5512   for (Field **ptr= field; *ptr; ptr++)
5513   {
5514     Item_field *item= new (in_use->mem_root) Item_field(in_use, *ptr);
5515     if (!item || item_list->push_back(item))
5516       return TRUE;
5517   }
5518   return FALSE;
5519 }
5520 
5521 /*
5522   Reset an existing list of Item_field items to point to the
5523   Fields of this table.
5524 
5525   SYNPOSIS
5526     TABLE::fill_item_list()
5527       item_list          a non-empty list with Item_fields
5528 
5529   DESCRIPTION
5530     This is a counterpart of fill_item_list used to redirect
5531     Item_fields to the fields of a newly created table.
5532     The caller must ensure that number of items in the item_list
5533     is the same as the number of columns in the table.
5534 */
5535 
reset_item_list(List<Item> * item_list,uint skip) const5536 void TABLE::reset_item_list(List<Item> *item_list, uint skip) const
5537 {
5538   List_iterator_fast<Item> it(*item_list);
5539   Field **ptr= field;
5540   for ( ; skip && *ptr; skip--)
5541     ptr++;
5542   for (; *ptr; ptr++)
5543   {
5544     Item_field *item_field= (Item_field*) it++;
5545     DBUG_ASSERT(item_field != 0);
5546     item_field->reset_field(*ptr);
5547   }
5548 }
5549 
5550 /*
5551   calculate md5 of query
5552 
5553   SYNOPSIS
5554     TABLE_LIST::calc_md5()
5555     buffer	buffer for md5 writing
5556 */
5557 
calc_md5(char * buffer)5558 void  TABLE_LIST::calc_md5(char *buffer)
5559 {
5560   uchar digest[16];
5561   compute_md5_hash(digest, select_stmt.str,
5562                    select_stmt.length);
5563   sprintf(buffer,
5564 	    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
5565 	    digest[0], digest[1], digest[2], digest[3],
5566 	    digest[4], digest[5], digest[6], digest[7],
5567 	    digest[8], digest[9], digest[10], digest[11],
5568 	    digest[12], digest[13], digest[14], digest[15]);
5569 }
5570 
5571 
5572 /**
5573   @brief
5574   Create field translation for mergeable derived table/view.
5575 
5576   @param thd  Thread handle
5577 
5578   @details
5579   Create field translation for mergeable derived table/view.
5580 
5581   @return FALSE ok.
5582   @return TRUE an error occur.
5583 */
5584 
create_field_translation(THD * thd)5585 bool TABLE_LIST::create_field_translation(THD *thd)
5586 {
5587   Item *item;
5588   Field_translator *transl;
5589   SELECT_LEX *select= get_single_select();
5590   List_iterator_fast<Item> it(select->item_list);
5591   uint field_count= 0;
5592   Query_arena *arena, backup;
5593   bool res= FALSE;
5594   DBUG_ENTER("TABLE_LIST::create_field_translation");
5595   DBUG_PRINT("enter", ("Alias: '%s'  Unit: %p",
5596                       (alias.str ? alias.str : "<NULL>"),
5597                        get_unit()));
5598 
5599   if (thd->stmt_arena->is_conventional() ||
5600       thd->stmt_arena->is_stmt_prepare_or_first_sp_execute())
5601   {
5602     /* initialize lists */
5603     used_items.empty();
5604     persistent_used_items.empty();
5605   }
5606   else
5607   {
5608     /*
5609       Copy the list created by natural join procedure because the procedure
5610       will not be repeated.
5611     */
5612     used_items= persistent_used_items;
5613   }
5614 
5615   if (field_translation)
5616   {
5617     /*
5618       Update items in the field translation after view have been prepared.
5619       It's needed because some items in the select list, like IN subselects,
5620       might be substituted for optimized ones.
5621     */
5622     if (is_view() && get_unit()->prepared && !field_translation_updated)
5623     {
5624       field_translation_updated= TRUE;
5625       if (static_cast<uint>(field_translation_end - field_translation) <
5626           select->item_list.elements)
5627         goto allocate;
5628       while ((item= it++))
5629       {
5630         field_translation[field_count++].item= item;
5631       }
5632     }
5633 
5634     DBUG_RETURN(FALSE);
5635   }
5636 
5637 allocate:
5638   arena= thd->activate_stmt_arena_if_needed(&backup);
5639 
5640   /* Create view fields translation table */
5641 
5642   if (!(transl=
5643         (Field_translator*)(thd->stmt_arena->
5644                             alloc(select->item_list.elements *
5645                                   sizeof(Field_translator)))))
5646   {
5647     res= TRUE;
5648     goto exit;
5649   }
5650 
5651   while ((item= it++))
5652   {
5653     DBUG_ASSERT(item->name.str && item->name.str[0]);
5654     transl[field_count].name.str=    thd->strmake(item->name.str, item->name.length);
5655     transl[field_count].name.length= item->name.length;
5656     transl[field_count++].item= item;
5657   }
5658   field_translation= transl;
5659   field_translation_end= transl + field_count;
5660   /* It's safe to cache this table for prepared statements */
5661   cacheable_table= 1;
5662 
5663 exit:
5664   if (arena)
5665     thd->restore_active_arena(arena, &backup);
5666 
5667   DBUG_RETURN(res);
5668 }
5669 
5670 
5671 /**
5672   @brief
5673   Create field translation for mergeable derived table/view.
5674 
5675   @param thd  Thread handle
5676 
5677   @details
5678   Create field translation for mergeable derived table/view.
5679 
5680   @return FALSE ok.
5681   @return TRUE an error occur.
5682 */
5683 
setup_underlying(THD * thd)5684 bool TABLE_LIST::setup_underlying(THD *thd)
5685 {
5686   DBUG_ENTER("TABLE_LIST::setup_underlying");
5687 
5688   if (!view || (!field_translation && merge_underlying_list))
5689   {
5690     SELECT_LEX *select= get_single_select();
5691 
5692     if (create_field_translation(thd))
5693       DBUG_RETURN(TRUE);
5694 
5695     /* full text function moving to current select */
5696     if (select->ftfunc_list->elements)
5697     {
5698       Item_func_match *ifm;
5699       SELECT_LEX *current_select= thd->lex->current_select;
5700       List_iterator_fast<Item_func_match>
5701         li(*(select_lex->ftfunc_list));
5702       while ((ifm= li++))
5703         current_select->ftfunc_list->push_front(ifm);
5704     }
5705   }
5706   DBUG_RETURN(FALSE);
5707 }
5708 
5709 
5710 /*
5711    Prepare where expression of derived table/view
5712 
5713   SYNOPSIS
5714     TABLE_LIST::prep_where()
5715     thd             - thread handler
5716     conds           - condition of this JOIN
5717     no_where_clause - do not build WHERE or ON outer qwery do not need it
5718                       (it is INSERT), we do not need conds if this flag is set
5719 
5720   NOTE: have to be called befor CHECK OPTION preparation, because it makes
5721   fix_fields for view WHERE clause
5722 
5723   RETURN
5724     FALSE - OK
5725     TRUE  - error
5726 */
5727 
prep_where(THD * thd,Item ** conds,bool no_where_clause)5728 bool TABLE_LIST::prep_where(THD *thd, Item **conds,
5729                                bool no_where_clause)
5730 {
5731   DBUG_ENTER("TABLE_LIST::prep_where");
5732   bool res= FALSE;
5733 
5734   for (TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
5735   {
5736     if (tbl->is_view_or_derived() &&
5737         tbl->prep_where(thd, conds, no_where_clause))
5738     {
5739       DBUG_RETURN(TRUE);
5740     }
5741   }
5742 
5743   if (where)
5744   {
5745     if (where->is_fixed())
5746       where->update_used_tables();
5747     else if (where->fix_fields(thd, &where))
5748       DBUG_RETURN(TRUE);
5749 
5750     /*
5751       check that it is not VIEW in which we insert with INSERT SELECT
5752       (in this case we can't add view WHERE condition to main SELECT_LEX)
5753     */
5754     if (!no_where_clause && !where_processed)
5755     {
5756       TABLE_LIST *tbl= this;
5757       Query_arena *arena= thd->stmt_arena, backup;
5758       arena= thd->activate_stmt_arena_if_needed(&backup);  // For easier test
5759 
5760       /* Go up to join tree and try to find left join */
5761       for (; tbl; tbl= tbl->embedding)
5762       {
5763         if (tbl->outer_join)
5764         {
5765           /*
5766             Store WHERE condition to ON expression for outer join, because
5767             we can't use WHERE to correctly execute left joins on VIEWs and
5768             this expression will not be moved to WHERE condition (i.e. will
5769             be clean correctly for PS/SP)
5770           */
5771           tbl->on_expr= and_conds(thd, tbl->on_expr,
5772                                   where->copy_andor_structure(thd));
5773           break;
5774         }
5775       }
5776       if (tbl == 0)
5777       {
5778         if (*conds)
5779           res= (*conds)->fix_fields_if_needed_for_bool(thd, conds);
5780         if (!res)
5781           *conds= and_conds(thd, *conds, where->copy_andor_structure(thd));
5782         if (*conds && !res)
5783           res= (*conds)->fix_fields_if_needed_for_bool(thd, conds);
5784       }
5785       if (arena)
5786         thd->restore_active_arena(arena, &backup);
5787       where_processed= TRUE;
5788     }
5789   }
5790 
5791   DBUG_RETURN(res);
5792 }
5793 
5794 /**
5795   Check that table/view is updatable and if it has single
5796   underlying tables/views it is also updatable
5797 
5798   @return Result of the check.
5799 */
5800 
single_table_updatable()5801 bool TABLE_LIST::single_table_updatable()
5802 {
5803   if (!updatable)
5804     return false;
5805   if (view && view->first_select_lex()->table_list.elements == 1)
5806   {
5807     /*
5808       We need to check deeply only single table views. Multi-table views
5809       will be turned to multi-table updates and then checked by leaf tables
5810     */
5811     return (((TABLE_LIST *)view->first_select_lex()->table_list.first)->
5812             single_table_updatable());
5813   }
5814   return true;
5815 }
5816 
5817 
5818 /*
5819   Merge ON expressions for a view
5820 
5821   SYNOPSIS
5822     merge_on_conds()
5823     thd             thread handle
5824     table           table for the VIEW
5825     is_cascaded     TRUE <=> merge ON expressions from underlying views
5826 
5827   DESCRIPTION
5828     This function returns the result of ANDing the ON expressions
5829     of the given view and all underlying views. The ON expressions
5830     of the underlying views are added only if is_cascaded is TRUE.
5831 
5832   RETURN
5833     Pointer to the built expression if there is any.
5834     Otherwise and in the case of a failure NULL is returned.
5835 */
5836 
5837 static Item *
merge_on_conds(THD * thd,TABLE_LIST * table,bool is_cascaded)5838 merge_on_conds(THD *thd, TABLE_LIST *table, bool is_cascaded)
5839 {
5840   DBUG_ENTER("merge_on_conds");
5841 
5842   Item *cond= NULL;
5843   DBUG_PRINT("info", ("alias: %s", table->alias.str));
5844   if (table->on_expr)
5845     cond= table->on_expr->copy_andor_structure(thd);
5846   if (!table->view)
5847     DBUG_RETURN(cond);
5848   for (TABLE_LIST *tbl=
5849          (TABLE_LIST*)table->view->first_select_lex()->table_list.first;
5850        tbl;
5851        tbl= tbl->next_local)
5852   {
5853     if (tbl->view && !is_cascaded)
5854       continue;
5855     cond= and_conds(thd, cond, merge_on_conds(thd, tbl, is_cascaded));
5856   }
5857   DBUG_RETURN(cond);
5858 }
5859 
5860 
5861 /*
5862   Prepare check option expression of table
5863 
5864   SYNOPSIS
5865     TABLE_LIST::prep_check_option()
5866     thd             - thread handler
5867     check_opt_type  - WITH CHECK OPTION type (VIEW_CHECK_NONE,
5868                       VIEW_CHECK_LOCAL, VIEW_CHECK_CASCADED)
5869                       we use this parameter instead of direct check of
5870                       effective_with_check to change type of underlying
5871                       views to VIEW_CHECK_CASCADED if outer view have
5872                       such option and prevent processing of underlying
5873                       view check options if outer view have just
5874                       VIEW_CHECK_LOCAL option.
5875 
5876   NOTE
5877     This method builds check option condition to use it later on
5878     every call (usual execution or every SP/PS call).
5879     This method have to be called after WHERE preparation
5880     (TABLE_LIST::prep_where)
5881 
5882   RETURN
5883     FALSE - OK
5884     TRUE  - error
5885 */
5886 
prep_check_option(THD * thd,uint8 check_opt_type)5887 bool TABLE_LIST::prep_check_option(THD *thd, uint8 check_opt_type)
5888 {
5889   DBUG_ENTER("TABLE_LIST::prep_check_option");
5890   bool is_cascaded= check_opt_type == VIEW_CHECK_CASCADED;
5891   TABLE_LIST *merge_underlying_list= view->first_select_lex()->get_table_list();
5892   for (TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
5893   {
5894     /* see comment of check_opt_type parameter */
5895     if (tbl->view && tbl->prep_check_option(thd, (is_cascaded ?
5896                                                   VIEW_CHECK_CASCADED :
5897                                                   VIEW_CHECK_NONE)))
5898       DBUG_RETURN(TRUE);
5899   }
5900 
5901   if (check_opt_type && !check_option_processed)
5902   {
5903     Query_arena *arena= thd->stmt_arena, backup;
5904     arena= thd->activate_stmt_arena_if_needed(&backup);  // For easier test
5905 
5906     if (where)
5907     {
5908       check_option= where->copy_andor_structure(thd);
5909     }
5910     if (is_cascaded)
5911     {
5912       for (TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
5913       {
5914         if (tbl->check_option)
5915           check_option= and_conds(thd, check_option, tbl->check_option);
5916       }
5917     }
5918     check_option= and_conds(thd, check_option,
5919                             merge_on_conds(thd, this, is_cascaded));
5920 
5921     if (arena)
5922       thd->restore_active_arena(arena, &backup);
5923     check_option_processed= TRUE;
5924 
5925   }
5926 
5927   if (check_option)
5928   {
5929     const char *save_where= thd->where;
5930     thd->where= "check option";
5931     if (check_option->fix_fields_if_needed_for_bool(thd, &check_option))
5932       DBUG_RETURN(TRUE);
5933     thd->where= save_where;
5934   }
5935   DBUG_RETURN(FALSE);
5936 }
5937 
5938 
5939 /**
5940   Hide errors which show view underlying table information.
5941   There are currently two mechanisms at work that handle errors for views,
5942   this one and a more general mechanism based on an Internal_error_handler,
5943   see Show_create_error_handler. The latter handles errors encountered during
5944   execution of SHOW CREATE VIEW, while the mechanism using this method is
5945   handles SELECT from views. The two methods should not clash.
5946 
5947   @param[in,out]  thd     thread handler
5948 
5949   @pre This method can be called only if there is an error.
5950 */
5951 
hide_view_error(THD * thd)5952 void TABLE_LIST::hide_view_error(THD *thd)
5953 {
5954   if ((thd->killed && !thd->is_error())|| thd->get_internal_handler())
5955     return;
5956   /* Hide "Unknown column" or "Unknown function" error */
5957   DBUG_ASSERT(thd->is_error());
5958   switch (thd->get_stmt_da()->sql_errno()) {
5959     case ER_BAD_FIELD_ERROR:
5960     case ER_SP_DOES_NOT_EXIST:
5961     case ER_FUNC_INEXISTENT_NAME_COLLISION:
5962     case ER_PROCACCESS_DENIED_ERROR:
5963     case ER_COLUMNACCESS_DENIED_ERROR:
5964     case ER_TABLEACCESS_DENIED_ERROR:
5965     case ER_TABLE_NOT_LOCKED:
5966     case ER_NO_SUCH_TABLE:
5967     {
5968       TABLE_LIST *top= top_table();
5969       thd->clear_error();
5970       my_error(ER_VIEW_INVALID, MYF(0),
5971                top->view_db.str, top->view_name.str);
5972       break;
5973     }
5974 
5975     case ER_NO_DEFAULT_FOR_FIELD:
5976     {
5977       TABLE_LIST *top= top_table();
5978       thd->clear_error();
5979       // TODO: make correct error message
5980       my_error(ER_NO_DEFAULT_FOR_VIEW_FIELD, MYF(0),
5981                top->view_db.str, top->view_name.str);
5982       break;
5983     }
5984   }
5985 }
5986 
5987 
5988 /*
5989   Find underlying base tables (TABLE_LIST) which represent given
5990   table_to_find (TABLE)
5991 
5992   SYNOPSIS
5993     TABLE_LIST::find_underlying_table()
5994     table_to_find table to find
5995 
5996   RETURN
5997     0  table is not found
5998     found table reference
5999 */
6000 
find_underlying_table(TABLE * table_to_find)6001 TABLE_LIST *TABLE_LIST::find_underlying_table(TABLE *table_to_find)
6002 {
6003   /* is this real table and table which we are looking for? */
6004   if (table == table_to_find && view == 0)
6005     return this;
6006   if (!view)
6007     return 0;
6008 
6009   for (TABLE_LIST *tbl= view->first_select_lex()->get_table_list();
6010        tbl;
6011        tbl= tbl->next_local)
6012   {
6013     TABLE_LIST *result;
6014     if ((result= tbl->find_underlying_table(table_to_find)))
6015       return result;
6016   }
6017   return 0;
6018 }
6019 
6020 /*
6021   cleanup items belonged to view fields translation table
6022 
6023   SYNOPSIS
6024     TABLE_LIST::cleanup_items()
6025 */
6026 
cleanup_items()6027 void TABLE_LIST::cleanup_items()
6028 {
6029   if (!field_translation)
6030     return;
6031 
6032   for (Field_translator *transl= field_translation;
6033        transl < field_translation_end;
6034        transl++)
6035     transl->item->walk(&Item::cleanup_processor, 0, 0);
6036 }
6037 
6038 
6039 /*
6040   check CHECK OPTION condition both for view and underlying table
6041 
6042   SYNOPSIS
6043     TABLE_LIST::view_check_option()
6044     ignore_failure ignore check option fail
6045 
6046   RETURN
6047     VIEW_CHECK_OK     OK
6048     VIEW_CHECK_ERROR  FAILED
6049     VIEW_CHECK_SKIP   FAILED, but continue
6050 */
6051 
6052 
view_check_option(THD * thd,bool ignore_failure)6053 int TABLE_LIST::view_check_option(THD *thd, bool ignore_failure)
6054 {
6055   if (check_option)
6056   {
6057     /* VIEW's CHECK OPTION CLAUSE */
6058     Counting_error_handler ceh;
6059     thd->push_internal_handler(&ceh);
6060     bool res= check_option->val_int() == 0;
6061     thd->pop_internal_handler();
6062     if (ceh.errors)
6063       return(VIEW_CHECK_ERROR);
6064     if (res)
6065     {
6066       TABLE_LIST *main_view= top_table();
6067       const char *name_db= (main_view->view ? main_view->view_db.str :
6068                             main_view->db.str);
6069       const char *name_table= (main_view->view ? main_view->view_name.str :
6070                                main_view->table_name.str);
6071       my_error(ER_VIEW_CHECK_FAILED, MYF(ignore_failure ? ME_WARNING : 0),
6072                name_db, name_table);
6073       return ignore_failure ? VIEW_CHECK_SKIP : VIEW_CHECK_ERROR;
6074     }
6075   }
6076   return table->verify_constraints(ignore_failure);
6077 }
6078 
6079 
verify_constraints(bool ignore_failure)6080 int TABLE::verify_constraints(bool ignore_failure)
6081 {
6082   /*
6083     We have to check is_error() first as we are checking it for each
6084     constraint to catch fatal warnings.
6085   */
6086   if (in_use->is_error())
6087     return (VIEW_CHECK_ERROR);
6088 
6089   /* go trough check option clauses for fields and table */
6090   if (check_constraints &&
6091       !(in_use->variables.option_bits & OPTION_NO_CHECK_CONSTRAINT_CHECKS))
6092   {
6093     if (versioned() && !vers_end_field()->is_max())
6094       return VIEW_CHECK_OK;
6095     for (Virtual_column_info **chk= check_constraints ; *chk ; chk++)
6096     {
6097       /*
6098         yes! NULL is ok.
6099         see 4.23.3.4 Table check constraints, part 2, SQL:2016
6100       */
6101       if (((*chk)->expr->val_int() == 0 && !(*chk)->expr->null_value) ||
6102           in_use->is_error())
6103       {
6104         StringBuffer<MAX_FIELD_WIDTH> field_error(system_charset_info);
6105         enum_vcol_info_type vcol_type= (*chk)->get_vcol_type();
6106         DBUG_ASSERT(vcol_type == VCOL_CHECK_TABLE ||
6107                     vcol_type == VCOL_CHECK_FIELD);
6108         if (vcol_type == VCOL_CHECK_FIELD)
6109         {
6110           field_error.append(s->table_name.str);
6111           field_error.append(".");
6112         }
6113         field_error.append((*chk)->name.str);
6114         my_error(ER_CONSTRAINT_FAILED,
6115                  MYF(ignore_failure ? ME_WARNING : 0), field_error.c_ptr(),
6116                  s->db.str, s->table_name.str);
6117         return ignore_failure ? VIEW_CHECK_SKIP : VIEW_CHECK_ERROR;
6118       }
6119     }
6120   }
6121   /*
6122     We have to check in_use() as checking constraints may have generated
6123     warnings that should be treated as errors
6124   */
6125   return(!in_use->is_error() ? VIEW_CHECK_OK : VIEW_CHECK_ERROR);
6126 }
6127 
6128 /*
6129   Find table in underlying tables by mask and check that only this
6130   table belong to given mask
6131 
6132   SYNOPSIS
6133     TABLE_LIST::check_single_table()
6134     table_arg	reference on variable where to store found table
6135 		(should be 0 on call, to find table, or point to table for
6136 		unique test)
6137     map         bit mask of tables
6138     view_arg    view for which we are looking table
6139 
6140   RETURN
6141     FALSE table not found or found only one
6142     TRUE  found several tables
6143 */
6144 
check_single_table(TABLE_LIST ** table_arg,table_map map,TABLE_LIST * view_arg)6145 bool TABLE_LIST::check_single_table(TABLE_LIST **table_arg,
6146                                        table_map map,
6147                                        TABLE_LIST *view_arg)
6148 {
6149   if (!select_lex)
6150     return FALSE;
6151   DBUG_ASSERT(is_merged_derived());
6152   for (TABLE_LIST *tbl= get_single_select()->get_table_list();
6153        tbl;
6154        tbl= tbl->next_local)
6155   {
6156     /*
6157       Merged view has also temporary table attached (in 5.2 if it has table
6158       then it was real table), so we have filter such temporary tables out
6159       by checking that it is not merged view
6160     */
6161     if (tbl->table &&
6162         !(tbl->is_view() &&
6163           tbl->is_merged_derived()))
6164     {
6165       if (tbl->table->map & map)
6166       {
6167 	if (*table_arg)
6168 	  return TRUE;
6169         *table_arg= tbl;
6170         tbl->check_option= view_arg->check_option;
6171       }
6172     }
6173     else if (tbl->check_single_table(table_arg, map, view_arg))
6174       return TRUE;
6175   }
6176   return FALSE;
6177 }
6178 
6179 
6180 /*
6181   Set insert_values buffer
6182 
6183   SYNOPSIS
6184     set_insert_values()
6185     mem_root   memory pool for allocating
6186 
6187   RETURN
6188     FALSE - OK
6189     TRUE  - out of memory
6190 */
6191 
set_insert_values(MEM_ROOT * mem_root)6192 bool TABLE_LIST::set_insert_values(MEM_ROOT *mem_root)
6193 {
6194   DBUG_ENTER("set_insert_values");
6195   if (table)
6196   {
6197     DBUG_PRINT("info", ("setting insert_value for table"));
6198     if (!table->insert_values &&
6199         !(table->insert_values= (uchar *)alloc_root(mem_root,
6200                                                    table->s->rec_buff_length)))
6201       DBUG_RETURN(TRUE);
6202   }
6203   else
6204   {
6205     DBUG_PRINT("info", ("setting insert_value for view"));
6206     DBUG_ASSERT(is_view_or_derived() && is_merged_derived());
6207     for (TABLE_LIST *tbl=
6208            (TABLE_LIST*)view->first_select_lex()->table_list.first;
6209          tbl;
6210          tbl= tbl->next_local)
6211       if (tbl->set_insert_values(mem_root))
6212         DBUG_RETURN(TRUE);
6213   }
6214   DBUG_RETURN(FALSE);
6215 }
6216 
6217 
6218 /*
6219   Test if this is a leaf with respect to name resolution.
6220 
6221   SYNOPSIS
6222     TABLE_LIST::is_leaf_for_name_resolution()
6223 
6224   DESCRIPTION
6225     A table reference is a leaf with respect to name resolution if
6226     it is either a leaf node in a nested join tree (table, view,
6227     schema table, subquery), or an inner node that represents a
6228     NATURAL/USING join, or a nested join with materialized join
6229     columns.
6230 
6231   RETURN
6232     TRUE if a leaf, FALSE otherwise.
6233 */
is_leaf_for_name_resolution()6234 bool TABLE_LIST::is_leaf_for_name_resolution()
6235 {
6236   return (is_merged_derived() || is_natural_join || is_join_columns_complete ||
6237           !nested_join);
6238 }
6239 
6240 
6241 /*
6242   Retrieve the first (left-most) leaf in a nested join tree with
6243   respect to name resolution.
6244 
6245   SYNOPSIS
6246     TABLE_LIST::first_leaf_for_name_resolution()
6247 
6248   DESCRIPTION
6249     Given that 'this' is a nested table reference, recursively walk
6250     down the left-most children of 'this' until we reach a leaf
6251     table reference with respect to name resolution.
6252 
6253   IMPLEMENTATION
6254     The left-most child of a nested table reference is the last element
6255     in the list of children because the children are inserted in
6256     reverse order.
6257 
6258   RETURN
6259     If 'this' is a nested table reference - the left-most child of
6260       the tree rooted in 'this',
6261     else return 'this'
6262 */
6263 
first_leaf_for_name_resolution()6264 TABLE_LIST *TABLE_LIST::first_leaf_for_name_resolution()
6265 {
6266   TABLE_LIST *UNINIT_VAR(cur_table_ref);
6267   NESTED_JOIN *cur_nested_join;
6268 
6269   if (is_leaf_for_name_resolution())
6270     return this;
6271   DBUG_ASSERT(nested_join);
6272 
6273   for (cur_nested_join= nested_join;
6274        cur_nested_join;
6275        cur_nested_join= cur_table_ref->nested_join)
6276   {
6277     List_iterator_fast<TABLE_LIST> it(cur_nested_join->join_list);
6278     cur_table_ref= it++;
6279     /*
6280       If the current nested join is a RIGHT JOIN, the operands in
6281       'join_list' are in reverse order, thus the first operand is
6282       already at the front of the list. Otherwise the first operand
6283       is in the end of the list of join operands.
6284     */
6285     if (!(cur_table_ref->outer_join & JOIN_TYPE_RIGHT))
6286     {
6287       TABLE_LIST *next;
6288       while ((next= it++))
6289         cur_table_ref= next;
6290     }
6291     if (cur_table_ref->is_leaf_for_name_resolution())
6292       break;
6293   }
6294   return cur_table_ref;
6295 }
6296 
6297 
6298 /*
6299   Retrieve the last (right-most) leaf in a nested join tree with
6300   respect to name resolution.
6301 
6302   SYNOPSIS
6303     TABLE_LIST::last_leaf_for_name_resolution()
6304 
6305   DESCRIPTION
6306     Given that 'this' is a nested table reference, recursively walk
6307     down the right-most children of 'this' until we reach a leaf
6308     table reference with respect to name resolution.
6309 
6310   IMPLEMENTATION
6311     The right-most child of a nested table reference is the first
6312     element in the list of children because the children are inserted
6313     in reverse order.
6314 
6315   RETURN
6316     - If 'this' is a nested table reference - the right-most child of
6317       the tree rooted in 'this',
6318     - else - 'this'
6319 */
6320 
last_leaf_for_name_resolution()6321 TABLE_LIST *TABLE_LIST::last_leaf_for_name_resolution()
6322 {
6323   TABLE_LIST *cur_table_ref= this;
6324   NESTED_JOIN *cur_nested_join;
6325 
6326   if (is_leaf_for_name_resolution())
6327     return this;
6328   DBUG_ASSERT(nested_join);
6329 
6330   for (cur_nested_join= nested_join;
6331        cur_nested_join;
6332        cur_nested_join= cur_table_ref->nested_join)
6333   {
6334     cur_table_ref= cur_nested_join->join_list.head();
6335     /*
6336       If the current nested is a RIGHT JOIN, the operands in
6337       'join_list' are in reverse order, thus the last operand is in the
6338       end of the list.
6339     */
6340     if ((cur_table_ref->outer_join & JOIN_TYPE_RIGHT))
6341     {
6342       List_iterator_fast<TABLE_LIST> it(cur_nested_join->join_list);
6343       TABLE_LIST *next;
6344       cur_table_ref= it++;
6345       while ((next= it++))
6346         cur_table_ref= next;
6347     }
6348     if (cur_table_ref->is_leaf_for_name_resolution())
6349       break;
6350   }
6351   return cur_table_ref;
6352 }
6353 
6354 
6355 /*
6356   Register access mode which we need for underlying tables
6357 
6358   SYNOPSIS
6359     register_want_access()
6360     want_access          Acess which we require
6361 */
6362 
register_want_access(privilege_t want_access)6363 void TABLE_LIST::register_want_access(privilege_t want_access)
6364 {
6365   /* Remove SHOW_VIEW_ACL, because it will be checked during making view */
6366   want_access&= ~SHOW_VIEW_ACL;
6367   if (belong_to_view)
6368   {
6369     grant.want_privilege= want_access;
6370     if (table)
6371       table->grant.want_privilege= want_access;
6372   }
6373   if (!view)
6374     return;
6375   for (TABLE_LIST *tbl= view->first_select_lex()->get_table_list();
6376        tbl;
6377        tbl= tbl->next_local)
6378     tbl->register_want_access(want_access);
6379 }
6380 
6381 
6382 /*
6383   Load security context information for this view
6384 
6385   SYNOPSIS
6386     TABLE_LIST::prepare_view_security_context()
6387     thd                  [in] thread handler
6388 
6389   RETURN
6390     FALSE  OK
6391     TRUE   Error
6392 */
6393 
6394 #ifndef NO_EMBEDDED_ACCESS_CHECKS
prepare_view_security_context(THD * thd)6395 bool TABLE_LIST::prepare_view_security_context(THD *thd)
6396 {
6397   DBUG_ENTER("TABLE_LIST::prepare_view_security_context");
6398   DBUG_PRINT("enter", ("table: %s", alias.str));
6399 
6400   DBUG_ASSERT(!prelocking_placeholder && view);
6401   if (view_suid)
6402   {
6403     DBUG_PRINT("info", ("This table is suid view => load contest"));
6404     DBUG_ASSERT(view && view_sctx);
6405     if (acl_getroot(view_sctx, definer.user.str, definer.host.str,
6406                                 definer.host.str, thd->db.str))
6407     {
6408       if ((thd->lex->sql_command == SQLCOM_SHOW_CREATE) ||
6409           (thd->lex->sql_command == SQLCOM_SHOW_FIELDS))
6410       {
6411         push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
6412                             ER_NO_SUCH_USER,
6413                             ER_THD(thd, ER_NO_SUCH_USER),
6414                             definer.user.str, definer.host.str);
6415       }
6416       else
6417       {
6418         if (thd->security_ctx->master_access & PRIV_REVEAL_MISSING_DEFINER)
6419         {
6420           my_error(ER_NO_SUCH_USER, MYF(0), definer.user.str, definer.host.str);
6421 
6422         }
6423         else
6424         {
6425           if (thd->password == 2)
6426             my_error(ER_ACCESS_DENIED_NO_PASSWORD_ERROR, MYF(0),
6427                      thd->security_ctx->priv_user,
6428                      thd->security_ctx->priv_host);
6429           else
6430             my_error(ER_ACCESS_DENIED_ERROR, MYF(0),
6431                      thd->security_ctx->priv_user,
6432                      thd->security_ctx->priv_host,
6433                      (thd->password ?  ER_THD(thd, ER_YES) :
6434                       ER_THD(thd, ER_NO)));
6435         }
6436         DBUG_RETURN(TRUE);
6437       }
6438     }
6439   }
6440   DBUG_RETURN(FALSE);
6441 
6442 }
6443 #endif
6444 
6445 
6446 /*
6447   Find security context of current view
6448 
6449   SYNOPSIS
6450     TABLE_LIST::find_view_security_context()
6451     thd                  [in] thread handler
6452 
6453 */
6454 
6455 #ifndef NO_EMBEDDED_ACCESS_CHECKS
find_view_security_context(THD * thd)6456 Security_context *TABLE_LIST::find_view_security_context(THD *thd)
6457 {
6458   Security_context *sctx;
6459   TABLE_LIST *upper_view= this;
6460   DBUG_ENTER("TABLE_LIST::find_view_security_context");
6461 
6462   DBUG_ASSERT(view);
6463   while (upper_view && !upper_view->view_suid)
6464   {
6465     DBUG_ASSERT(!upper_view->prelocking_placeholder);
6466     upper_view= upper_view->referencing_view;
6467   }
6468   if (upper_view)
6469   {
6470     DBUG_PRINT("info", ("Securety context of view %s will be used",
6471                         upper_view->alias.str));
6472     sctx= upper_view->view_sctx;
6473     DBUG_ASSERT(sctx);
6474   }
6475   else
6476   {
6477     DBUG_PRINT("info", ("Current global context will be used"));
6478     sctx= thd->security_ctx;
6479   }
6480   DBUG_RETURN(sctx);
6481 }
6482 #endif
6483 
6484 
6485 /*
6486   Prepare security context and load underlying tables priveleges for view
6487 
6488   SYNOPSIS
6489     TABLE_LIST::prepare_security()
6490     thd                  [in] thread handler
6491 
6492   RETURN
6493     FALSE  OK
6494     TRUE   Error
6495 */
6496 
prepare_security(THD * thd)6497 bool TABLE_LIST::prepare_security(THD *thd)
6498 {
6499   List_iterator_fast<TABLE_LIST> tb(*view_tables);
6500   TABLE_LIST *tbl;
6501   DBUG_ENTER("TABLE_LIST::prepare_security");
6502 #ifndef NO_EMBEDDED_ACCESS_CHECKS
6503   Security_context *save_security_ctx= thd->security_ctx;
6504 
6505   DBUG_ASSERT(!prelocking_placeholder);
6506   if (prepare_view_security_context(thd))
6507     DBUG_RETURN(TRUE);
6508   thd->security_ctx= find_view_security_context(thd);
6509   opt_trace_disable_if_no_security_context_access(thd);
6510   while ((tbl= tb++))
6511   {
6512     DBUG_ASSERT(tbl->referencing_view);
6513     const char *local_db, *local_table_name;
6514     if (tbl->view)
6515     {
6516       local_db= tbl->view_db.str;
6517       local_table_name= tbl->view_name.str;
6518     }
6519     else
6520     {
6521       local_db= tbl->db.str;
6522       local_table_name= tbl->table_name.str;
6523     }
6524     fill_effective_table_privileges(thd, &tbl->grant, local_db,
6525                                     local_table_name);
6526     if (tbl->table)
6527       tbl->table->grant= grant;
6528   }
6529   thd->security_ctx= save_security_ctx;
6530 #else
6531   while ((tbl= tb++))
6532     tbl->grant.privilege= ALL_KNOWN_ACL;
6533 #endif
6534   DBUG_RETURN(FALSE);
6535 }
6536 
6537 #ifndef DBUG_OFF
set_check_merged()6538 void TABLE_LIST::set_check_merged()
6539 {
6540   DBUG_ASSERT(derived);
6541   /*
6542     It is not simple to check all, but at least this should be checked:
6543     this select is not excluded or the exclusion came from above.
6544   */
6545   DBUG_ASSERT(derived->is_excluded() ||
6546               !derived->first_select()->exclude_from_table_unique_test ||
6547               derived->outer_select()->
6548               exclude_from_table_unique_test);
6549 }
6550 #endif
6551 
set_check_materialized()6552 void TABLE_LIST::set_check_materialized()
6553 {
6554   DBUG_ENTER("TABLE_LIST::set_check_materialized");
6555   SELECT_LEX_UNIT *derived= this->derived;
6556   if (view)
6557     derived= &view->unit;
6558   DBUG_ASSERT(derived);
6559   DBUG_ASSERT(!derived->is_excluded());
6560   if (!derived->first_select()->exclude_from_table_unique_test)
6561     derived->set_unique_exclude();
6562   else
6563   {
6564     /*
6565       The subtree should be already excluded
6566     */
6567     DBUG_ASSERT(!derived->first_select()->first_inner_unit() ||
6568                 derived->first_select()->first_inner_unit()->with_element ||
6569                 derived->first_select()->first_inner_unit()->first_select()->
6570                 exclude_from_table_unique_test);
6571   }
6572   DBUG_VOID_RETURN;
6573 }
6574 
get_real_join_table()6575 TABLE *TABLE_LIST::get_real_join_table()
6576 {
6577   TABLE_LIST *tbl= this;
6578   while (tbl->table == NULL || tbl->table->reginfo.join_tab == NULL)
6579   {
6580     if ((tbl->view == NULL && tbl->derived == NULL) ||
6581         tbl->is_materialized_derived())
6582       break;
6583     /* we do not support merging of union yet */
6584     DBUG_ASSERT(tbl->view == NULL ||
6585                tbl->view->first_select_lex()->next_select() == NULL);
6586     DBUG_ASSERT(tbl->derived == NULL ||
6587                tbl->derived->first_select()->next_select() == NULL);
6588 
6589     {
6590       List_iterator_fast<TABLE_LIST>
6591         ti(tbl->view != NULL ?
6592            tbl->view->first_select_lex()->top_join_list :
6593            tbl->derived->first_select()->top_join_list);
6594       for (;;)
6595       {
6596         tbl= NULL;
6597         /*
6598           Find left table in outer join on this level
6599           (the list is reverted).
6600         */
6601         for (TABLE_LIST *t= ti++; t; t= ti++)
6602           tbl= t;
6603         if (!tbl)
6604           return NULL; // view/derived with no tables
6605         if (!tbl->nested_join)
6606           break;
6607         /* go deeper if we've found nested join */
6608         ti= tbl->nested_join->join_list;
6609       }
6610     }
6611   }
6612 
6613   return tbl->table;
6614 }
6615 
6616 
Natural_join_column(Field_translator * field_param,TABLE_LIST * tab)6617 Natural_join_column::Natural_join_column(Field_translator *field_param,
6618                                          TABLE_LIST *tab)
6619 {
6620   DBUG_ASSERT(tab->field_translation);
6621   view_field= field_param;
6622   table_field= NULL;
6623   table_ref= tab;
6624   is_common= FALSE;
6625 }
6626 
6627 
Natural_join_column(Item_field * field_param,TABLE_LIST * tab)6628 Natural_join_column::Natural_join_column(Item_field *field_param,
6629                                          TABLE_LIST *tab)
6630 {
6631   DBUG_ASSERT(tab->table == field_param->field->table);
6632   table_field= field_param;
6633   view_field= NULL;
6634   table_ref= tab;
6635   is_common= FALSE;
6636 }
6637 
6638 
name()6639 LEX_CSTRING *Natural_join_column::name()
6640 {
6641   if (view_field)
6642   {
6643     DBUG_ASSERT(table_field == NULL);
6644     return &view_field->name;
6645   }
6646 
6647   return &table_field->field_name;
6648 }
6649 
6650 
create_item(THD * thd)6651 Item *Natural_join_column::create_item(THD *thd)
6652 {
6653   if (view_field)
6654   {
6655     DBUG_ASSERT(table_field == NULL);
6656     return create_view_field(thd, table_ref, &view_field->item,
6657                              &view_field->name);
6658   }
6659   return table_field;
6660 }
6661 
6662 
field()6663 Field *Natural_join_column::field()
6664 {
6665   if (view_field)
6666   {
6667     DBUG_ASSERT(table_field == NULL);
6668     return NULL;
6669   }
6670   return table_field->field;
6671 }
6672 
6673 
safe_table_name()6674 const char *Natural_join_column::safe_table_name()
6675 {
6676   DBUG_ASSERT(table_ref);
6677   return table_ref->alias.str ? table_ref->alias.str : "";
6678 }
6679 
6680 
safe_db_name()6681 const char *Natural_join_column::safe_db_name()
6682 {
6683   if (view_field)
6684     return table_ref->view_db.str ? table_ref->view_db.str : "";
6685 
6686   /*
6687     Test that TABLE_LIST::db is the same as TABLE_SHARE::db to
6688     ensure consistency. An exception are I_S schema tables, which
6689     are inconsistent in this respect.
6690   */
6691   DBUG_ASSERT(!cmp(&table_ref->db,
6692                    &table_ref->table->s->db) ||
6693               (table_ref->schema_table &&
6694                is_infoschema_db(&table_ref->table->s->db)) ||
6695               table_ref->is_materialized_derived());
6696   return table_ref->db.str ? table_ref->db.str : "";
6697 }
6698 
6699 
grant()6700 GRANT_INFO *Natural_join_column::grant()
6701 {
6702 /*  if (view_field)
6703     return &(table_ref->grant);
6704   return &(table_ref->table->grant);*/
6705   /*
6706     Have to check algorithm because merged derived also has
6707     field_translation.
6708   */
6709 //if (table_ref->effective_algorithm == DTYPE_ALGORITHM_MERGE)
6710   if (table_ref->is_merged_derived())
6711     return &(table_ref->grant);
6712   return &(table_ref->table->grant);
6713 }
6714 
6715 
set(TABLE_LIST * table)6716 void Field_iterator_view::set(TABLE_LIST *table)
6717 {
6718   DBUG_ASSERT(table->field_translation);
6719   view= table;
6720   ptr= table->field_translation;
6721   array_end= table->field_translation_end;
6722 }
6723 
6724 
name()6725 LEX_CSTRING *Field_iterator_table::name()
6726 {
6727   return &(*ptr)->field_name;
6728 }
6729 
6730 
create_item(THD * thd)6731 Item *Field_iterator_table::create_item(THD *thd)
6732 {
6733   SELECT_LEX *select= thd->lex->current_select;
6734 
6735   Item_field *item= new (thd->mem_root) Item_field(thd, &select->context, *ptr);
6736   DBUG_ASSERT(strlen(item->name.str) == item->name.length);
6737   if (item && thd->variables.sql_mode & MODE_ONLY_FULL_GROUP_BY &&
6738       !thd->lex->in_sum_func && select->cur_pos_in_select_list != UNDEF_POS &&
6739       select->join)
6740   {
6741     select->join->non_agg_fields.push_back(item);
6742     item->marker= select->cur_pos_in_select_list;
6743     select->set_non_agg_field_used(true);
6744   }
6745   return item;
6746 }
6747 
6748 
name()6749 LEX_CSTRING *Field_iterator_view::name()
6750 {
6751   return &ptr->name;
6752 }
6753 
6754 
create_item(THD * thd)6755 Item *Field_iterator_view::create_item(THD *thd)
6756 {
6757   return create_view_field(thd, view, &ptr->item, &ptr->name);
6758 }
6759 
create_view_field(THD * thd,TABLE_LIST * view,Item ** field_ref,LEX_CSTRING * name)6760 Item *create_view_field(THD *thd, TABLE_LIST *view, Item **field_ref,
6761                         LEX_CSTRING *name)
6762 {
6763   bool save_wrapper= thd->lex->first_select_lex()->no_wrap_view_item;
6764   Item *field= *field_ref;
6765   DBUG_ENTER("create_view_field");
6766 
6767   if (view->schema_table_reformed)
6768   {
6769     /*
6770       Translation table items are always Item_fields and already fixed
6771       ('mysql_schema_table' function). So we can return directly the
6772       field. This case happens only for 'show & where' commands.
6773     */
6774     DBUG_ASSERT(field && field->is_fixed());
6775     DBUG_RETURN(field);
6776   }
6777 
6778   DBUG_ASSERT(field);
6779   thd->lex->current_select->no_wrap_view_item= TRUE;
6780   if (!field->is_fixed())
6781   {
6782     if (field->fix_fields(thd, field_ref))
6783     {
6784       thd->lex->current_select->no_wrap_view_item= save_wrapper;
6785       DBUG_RETURN(0);
6786     }
6787     field= *field_ref;
6788   }
6789   thd->lex->current_select->no_wrap_view_item= save_wrapper;
6790   if (save_wrapper)
6791   {
6792     DBUG_RETURN(field);
6793   }
6794   Name_resolution_context *context= (view->view ?
6795                                      &view->view->first_select_lex()->context:
6796                                      &thd->lex->first_select_lex()->context);
6797   Item *item= (new (thd->mem_root)
6798                Item_direct_view_ref(thd, context, field_ref, view->alias,
6799                                     *name, view));
6800   if (!item)
6801     return NULL;
6802   /*
6803     Force creation of nullable item for the result tmp table for outer joined
6804     views/derived tables.
6805   */
6806   if (view->table && view->table->maybe_null)
6807     item->maybe_null= TRUE;
6808   /* Save item in case we will need to fall back to materialization. */
6809   view->used_items.push_front(item, thd->mem_root);
6810   /*
6811     If we create this reference on persistent memory then it should be
6812     present in persistent list
6813   */
6814   if (thd->mem_root == thd->stmt_arena->mem_root)
6815     view->persistent_used_items.push_front(item, thd->mem_root);
6816   DBUG_RETURN(item);
6817 }
6818 
6819 
set(TABLE_LIST * table_ref)6820 void Field_iterator_natural_join::set(TABLE_LIST *table_ref)
6821 {
6822   DBUG_ASSERT(table_ref->join_columns);
6823   column_ref_it.init(*(table_ref->join_columns));
6824   cur_column_ref= column_ref_it++;
6825 }
6826 
6827 
next()6828 void Field_iterator_natural_join::next()
6829 {
6830   cur_column_ref= column_ref_it++;
6831   DBUG_ASSERT(!cur_column_ref || ! cur_column_ref->table_field ||
6832               cur_column_ref->table_ref->table ==
6833               cur_column_ref->table_field->field->table);
6834 }
6835 
6836 
set_field_iterator()6837 void Field_iterator_table_ref::set_field_iterator()
6838 {
6839   DBUG_ENTER("Field_iterator_table_ref::set_field_iterator");
6840   /*
6841     If the table reference we are iterating over is a natural join, or it is
6842     an operand of a natural join, and TABLE_LIST::join_columns contains all
6843     the columns of the join operand, then we pick the columns from
6844     TABLE_LIST::join_columns, instead of the  orginial container of the
6845     columns of the join operator.
6846   */
6847   if (table_ref->is_join_columns_complete)
6848   {
6849     /* Necesary, but insufficient conditions. */
6850     DBUG_ASSERT(table_ref->is_natural_join ||
6851                 table_ref->nested_join ||
6852                 (table_ref->join_columns &&
6853                  /* This is a merge view. */
6854                  ((table_ref->field_translation &&
6855                    table_ref->join_columns->elements ==
6856                    (ulong)(table_ref->field_translation_end -
6857                            table_ref->field_translation)) ||
6858                   /* This is stored table or a tmptable view. */
6859                   (!table_ref->field_translation &&
6860                    table_ref->join_columns->elements ==
6861                    table_ref->table->s->fields))));
6862     field_it= &natural_join_it;
6863     DBUG_PRINT("info",("field_it for '%s' is Field_iterator_natural_join",
6864                        table_ref->alias.str));
6865   }
6866   /* This is a merge view, so use field_translation. */
6867   else if (table_ref->field_translation)
6868   {
6869     DBUG_ASSERT(table_ref->is_merged_derived());
6870     field_it= &view_field_it;
6871     DBUG_PRINT("info", ("field_it for '%s' is Field_iterator_view",
6872                         table_ref->alias.str));
6873   }
6874   /* This is a base table or stored view. */
6875   else
6876   {
6877     DBUG_ASSERT(table_ref->table || table_ref->view);
6878     field_it= &table_field_it;
6879     DBUG_PRINT("info", ("field_it for '%s' is Field_iterator_table",
6880                         table_ref->alias.str));
6881   }
6882   field_it->set(table_ref);
6883   DBUG_VOID_RETURN;
6884 }
6885 
6886 
set(TABLE_LIST * table)6887 void Field_iterator_table_ref::set(TABLE_LIST *table)
6888 {
6889   DBUG_ASSERT(table);
6890   first_leaf= table->first_leaf_for_name_resolution();
6891   last_leaf=  table->last_leaf_for_name_resolution();
6892   DBUG_ASSERT(first_leaf && last_leaf);
6893   table_ref= first_leaf;
6894   set_field_iterator();
6895 }
6896 
6897 
next()6898 void Field_iterator_table_ref::next()
6899 {
6900   /* Move to the next field in the current table reference. */
6901   field_it->next();
6902   /*
6903     If all fields of the current table reference are exhausted, move to
6904     the next leaf table reference.
6905   */
6906   if (field_it->end_of_fields() && table_ref != last_leaf)
6907   {
6908     table_ref= table_ref->next_name_resolution_table;
6909     DBUG_ASSERT(table_ref);
6910     set_field_iterator();
6911   }
6912 }
6913 
6914 
get_table_name()6915 const char *Field_iterator_table_ref::get_table_name()
6916 {
6917   if (table_ref->view)
6918     return table_ref->view_name.str;
6919   if (table_ref->is_derived())
6920     return table_ref->table->s->table_name.str;
6921   else if (table_ref->is_natural_join)
6922     return natural_join_it.column_ref()->safe_table_name();
6923 
6924   DBUG_ASSERT(!strcmp(table_ref->table_name.str,
6925                       table_ref->table->s->table_name.str) ||
6926               table_ref->schema_table);
6927   return table_ref->table_name.str;
6928 }
6929 
6930 
get_db_name()6931 const char *Field_iterator_table_ref::get_db_name()
6932 {
6933   if (table_ref->view)
6934     return table_ref->view_db.str;
6935   else if (table_ref->is_natural_join)
6936     return natural_join_it.column_ref()->safe_db_name();
6937 
6938   /*
6939     Test that TABLE_LIST::db is the same as TABLE_SHARE::db to
6940     ensure consistency. An exception are I_S schema tables, which
6941     are inconsistent in this respect.
6942   */
6943   DBUG_ASSERT(!cmp(&table_ref->db, &table_ref->table->s->db) ||
6944               (table_ref->schema_table &&
6945                is_infoschema_db(&table_ref->table->s->db)));
6946 
6947   return table_ref->db.str;
6948 }
6949 
6950 
grant()6951 GRANT_INFO *Field_iterator_table_ref::grant()
6952 {
6953   if (table_ref->view)
6954     return &(table_ref->grant);
6955   else if (table_ref->is_natural_join)
6956     return natural_join_it.column_ref()->grant();
6957   return &(table_ref->table->grant);
6958 }
6959 
6960 
6961 /*
6962   Create new or return existing column reference to a column of a
6963   natural/using join.
6964 
6965   SYNOPSIS
6966     Field_iterator_table_ref::get_or_create_column_ref()
6967     parent_table_ref  the parent table reference over which the
6968                       iterator is iterating
6969 
6970   DESCRIPTION
6971     Create a new natural join column for the current field of the
6972     iterator if no such column was created, or return an already
6973     created natural join column. The former happens for base tables or
6974     views, and the latter for natural/using joins. If a new field is
6975     created, then the field is added to 'parent_table_ref' if it is
6976     given, or to the original table referene of the field if
6977     parent_table_ref == NULL.
6978 
6979   NOTES
6980     This method is designed so that when a Field_iterator_table_ref
6981     walks through the fields of a table reference, all its fields
6982     are created and stored as follows:
6983     - If the table reference being iterated is a stored table, view or
6984       natural/using join, store all natural join columns in a list
6985       attached to that table reference.
6986     - If the table reference being iterated is a nested join that is
6987       not natural/using join, then do not materialize its result
6988       fields. This is OK because for such table references
6989       Field_iterator_table_ref iterates over the fields of the nested
6990       table references (recursively). In this way we avoid the storage
6991       of unnecessay copies of result columns of nested joins.
6992 
6993   RETURN
6994     #     Pointer to a column of a natural join (or its operand)
6995     NULL  No memory to allocate the column
6996 */
6997 
6998 Natural_join_column *
get_or_create_column_ref(THD * thd,TABLE_LIST * parent_table_ref)6999 Field_iterator_table_ref::get_or_create_column_ref(THD *thd, TABLE_LIST *parent_table_ref)
7000 {
7001   Natural_join_column *nj_col;
7002   bool is_created= TRUE;
7003   uint UNINIT_VAR(field_count);
7004   TABLE_LIST *add_table_ref= parent_table_ref ?
7005                              parent_table_ref : table_ref;
7006 
7007   if (field_it == &table_field_it)
7008   {
7009     /* The field belongs to a stored table. */
7010     Field *tmp_field= table_field_it.field();
7011     Item_field *tmp_item=
7012       new (thd->mem_root) Item_field(thd, &thd->lex->current_select->context, tmp_field);
7013     if (!tmp_item)
7014       return NULL;
7015     nj_col= new Natural_join_column(tmp_item, table_ref);
7016     field_count= table_ref->table->s->fields;
7017   }
7018   else if (field_it == &view_field_it)
7019   {
7020     /* The field belongs to a merge view or information schema table. */
7021     Field_translator *translated_field= view_field_it.field_translator();
7022     nj_col= new Natural_join_column(translated_field, table_ref);
7023     field_count= (uint)(table_ref->field_translation_end -
7024                  table_ref->field_translation);
7025   }
7026   else
7027   {
7028     /*
7029       The field belongs to a NATURAL join, therefore the column reference was
7030       already created via one of the two constructor calls above. In this case
7031       we just return the already created column reference.
7032     */
7033     DBUG_ASSERT(table_ref->is_join_columns_complete);
7034     is_created= FALSE;
7035     nj_col= natural_join_it.column_ref();
7036     DBUG_ASSERT(nj_col);
7037   }
7038   DBUG_ASSERT(!nj_col->table_field || !nj_col->table_field->field ||
7039               nj_col->table_ref->table == nj_col->table_field->field->table);
7040 
7041   /*
7042     If the natural join column was just created add it to the list of
7043     natural join columns of either 'parent_table_ref' or to the table
7044     reference that directly contains the original field.
7045   */
7046   if (is_created)
7047   {
7048     /* Make sure not all columns were materialized. */
7049     DBUG_ASSERT(!add_table_ref->is_join_columns_complete);
7050     if (!add_table_ref->join_columns)
7051     {
7052       /* Create a list of natural join columns on demand. */
7053       if (!(add_table_ref->join_columns= new List<Natural_join_column>))
7054         return NULL;
7055       add_table_ref->is_join_columns_complete= FALSE;
7056     }
7057     add_table_ref->join_columns->push_back(nj_col);
7058     /*
7059       If new fields are added to their original table reference, mark if
7060       all fields were added. We do it here as the caller has no easy way
7061       of knowing when to do it.
7062       If the fields are being added to parent_table_ref, then the caller
7063       must take care to mark when all fields are created/added.
7064     */
7065     if (!parent_table_ref &&
7066         add_table_ref->join_columns->elements == field_count)
7067       add_table_ref->is_join_columns_complete= TRUE;
7068   }
7069 
7070   return nj_col;
7071 }
7072 
7073 
7074 /*
7075   Return an existing reference to a column of a natural/using join.
7076 
7077   SYNOPSIS
7078     Field_iterator_table_ref::get_natural_column_ref()
7079 
7080   DESCRIPTION
7081     The method should be called in contexts where it is expected that
7082     all natural join columns are already created, and that the column
7083     being retrieved is a Natural_join_column.
7084 
7085   RETURN
7086     #     Pointer to a column of a natural join (or its operand)
7087     NULL  We didn't originally have memory to allocate the column
7088 */
7089 
7090 Natural_join_column *
get_natural_column_ref()7091 Field_iterator_table_ref::get_natural_column_ref()
7092 {
7093   Natural_join_column *nj_col;
7094 
7095   DBUG_ASSERT(field_it == &natural_join_it);
7096   /*
7097     The field belongs to a NATURAL join, therefore the column reference was
7098     already created via one of the two constructor calls above. In this case
7099     we just return the already created column reference.
7100   */
7101   nj_col= natural_join_it.column_ref();
7102   DBUG_ASSERT(nj_col &&
7103               (!nj_col->table_field || !nj_col->table_field->field ||
7104                nj_col->table_ref->table == nj_col->table_field->field->table));
7105   return nj_col;
7106 }
7107 
7108 /*****************************************************************************
7109   Functions to handle column usage bitmaps (read_set, write_set etc...)
7110 *****************************************************************************/
7111 
7112 /* Reset all columns bitmaps */
7113 
clear_column_bitmaps()7114 void TABLE::clear_column_bitmaps()
7115 {
7116   /*
7117     Reset column read/write usage. It's identical to:
7118     bitmap_clear_all(&table->def_read_set);
7119     bitmap_clear_all(&table->def_write_set);
7120     The code assumes that the bitmaps are allocated after each other, as
7121     guaranteed by open_table_from_share()
7122   */
7123   bzero((char*) def_read_set.bitmap,
7124         s->column_bitmap_size * (s->virtual_fields ? 3 : 2));
7125   column_bitmaps_set(&def_read_set, &def_write_set);
7126   rpl_write_set= 0;                             // Safety
7127 }
7128 
7129 
7130 /*
7131   Tell handler we are going to call position() and rnd_pos() later.
7132 
7133   NOTES:
7134   This is needed for handlers that uses the primary key to find the
7135   row. In this case we have to extend the read bitmap with the primary
7136   key fields.
7137 */
7138 
prepare_for_position()7139 void TABLE::prepare_for_position()
7140 {
7141   DBUG_ENTER("TABLE::prepare_for_position");
7142 
7143   if ((file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
7144       s->primary_key < MAX_KEY)
7145   {
7146     mark_index_columns_for_read(s->primary_key);
7147     /* signal change */
7148     file->column_bitmaps_signal();
7149   }
7150   DBUG_VOID_RETURN;
7151 }
7152 
7153 
prepare_for_keyread(uint index,MY_BITMAP * map)7154 MY_BITMAP *TABLE::prepare_for_keyread(uint index, MY_BITMAP *map)
7155 {
7156   MY_BITMAP *backup= read_set;
7157   DBUG_ENTER("TABLE::prepare_for_keyread");
7158   if (!no_keyread)
7159     file->ha_start_keyread(index);
7160   if (map != read_set || !(file->index_flags(index, 0, 1) & HA_CLUSTERED_INDEX))
7161   {
7162     mark_index_columns(index, map);
7163     column_bitmaps_set(map);
7164   }
7165   DBUG_RETURN(backup);
7166 }
7167 
7168 
7169 /*
7170   Mark that only fields from one key is used. Useful before keyread.
7171 */
7172 
mark_index_columns(uint index,MY_BITMAP * bitmap)7173 void TABLE::mark_index_columns(uint index, MY_BITMAP *bitmap)
7174 {
7175   DBUG_ENTER("TABLE::mark_index_columns");
7176 
7177   bitmap_clear_all(bitmap);
7178   mark_index_columns_no_reset(index, bitmap);
7179   DBUG_VOID_RETURN;
7180 }
7181 
7182 /*
7183   Restore to use normal column maps after key read
7184 
7185   NOTES
7186     This reverse the change done by mark_index_columns
7187 
7188   WARNING
7189     For this to work, one must have the normal table maps in place
7190     when calling mark_index_columns
7191 */
7192 
restore_column_maps_after_keyread(MY_BITMAP * backup)7193 void TABLE::restore_column_maps_after_keyread(MY_BITMAP *backup)
7194 {
7195   DBUG_ENTER("TABLE::restore_column_maps_after_mark_index");
7196   file->ha_end_keyread();
7197   read_set= backup;
7198   file->column_bitmaps_signal();
7199   DBUG_VOID_RETURN;
7200 }
7201 
do_mark_index_columns(TABLE * table,uint index,MY_BITMAP * bitmap,bool read)7202 static void do_mark_index_columns(TABLE *table, uint index,
7203                                   MY_BITMAP *bitmap, bool read)
7204 {
7205   KEY_PART_INFO *key_part= table->key_info[index].key_part;
7206   uint key_parts= table->key_info[index].user_defined_key_parts;
7207   for (uint k= 0; k < key_parts; k++)
7208     if (read)
7209       key_part[k].field->register_field_in_read_map();
7210     else
7211       bitmap_set_bit(bitmap, key_part[k].fieldnr-1);
7212   if (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX &&
7213       table->s->primary_key != MAX_KEY && table->s->primary_key != index)
7214     do_mark_index_columns(table, table->s->primary_key, bitmap, read);
7215 
7216 }
7217 /*
7218   mark columns used by key, but don't reset other fields
7219 */
7220 
mark_index_columns_no_reset(uint index,MY_BITMAP * bitmap)7221 inline void TABLE::mark_index_columns_no_reset(uint index, MY_BITMAP *bitmap)
7222 {
7223   do_mark_index_columns(this, index, bitmap, false);
7224 }
7225 
7226 
mark_index_columns_for_read(uint index)7227 inline void TABLE::mark_index_columns_for_read(uint index)
7228 {
7229   do_mark_index_columns(this, index, read_set, true);
7230 }
7231 
7232 /*
7233   Mark auto-increment fields as used fields in both read and write maps
7234 
7235   NOTES
7236     This is needed in insert & update as the auto-increment field is
7237     always set and sometimes read.
7238 */
7239 
mark_auto_increment_column()7240 void TABLE::mark_auto_increment_column()
7241 {
7242   DBUG_ASSERT(found_next_number_field);
7243   /*
7244     We must set bit in read set as update_auto_increment() is using the
7245     store() to check overflow of auto_increment values
7246   */
7247   bitmap_set_bit(read_set, found_next_number_field->field_index);
7248   bitmap_set_bit(write_set, found_next_number_field->field_index);
7249   if (s->next_number_keypart)
7250     mark_index_columns_for_read(s->next_number_index);
7251   file->column_bitmaps_signal();
7252 }
7253 
7254 
7255 /*
7256   Mark columns needed for doing an delete of a row
7257 
7258   DESCRIPTON
7259     Some table engines don't have a cursor on the retrieve rows
7260     so they need either to use the primary key or all columns to
7261     be able to delete a row.
7262 
7263     If the engine needs this, the function works as follows:
7264     - If primary key exits, mark the primary key columns to be read.
7265     - If not, mark all columns to be read
7266 
7267     If the engine has HA_REQUIRES_KEY_COLUMNS_FOR_DELETE, we will
7268     mark all key columns as 'to-be-read'. This allows the engine to
7269     loop over the given record to find all keys and doesn't have to
7270     retrieve the row again.
7271 */
7272 
mark_columns_needed_for_delete()7273 void TABLE::mark_columns_needed_for_delete()
7274 {
7275   bool need_signal= false;
7276   mark_columns_per_binlog_row_image();
7277 
7278   if (triggers)
7279     triggers->mark_fields_used(TRG_EVENT_DELETE);
7280   if (file->ha_table_flags() & HA_REQUIRES_KEY_COLUMNS_FOR_DELETE)
7281   {
7282     Field **reg_field;
7283     for (reg_field= field ; *reg_field ; reg_field++)
7284     {
7285       if ((*reg_field)->flags & (PART_KEY_FLAG | PART_INDIRECT_KEY_FLAG))
7286         mark_column_with_deps(*reg_field);
7287     }
7288     need_signal= true;
7289   }
7290   if (file->ha_table_flags() & HA_PRIMARY_KEY_REQUIRED_FOR_DELETE)
7291   {
7292     /*
7293       If the handler has no cursor capabilites, we have to read either
7294       the primary key, the hidden primary key or all columns to be
7295       able to do an delete
7296     */
7297     if (s->primary_key == MAX_KEY)
7298       file->use_hidden_primary_key();
7299     else
7300     {
7301       mark_index_columns_for_read(s->primary_key);
7302       need_signal= true;
7303     }
7304   }
7305 
7306   if (s->versioned)
7307   {
7308     bitmap_set_bit(read_set, s->vers.start_fieldno);
7309     bitmap_set_bit(read_set, s->vers.end_fieldno);
7310     bitmap_set_bit(write_set, s->vers.end_fieldno);
7311     need_signal= true;
7312   }
7313 
7314   if (need_signal)
7315     file->column_bitmaps_signal();
7316 }
7317 
7318 
7319 /*
7320   Mark columns needed for doing an update of a row
7321 
7322   DESCRIPTON
7323     Some engines needs to have all columns in an update (to be able to
7324     build a complete row). If this is the case, we mark all not
7325     updated columns to be read.
7326 
7327     If this is no the case, we do like in the delete case and mark
7328     if needed, either the primary key column or all columns to be read.
7329     (see mark_columns_needed_for_delete() for details)
7330 
7331     If the engine has HA_REQUIRES_KEY_COLUMNS_FOR_DELETE, we will
7332     mark all USED key columns as 'to-be-read'. This allows the engine to
7333     loop over the given record to find all changed keys and doesn't have to
7334     retrieve the row again.
7335 */
7336 
mark_columns_needed_for_update()7337 void TABLE::mark_columns_needed_for_update()
7338 {
7339   DBUG_ENTER("TABLE::mark_columns_needed_for_update");
7340   bool need_signal= false;
7341 
7342 
7343   if (triggers)
7344     triggers->mark_fields_used(TRG_EVENT_UPDATE);
7345   if (default_field)
7346     mark_default_fields_for_write(FALSE);
7347   if (vfield)
7348     need_signal|= mark_virtual_columns_for_write(FALSE);
7349   if (file->ha_table_flags() & HA_REQUIRES_KEY_COLUMNS_FOR_DELETE)
7350   {
7351     KEY *end= key_info + s->keys;
7352     for (KEY *k= key_info; k < end; k++)
7353     {
7354       KEY_PART_INFO *kpend= k->key_part + k->ext_key_parts;
7355       int any_written= 0, all_read= 1;
7356       for (KEY_PART_INFO *kp= k->key_part; kp < kpend; kp++)
7357       {
7358         int idx= kp->fieldnr - 1;
7359         any_written|= bitmap_is_set(write_set, idx);
7360         all_read&= bitmap_is_set(read_set, idx);
7361       }
7362       if (any_written && !all_read)
7363       {
7364         for (KEY_PART_INFO *kp= k->key_part; kp < kpend; kp++)
7365           mark_column_with_deps(field[kp->fieldnr - 1]);
7366       }
7367     }
7368     need_signal= true;
7369   }
7370   else
7371   {
7372     if (found_next_number_field)
7373       mark_auto_increment_column();
7374   }
7375 
7376   if (file->ha_table_flags() & HA_PRIMARY_KEY_REQUIRED_FOR_DELETE)
7377   {
7378     /*
7379       If the handler has no cursor capabilites, we have to read either
7380       the primary key, the hidden primary key or all columns to be
7381       able to do an update
7382     */
7383     if (s->primary_key == MAX_KEY)
7384       file->use_hidden_primary_key();
7385     else
7386     {
7387       mark_index_columns_for_read(s->primary_key);
7388       need_signal= true;
7389     }
7390   }
7391   if (s->versioned)
7392   {
7393     /*
7394       For System Versioning we have to read all columns since we store
7395       a copy of previous row with modified row_end back to a table.
7396     */
7397     bitmap_union(read_set, &s->all_set);
7398     need_signal= true;
7399   }
7400   if (check_constraints)
7401   {
7402     mark_check_constraint_columns_for_read();
7403     need_signal= true;
7404   }
7405 
7406   /*
7407     If a timestamp field settable on UPDATE is present then to avoid wrong
7408     update force the table handler to retrieve write-only fields to be able
7409     to compare records and detect data change.
7410   */
7411   if ((file->ha_table_flags() & HA_PARTIAL_COLUMN_READ) &&
7412       default_field && s->has_update_default_function)
7413   {
7414     bitmap_union(read_set, write_set);
7415     need_signal= true;
7416   }
7417   mark_columns_per_binlog_row_image();
7418   if (need_signal)
7419     file->column_bitmaps_signal();
7420   DBUG_VOID_RETURN;
7421 }
7422 
7423 
7424 /*
7425   Mark columns the handler needs for doing an insert
7426 
7427   For now, this is used to mark fields used by the trigger
7428   as changed.
7429 */
7430 
mark_columns_needed_for_insert()7431 void TABLE::mark_columns_needed_for_insert()
7432 {
7433   DBUG_ENTER("mark_columns_needed_for_insert");
7434 
7435   if (triggers)
7436   {
7437     /*
7438       We don't need to mark columns which are used by ON DELETE and
7439       ON UPDATE triggers, which may be invoked in case of REPLACE or
7440       INSERT ... ON DUPLICATE KEY UPDATE, since before doing actual
7441       row replacement or update write_record() will mark all table
7442       fields as used.
7443     */
7444     triggers->mark_fields_used(TRG_EVENT_INSERT);
7445   }
7446   if (found_next_number_field)
7447     mark_auto_increment_column();
7448   if (default_field)
7449     mark_default_fields_for_write(TRUE);
7450   /* Mark virtual columns for insert */
7451   if (vfield)
7452     mark_virtual_columns_for_write(TRUE);
7453   mark_columns_per_binlog_row_image();
7454   if (check_constraints)
7455     mark_check_constraint_columns_for_read();
7456   DBUG_VOID_RETURN;
7457 }
7458 
7459 /*
7460   Mark columns according the binlog row image option.
7461 
7462   Columns to be written are stored in 'rpl_write_set'
7463 
7464   When logging in RBR, the user can select whether to
7465   log partial or full rows, depending on the table
7466   definition, and the value of binlog_row_image.
7467 
7468   Semantics of the binlog_row_image are the following
7469   (PKE - primary key equivalent, ie, PK fields if PK
7470   exists, all fields otherwise):
7471 
7472   binlog_row_image= MINIMAL
7473     - This marks the PKE fields in the read_set
7474     - This marks all fields where a value was specified
7475       in the rpl_write_set
7476 
7477   binlog_row_image= NOBLOB
7478     - This marks PKE + all non-blob fields in the read_set
7479     - This marks all fields where a value was specified
7480       and all non-blob fields in the rpl_write_set
7481 
7482   binlog_row_image= FULL
7483     - all columns in the read_set
7484     - all columns in the rpl_write_set
7485 
7486   This marking is done without resetting the original
7487   bitmaps. This means that we will strip extra fields in
7488   the read_set at binlogging time (for those cases that
7489   we only want to log a PK and we needed other fields for
7490   execution).
7491 */
7492 
mark_columns_per_binlog_row_image()7493 void TABLE::mark_columns_per_binlog_row_image()
7494 {
7495   THD *thd= in_use;
7496   DBUG_ENTER("mark_columns_per_binlog_row_image");
7497   DBUG_ASSERT(read_set->bitmap);
7498   DBUG_ASSERT(write_set->bitmap);
7499 
7500   /* If not using row format */
7501   rpl_write_set= write_set;
7502 
7503   /**
7504     If in RBR we may need to mark some extra columns,
7505     depending on the binlog-row-image command line argument.
7506    */
7507   if (file->row_logging &&
7508       !ha_check_storage_engine_flag(s->db_type(), HTON_NO_BINLOG_ROW_OPT))
7509   {
7510     /* if there is no PK, then mark all columns for the BI. */
7511     if (s->primary_key >= MAX_KEY)
7512     {
7513       bitmap_set_all(read_set);
7514       rpl_write_set= read_set;
7515     }
7516     else
7517     {
7518       switch (thd->variables.binlog_row_image) {
7519       case BINLOG_ROW_IMAGE_FULL:
7520         bitmap_set_all(read_set);
7521         /* Set of columns that should be written (all) */
7522         rpl_write_set= read_set;
7523         break;
7524       case BINLOG_ROW_IMAGE_NOBLOB:
7525         /* Only write changed columns + not blobs */
7526         rpl_write_set= &def_rpl_write_set;
7527         bitmap_copy(rpl_write_set, write_set);
7528 
7529         /*
7530           for every field that is not set, mark it unless it is a blob or
7531           part of a primary key
7532         */
7533         for (Field **ptr=field ; *ptr ; ptr++)
7534         {
7535           Field *my_field= *ptr;
7536           /*
7537             bypass blob fields. These can be set or not set, we don't care.
7538             Later, at binlogging time, if we don't need them in the before
7539             image, we will discard them.
7540 
7541             If set in the AI, then the blob is really needed, there is
7542             nothing we can do about it.
7543           */
7544           if ((my_field->flags & PRI_KEY_FLAG) ||
7545               (my_field->type() != MYSQL_TYPE_BLOB))
7546           {
7547             my_field->register_field_in_read_map();
7548             bitmap_set_bit(rpl_write_set, my_field->field_index);
7549           }
7550         }
7551         break;
7552       case BINLOG_ROW_IMAGE_MINIMAL:
7553         /*
7554           mark the primary key in the read set so that we can find the row
7555           that is updated / deleted.
7556           We don't need to mark the primary key in the rpl_write_set as the
7557           binary log will include all columns read anyway.
7558         */
7559         mark_index_columns_for_read(s->primary_key);
7560         if (versioned())
7561         {
7562           // TODO: After MDEV-18432 we don't pass history rows, so remove this:
7563           rpl_write_set= &s->all_set;
7564         }
7565         else
7566         {
7567           /* Only write columns that have changed */
7568           rpl_write_set= write_set;
7569         }
7570         break;
7571 
7572       default:
7573         DBUG_ASSERT(FALSE);
7574       }
7575     }
7576     file->column_bitmaps_signal();
7577   }
7578 
7579   DBUG_VOID_RETURN;
7580 }
7581 
7582 
7583 /*
7584   @brief Mark virtual columns for update/insert commands
7585 
7586   @param insert_fl    true if virtual columns are marked for insert command
7587                       For the moment this is not used, may be used in future.
7588 
7589   @details
7590     The function marks virtual columns used in a update/insert commands
7591     in the vcol_set bitmap.
7592     For an insert command a virtual column is always marked in write_set if
7593     it is a stored column.
7594     If a virtual column is from write_set it is always marked in vcol_set.
7595     If a stored virtual column is not from write_set but it is computed
7596     through columns from write_set it is also marked in vcol_set, and,
7597     besides, it is added to write_set.
7598 
7599   @return whether a bitmap was updated
7600 
7601   @note
7602     Let table t1 have columns a,b,c and let column c be a stored virtual
7603     column computed through columns a and b. Then for the query
7604       UPDATE t1 SET a=1
7605     column c will be placed into vcol_set and into write_set while
7606     column b will be placed into read_set.
7607     If column c was a virtual column, but not a stored virtual column
7608     then it would not be added to any of the sets. Column b would not
7609     be added to read_set either.
7610 */
7611 
mark_virtual_columns_for_write(bool insert_fl)7612 bool TABLE::mark_virtual_columns_for_write(bool insert_fl
7613                                            __attribute__((unused)))
7614 {
7615   Field **vfield_ptr, *tmp_vfield;
7616   bool bitmap_updated= false;
7617   DBUG_ENTER("mark_virtual_columns_for_write");
7618 
7619   for (vfield_ptr= vfield; *vfield_ptr; vfield_ptr++)
7620   {
7621     tmp_vfield= *vfield_ptr;
7622     if (bitmap_is_set(write_set, tmp_vfield->field_index))
7623       bitmap_updated|= mark_virtual_column_with_deps(tmp_vfield);
7624     else if (tmp_vfield->vcol_info->stored_in_db ||
7625              (tmp_vfield->flags & (PART_KEY_FLAG | FIELD_IN_PART_FUNC_FLAG |
7626                                    PART_INDIRECT_KEY_FLAG)))
7627     {
7628       bitmap_set_bit(write_set, tmp_vfield->field_index);
7629       mark_virtual_column_with_deps(tmp_vfield);
7630       bitmap_updated= true;
7631     }
7632   }
7633   if (bitmap_updated)
7634     file->column_bitmaps_signal();
7635   DBUG_RETURN(bitmap_updated);
7636 }
7637 
7638 
7639 /**
7640    Check if a virtual not stored column field is in read set
7641 
7642    @retval FALSE  No virtual not stored column is used
7643    @retval TRUE   At least one virtual not stored column is used
7644 */
7645 
check_virtual_columns_marked_for_read()7646 bool TABLE::check_virtual_columns_marked_for_read()
7647 {
7648   if (vfield)
7649   {
7650     Field **vfield_ptr;
7651     for (vfield_ptr= vfield; *vfield_ptr; vfield_ptr++)
7652     {
7653       Field *tmp_vfield= *vfield_ptr;
7654       if (bitmap_is_set(read_set, tmp_vfield->field_index) &&
7655           !tmp_vfield->vcol_info->stored_in_db)
7656         return TRUE;
7657     }
7658   }
7659   return FALSE;
7660 }
7661 
7662 
7663 /**
7664    Check if a stored virtual column field is marked for write
7665 
7666    This can be used to check if any column that is part of a virtual
7667    stored column is changed
7668 
7669    @retval FALSE  No stored virtual column is used
7670    @retval TRUE   At least one stored virtual column is used
7671 */
7672 
check_virtual_columns_marked_for_write()7673 bool TABLE::check_virtual_columns_marked_for_write()
7674 {
7675   if (vfield)
7676   {
7677     Field **vfield_ptr;
7678     for (vfield_ptr= vfield; *vfield_ptr; vfield_ptr++)
7679     {
7680       Field *tmp_vfield= *vfield_ptr;
7681       if (bitmap_is_set(write_set, tmp_vfield->field_index) &&
7682                         tmp_vfield->vcol_info->stored_in_db)
7683         return TRUE;
7684     }
7685   }
7686   return FALSE;
7687 }
7688 
7689 
7690 /*
7691   Mark fields used by check constraints into s->check_set.
7692   Mark all fields used in an expression that is part of an index
7693   with PART_INDIRECT_KEY_FLAG
7694 
7695   This is done once for the TABLE_SHARE the first time the table is opened.
7696   The marking must be done non-destructively to handle the case when
7697   this could be run in parallely by two threads
7698 */
7699 
mark_columns_used_by_virtual_fields(void)7700 void TABLE::mark_columns_used_by_virtual_fields(void)
7701 {
7702   MY_BITMAP *save_read_set;
7703   Field **vfield_ptr;
7704   TABLE_SHARE::enum_v_keys v_keys= TABLE_SHARE::NO_V_KEYS;
7705 
7706   /* If there is virtual fields are already initialized */
7707   if (s->check_set_initialized)
7708     return;
7709 
7710   if (s->tmp_table == NO_TMP_TABLE)
7711     mysql_mutex_lock(&s->LOCK_share);
7712   if (s->check_set)
7713   {
7714     /* Mark fields used by check constraint */
7715     save_read_set= read_set;
7716     read_set= s->check_set;
7717 
7718     for (Virtual_column_info **chk= check_constraints ; *chk ; chk++)
7719       (*chk)->expr->walk(&Item::register_field_in_read_map, 1, 0);
7720     read_set= save_read_set;
7721   }
7722 
7723   /*
7724     mark all fields that part of a virtual indexed field with
7725     PART_INDIRECT_KEY_FLAG. This is used to ensure that all fields
7726     that are part of an index exits before write/delete/update.
7727 
7728     As this code is only executed once per open share, it's reusing
7729     existing functionality instead of adding an extra argument to
7730     add_field_to_set_processor or adding another processor.
7731   */
7732   if (vfield)
7733   {
7734     for (vfield_ptr= vfield; *vfield_ptr; vfield_ptr++)
7735     {
7736       if ((*vfield_ptr)->flags & PART_KEY_FLAG)
7737         (*vfield_ptr)->vcol_info->expr->walk(&Item::add_field_to_set_processor,
7738                                              1, this);
7739     }
7740     for (uint i= 0 ; i < s->fields ; i++)
7741     {
7742       if (bitmap_is_set(&tmp_set, i))
7743       {
7744         s->field[i]->flags|= PART_INDIRECT_KEY_FLAG;
7745         v_keys= TABLE_SHARE::V_KEYS;
7746       }
7747     }
7748     bitmap_clear_all(&tmp_set);
7749   }
7750   s->check_set_initialized= v_keys;
7751   if (s->tmp_table == NO_TMP_TABLE)
7752     mysql_mutex_unlock(&s->LOCK_share);
7753 }
7754 
7755 /* Add fields used by CHECK CONSTRAINT to read map */
7756 
mark_check_constraint_columns_for_read(void)7757 void TABLE::mark_check_constraint_columns_for_read(void)
7758 {
7759   bitmap_union(read_set, s->check_set);
7760 }
7761 
7762 
7763 /**
7764   Add all fields that have a default function to the table write set.
7765 */
7766 
mark_default_fields_for_write(bool is_insert)7767 void TABLE::mark_default_fields_for_write(bool is_insert)
7768 {
7769   DBUG_ENTER("mark_default_fields_for_write");
7770   Field **field_ptr, *field;
7771   for (field_ptr= default_field; *field_ptr; field_ptr++)
7772   {
7773     field= (*field_ptr);
7774     if (is_insert && field->default_value)
7775     {
7776       bitmap_set_bit(write_set, field->field_index);
7777       field->default_value->expr->
7778         walk(&Item::register_field_in_read_map, 1, 0);
7779     }
7780     else if (!is_insert && field->has_update_default_function())
7781       bitmap_set_bit(write_set, field->field_index);
7782   }
7783   DBUG_VOID_RETURN;
7784 }
7785 
7786 
move_fields(Field ** ptr,const uchar * to,const uchar * from)7787 void TABLE::move_fields(Field **ptr, const uchar *to, const uchar *from)
7788 {
7789   my_ptrdiff_t diff= to - from;
7790   if (diff)
7791   {
7792     do
7793     {
7794       (*ptr)->move_field_offset(diff);
7795     } while (*(++ptr));
7796   }
7797 }
7798 
7799 
7800 /*
7801   Store all allocated virtual fields blob values
7802   Used by InnoDB when calculating virtual fields for it's own internal
7803   records
7804 */
7805 
remember_blob_values(String * blob_storage)7806 void TABLE::remember_blob_values(String *blob_storage)
7807 {
7808   Field **vfield_ptr;
7809   for (vfield_ptr= vfield; *vfield_ptr; vfield_ptr++)
7810   {
7811     if ((*vfield_ptr)->type() == MYSQL_TYPE_BLOB &&
7812         !(*vfield_ptr)->vcol_info->stored_in_db)
7813     {
7814       Field_blob *blob= ((Field_blob*) *vfield_ptr);
7815       memcpy((void*) blob_storage, (void*) &blob->value, sizeof(blob->value));
7816       blob_storage++;
7817       blob->value.release();
7818     }
7819   }
7820 }
7821 
7822 
7823 /*
7824   Restore all allocated virtual fields blob values
7825   Used by InnoDB when calculating virtual fields for it's own internal
7826   records
7827 */
7828 
restore_blob_values(String * blob_storage)7829 void TABLE::restore_blob_values(String *blob_storage)
7830 {
7831   Field **vfield_ptr;
7832   for (vfield_ptr= vfield; *vfield_ptr; vfield_ptr++)
7833   {
7834     if ((*vfield_ptr)->type() == MYSQL_TYPE_BLOB &&
7835         !(*vfield_ptr)->vcol_info->stored_in_db)
7836     {
7837       Field_blob *blob= ((Field_blob*) *vfield_ptr);
7838       blob->value.free();
7839       memcpy((void*) &blob->value, (void*) blob_storage, sizeof(blob->value));
7840       blob_storage++;
7841     }
7842   }
7843 }
7844 
7845 
7846 /**
7847   @brief
7848   Allocate space for keys
7849 
7850   @param key_count  number of keys to allocate additionally
7851 
7852   @details
7853   The function allocates memory  to fit additionally 'key_count' keys
7854   for this table.
7855 
7856   @return FALSE   space was successfully allocated
7857   @return TRUE    an error occur
7858 */
7859 
alloc_keys(uint key_count)7860 bool TABLE::alloc_keys(uint key_count)
7861 {
7862   KEY *new_key_info;
7863   key_part_map *new_const_key_parts;
7864   DBUG_ASSERT(s->tmp_table == INTERNAL_TMP_TABLE);
7865 
7866   if (!multi_alloc_root(&mem_root,
7867                         &new_key_info, sizeof(*key_info)*(s->keys+key_count),
7868                         &new_const_key_parts,
7869                         sizeof(*new_const_key_parts)*(s->keys+key_count),
7870                         NullS))
7871     return TRUE;
7872   if (s->keys)
7873   {
7874     memmove(new_key_info, s->key_info, sizeof(*key_info) * s->keys);
7875     memmove(new_const_key_parts, const_key_parts,
7876             s->keys * sizeof(const_key_parts));
7877   }
7878   s->key_info= key_info= new_key_info;
7879   const_key_parts= new_const_key_parts;
7880   bzero((char*) (const_key_parts + s->keys),
7881         sizeof(*const_key_parts) * key_count);
7882   max_keys= s->keys+key_count;
7883   return FALSE;
7884 }
7885 
7886 
7887 /**
7888   @brief
7889   Populate a KEY_PART_INFO structure with the data related to a field entry.
7890 
7891   @param key_part_info  The structure to fill.
7892   @param field          The field entry that represents the key part.
7893   @param fleldnr        The number of the field, count starting from 1.
7894 
7895   TODO: This method does not make use of any table specific fields. It
7896   could be refactored to act as a constructor for KEY_PART_INFO instead.
7897 */
7898 
create_key_part_by_field(KEY_PART_INFO * key_part_info,Field * field,uint fieldnr)7899 void TABLE::create_key_part_by_field(KEY_PART_INFO *key_part_info,
7900                                      Field *field, uint fieldnr)
7901 {
7902   DBUG_ASSERT(field->field_index + 1 == (int)fieldnr);
7903   key_part_info->null_bit= field->null_bit;
7904   key_part_info->null_offset= (uint) (field->null_ptr -
7905                                       (uchar*) record[0]);
7906   key_part_info->field= field;
7907   key_part_info->fieldnr= fieldnr;
7908   key_part_info->offset= field->offset(record[0]);
7909   /*
7910      field->key_length() accounts for the raw length of the field, excluding
7911      any metadata such as length of field or the NULL flag.
7912   */
7913   key_part_info->length= (uint16) field->key_length();
7914   key_part_info->key_part_flag= 0;
7915   /* TODO:
7916     The below method of computing the key format length of the
7917     key part is a copy/paste from opt_range.cc, and table.cc.
7918     This should be factored out, e.g. as a method of Field.
7919     In addition it is not clear if any of the Field::*_length
7920     methods is supposed to compute the same length. If so, it
7921     might be reused.
7922   */
7923   key_part_info->store_length= key_part_info->length;
7924   /*
7925     For BIT fields null_bit is not set to 0 even if the field is defined
7926     as NOT NULL, look at Field_bit::Field_bit
7927   */
7928   if (!field->real_maybe_null())
7929   {
7930     key_part_info->null_bit= 0;
7931   }
7932 
7933   /*
7934      The total store length of the key part is the raw length of the field +
7935      any metadata information, such as its length for strings and/or the null
7936      flag.
7937   */
7938   if (field->real_maybe_null())
7939   {
7940     key_part_info->store_length+= HA_KEY_NULL_LENGTH;
7941   }
7942 
7943   key_part_info->key_part_flag|= field->key_part_flag();
7944   key_part_info->store_length+= field->key_part_length_bytes();
7945 
7946   key_part_info->type=     (uint8) field->key_type();
7947   key_part_info->key_type =
7948     ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
7949     (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
7950     (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
7951     0 : FIELDFLAG_BINARY;
7952 }
7953 
7954 
7955 /**
7956   @brief
7957   Check validity of a possible key for the derived table
7958 
7959   @param key            the number of the key
7960   @param key_parts      number of components of the key
7961   @param next_field_no  the call-back function that returns the number of
7962                         the field used as the next component of the key
7963   @param arg            the argument for the above function
7964 
7965   @details
7966   The function checks whether a possible key satisfies the constraints
7967   imposed on the keys of any temporary table.
7968 
7969   We need to filter out BLOB columns here, because ref access optimizer creates
7970   KEYUSE objects for equalities for non-key columns for two puproses:
7971   1. To discover possible keys for derived_with_keys optimization
7972   2. To do hash joins
7973   For the purpose of #1, KEYUSE objects are not created for "blob_column=..." .
7974   However, they might be created for #2. In order to catch that case, we filter
7975   them out here.
7976 
7977   @return TRUE if the key is valid
7978   @return FALSE otherwise
7979 */
7980 
check_tmp_key(uint key,uint key_parts,uint (* next_field_no)(uchar *),uchar * arg)7981 bool TABLE::check_tmp_key(uint key, uint key_parts,
7982                           uint (*next_field_no) (uchar *), uchar *arg)
7983 {
7984   Field **reg_field;
7985   uint i;
7986   uint key_len= 0;
7987 
7988   for (i= 0; i < key_parts; i++)
7989   {
7990     uint fld_idx= next_field_no(arg);
7991     reg_field= field + fld_idx;
7992     if ((*reg_field)->type() == MYSQL_TYPE_BLOB)
7993       return FALSE;
7994     uint fld_store_len= (uint16) (*reg_field)->key_length();
7995     if ((*reg_field)->real_maybe_null())
7996       fld_store_len+= HA_KEY_NULL_LENGTH;
7997     if ((*reg_field)->real_type() == MYSQL_TYPE_VARCHAR ||
7998         (*reg_field)->type() == MYSQL_TYPE_GEOMETRY)
7999       fld_store_len+= HA_KEY_BLOB_LENGTH;
8000     key_len+= fld_store_len;
8001   }
8002   /*
8003     We use MI_MAX_KEY_LENGTH (myisam's default) below because it is
8004     smaller than MAX_KEY_LENGTH (heap's default) and it's unknown whether
8005     myisam or heap will be used for the temporary table.
8006   */
8007   return key_len <= MI_MAX_KEY_LENGTH;
8008 }
8009 
8010 /**
8011   @brief
8012   Add one key to a temporary table
8013 
8014   @param key            the number of the key
8015   @param key_parts      number of components of the key
8016   @param next_field_no  the call-back function that returns the number of
8017                         the field used as the next component of the key
8018   @param arg            the argument for the above function
8019   @param unique         TRUE <=> it is a unique index
8020 
8021   @details
8022   The function adds a new key to the table that is assumed to be a temporary
8023   table. At each its invocation the call-back function must return
8024   the number of the field that is used as the next component of this key.
8025 
8026   @return FALSE is a success
8027   @return TRUE if a failure
8028 
8029 */
8030 
add_tmp_key(uint key,uint key_parts,uint (* next_field_no)(uchar *),uchar * arg,bool unique)8031 bool TABLE::add_tmp_key(uint key, uint key_parts,
8032                         uint (*next_field_no) (uchar *), uchar *arg,
8033                         bool unique)
8034 {
8035   DBUG_ASSERT(key < max_keys);
8036 
8037   char buf[NAME_CHAR_LEN];
8038   KEY* keyinfo;
8039   Field **reg_field;
8040   uint i;
8041 
8042   bool key_start= TRUE;
8043   KEY_PART_INFO* key_part_info=
8044       (KEY_PART_INFO*) alloc_root(&mem_root, sizeof(KEY_PART_INFO)*key_parts);
8045   if (!key_part_info)
8046     return TRUE;
8047   keyinfo= key_info + key;
8048   keyinfo->key_part= key_part_info;
8049   keyinfo->usable_key_parts= keyinfo->user_defined_key_parts = key_parts;
8050   keyinfo->ext_key_parts= keyinfo->user_defined_key_parts;
8051   keyinfo->key_length=0;
8052   keyinfo->algorithm= HA_KEY_ALG_UNDEF;
8053   keyinfo->flags= HA_GENERATED_KEY;
8054   keyinfo->ext_key_flags= keyinfo->flags;
8055   keyinfo->is_statistics_from_stat_tables= FALSE;
8056   if (unique)
8057     keyinfo->flags|= HA_NOSAME;
8058   sprintf(buf, "key%i", key);
8059   keyinfo->name.length= strlen(buf);
8060   if (!(keyinfo->name.str= strmake_root(&mem_root, buf, keyinfo->name.length)))
8061     return TRUE;
8062   keyinfo->rec_per_key= (ulong*) alloc_root(&mem_root,
8063                                             sizeof(ulong)*key_parts);
8064   if (!keyinfo->rec_per_key)
8065     return TRUE;
8066   bzero(keyinfo->rec_per_key, sizeof(ulong)*key_parts);
8067   keyinfo->read_stats= NULL;
8068   keyinfo->collected_stats= NULL;
8069 
8070   for (i= 0; i < key_parts; i++)
8071   {
8072     uint fld_idx= next_field_no(arg);
8073     reg_field= field + fld_idx;
8074     if (key_start)
8075       (*reg_field)->key_start.set_bit(key);
8076     (*reg_field)->part_of_key.set_bit(key);
8077     create_key_part_by_field(key_part_info, *reg_field, fld_idx+1);
8078     keyinfo->key_length += key_part_info->store_length;
8079     (*reg_field)->flags|= PART_KEY_FLAG;
8080     key_start= FALSE;
8081     key_part_info++;
8082   }
8083 
8084   /*
8085     For the case when there is a derived table that would give distinct rows,
8086     the index statistics are passed to the join optimizer to tell that a ref
8087     access to all the fields of the derived table will produce only one row.
8088   */
8089 
8090   st_select_lex_unit* derived= pos_in_table_list ?
8091                                pos_in_table_list->derived: NULL;
8092   if (derived)
8093   {
8094     st_select_lex* first= derived->first_select();
8095     uint select_list_items= first->get_item_list()->elements;
8096     if (key_parts == select_list_items)
8097     {
8098       if ((!first->is_part_of_union() && (first->options & SELECT_DISTINCT)) ||
8099           derived->check_distinct_in_union())
8100         keyinfo->rec_per_key[key_parts - 1]= 1;
8101     }
8102   }
8103 
8104   set_if_bigger(s->max_key_length, keyinfo->key_length);
8105   s->keys++;
8106   return FALSE;
8107 }
8108 
8109 /*
8110   @brief
8111   Drop all indexes except specified one.
8112 
8113   @param key_to_save the key to save
8114 
8115   @details
8116   Drop all indexes on this table except 'key_to_save'. The saved key becomes
8117   key #0. Memory occupied by key parts of dropped keys are freed.
8118   If the 'key_to_save' is negative then all keys are freed.
8119 */
8120 
use_index(int key_to_save)8121 void TABLE::use_index(int key_to_save)
8122 {
8123   uint i= 1;
8124   DBUG_ASSERT(!created && key_to_save < (int)s->keys);
8125   if (key_to_save >= 0)
8126     /* Save the given key. */
8127     memmove(key_info, key_info + key_to_save, sizeof(KEY));
8128   else
8129     /* Drop all keys; */
8130     i= 0;
8131 
8132   s->keys= i;
8133 }
8134 
8135 /*
8136   Return TRUE if the table is filled at execution phase
8137 
8138   (and so, the optimizer must not do anything that depends on the contents of
8139    the table, like range analysis or constant table detection)
8140 */
8141 
is_filled_at_execution()8142 bool TABLE::is_filled_at_execution()
8143 {
8144   /*
8145     pos_in_table_list == NULL for internal temporary tables because they
8146     do not have a corresponding table reference. Such tables are filled
8147     during execution.
8148   */
8149   return MY_TEST(!pos_in_table_list ||
8150                  pos_in_table_list->jtbm_subselect ||
8151                  pos_in_table_list->is_active_sjm());
8152 }
8153 
8154 
8155 /**
8156   @brief
8157   Get actual number of key components
8158 
8159   @param keyinfo
8160 
8161   @details
8162   The function calculates actual number of key components, possibly including
8163   components of extended keys, taken into consideration by the optimizer for the
8164   key described by the parameter keyinfo.
8165 
8166   @return number of considered key components
8167 */
8168 
actual_n_key_parts(KEY * keyinfo)8169 uint TABLE::actual_n_key_parts(KEY *keyinfo)
8170 {
8171   return optimizer_flag(in_use, OPTIMIZER_SWITCH_EXTENDED_KEYS) ?
8172            keyinfo->ext_key_parts : keyinfo->user_defined_key_parts;
8173 }
8174 
8175 
8176 /**
8177   @brief
8178   Get actual key flags for a table key
8179 
8180   @param keyinfo
8181 
8182   @details
8183   The function finds out actual key flags taken into consideration by the
8184   optimizer for the key described by the parameter keyinfo.
8185 
8186   @return actual key flags
8187 */
8188 
actual_key_flags(KEY * keyinfo)8189 ulong TABLE::actual_key_flags(KEY *keyinfo)
8190 {
8191   return optimizer_flag(in_use, OPTIMIZER_SWITCH_EXTENDED_KEYS) ?
8192            keyinfo->ext_key_flags : keyinfo->flags;
8193 }
8194 
8195 
8196 /*
8197   Cleanup this table for re-execution.
8198 
8199   SYNOPSIS
8200     TABLE_LIST::reinit_before_use()
8201 */
8202 
reinit_before_use(THD * thd)8203 void TABLE_LIST::reinit_before_use(THD *thd)
8204 {
8205   /*
8206     Reset old pointers to TABLEs: they are not valid since the tables
8207     were closed in the end of previous prepare or execute call.
8208   */
8209   table= 0;
8210   /* Reset is_schema_table_processed value(needed for I_S tables */
8211   schema_table_state= NOT_PROCESSED;
8212 
8213   TABLE_LIST *embedded; /* The table at the current level of nesting. */
8214   TABLE_LIST *parent_embedding= this; /* The parent nested table reference. */
8215   do
8216   {
8217     embedded= parent_embedding;
8218     if (embedded->prep_on_expr)
8219       embedded->on_expr= embedded->prep_on_expr->copy_andor_structure(thd);
8220     parent_embedding= embedded->embedding;
8221   }
8222   while (parent_embedding &&
8223          parent_embedding->nested_join->join_list.head() == embedded);
8224 
8225   mdl_request.ticket= NULL;
8226 }
8227 
8228 
8229 /*
8230   Return subselect that contains the FROM list this table is taken from
8231 
8232   SYNOPSIS
8233     TABLE_LIST::containing_subselect()
8234 
8235   RETURN
8236     Subselect item for the subquery that contains the FROM list
8237     this table is taken from if there is any
8238     0 - otherwise
8239 
8240 */
8241 
containing_subselect()8242 Item_subselect *TABLE_LIST::containing_subselect()
8243 {
8244   return (select_lex ? select_lex->master_unit()->item : 0);
8245 }
8246 
8247 /*
8248   Compiles the tagged hints list and fills up the bitmasks.
8249 
8250   SYNOPSIS
8251     process_index_hints()
8252       table         the TABLE to operate on.
8253 
8254   DESCRIPTION
8255     The parser collects the index hints for each table in a "tagged list"
8256     (TABLE_LIST::index_hints). Using the information in this tagged list
8257     this function sets the members TABLE::keys_in_use_for_query,
8258     TABLE::keys_in_use_for_group_by, TABLE::keys_in_use_for_order_by,
8259     TABLE::force_index, TABLE::force_index_order,
8260     TABLE::force_index_group and TABLE::covering_keys.
8261 
8262     Current implementation of the runtime does not allow mixing FORCE INDEX
8263     and USE INDEX, so this is checked here. Then the FORCE INDEX list
8264     (if non-empty) is appended to the USE INDEX list and a flag is set.
8265 
8266     Multiple hints of the same kind are processed so that each clause
8267     is applied to what is computed in the previous clause.
8268     For example:
8269         USE INDEX (i1) USE INDEX (i2)
8270     is equivalent to
8271         USE INDEX (i1,i2)
8272     and means "consider only i1 and i2".
8273 
8274     Similarly
8275         USE INDEX () USE INDEX (i1)
8276     is equivalent to
8277         USE INDEX (i1)
8278     and means "consider only the index i1"
8279 
8280     It is OK to have the same index several times, e.g. "USE INDEX (i1,i1)" is
8281     not an error.
8282 
8283     Different kind of hints (USE/FORCE/IGNORE) are processed in the following
8284     order:
8285       1. All indexes in USE (or FORCE) INDEX are added to the mask.
8286       2. All IGNORE INDEX
8287 
8288     e.g. "USE INDEX i1, IGNORE INDEX i1, USE INDEX i1" will not use i1 at all
8289     as if we had "USE INDEX i1, USE INDEX i1, IGNORE INDEX i1".
8290 
8291     As an optimization if there is a covering index, and we have
8292     IGNORE INDEX FOR GROUP/ORDER, and this index is used for the JOIN part,
8293     then we have to ignore the IGNORE INDEX FROM GROUP/ORDER.
8294 
8295   RETURN VALUE
8296     FALSE                no errors found
8297     TRUE                 found and reported an error.
8298 */
process_index_hints(TABLE * tbl)8299 bool TABLE_LIST::process_index_hints(TABLE *tbl)
8300 {
8301   /* initialize the result variables */
8302   tbl->keys_in_use_for_query= tbl->keys_in_use_for_group_by=
8303     tbl->keys_in_use_for_order_by= tbl->s->keys_in_use;
8304 
8305   /* index hint list processing */
8306   if (index_hints)
8307   {
8308     key_map index_join[INDEX_HINT_FORCE + 1];
8309     key_map index_order[INDEX_HINT_FORCE + 1];
8310     key_map index_group[INDEX_HINT_FORCE + 1];
8311     Index_hint *hint;
8312     int type;
8313     bool have_empty_use_join= FALSE, have_empty_use_order= FALSE,
8314          have_empty_use_group= FALSE;
8315     List_iterator <Index_hint> iter(*index_hints);
8316 
8317     /* initialize temporary variables used to collect hints of each kind */
8318     for (type= INDEX_HINT_IGNORE; type <= INDEX_HINT_FORCE; type++)
8319     {
8320       index_join[type].clear_all();
8321       index_order[type].clear_all();
8322       index_group[type].clear_all();
8323     }
8324 
8325     /* iterate over the hints list */
8326     while ((hint= iter++))
8327     {
8328       uint pos;
8329 
8330       /* process empty USE INDEX () */
8331       if (hint->type == INDEX_HINT_USE && !hint->key_name.str)
8332       {
8333         if (hint->clause & INDEX_HINT_MASK_JOIN)
8334         {
8335           index_join[hint->type].clear_all();
8336           have_empty_use_join= TRUE;
8337         }
8338         if (hint->clause & INDEX_HINT_MASK_ORDER)
8339         {
8340           index_order[hint->type].clear_all();
8341           have_empty_use_order= TRUE;
8342         }
8343         if (hint->clause & INDEX_HINT_MASK_GROUP)
8344         {
8345           index_group[hint->type].clear_all();
8346           have_empty_use_group= TRUE;
8347         }
8348         continue;
8349       }
8350 
8351       /*
8352         Check if an index with the given name exists and get his offset in
8353         the keys bitmask for the table
8354       */
8355       if (tbl->s->keynames.type_names == 0 ||
8356           (pos= find_type(&tbl->s->keynames, hint->key_name.str,
8357                           hint->key_name.length, 1)) <= 0)
8358       {
8359         my_error(ER_KEY_DOES_NOT_EXISTS, MYF(0), hint->key_name.str, alias.str);
8360         return 1;
8361       }
8362 
8363       pos--;
8364 
8365       /* add to the appropriate clause mask */
8366       if (hint->clause & INDEX_HINT_MASK_JOIN)
8367         index_join[hint->type].set_bit (pos);
8368       if (hint->clause & INDEX_HINT_MASK_ORDER)
8369         index_order[hint->type].set_bit (pos);
8370       if (hint->clause & INDEX_HINT_MASK_GROUP)
8371         index_group[hint->type].set_bit (pos);
8372     }
8373 
8374     /* cannot mix USE INDEX and FORCE INDEX */
8375     if ((!index_join[INDEX_HINT_FORCE].is_clear_all() ||
8376          !index_order[INDEX_HINT_FORCE].is_clear_all() ||
8377          !index_group[INDEX_HINT_FORCE].is_clear_all()) &&
8378         (!index_join[INDEX_HINT_USE].is_clear_all() ||  have_empty_use_join ||
8379          !index_order[INDEX_HINT_USE].is_clear_all() || have_empty_use_order ||
8380          !index_group[INDEX_HINT_USE].is_clear_all() || have_empty_use_group))
8381     {
8382       my_error(ER_WRONG_USAGE, MYF(0), index_hint_type_name[INDEX_HINT_USE],
8383                index_hint_type_name[INDEX_HINT_FORCE]);
8384       return 1;
8385     }
8386 
8387     /* process FORCE INDEX as USE INDEX with a flag */
8388     if (!index_order[INDEX_HINT_FORCE].is_clear_all())
8389     {
8390       tbl->force_index_order= TRUE;
8391       index_order[INDEX_HINT_USE].merge(index_order[INDEX_HINT_FORCE]);
8392     }
8393 
8394     if (!index_group[INDEX_HINT_FORCE].is_clear_all())
8395     {
8396       tbl->force_index_group= TRUE;
8397       index_group[INDEX_HINT_USE].merge(index_group[INDEX_HINT_FORCE]);
8398     }
8399 
8400     /*
8401       TODO: get rid of tbl->force_index (on if any FORCE INDEX is specified)
8402       and create tbl->force_index_join instead.
8403       Then use the correct force_index_XX instead of the global one.
8404     */
8405     if (!index_join[INDEX_HINT_FORCE].is_clear_all() ||
8406         tbl->force_index_group || tbl->force_index_order)
8407     {
8408       tbl->force_index= TRUE;
8409       index_join[INDEX_HINT_USE].merge(index_join[INDEX_HINT_FORCE]);
8410     }
8411 
8412     /* apply USE INDEX */
8413     if (!index_join[INDEX_HINT_USE].is_clear_all() || have_empty_use_join)
8414       tbl->keys_in_use_for_query.intersect(index_join[INDEX_HINT_USE]);
8415     if (!index_order[INDEX_HINT_USE].is_clear_all() || have_empty_use_order)
8416       tbl->keys_in_use_for_order_by.intersect (index_order[INDEX_HINT_USE]);
8417     if (!index_group[INDEX_HINT_USE].is_clear_all() || have_empty_use_group)
8418       tbl->keys_in_use_for_group_by.intersect (index_group[INDEX_HINT_USE]);
8419 
8420     /* apply IGNORE INDEX */
8421     tbl->keys_in_use_for_query.subtract (index_join[INDEX_HINT_IGNORE]);
8422     tbl->keys_in_use_for_order_by.subtract (index_order[INDEX_HINT_IGNORE]);
8423     tbl->keys_in_use_for_group_by.subtract (index_group[INDEX_HINT_IGNORE]);
8424   }
8425 
8426   /* make sure covering_keys don't include indexes disabled with a hint */
8427   tbl->covering_keys.intersect(tbl->keys_in_use_for_query);
8428   return 0;
8429 }
8430 
8431 
max_row_length(TABLE * table,MY_BITMAP const * cols,const uchar * data)8432 size_t max_row_length(TABLE *table, MY_BITMAP const *cols, const uchar *data)
8433 {
8434   TABLE_SHARE *table_s= table->s;
8435   size_t length= table_s->reclength + 2 * table_s->fields;
8436   uint *const beg= table_s->blob_field;
8437   uint *const end= beg + table_s->blob_fields;
8438   my_ptrdiff_t const rec_offset= (my_ptrdiff_t) (data - table->record[0]);
8439   DBUG_ENTER("max_row_length");
8440 
8441   for (uint *ptr= beg ; ptr != end ; ++ptr)
8442   {
8443     Field * const field= table->field[*ptr];
8444     if (bitmap_is_set(cols, field->field_index) &&
8445         !field->is_null(rec_offset))
8446     {
8447       Field_blob * const blob= (Field_blob*) field;
8448       length+= blob->get_length(rec_offset) + 8; /* max blob store length */
8449     }
8450   }
8451   DBUG_PRINT("exit", ("length: %lld", (longlong) length));
8452   DBUG_RETURN(length);
8453 }
8454 
8455 
8456 /**
8457    Helper function which allows to allocate metadata lock request
8458    objects for all elements of table list.
8459 */
8460 
init_mdl_requests(TABLE_LIST * table_list)8461 void init_mdl_requests(TABLE_LIST *table_list)
8462 {
8463   for ( ; table_list ; table_list= table_list->next_global)
8464     MDL_REQUEST_INIT(&table_list->mdl_request, MDL_key::TABLE,
8465                      table_list->db.str, table_list->table_name.str,
8466                      table_list->lock_type >= TL_WRITE_ALLOW_WRITE
8467                      ? MDL_SHARED_WRITE : MDL_SHARED_READ, MDL_TRANSACTION);
8468 }
8469 
8470 
8471 /**
8472   Update TABLE::const_key_parts for single table UPDATE/DELETE query
8473 
8474   @param conds               WHERE clause expression
8475 
8476   @retval TRUE   error (OOM)
8477   @retval FALSE  success
8478 
8479   @note
8480     Set const_key_parts bits if key fields are equal to constants in
8481     the WHERE expression.
8482 */
8483 
update_const_key_parts(COND * conds)8484 bool TABLE::update_const_key_parts(COND *conds)
8485 {
8486   bzero((char*) const_key_parts, sizeof(key_part_map) * s->keys);
8487 
8488   if (conds == NULL)
8489     return FALSE;
8490 
8491   for (uint index= 0; index < s->keys; index++)
8492   {
8493     KEY_PART_INFO *keyinfo= key_info[index].key_part;
8494     KEY_PART_INFO *keyinfo_end= keyinfo + key_info[index].user_defined_key_parts;
8495 
8496     for (key_part_map part_map= (key_part_map)1;
8497         keyinfo < keyinfo_end;
8498         keyinfo++, part_map<<= 1)
8499     {
8500       if (const_expression_in_where(conds, NULL, keyinfo->field))
8501         const_key_parts[index]|= part_map;
8502     }
8503   }
8504   return FALSE;
8505 }
8506 
8507 /**
8508   Test if the order list consists of simple field expressions
8509 
8510   @param order                Linked list of ORDER BY arguments
8511 
8512   @return TRUE if @a order is empty or consist of simple field expressions
8513 */
8514 
is_simple_order(ORDER * order)8515 bool is_simple_order(ORDER *order)
8516 {
8517   for (ORDER *ord= order; ord; ord= ord->next)
8518   {
8519     if (ord->item[0]->real_item()->type() != Item::FIELD_ITEM)
8520       return FALSE;
8521   }
8522   return TRUE;
8523 }
8524 
8525 class Turn_errors_to_warnings_handler : public Internal_error_handler
8526 {
8527 public:
Turn_errors_to_warnings_handler()8528   Turn_errors_to_warnings_handler() {}
handle_condition(THD * thd,uint sql_errno,const char * sqlstate,Sql_condition::enum_warning_level * level,const char * msg,Sql_condition ** cond_hdl)8529   bool handle_condition(THD *thd,
8530                         uint sql_errno,
8531                         const char* sqlstate,
8532                         Sql_condition::enum_warning_level *level,
8533                         const char* msg,
8534                         Sql_condition ** cond_hdl)
8535   {
8536     *cond_hdl= NULL;
8537     if (*level == Sql_condition::WARN_LEVEL_ERROR)
8538       *level= Sql_condition::WARN_LEVEL_WARN;
8539     return(0);
8540   }
8541 };
8542 
8543 
8544 /*
8545   to satisfy marked_for_write_or_computed() Field's assert we temporarily
8546   mark field for write before storing the generated value in it
8547 */
8548 #ifdef DBUG_ASSERT_EXISTS
8549 #define DBUG_FIX_WRITE_SET(f) bool _write_set_fixed= !bitmap_fast_test_and_set(write_set, (f)->field_index)
8550 #define DBUG_RESTORE_WRITE_SET(f) if (_write_set_fixed) bitmap_clear_bit(write_set, (f)->field_index)
8551 #else
8552 #define DBUG_FIX_WRITE_SET(f)
8553 #define DBUG_RESTORE_WRITE_SET(f)
8554 #endif
8555 
8556 
8557 /*
8558   @brief Compute values for virtual columns used in query
8559 
8560   @param  update_mode Specifies what virtual column are computed
8561 
8562   @details
8563     The function computes the values of the virtual columns of the table and
8564     stores them in the table record buffer.
8565     This will be done even if is_error() is set either when function was called
8566     or by calculating the virtual function, as most calls to this
8567     function doesn't check the result. We also want to ensure that as many
8568     fields as possible has the right value so that we can optionally
8569     return the partly-faulty-row from a storage engine with a virtual
8570     field that gives an error on storage for an existing row.
8571 
8572   @todo
8573     Ensure that all caller checks the value of this function and
8574     either properly ignores it (and resets the error) or sends the
8575     error forward to the caller.
8576 
8577   @retval
8578     0    Success
8579   @retval
8580     >0   Error occurred when storing a virtual field value or potentially
8581          is_error() was set when function was called.
8582 */
8583 
update_virtual_fields(handler * h,enum_vcol_update_mode update_mode)8584 int TABLE::update_virtual_fields(handler *h, enum_vcol_update_mode update_mode)
8585 {
8586   DBUG_ENTER("TABLE::update_virtual_fields");
8587   DBUG_PRINT("enter", ("update_mode: %d  is_error: %d", update_mode,
8588                        in_use->is_error()));
8589   Field **vfield_ptr, *vf;
8590   Query_arena backup_arena;
8591   Turn_errors_to_warnings_handler Suppress_errors;
8592   bool handler_pushed= 0, update_all_columns= 1;
8593   DBUG_ASSERT(vfield);
8594 
8595   if (h->keyread_enabled())
8596     DBUG_RETURN(0);
8597 
8598   in_use->set_n_backup_active_arena(expr_arena, &backup_arena);
8599 
8600   /* When reading or deleting row, ignore errors from virtual columns */
8601   if (update_mode == VCOL_UPDATE_FOR_READ ||
8602       update_mode == VCOL_UPDATE_FOR_DELETE ||
8603       update_mode == VCOL_UPDATE_INDEXED)
8604   {
8605     in_use->push_internal_handler(&Suppress_errors);
8606     handler_pushed= 1;
8607   }
8608   else if (update_mode == VCOL_UPDATE_FOR_REPLACE &&
8609            in_use->is_current_stmt_binlog_format_row() &&
8610            in_use->variables.binlog_row_image != BINLOG_ROW_IMAGE_MINIMAL)
8611   {
8612     /*
8613       If we are doing a replace with not minimal binary logging, we have to
8614       calculate all virtual columns.
8615     */
8616     update_all_columns= 1;
8617   }
8618 
8619   /* Iterate over virtual fields in the table */
8620   for (vfield_ptr= vfield; *vfield_ptr ; vfield_ptr++)
8621   {
8622     vf= (*vfield_ptr);
8623     Virtual_column_info *vcol_info= vf->vcol_info;
8624     DBUG_ASSERT(vcol_info);
8625     DBUG_ASSERT(vcol_info->expr);
8626 
8627     bool update= 0, swap_values= 0;
8628     switch (update_mode) {
8629     case VCOL_UPDATE_FOR_READ:
8630       update= (!vcol_info->stored_in_db &&
8631                bitmap_is_set(read_set, vf->field_index));
8632       swap_values= 1;
8633       break;
8634     case VCOL_UPDATE_FOR_DELETE:
8635     case VCOL_UPDATE_FOR_WRITE:
8636       update= bitmap_is_set(read_set, vf->field_index);
8637       break;
8638     case VCOL_UPDATE_FOR_REPLACE:
8639       update= ((!vcol_info->stored_in_db &&
8640                 (vf->flags & (PART_KEY_FLAG | PART_INDIRECT_KEY_FLAG)) &&
8641                 bitmap_is_set(read_set, vf->field_index)) ||
8642                update_all_columns);
8643       if (update && (vf->flags & BLOB_FLAG))
8644       {
8645         /*
8646           The row has been read into record[1] and Field_blob::value
8647           contains the value for record[0].  Swap value and read_value
8648           to ensure that the virtual column data for the read row will
8649           be in read_value at the end of this function
8650         */
8651         ((Field_blob*) vf)->swap_value_and_read_value();
8652         /* Ensure we call swap_value_and_read_value() after update */
8653         swap_values= 1;
8654       }
8655       break;
8656     case VCOL_UPDATE_INDEXED:
8657     case VCOL_UPDATE_INDEXED_FOR_UPDATE:
8658       /* Read indexed fields that was not updated in VCOL_UPDATE_FOR_READ */
8659       update= (!vcol_info->stored_in_db &&
8660                (vf->flags & (PART_KEY_FLAG | PART_INDIRECT_KEY_FLAG)) &&
8661                !bitmap_is_set(read_set, vf->field_index));
8662       swap_values= 1;
8663       break;
8664     }
8665 
8666     if (update)
8667     {
8668       int field_error __attribute__((unused)) = 0;
8669       /* Compute the actual value of the virtual fields */
8670       DBUG_FIX_WRITE_SET(vf);
8671       field_error= vcol_info->expr->save_in_field(vf, 0);
8672       DBUG_RESTORE_WRITE_SET(vf);
8673       DBUG_PRINT("info", ("field '%s' - updated  error: %d",
8674                           vf->field_name.str, field_error));
8675       if (swap_values && (vf->flags & BLOB_FLAG))
8676       {
8677         /*
8678           Remember the read value to allow other update_virtual_field() calls
8679           for the same blob field for the row to be updated.
8680           Field_blob->read_value always contains the virtual column data for
8681           any read row.
8682         */
8683         ((Field_blob*) vf)->swap_value_and_read_value();
8684       }
8685     }
8686     else
8687     {
8688       DBUG_PRINT("info", ("field '%s' - skipped", vf->field_name.str));
8689     }
8690   }
8691   if (handler_pushed)
8692     in_use->pop_internal_handler();
8693   in_use->restore_active_arena(expr_arena, &backup_arena);
8694 
8695   /* Return 1 only of we got a fatal error, not a warning */
8696   DBUG_RETURN(in_use->is_error());
8697 }
8698 
update_virtual_field(Field * vf)8699 int TABLE::update_virtual_field(Field *vf)
8700 {
8701   DBUG_ENTER("TABLE::update_virtual_field");
8702   Query_arena backup_arena;
8703   Counting_error_handler count_errors;
8704   in_use->push_internal_handler(&count_errors);
8705   in_use->set_n_backup_active_arena(expr_arena, &backup_arena);
8706   bitmap_clear_all(&tmp_set);
8707   vf->vcol_info->expr->walk(&Item::update_vcol_processor, 0, &tmp_set);
8708   DBUG_FIX_WRITE_SET(vf);
8709   vf->vcol_info->expr->save_in_field(vf, 0);
8710   DBUG_RESTORE_WRITE_SET(vf);
8711   in_use->restore_active_arena(expr_arena, &backup_arena);
8712   in_use->pop_internal_handler();
8713   DBUG_RETURN(count_errors.errors);
8714 }
8715 
8716 
8717 /**
8718   Update all DEFAULT and/or ON INSERT fields.
8719 
8720   @details
8721     Compute and set the default value of all fields with a default function.
8722     There are two kinds of default functions - one is used for INSERT-like
8723     operations, the other for UPDATE-like operations. Depending on the field
8724     definition and the current operation one or the other kind of update
8725     function is evaluated.
8726 
8727   @param update_command   True if command was an update else insert
8728   @param ignore_errors    True if we should ignore errors
8729 
8730   @retval
8731     0    Success
8732   @retval
8733     >0   Error occurred when storing a virtual field value and
8734          ignore_errors == 0. If set then an error was generated.
8735 */
8736 
update_default_fields(bool ignore_errors)8737 int TABLE::update_default_fields(bool ignore_errors)
8738 {
8739   Query_arena backup_arena;
8740   Field **field_ptr;
8741   int res= 0;
8742   DBUG_ENTER("TABLE::update_default_fields");
8743   DBUG_ASSERT(default_field);
8744 
8745   in_use->set_n_backup_active_arena(expr_arena, &backup_arena);
8746 
8747   /* Iterate over fields with default functions in the table */
8748   for (field_ptr= default_field; *field_ptr ; field_ptr++)
8749   {
8750     Field *field= (*field_ptr);
8751     /*
8752       If an explicit default value for a field overrides the default,
8753       do not update the field with its automatic default value.
8754     */
8755     if (!field->has_explicit_value())
8756     {
8757       if (field->default_value &&
8758           (field->default_value->flags || field->flags & BLOB_FLAG))
8759         res|= (field->default_value->expr->save_in_field(field, 0) < 0);
8760       if (!ignore_errors && res)
8761       {
8762         my_error(ER_CALCULATING_DEFAULT_VALUE, MYF(0), field->field_name.str);
8763         break;
8764       }
8765       res= 0;
8766     }
8767   }
8768   in_use->restore_active_arena(expr_arena, &backup_arena);
8769   DBUG_RETURN(res);
8770 }
8771 
update_generated_fields()8772 int TABLE::update_generated_fields()
8773 {
8774   int res= 0;
8775   if (found_next_number_field)
8776   {
8777     next_number_field= found_next_number_field;
8778     res= found_next_number_field->set_default();
8779     if (likely(!res))
8780       res= file->update_auto_increment();
8781   }
8782 
8783   if (likely(!res) && vfield)
8784     res= update_virtual_fields(file, VCOL_UPDATE_FOR_WRITE);
8785   if (likely(!res) && versioned())
8786     vers_update_fields();
8787   if (likely(!res))
8788     res= verify_constraints(false) == VIEW_CHECK_ERROR;
8789   return res;
8790 }
8791 
period_make_insert(Item * src,Field * dst)8792 int TABLE::period_make_insert(Item *src, Field *dst)
8793 {
8794   THD *thd= in_use;
8795 
8796   ulonglong prev_insert_id= file->next_insert_id;
8797   store_record(this, record[1]);
8798   int res= src->save_in_field(dst, true);
8799 
8800   if (likely(!res))
8801     res= update_generated_fields();
8802 
8803   if (likely(!res) && triggers)
8804     res= triggers->process_triggers(thd, TRG_EVENT_INSERT,
8805                                     TRG_ACTION_BEFORE, true);
8806 
8807   if (likely(!res))
8808     res = file->ha_write_row(record[0]);
8809 
8810   if (likely(!res) && triggers)
8811     res= triggers->process_triggers(thd, TRG_EVENT_INSERT,
8812                                     TRG_ACTION_AFTER, true);
8813 
8814   restore_record(this, record[1]);
8815   if (res)
8816     file->restore_auto_increment(prev_insert_id);
8817   return res;
8818 }
8819 
insert_portion_of_time(THD * thd,const vers_select_conds_t & period_conds,ha_rows * rows_inserted)8820 int TABLE::insert_portion_of_time(THD *thd,
8821                                   const vers_select_conds_t &period_conds,
8822                                   ha_rows *rows_inserted)
8823 {
8824   bool lcond= period_conds.field_start->val_datetime_packed(thd)
8825               < period_conds.start.item->val_datetime_packed(thd);
8826   bool rcond= period_conds.field_end->val_datetime_packed(thd)
8827               > period_conds.end.item->val_datetime_packed(thd);
8828 
8829   int res= 0;
8830   if (lcond)
8831   {
8832     res= period_make_insert(period_conds.start.item,
8833                             field[s->period.end_fieldno]);
8834     if (likely(!res))
8835       ++*rows_inserted;
8836   }
8837   if (likely(!res) && rcond)
8838   {
8839     res= period_make_insert(period_conds.end.item,
8840                             field[s->period.start_fieldno]);
8841     if (likely(!res))
8842       ++*rows_inserted;
8843   }
8844 
8845   return res;
8846 }
8847 
evaluate_update_default_function()8848 void TABLE::evaluate_update_default_function()
8849 {
8850   DBUG_ENTER("TABLE::evaluate_update_default_function");
8851 
8852   if (s->has_update_default_function)
8853     for (Field **field_ptr= default_field; *field_ptr ; field_ptr++)
8854     {
8855       Field *field= (*field_ptr);
8856       if (!field->has_explicit_value() && field->has_update_default_function())
8857         field->set_time();
8858     }
8859   DBUG_VOID_RETURN;
8860 }
8861 
8862 /**
8863   Compare two records by a specific key (that has WITHOUT OVERLAPS clause)
8864 
8865   @return  true,     key values are equal and periods overlap
8866            false,    either key values differ or periods don't overlap
8867  */
check_period_overlaps(const KEY & key,const uchar * lhs,const uchar * rhs)8868 bool TABLE::check_period_overlaps(const KEY &key,
8869                                  const uchar *lhs, const uchar *rhs)
8870 {
8871   DBUG_ASSERT(key.without_overlaps);
8872   uint base_part_nr= key.user_defined_key_parts - 2;
8873   for (uint part_nr= 0; part_nr < base_part_nr; part_nr++)
8874   {
8875     Field *f= key.key_part[part_nr].field;
8876     if (key.key_part[part_nr].null_bit)
8877       if (f->is_null_in_record(lhs) || f->is_null_in_record(rhs))
8878         return false;
8879     uint kp_len= key.key_part[part_nr].length;
8880     if (f->cmp_prefix(f->ptr_in_record(lhs), f->ptr_in_record(rhs),
8881                       kp_len) != 0)
8882       return false;
8883   }
8884 
8885   uint period_start= key.user_defined_key_parts - 1;
8886   uint period_end= key.user_defined_key_parts - 2;
8887   const Field *fs= key.key_part[period_start].field;
8888   const Field *fe= key.key_part[period_end].field;
8889 
8890   if (fs->cmp(fe->ptr_in_record(lhs), fs->ptr_in_record(rhs)) <= 0)
8891     return false;
8892   if (fs->cmp(fs->ptr_in_record(lhs), fe->ptr_in_record(rhs)) >= 0)
8893     return false;
8894   return true;
8895 }
8896 
vers_update_fields()8897 void TABLE::vers_update_fields()
8898 {
8899   bitmap_set_bit(write_set, vers_start_field()->field_index);
8900   bitmap_set_bit(write_set, vers_end_field()->field_index);
8901 
8902   if (!vers_write)
8903   {
8904     file->column_bitmaps_signal();
8905     return;
8906   }
8907 
8908   if (versioned(VERS_TIMESTAMP))
8909   {
8910     if (vers_start_field()->store_timestamp(in_use->query_start(),
8911                                           in_use->query_start_sec_part()))
8912     {
8913       DBUG_ASSERT(0);
8914     }
8915     vers_start_field()->set_has_explicit_value();
8916   }
8917 
8918   vers_end_field()->set_max();
8919   vers_end_field()->set_has_explicit_value();
8920   bitmap_set_bit(read_set, vers_end_field()->field_index);
8921   file->column_bitmaps_signal();
8922   if (vfield)
8923     update_virtual_fields(file, VCOL_UPDATE_FOR_READ);
8924 }
8925 
8926 
vers_update_end()8927 void TABLE::vers_update_end()
8928 {
8929   if (vers_end_field()->store_timestamp(in_use->query_start(),
8930                                         in_use->query_start_sec_part()))
8931     DBUG_ASSERT(0);
8932   vers_end_field()->set_has_explicit_value();
8933 }
8934 
8935 /**
8936    Reset markers that fields are being updated
8937 */
8938 
reset_default_fields()8939 void TABLE::reset_default_fields()
8940 {
8941   DBUG_ENTER("reset_default_fields");
8942   bitmap_clear_all(&has_value_set);
8943   DBUG_VOID_RETURN;
8944 }
8945 
8946 /*
8947   Prepare triggers  for INSERT-like statement.
8948 
8949   SYNOPSIS
8950     prepare_triggers_for_insert_stmt_or_event()
8951 
8952   NOTE
8953     Prepare triggers for INSERT-like statement by marking fields
8954     used by triggers and inform handlers that batching of UPDATE/DELETE
8955     cannot be done if there are BEFORE UPDATE/DELETE triggers.
8956 */
8957 
prepare_triggers_for_insert_stmt_or_event()8958 void TABLE::prepare_triggers_for_insert_stmt_or_event()
8959 {
8960   if (triggers)
8961   {
8962     if (triggers->has_triggers(TRG_EVENT_DELETE,
8963                                TRG_ACTION_AFTER))
8964     {
8965       /*
8966         The table has AFTER DELETE triggers that might access to
8967         subject table and therefore might need delete to be done
8968         immediately. So we turn-off the batching.
8969       */
8970       (void) file->extra(HA_EXTRA_DELETE_CANNOT_BATCH);
8971     }
8972     if (triggers->has_triggers(TRG_EVENT_UPDATE,
8973                                TRG_ACTION_AFTER))
8974     {
8975       /*
8976         The table has AFTER UPDATE triggers that might access to subject
8977         table and therefore might need update to be done immediately.
8978         So we turn-off the batching.
8979       */
8980       (void) file->extra(HA_EXTRA_UPDATE_CANNOT_BATCH);
8981     }
8982   }
8983 }
8984 
8985 
prepare_triggers_for_delete_stmt_or_event()8986 bool TABLE::prepare_triggers_for_delete_stmt_or_event()
8987 {
8988   if (triggers &&
8989       triggers->has_triggers(TRG_EVENT_DELETE,
8990                              TRG_ACTION_AFTER))
8991   {
8992     /*
8993       The table has AFTER DELETE triggers that might access to subject table
8994       and therefore might need delete to be done immediately. So we turn-off
8995       the batching.
8996     */
8997     (void) file->extra(HA_EXTRA_DELETE_CANNOT_BATCH);
8998     return TRUE;
8999   }
9000   return FALSE;
9001 }
9002 
9003 
prepare_triggers_for_update_stmt_or_event()9004 bool TABLE::prepare_triggers_for_update_stmt_or_event()
9005 {
9006   if (triggers &&
9007       triggers->has_triggers(TRG_EVENT_UPDATE,
9008                              TRG_ACTION_AFTER))
9009   {
9010     /*
9011       The table has AFTER UPDATE triggers that might access to subject
9012       table and therefore might need update to be done immediately.
9013       So we turn-off the batching.
9014     */
9015     (void) file->extra(HA_EXTRA_UPDATE_CANNOT_BATCH);
9016     return TRUE;
9017   }
9018   return FALSE;
9019 }
9020 
9021 
9022 /**
9023   Validates default value of fields which are not specified in
9024   the column list of INSERT/LOAD statement.
9025 
9026   @Note s->default_values should be properly populated
9027         before calling this function.
9028 
9029   @param thd              thread context
9030   @param record           the record to check values in
9031 
9032   @return
9033     @retval false Success.
9034     @retval true  Failure.
9035 */
9036 
validate_default_values_of_unset_fields(THD * thd) const9037 bool TABLE::validate_default_values_of_unset_fields(THD *thd) const
9038 {
9039   DBUG_ENTER("TABLE::validate_default_values_of_unset_fields");
9040   for (Field **fld= field; *fld; fld++)
9041   {
9042     if (!bitmap_is_set(write_set, (*fld)->field_index) &&
9043         !((*fld)->flags & (NO_DEFAULT_VALUE_FLAG | VERS_SYSTEM_FIELD)))
9044     {
9045       if (!(*fld)->is_null_in_record(s->default_values) &&
9046           (*fld)->validate_value_in_record_with_warn(thd, s->default_values) &&
9047           thd->is_error())
9048       {
9049         /*
9050           We're here if:
9051           - validate_value_in_record_with_warn() failed and
9052             strict mo validate_default_values_of_unset_fieldsde converted WARN to ERROR
9053           - or the connection was killed, or closed unexpectedly
9054         */
9055         DBUG_RETURN(true);
9056       }
9057     }
9058   }
9059   DBUG_RETURN(false);
9060 }
9061 
9062 
insert_all_rows_into_tmp_table(THD * thd,TABLE * tmp_table,TMP_TABLE_PARAM * tmp_table_param,bool with_cleanup)9063 bool TABLE::insert_all_rows_into_tmp_table(THD *thd,
9064                                            TABLE *tmp_table,
9065                                            TMP_TABLE_PARAM *tmp_table_param,
9066                                            bool with_cleanup)
9067 {
9068   int write_err= 0;
9069 
9070   DBUG_ENTER("TABLE::insert_all_rows_into_tmp_table");
9071 
9072   if (with_cleanup)
9073   {
9074    if ((write_err= tmp_table->file->ha_delete_all_rows()))
9075       goto err;
9076   }
9077 
9078   if (file->indexes_are_disabled())
9079     tmp_table->file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
9080   file->ha_index_or_rnd_end();
9081 
9082   if (unlikely(file->ha_rnd_init_with_error(1)))
9083     DBUG_RETURN(1);
9084 
9085   if (tmp_table->no_rows)
9086     tmp_table->file->extra(HA_EXTRA_NO_ROWS);
9087   else
9088   {
9089     /* update table->file->stats.records */
9090     file->info(HA_STATUS_VARIABLE);
9091     tmp_table->file->ha_start_bulk_insert(file->stats.records);
9092   }
9093 
9094   while (likely(!file->ha_rnd_next(tmp_table->record[0])))
9095   {
9096     write_err= tmp_table->file->ha_write_tmp_row(tmp_table->record[0]);
9097     if (unlikely(write_err))
9098     {
9099       bool is_duplicate;
9100       if (tmp_table->file->is_fatal_error(write_err, HA_CHECK_DUP) &&
9101           create_internal_tmp_table_from_heap(thd, tmp_table,
9102                                               tmp_table_param->start_recinfo,
9103                                               &tmp_table_param->recinfo,
9104                                               write_err, 1, &is_duplicate))
9105 	DBUG_RETURN(1);
9106 
9107     }
9108     if (unlikely(thd->check_killed()))
9109       goto err_killed;
9110   }
9111   if (!tmp_table->no_rows && tmp_table->file->ha_end_bulk_insert())
9112     goto err;
9113   DBUG_RETURN(0);
9114 
9115 err:
9116   DBUG_PRINT("error",("Got error: %d",write_err));
9117   file->print_error(write_err, MYF(0));
9118 err_killed:
9119   (void) file->ha_rnd_end();
9120   DBUG_RETURN(1);
9121 }
9122 
9123 
9124 
9125 /*
9126   @brief Reset const_table flag
9127 
9128   @detail
9129   Reset const_table flag for this table. If this table is a merged derived
9130   table/view the flag is recursively reseted for all tables of the underlying
9131   select.
9132 */
9133 
reset_const_table()9134 void TABLE_LIST::reset_const_table()
9135 {
9136   table->const_table= 0;
9137   if (is_merged_derived())
9138   {
9139     SELECT_LEX *select_lex= get_unit()->first_select();
9140     TABLE_LIST *tl;
9141     List_iterator<TABLE_LIST> ti(select_lex->leaf_tables);
9142     while ((tl= ti++))
9143       tl->reset_const_table();
9144   }
9145 }
9146 
9147 
9148 /*
9149   @brief Run derived tables/view handling phases on underlying select_lex.
9150 
9151   @param lex    LEX for this thread
9152   @param phases derived tables/views handling phases to run
9153                 (set of DT_XXX constants)
9154   @details
9155   This function runs this derived table through specified 'phases'.
9156   Underlying tables of this select are handled prior to this derived.
9157   'lex' is passed as an argument to called functions.
9158 
9159   @return TRUE on error
9160   @return FALSE ok
9161 */
9162 
handle_derived(LEX * lex,uint phases)9163 bool TABLE_LIST::handle_derived(LEX *lex, uint phases)
9164 {
9165   SELECT_LEX_UNIT *unit= get_unit();
9166   DBUG_ENTER("handle_derived");
9167   DBUG_PRINT("enter", ("phases: 0x%x", phases));
9168 
9169   if (unit)
9170   {
9171     if (!is_with_table_recursive_reference())
9172     {
9173       for (SELECT_LEX *sl= unit->first_select(); sl; sl= sl->next_select())
9174         if (sl->handle_derived(lex, phases))
9175           DBUG_RETURN(TRUE);
9176     }
9177     if (mysql_handle_single_derived(lex, this, phases))
9178       DBUG_RETURN(TRUE);
9179   }
9180   DBUG_RETURN(FALSE);
9181 }
9182 
9183 /**
9184   @brief
9185   Return unit of this derived table/view
9186 
9187   @return reference to a unit  if it's a derived table/view.
9188   @return 0                    when it's not a derived table/view.
9189 */
9190 
get_unit()9191 st_select_lex_unit *TABLE_LIST::get_unit()
9192 {
9193   return (view ? &view->unit : derived);
9194 }
9195 
9196 
9197 /**
9198   @brief
9199   Return select_lex of this derived table/view
9200 
9201   @return select_lex of this derived table/view.
9202   @return 0          when it's not a derived table.
9203 */
9204 
get_single_select()9205 st_select_lex *TABLE_LIST::get_single_select()
9206 {
9207   SELECT_LEX_UNIT *unit= get_unit();
9208   return (unit ? unit->first_select() : 0);
9209 }
9210 
9211 
9212 /**
9213   @brief
9214   Attach a join table list as a nested join to this TABLE_LIST.
9215 
9216   @param join_list join table list to attach
9217 
9218   @details
9219   This function wraps 'join_list' into a nested_join of this table, thus
9220   turning it to a nested join leaf.
9221 */
9222 
wrap_into_nested_join(List<TABLE_LIST> & join_list)9223 void TABLE_LIST::wrap_into_nested_join(List<TABLE_LIST> &join_list)
9224 {
9225   TABLE_LIST *tl;
9226   /*
9227     Walk through derived table top list and set 'embedding' to point to
9228     the nesting table.
9229   */
9230   nested_join->join_list.empty();
9231   List_iterator_fast<TABLE_LIST> li(join_list);
9232   nested_join->join_list= join_list;
9233   while ((tl= li++))
9234   {
9235     tl->embedding= this;
9236     tl->join_list= &nested_join->join_list;
9237   }
9238 }
9239 
9240 
9241 /**
9242   Check whether optimization has been performed and a derived table either
9243   been merged to upper select level or materialized.
9244 
9245   @param table  a TABLE_LIST object containing a derived table
9246 
9247   @return true in case the derived table has been merged to surrounding select,
9248           false otherwise
9249 */
9250 
derived_table_optimization_done(TABLE_LIST * table)9251 static inline bool derived_table_optimization_done(TABLE_LIST *table)
9252 {
9253   return table->derived &&
9254       (table->derived->is_excluded() ||
9255        table->is_materialized_derived());
9256 }
9257 
9258 
9259 /**
9260   @brief
9261   Initialize this derived table/view
9262 
9263   @param thd  Thread handle
9264 
9265   @details
9266   This function makes initial preparations of this derived table/view for
9267   further processing:
9268     if it's a derived table this function marks it either as mergeable or
9269       materializable
9270     creates temporary table for name resolution purposes
9271     creates field translation for mergeable derived table/view
9272 
9273   @return TRUE  an error occur
9274   @return FALSE ok
9275 */
9276 
init_derived(THD * thd,bool init_view)9277 bool TABLE_LIST::init_derived(THD *thd, bool init_view)
9278 {
9279   SELECT_LEX *first_select= get_single_select();
9280   SELECT_LEX_UNIT *unit= get_unit();
9281 
9282   if (!unit)
9283     return FALSE;
9284   /*
9285     Check whether we can merge this derived table into main select.
9286     Depending on the result field translation will or will not
9287     be created.
9288   */
9289   TABLE_LIST *first_table= (TABLE_LIST *) first_select->table_list.first;
9290   if (first_select->table_list.elements > 1 ||
9291       (first_table && first_table->is_multitable()))
9292     set_multitable();
9293 
9294   if (!unit->derived)
9295     unit->derived= this;
9296   else if (!is_with_table_recursive_reference() && unit->derived != this)
9297   {
9298     if (unit->derived->is_with_table_recursive_reference())
9299       unit->derived= this;
9300     else if (vers_conditions.eq(unit->derived->vers_conditions))
9301       vers_conditions.empty();
9302     else
9303     {
9304       my_error(ER_CONFLICTING_FOR_SYSTEM_TIME, MYF(0));
9305       return TRUE;
9306     }
9307   }
9308 
9309   if (init_view && !view &&
9310       !derived_table_optimization_done(this))
9311   {
9312     /* This is all what we can do for a derived table for now. */
9313     set_derived();
9314   }
9315 
9316   if (!is_view() &&
9317       !derived_table_optimization_done(this))
9318   {
9319     /* A subquery might be forced to be materialized due to a side-effect. */
9320     if (!is_materialized_derived() && first_select->is_mergeable() &&
9321         optimizer_flag(thd, OPTIMIZER_SWITCH_DERIVED_MERGE) &&
9322         !thd->lex->can_not_use_merged() &&
9323         !(thd->lex->sql_command == SQLCOM_UPDATE_MULTI ||
9324           thd->lex->sql_command == SQLCOM_DELETE_MULTI) &&
9325         !is_recursive_with_table())
9326       set_merged_derived();
9327     else
9328       set_materialized_derived();
9329   }
9330   /*
9331     Derived tables/view are materialized prior to UPDATE, thus we can skip
9332     them from table uniqueness check
9333   */
9334   if (is_materialized_derived())
9335   {
9336     set_check_materialized();
9337   }
9338 
9339   /*
9340     Create field translation for mergeable derived tables/views.
9341     For derived tables field translation can be created only after
9342     unit is prepared so all '*' are get unrolled.
9343   */
9344   if (is_merged_derived())
9345   {
9346     if (is_view() ||
9347         (unit->prepared &&
9348 	!(thd->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW)))
9349       create_field_translation(thd);
9350   }
9351 
9352   return FALSE;
9353 }
9354 
9355 
9356 /**
9357   @brief
9358   Retrieve number of rows in the table
9359 
9360   @details
9361   Retrieve number of rows in the table referred by this TABLE_LIST and
9362   store it in the table's stats.records variable. If this TABLE_LIST refers
9363   to a materialized derived table/view then the estimated number of rows of
9364   the derived table/view is used instead.
9365 
9366   @return 0          ok
9367   @return non zero   error
9368 */
9369 
fetch_number_of_rows()9370 int TABLE_LIST::fetch_number_of_rows()
9371 {
9372   int error= 0;
9373   if (jtbm_subselect)
9374   {
9375     if (jtbm_subselect->is_jtbm_merged)
9376     {
9377       table->file->stats.records= (ha_rows)jtbm_subselect->jtbm_record_count;
9378       set_if_bigger(table->file->stats.records, 2);
9379       table->used_stat_records= table->file->stats.records;
9380     }
9381     return 0;
9382   }
9383   if (is_materialized_derived() && !fill_me)
9384   {
9385     table->file->stats.records= get_unit()->result->est_records;
9386     set_if_bigger(table->file->stats.records, 2);
9387     table->used_stat_records= table->file->stats.records;
9388   }
9389   else
9390     error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
9391   return error;
9392 }
9393 
9394 /*
9395   Procedure of keys generation for result tables of materialized derived
9396   tables/views.
9397 
9398   A key is generated for each equi-join pair derived table-another table.
9399   Each generated key consists of fields of derived table used in equi-join.
9400   Example:
9401 
9402     SELECT * FROM (SELECT * FROM t1 GROUP BY 1) tt JOIN
9403                   t1 ON tt.f1=t1.f3 and tt.f2.=t1.f4;
9404   In this case for the derived table tt one key will be generated. It will
9405   consist of two parts f1 and f2.
9406   Example:
9407 
9408     SELECT * FROM (SELECT * FROM t1 GROUP BY 1) tt JOIN
9409                   t1 ON tt.f1=t1.f3 JOIN
9410                   t2 ON tt.f2=t2.f4;
9411   In this case for the derived table tt two keys will be generated.
9412   One key over f1 field, and another key over f2 field.
9413   Currently optimizer may choose to use only one such key, thus the second
9414   one will be dropped after range optimizer is finished.
9415   See also JOIN::drop_unused_derived_keys function.
9416   Example:
9417 
9418     SELECT * FROM (SELECT * FROM t1 GROUP BY 1) tt JOIN
9419                   t1 ON tt.f1=a_function(t1.f3);
9420   In this case for the derived table tt one key will be generated. It will
9421   consist of one field - f1.
9422 */
9423 
9424 
9425 
9426 /*
9427   @brief
9428   Change references to underlying items of a merged derived table/view
9429   for fields in derived table's result table.
9430 
9431   @return FALSE ok
9432   @return TRUE  Out of memory
9433 */
change_refs_to_fields()9434 bool TABLE_LIST::change_refs_to_fields()
9435 {
9436   List_iterator<Item> li(used_items);
9437   Item_direct_ref *ref;
9438   Field_iterator_view field_it;
9439   THD *thd= table->in_use;
9440   DBUG_ASSERT(is_merged_derived());
9441 
9442   if (!used_items.elements)
9443     return FALSE;
9444 
9445   Item **materialized_items=
9446       (Item **)thd->calloc(sizeof(void *) * table->s->fields);
9447   if (!materialized_items)
9448     return TRUE;
9449 
9450   while ((ref= (Item_direct_ref*)li++))
9451   {
9452     uint idx;
9453     Item *orig_item= *ref->ref;
9454     field_it.set(this);
9455     for (idx= 0; !field_it.end_of_fields(); field_it.next(), idx++)
9456     {
9457       if (field_it.item() == orig_item)
9458         break;
9459     }
9460     DBUG_ASSERT(!field_it.end_of_fields());
9461     if (!materialized_items[idx])
9462     {
9463       materialized_items[idx]= new (thd->mem_root) Item_field(thd, table->field[idx]);
9464       if (!materialized_items[idx])
9465         return TRUE;
9466     }
9467     /*
9468       We need to restore the pointers after the execution of the
9469       prepared statement.
9470     */
9471     thd->change_item_tree((Item **)&ref->ref,
9472                           (Item*)(materialized_items + idx));
9473   }
9474 
9475   return FALSE;
9476 }
9477 
9478 
set_lock_type(THD * thd,enum thr_lock_type lock)9479 void TABLE_LIST::set_lock_type(THD *thd, enum thr_lock_type lock)
9480 {
9481   if (check_stack_overrun(thd, STACK_MIN_SIZE, (uchar *)&lock))
9482     return;
9483   /* we call it only when table is opened and it is "leaf" table*/
9484   DBUG_ASSERT(table);
9485   lock_type= lock;
9486   /* If not derived tables */
9487   if (table->file && table->file->is_open())
9488     table->file->set_lock_type(lock);
9489   if (is_merged_derived())
9490   {
9491     for (TABLE_LIST *table= get_single_select()->get_table_list();
9492          table;
9493          table= table->next_local)
9494     {
9495       table->set_lock_type(thd, lock);
9496     }
9497   }
9498 }
9499 
is_with_table()9500 bool TABLE_LIST::is_with_table()
9501 {
9502   return derived && derived->with_element;
9503 }
9504 
actual_n_key_parts(THD * thd)9505 uint TABLE_SHARE::actual_n_key_parts(THD *thd)
9506 {
9507   return use_ext_keys &&
9508          optimizer_flag(thd, OPTIMIZER_SWITCH_EXTENDED_KEYS) ?
9509            ext_key_parts : key_parts;
9510 }
9511 
9512 
actual_rec_per_key(uint i)9513 double KEY::actual_rec_per_key(uint i)
9514 {
9515   if (rec_per_key == 0)
9516     return 0;
9517   return (is_statistics_from_stat_tables ?
9518           read_stats->get_avg_frequency(i) : (double) rec_per_key[i]);
9519 }
9520 
9521 /*
9522    find total number of field in hash expr
9523 */
fields_in_hash_keyinfo(KEY * keyinfo)9524 int fields_in_hash_keyinfo(KEY *keyinfo)
9525 {
9526   Item_func_hash * temp= (Item_func_hash *)
9527                      keyinfo->key_part->field->vcol_info->expr;
9528   return temp->argument_count();
9529 }
9530 /*
9531   setup_keyinfo_hash changes the key_info->key_part
9532   to be same as defined by user
9533  */
setup_keyinfo_hash(KEY * key_info)9534 void setup_keyinfo_hash(KEY *key_info)
9535 {
9536   DBUG_ASSERT(key_info->algorithm == HA_KEY_ALG_LONG_HASH);
9537   DBUG_ASSERT(key_info->key_part->field->flags & LONG_UNIQUE_HASH_FIELD);
9538   uint no_of_keyparts= fields_in_hash_keyinfo(key_info);
9539   key_info->key_part-= no_of_keyparts;
9540   key_info->user_defined_key_parts= key_info->usable_key_parts=
9541                key_info->ext_key_parts= no_of_keyparts;
9542   key_info->flags|= HA_NOSAME;
9543 }
9544 /*
9545   re_setup_keyinfo_hash reverts th setup_keyinfo_hash and this type of
9546   arrangement is expected by storage engine
9547  */
9548 
re_setup_keyinfo_hash(KEY * key_info)9549 void re_setup_keyinfo_hash(KEY *key_info)
9550 {
9551   DBUG_ASSERT(key_info->algorithm == HA_KEY_ALG_LONG_HASH);
9552   DBUG_ASSERT(!(key_info->key_part->field->flags & LONG_UNIQUE_HASH_FIELD));
9553   while(!(key_info->key_part->field->flags & LONG_UNIQUE_HASH_FIELD))
9554     key_info->key_part++;
9555   key_info->user_defined_key_parts= key_info->usable_key_parts=
9556                key_info->ext_key_parts= 1;
9557   key_info->flags&= ~HA_NOSAME;
9558 }
9559 
fk_option_name(enum_fk_option opt)9560 LEX_CSTRING *fk_option_name(enum_fk_option opt)
9561 {
9562   static LEX_CSTRING names[]=
9563   {
9564     { STRING_WITH_LEN("???") },
9565     { STRING_WITH_LEN("RESTRICT") },
9566     { STRING_WITH_LEN("CASCADE") },
9567     { STRING_WITH_LEN("SET NULL") },
9568     { STRING_WITH_LEN("NO ACTION") },
9569     { STRING_WITH_LEN("SET DEFAULT") }
9570   };
9571   return names + opt;
9572 }
9573 
fk_modifies_child(enum_fk_option opt)9574 bool fk_modifies_child(enum_fk_option opt)
9575 {
9576   static bool can_write[]= { false, false, true, true, false, true };
9577   return can_write[opt];
9578 }
9579 
9580 enum TR_table::enabled TR_table::use_transaction_registry= TR_table::MAYBE;
9581 
TR_table(THD * _thd,bool rw)9582 TR_table::TR_table(THD* _thd, bool rw) :
9583   thd(_thd), open_tables_backup(NULL)
9584 {
9585   init_one_table(&MYSQL_SCHEMA_NAME, &TRANSACTION_REG_NAME,
9586                  NULL, rw ? TL_WRITE : TL_READ);
9587 }
9588 
open()9589 bool TR_table::open()
9590 {
9591   DBUG_ASSERT(!table);
9592   open_tables_backup= new Open_tables_backup;
9593   if (!open_tables_backup)
9594   {
9595     my_error(ER_OUT_OF_RESOURCES, MYF(0));
9596     return true;
9597   }
9598 
9599   All_tmp_tables_list *temporary_tables= thd->temporary_tables;
9600   bool error= !open_log_table(thd, this, open_tables_backup);
9601   thd->temporary_tables= temporary_tables;
9602 
9603   if (use_transaction_registry == MAYBE)
9604     error= check(error);
9605 
9606   use_transaction_registry= error ? NO : YES;
9607 
9608   return error;
9609 }
9610 
~TR_table()9611 TR_table::~TR_table()
9612 {
9613   if (table)
9614   {
9615     thd->temporary_tables= NULL;
9616     close_log_table(thd, open_tables_backup);
9617   }
9618   delete open_tables_backup;
9619 }
9620 
store(uint field_id,ulonglong val)9621 void TR_table::store(uint field_id, ulonglong val)
9622 {
9623   table->field[field_id]->store(val, true);
9624   table->field[field_id]->set_notnull();
9625 }
9626 
store(uint field_id,timeval ts)9627 void TR_table::store(uint field_id, timeval ts)
9628 {
9629   table->field[field_id]->store_timestamp(ts.tv_sec, ts.tv_usec);
9630   table->field[field_id]->set_notnull();
9631 }
9632 
iso_level() const9633 enum_tx_isolation TR_table::iso_level() const
9634 {
9635   enum_tx_isolation res= (enum_tx_isolation) ((*this)[FLD_ISO_LEVEL]->val_int() - 1);
9636   DBUG_ASSERT(res <= ISO_SERIALIZABLE);
9637   return res;
9638 }
9639 
update(ulonglong start_id,ulonglong end_id)9640 bool TR_table::update(ulonglong start_id, ulonglong end_id)
9641 {
9642   if (!table && open())
9643     return true;
9644 
9645   store(FLD_BEGIN_TS, thd->transaction_time());
9646   thd->set_time();
9647   timeval end_time= {thd->query_start(), int(thd->query_start_sec_part())};
9648   store(FLD_TRX_ID, start_id);
9649   store(FLD_COMMIT_ID, end_id);
9650   store(FLD_COMMIT_TS, end_time);
9651   store_iso_level(thd->tx_isolation);
9652 
9653   int error= table->file->ha_write_row(table->record[0]);
9654   if (unlikely(error))
9655     table->file->print_error(error, MYF(0));
9656   return error;
9657 }
9658 
9659 #define newx new (thd->mem_root)
query(ulonglong trx_id)9660 bool TR_table::query(ulonglong trx_id)
9661 {
9662   if (!table && open())
9663     return false;
9664   SQL_SELECT_auto select;
9665   READ_RECORD info;
9666   int error;
9667   List<TABLE_LIST> dummy;
9668   SELECT_LEX &slex= *(thd->lex->first_select_lex());
9669   Name_resolution_context_backup backup(slex.context, *this);
9670   Item *field= newx Item_field(thd, &slex.context, (*this)[FLD_TRX_ID]);
9671   Item *value= newx Item_int(thd, trx_id);
9672   COND *conds= newx Item_func_eq(thd, field, value);
9673   if (unlikely((error= setup_conds(thd, this, dummy, &conds))))
9674     return false;
9675   select= make_select(table, 0, 0, conds, NULL, 0, &error);
9676   if (unlikely(error || !select))
9677   {
9678     my_error(ER_OUT_OF_RESOURCES, MYF(0));
9679     return false;
9680   }
9681   // FIXME: (performance) force index 'transaction_id'
9682   error= init_read_record(&info, thd, table, select, NULL,
9683                           1 /* use_record_cache */, true /* print_error */,
9684                           false /* disable_rr_cache */);
9685   while (!(error= info.read_record()) && !thd->killed && !thd->is_error())
9686   {
9687     if (select->skip_record(thd) > 0)
9688       return true;
9689   }
9690   my_error(ER_VERS_NO_TRX_ID, MYF(0), (longlong) trx_id);
9691   return false;
9692 }
9693 
query(MYSQL_TIME & commit_time,bool backwards)9694 bool TR_table::query(MYSQL_TIME &commit_time, bool backwards)
9695 {
9696   if (!table && open())
9697     return false;
9698   SQL_SELECT_auto select;
9699   READ_RECORD info;
9700   int error;
9701   List<TABLE_LIST> dummy;
9702   SELECT_LEX &slex= *(thd->lex->first_select_lex());
9703   Name_resolution_context_backup backup(slex.context, *this);
9704   Item *field= newx Item_field(thd, &slex.context, (*this)[FLD_COMMIT_TS]);
9705   Datetime dt(&commit_time);
9706   Item *value= newx Item_datetime_literal(thd, &dt, 6);
9707   COND *conds;
9708   if (backwards)
9709     conds= newx Item_func_ge(thd, field, value);
9710   else
9711     conds= newx Item_func_le(thd, field, value);
9712   if (unlikely((error= setup_conds(thd, this, dummy, &conds))))
9713     return false;
9714   // FIXME: (performance) force index 'commit_timestamp'
9715   select= make_select(table, 0, 0, conds, NULL, 0, &error);
9716   if (unlikely(error || !select))
9717     return false;
9718   error= init_read_record(&info, thd, table, select, NULL,
9719                           1 /* use_record_cache */, true /* print_error */,
9720                           false /* disable_rr_cache */);
9721 
9722   // With PK by transaction_id the records are ordered by PK, so we have to
9723   // scan TRT fully and collect min (backwards == true)
9724   // or max (backwards == false) stats.
9725   bool found= false;
9726   MYSQL_TIME found_ts;
9727   while (!(error= info.read_record()) && !thd->killed && !thd->is_error())
9728   {
9729     int res= select->skip_record(thd);
9730     if (res > 0)
9731     {
9732       MYSQL_TIME commit_ts;
9733       if ((*this)[FLD_COMMIT_TS]->get_date(&commit_ts, date_mode_t(0)))
9734       {
9735         found= false;
9736         break;
9737       }
9738       int c;
9739       if (!found || ((c= my_time_compare(&commit_ts, &found_ts)) &&
9740         (backwards ? c < 0 : c > 0)))
9741       {
9742         found_ts= commit_ts;
9743         found= true;
9744         // TODO: (performance) make ORDER DESC and break after first found.
9745         // Otherwise it is O(n) scan (+copy)!
9746         store_record(table, record[1]);
9747       }
9748     }
9749     else if (res < 0)
9750     {
9751       found= false;
9752       break;
9753     }
9754  }
9755   if (found)
9756     restore_record(table, record[1]);
9757   return found;
9758 }
9759 #undef newx
9760 
query_sees(bool & result,ulonglong trx_id1,ulonglong trx_id0,ulonglong commit_id1,enum_tx_isolation iso_level1,ulonglong commit_id0)9761 bool TR_table::query_sees(bool &result, ulonglong trx_id1, ulonglong trx_id0,
9762                           ulonglong commit_id1, enum_tx_isolation iso_level1,
9763                           ulonglong commit_id0)
9764 {
9765   if (trx_id1 == trx_id0)
9766   {
9767     return false;
9768   }
9769 
9770   if (trx_id1 == ULONGLONG_MAX || trx_id0 == 0)
9771   {
9772     result= true;
9773     return false;
9774   }
9775 
9776   if (trx_id0 == ULONGLONG_MAX || trx_id1 == 0)
9777   {
9778     result= false;
9779     return false;
9780   }
9781 
9782   if (!commit_id1)
9783   {
9784     if (!query(trx_id1))
9785       return true;
9786 
9787     commit_id1= (*this)[FLD_COMMIT_ID]->val_int();
9788     iso_level1= iso_level();
9789   }
9790 
9791   if (!commit_id0)
9792   {
9793     if (!query(trx_id0))
9794       return true;
9795 
9796     commit_id0= (*this)[FLD_COMMIT_ID]->val_int();
9797   }
9798 
9799   // Trivial case: TX1 started after TX0 committed
9800   if (trx_id1 > commit_id0
9801       // Concurrent transactions: TX1 committed after TX0 and TX1 is read (un)committed
9802       || (commit_id1 > commit_id0 && iso_level1 < ISO_REPEATABLE_READ))
9803   {
9804     result= true;
9805   }
9806   else // All other cases: TX1 does not see TX0
9807   {
9808     result= false;
9809   }
9810 
9811   return false;
9812 }
9813 
warn_schema_incorrect(const char * reason)9814 void TR_table::warn_schema_incorrect(const char *reason)
9815 {
9816   if (MYSQL_VERSION_ID == table->s->mysql_version)
9817   {
9818     sql_print_error("%`s.%`s schema is incorrect: %s.",
9819                     db.str, table_name.str, reason);
9820   }
9821   else
9822   {
9823     sql_print_error("%`s.%`s schema is incorrect: %s. Created with MariaDB %d, "
9824                     "now running %d.",
9825                     db.str, table_name.str, reason, MYSQL_VERSION_ID,
9826                     static_cast<int>(table->s->mysql_version));
9827   }
9828 }
9829 
check(bool error)9830 bool TR_table::check(bool error)
9831 {
9832   if (error)
9833   {
9834     sql_print_warning("%`s.%`s does not exist (open failed).", db.str,
9835                       table_name.str);
9836     return true;
9837   }
9838 
9839   if (table->file->ht->db_type != DB_TYPE_INNODB)
9840   {
9841     warn_schema_incorrect("Wrong table engine (expected InnoDB)");
9842     return true;
9843   }
9844 
9845 #define WARN_SCHEMA(...) \
9846   char reason[128]; \
9847   snprintf(reason, 128, __VA_ARGS__); \
9848   warn_schema_incorrect(reason);
9849 
9850   if (table->s->fields != FIELD_COUNT)
9851   {
9852     WARN_SCHEMA("Wrong field count (expected %d)", FIELD_COUNT);
9853     return true;
9854   }
9855 
9856   if (table->field[FLD_TRX_ID]->type() != MYSQL_TYPE_LONGLONG)
9857   {
9858     WARN_SCHEMA("Wrong field %d type (expected BIGINT UNSIGNED)", FLD_TRX_ID);
9859     return true;
9860   }
9861 
9862   if (table->field[FLD_COMMIT_ID]->type() != MYSQL_TYPE_LONGLONG)
9863   {
9864     WARN_SCHEMA("Wrong field %d type (expected BIGINT UNSIGNED)", FLD_COMMIT_ID);
9865     return true;
9866   }
9867 
9868   if (table->field[FLD_BEGIN_TS]->type() != MYSQL_TYPE_TIMESTAMP)
9869   {
9870     WARN_SCHEMA("Wrong field %d type (expected TIMESTAMP(6))", FLD_BEGIN_TS);
9871     return true;
9872   }
9873 
9874   if (table->field[FLD_COMMIT_TS]->type() != MYSQL_TYPE_TIMESTAMP)
9875   {
9876     WARN_SCHEMA("Wrong field %d type (expected TIMESTAMP(6))", FLD_COMMIT_TS);
9877     return true;
9878   }
9879 
9880   if (table->field[FLD_ISO_LEVEL]->type() != MYSQL_TYPE_STRING ||
9881       !(table->field[FLD_ISO_LEVEL]->flags & ENUM_FLAG))
9882   {
9883   wrong_enum:
9884     WARN_SCHEMA("Wrong field %d type (expected ENUM('READ-UNCOMMITTED', "
9885                 "'READ-COMMITTED', 'REPEATABLE-READ', 'SERIALIZABLE'))",
9886                 FLD_ISO_LEVEL);
9887     return true;
9888   }
9889 
9890   Field_enum *iso_level= static_cast<Field_enum *>(table->field[FLD_ISO_LEVEL]);
9891   const st_typelib *typelib= iso_level->typelib;
9892 
9893   if (typelib->count != 4)
9894     goto wrong_enum;
9895 
9896   if (strcmp(typelib->type_names[0], "READ-UNCOMMITTED") ||
9897       strcmp(typelib->type_names[1], "READ-COMMITTED") ||
9898       strcmp(typelib->type_names[2], "REPEATABLE-READ") ||
9899       strcmp(typelib->type_names[3], "SERIALIZABLE"))
9900   {
9901     goto wrong_enum;
9902   }
9903 
9904   if (!table->key_info || !table->key_info->key_part)
9905     goto wrong_pk;
9906 
9907   if (strcmp(table->key_info->key_part->field->field_name.str, "transaction_id"))
9908   {
9909   wrong_pk:
9910     WARN_SCHEMA("Wrong PRIMARY KEY (expected `transaction_id`)");
9911     return true;
9912   }
9913 
9914   return false;
9915 }
9916 
check_units(THD * thd)9917 bool vers_select_conds_t::check_units(THD *thd)
9918 {
9919   DBUG_ASSERT(type != SYSTEM_TIME_UNSPECIFIED);
9920   DBUG_ASSERT(start.item);
9921   return start.check_unit(thd) ||
9922          end.check_unit(thd);
9923 }
9924 
eq(const vers_select_conds_t & conds) const9925 bool vers_select_conds_t::eq(const vers_select_conds_t &conds) const
9926 {
9927   if (type != conds.type)
9928     return false;
9929   switch (type) {
9930   case SYSTEM_TIME_UNSPECIFIED:
9931   case SYSTEM_TIME_ALL:
9932     return true;
9933   case SYSTEM_TIME_BEFORE:
9934     break;
9935   case SYSTEM_TIME_HISTORY:
9936     break;
9937   case SYSTEM_TIME_AS_OF:
9938     return start.eq(conds.start);
9939   case SYSTEM_TIME_FROM_TO:
9940   case SYSTEM_TIME_BETWEEN:
9941     return start.eq(conds.start) && end.eq(conds.end);
9942   }
9943   DBUG_ASSERT(0);
9944   return false;
9945 }
9946 
9947 
check_unit(THD * thd)9948 bool Vers_history_point::check_unit(THD *thd)
9949 {
9950   if (!item)
9951     return false;
9952   if (item->fix_fields_if_needed(thd, &item))
9953     return true;
9954   const Type_handler *t= item->this_item()->real_type_handler();
9955   DBUG_ASSERT(t);
9956   if (!t->vers())
9957   {
9958     my_error(ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION, MYF(0),
9959              t->name().ptr(), "FOR SYSTEM_TIME");
9960     return true;
9961   }
9962   return false;
9963 }
9964 
9965 
fix_item()9966 void Vers_history_point::fix_item()
9967 {
9968   if (item && item->decimals == 0 && item->type() == Item::FUNC_ITEM &&
9969       ((Item_func*)item)->functype() == Item_func::NOW_FUNC)
9970     item->decimals= 6;
9971 }
9972 
9973 
eq(const vers_history_point_t & point) const9974 bool Vers_history_point::eq(const vers_history_point_t &point) const
9975 {
9976   return unit == point.unit && item->eq(point.item, false);
9977 }
9978 
print(String * str,enum_query_type query_type,const char * prefix,size_t plen) const9979 void Vers_history_point::print(String *str, enum_query_type query_type,
9980                                const char *prefix, size_t plen) const
9981 {
9982   const static LEX_CSTRING unit_type[]=
9983   {
9984     { STRING_WITH_LEN("") },
9985     { STRING_WITH_LEN("TIMESTAMP ") },
9986     { STRING_WITH_LEN("TRANSACTION ") }
9987   };
9988   str->append(prefix, plen);
9989   str->append(unit_type + unit);
9990   item->print(str, query_type);
9991 }
9992 
find_field_by_name(LEX_CSTRING * str) const9993 Field *TABLE::find_field_by_name(LEX_CSTRING *str) const
9994 {
9995   Field **tmp;
9996   size_t length= str->length;
9997   if (s->name_hash.records)
9998   {
9999     tmp= (Field**) my_hash_search(&s->name_hash, (uchar*) str->str, length);
10000     return tmp ? field[tmp - s->field] : NULL;
10001   }
10002   else
10003   {
10004     for (tmp= field; *tmp; tmp++)
10005     {
10006       if ((*tmp)->field_name.length == length &&
10007           !lex_string_cmp(system_charset_info, &(*tmp)->field_name, str))
10008         return *tmp;
10009     }
10010   }
10011   return NULL;
10012 }
10013 
10014 
export_structure(THD * thd,Row_definition_list * defs)10015 bool TABLE::export_structure(THD *thd, Row_definition_list *defs)
10016 {
10017   for (Field **src= field; *src; src++)
10018   {
10019     uint offs;
10020     if (defs->find_row_field_by_name(&src[0]->field_name, &offs))
10021     {
10022       my_error(ER_DUP_FIELDNAME, MYF(0), src[0]->field_name.str);
10023       return true;
10024     }
10025     Spvar_definition *def= new (thd->mem_root) Spvar_definition(thd, *src);
10026     if (!def)
10027       return true;
10028     def->flags&= (uint) ~NOT_NULL_FLAG;
10029     if ((def->sp_prepare_create_field(thd, thd->mem_root)) ||
10030         (defs->push_back(def, thd->mem_root)))
10031       return true;
10032   }
10033   return false;
10034 }
10035 
10036 /**
10037   @brief
10038     Initialize all the opt_range structures that are used to stored the
10039     estimates when the range optimizer is run.
10040     As these are initialized by the range optimizer for all index
10041     marked in opt_range_keys, we only mark the memory as undefined
10042     to be able to find wrong usage of data with valgrind or MSAN.
10043 */
10044 
initialize_opt_range_structures()10045 inline void TABLE::initialize_opt_range_structures()
10046 {
10047   TRASH_ALLOC((void*)&opt_range_keys, sizeof(opt_range_keys));
10048   TRASH_ALLOC(opt_range, s->keys * sizeof(*opt_range));
10049   TRASH_ALLOC(const_key_parts, s->keys * sizeof(*const_key_parts));
10050 }
10051 
10052 /*
10053   Mark table to be reopened after query
10054 */
10055 
mark_table_for_reopen()10056 void TABLE::mark_table_for_reopen()
10057 {
10058   THD *thd= in_use;
10059   DBUG_ASSERT(thd);
10060   thd->locked_tables_list.mark_table_for_reopen(thd, this);
10061 }
10062