MySQL  8.0.21
Source Code Documentation
dd_table.h
Go to the documentation of this file.
1 /* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef DD_TABLE_INCLUDED
24 #define DD_TABLE_INCLUDED
25 
26 #include <sys/types.h>
27 #include <memory> // std:unique_ptr
28 #include <string>
29 
30 #include "my_inttypes.h"
31 #include "sql/dd/result_type.h" // dd::ResultType
32 #include "sql/dd/string_type.h"
33 #include "sql/dd/types/column.h" // dd::enum_column_types
34 #include "sql/handler.h" // legacy_db_type
35 #include "sql/sql_alter.h" // Alter_info::enum_enable_or_disable
36 
37 class Create_field;
38 class FOREIGN_KEY;
39 class KEY;
40 class THD;
41 namespace dd {
42 class Schema;
43 } // namespace dd
44 struct TABLE;
45 
49 
50 struct HA_CREATE_INFO;
51 template <class T>
52 class List;
53 
54 namespace dd {
55 class Abstract_table;
56 class Foreign_key;
57 class Table;
58 
59 namespace cache {
60 class Dictionary_client;
61 }
62 
63 static const char FIELD_NAME_SEPARATOR_CHAR = ';';
64 static const char CHECK_CONSTRAINT_NAME_SUBSTR[] = "_chk_";
65 
66 /**
67  Prepares a dd::Table object from mysql_prepare_create_table() output
68  and return it to the caller. This function creates a user table, as
69  opposed to create_table() which can handle system tables as well.
70 
71  @param thd Thread handle
72  @param sch_obj Schema.
73  @param table_name Table name.
74  @param create_info HA_CREATE_INFO describing the table to be created.
75  @param create_fields List of fields for the table.
76  @param keyinfo Array with descriptions of keys for the table.
77  @param keys Number of keys.
78  @param keys_onoff keys ON or OFF
79  @param fk_keyinfo Array with descriptions of foreign keys for the
80  table.
81  @param fk_keys Number of foreign keys.
82  @param check_cons_spec Specification of check constraints for the table.
83  @param file handler instance for the table.
84 
85  @returns Constructed dd::Table object, or nullptr in case of an error.
86 */
87 std::unique_ptr<dd::Table> create_dd_user_table(
88  THD *thd, const dd::Schema &sch_obj, const dd::String_type &table_name,
89  HA_CREATE_INFO *create_info, const List<Create_field> &create_fields,
90  const KEY *keyinfo, uint keys,
92  const FOREIGN_KEY *fk_keyinfo, uint fk_keys,
93  const Sql_check_constraint_spec_list *check_cons_spec, handler *file);
94 
95 /**
96  Prepares a dd::Table object from mysql_prepare_create_table() output
97  and return it to the caller.
98 
99  @param thd Thread handle
100  @param sch_obj Schema.
101  @param table_name Table name.
102  @param create_info HA_CREATE_INFO describing the table to be created.
103  @param create_fields List of fields for the table.
104  @param keyinfo Array with descriptions of keys for the table.
105  @param keys Number of keys.
106  @param keys_onoff Enable or disable keys.
107  @param fk_keyinfo Array with descriptions of foreign keys for the
108  table.
109  @param fk_keys Number of foreign keys.
110  @param check_cons_spec Specification of check constraints for the table.
111  @param file handler instance for the table.
112 
113  @returns Constructed dd::Table object, or nullptr in case of an error.
114 */
115 std::unique_ptr<dd::Table> create_table(
116  THD *thd, const dd::Schema &sch_obj, const dd::String_type &table_name,
117  HA_CREATE_INFO *create_info, const List<Create_field> &create_fields,
118  const KEY *keyinfo, uint keys,
120  const FOREIGN_KEY *fk_keyinfo, uint fk_keys,
121  const Sql_check_constraint_spec_list *check_cons_spec, handler *file);
122 
123 /**
124  Prepares a dd::Table object for a temporary table from
125  mysql_prepare_create_table() output. Doesn't update DD tables,
126  instead returns dd::Table object to caller.
127 
128  @param thd Thread handle.
129  @param sch_obj Schema.
130  @param table_name Table name.
131  @param create_info HA_CREATE_INFO describing the table to be created.
132  @param create_fields List of fields for the table.
133  @param keyinfo Array with descriptions of keys for the table.
134  @param keys Number of keys.
135  @param keys_onoff Enable or disable keys.
136  @param check_cons_spec Specification of check constraints for the table.
137  @param file handler instance for the table.
138 
139  @returns Constructed dd::Table object, or nullptr in case of an error.
140 */
141 std::unique_ptr<dd::Table> create_tmp_table(
142  THD *thd, const dd::Schema &sch_obj, const dd::String_type &table_name,
143  HA_CREATE_INFO *create_info, const List<Create_field> &create_fields,
144  const KEY *keyinfo, uint keys,
146  const Sql_check_constraint_spec_list *check_cons_spec, handler *file);
147 
148 //////////////////////////////////////////////////////////////////////////
149 // Function common to 'table' and 'view' objects
150 //////////////////////////////////////////////////////////////////////////
151 
152 /*
153  Remove table metadata from the data dictionary.
154 
155  @param thd Thread context.
156  @param schema_name Schema of the table to be removed.
157  @param name Name of the table to be removed.
158  @param table_def dd::Table object for the table to be removed.
159 
160  @note The caller must rollback both statement and transaction on failure,
161  before any further accesses to DD. This is because such a failure
162  might be caused by a deadlock, which requires rollback before any
163  other operations on SE (including reads using attachable transactions)
164  can be done.
165 
166  @retval false on success
167  @retval true on failure
168 */
169 bool drop_table(THD *thd, const char *schema_name, const char *name,
170  const dd::Table &table_def);
171 /**
172  Check if a table or view exists
173 
174  table_exists() sets exists=true if such a table or a view exists.
175 
176  @param client The dictionary client.
177  @param schema_name The schema in which the object should be defined.
178  @param name The object name to search for.
179  @param [out] exists A boolean which is set to true if the object
180  is found.
181 
182  @retval true Failure (error has been reported).
183  @retval false Success.
184 */
185 bool table_exists(dd::cache::Dictionary_client *client, const char *schema_name,
186  const char *name, bool *exists);
187 
188 /**
189  Checking if the table is being created in a restricted
190  tablespace.
191 
192  @param thd Thread context.
193  @param schema_name Name of the schema where tha table is located.
194  @param table_name Name of the table.
195  @param create_info Table options, e.g. the tablespace name.
196 
197  @retval true Invalid usage (error has been reported).
198  @retval false Success, no invalid usage.
199 */
200 bool invalid_tablespace_usage(THD *thd, const dd::String_type &schema_name,
202  const HA_CREATE_INFO *create_info);
203 
204 /**
205  Check if foreign key name is generated one.
206 
207  @param table_name Table name.
208  @param table_name_length Table name length.
209  @param hton Table storage engine.
210  @param fk Foreign key to be checked.
211 
212  @note We assume that the name is generated if it starts with
213  (table name)(SE-specific or default foreign key name suffix)
214  (e.g. "_ibfk_" for InnoDB or "_fk_" for NDB).
215 
216  @returns true if name is generated, false otherwise.
217 */
218 
220  size_t table_name_length, handlerton *hton,
221  const dd::Foreign_key &fk);
222 
223 /**
224  Rename foreign keys which have generated names to
225  match the new name of the table.
226 
227  @param thd Thread context.
228  @param old_db Table's database before rename.
229  @param old_table_name Table name before rename.
230  @param hton Table's storage engine.
231  @param new_db Table's database after rename.
232  @param new_tab New version of the table with new name set.
233 
234  @returns true if error, false otherwise.
235 */
236 
237 bool rename_foreign_keys(THD *thd, const char *old_db,
238  const char *old_table_name, handlerton *hton,
239  const char *new_db, dd::Table *new_tab);
240 
241 //////////////////////////////////////////////////////////////////////////
242 // Functions for retrieving, inspecting and manipulating instances of
243 // dd::Abstract_table, dd::Table and dd::View
244 //////////////////////////////////////////////////////////////////////////
245 
246 /**
247  Get the legacy db type from the options of the given table.
248 
249  This function does not set error codes beyond what is set by the
250  functions it calls.
251 
252  @param[in] thd Thread context
253  @param[in] schema_name Name of the schema
254  @param[in] table_name Name of the table
255  @param[out] db_type Value of the legacy db type option
256 
257  @retval true Error, e.g. name is not a table, or no
258  legacy_db_type in the table's options.
259  In this case, the value of db_type is
260  undefined.
261  @retval false Success
262 */
263 bool table_legacy_db_type(THD *thd, const char *schema_name,
264  const char *table_name, enum legacy_db_type *db_type);
265 
266 /**
267  Get the storage engine handlerton for the given table or tablespace.
268 
269  This function sets explicit error codes if:
270  - The SE is invalid: ER_UNKNOWN_STORAGE_ENGINE
271 
272  @param[in] thd Thread context
273  @param[in] obj dd::Table or a dd::Tablespace object.
274  @param[out] hton Handlerton for the table's storage engine
275 
276  @retval true Error
277  @retval false Success
278 */
279 template <typename T>
280 bool table_storage_engine(THD *thd, const T *obj, handlerton **hton);
281 
282 /**
283  Regenerate a metadata locked table.
284 
285  This function does not set error codes beyond what is set by the
286  functions it calls.
287 
288  @pre There must be an exclusive MDL lock on the table.
289 
290  @param[in] thd Thread context
291  @param[in] schema_name Name of the schema
292  @param[in] table_name Name of the table
293 
294  @retval false Success
295  @retval true Error
296 */
297 bool recreate_table(THD *thd, const char *schema_name, const char *table_name);
298 
299 /**
300  Function prepares string representing columns data type.
301  This is required for IS implementation which uses views on DD tables
302 */
304  uint32 field_length, uint32 decimals,
305  bool maybe_null, bool is_unsigned,
306  const CHARSET_INFO *field_charset);
307 
308 /**
309  Convert field type from MySQL server type to new enum types in DD.
310  We have plans to retain both old and new enum values in DD tables so as
311  to handle client compatibility and information schema requirements.
312 
313  @param[in] type MySQL server field type.
314 
315  @retval field type used by DD framework.
316 */
317 
319 
320 /**
321  Update row format for the table with the value
322  value supplied by caller function.
323 
324  @pre There must be an exclusive MDL lock on the table.
325 
326  @param[in] thd Thread context.
327  @param[in] table Table object for the table.
328  @param[in] correct_row_type row_type to be set.
329 
330  @retval false Success
331  @retval true Error
332 */
333 bool fix_row_type(THD *thd, dd::Table *table, row_type correct_row_type);
334 
335 /**
336  Add column objects to dd::Abstract_table objects according to the
337  list of Create_field objects.
338 
339  @param thd Thread handle.
340  @param tab_obj dd::Table or dd::View's instance.
341  @param create_fields List of Create_field objects to fill
342  dd::Column object(s).
343  @param file handler instance for the table.
344 
345  @retval false On Success
346  @retval true On error.
347 */
348 
350  const List<Create_field> &create_fields,
351  handler *file);
352 
353 /**
354  @brief Function returns string representing column type by Create_field.
355  This is required for the IS implementation which uses views on DD
356  tables
357 
358  @param[in] table TABLE object.
359  @param[in] field Column information.
360 
361  @return dd::String_type representing column type.
362 */
363 
365  const Create_field &field);
366 
367 /**
368  Helper method to get numeric scale for types using Create_field type
369  object.
370 
371  @param[in] field Field object.
372  @param[out] scale numeric scale value for types.
373 
374  @retval false If numeric scale is calculated.
375  @retval true If numeric scale is not calculated;
376 */
377 
378 bool get_field_numeric_scale(const Create_field *field, uint *scale);
379 
380 /**
381  Helper method to get numeric precision for types using Create_field type
382  object.
383 
384  @param[in] field Field object.
385  @param[out] numeric_precision numeric precision value for types.
386 
387  @retval false If numeric precision is calculated.
388  @retval true If numeric precision is not calculated;
389 */
390 
391 bool get_field_numeric_precision(const Create_field *field,
392  uint *numeric_precision);
393 
394 /**
395  Helper method to get datetime precision for types using Create_field type
396  object.
397 
398  @param[in] field Field object.
399  @param[out] datetime_precision datetime precision value for types.
400 
401  @retval false If datetime precision is calculated.
402  @retval true If datetime precision is not calculated;
403 */
404 
406  uint *datetime_precision);
407 
408 /*
409  Does ENCRYPTION clause mean unencrypted or encrypted table ?
410 
411  @note SQL server expects value '', 'N' or 'n' to represent unecnryption.
412  We do not consider 'Y'/'y' as encryption, so that this allows storage
413  engines to accept any other string like 'aes' or other string to
414  represent encryption type.
415 
416  @param type - String given in ENCRYPTION clause.
417 
418  @return true if table would be encrypted, else false.
419 */
420 inline bool is_encrypted(const String_type &type) {
421  return (type.empty() == false && type != "" && type != "N" && type != "n");
422 }
423 
424 inline bool is_encrypted(const LEX_STRING &type) {
425  return is_encrypted(String_type(type.str, type.length));
426 }
427 
430  bool *found_tablespace);
431 
434  bool *found_tablespace);
435 
436 /**
437  Predicate which indicates if the table has real (non-hidden) primary key.
438 
439  @param t table to check
440  @return true if a non-hidden index has type dd::Index::IT_PRIMARY
441  */
442 bool has_primary_key(const Table &t);
443 
444 /**
445  Check if name of check constraint is generated one.
446 
447  @param table_name Table name.
448  @param table_name_length Table name length.
449  @param cc_name Check constraint name.
450  @param cc_name_length Check constraint name length.
451 
452  @retval true If check constraint name is generated one.
453  @retval false Otherwise.
454 */
456  size_t table_name_length,
457  const char *cc_name,
458  size_t cc_name_length);
459 
460 /**
461  Rename generated check constraint names to match the new name of the table.
462 
463  @param old_table_name Table name before rename.
464  @param new_tab New version of the table with new name set.
465 
466  @returns true if error, false otherwise.
467 */
468 bool rename_check_constraints(const char *old_table_name, dd::Table *new_tab);
469 
470 /**
471  Check if table uses general tablespace.
472 
473  @param t dd::Table instance.
474 
475  @returns true if table users general tablespace, false otherwise.
476 */
477 bool uses_general_tablespace(const Table &t);
478 
479 /**
480  Throw deprecation warnings if table uses prefix keys in the partitioning
481  function.
482 
483  @param thd Thread handler
484  @param schema_name Schema name
485  @param orig_table_name Original table name (required in case of ALTER TABLE,
486  since temporary table name is created)
487  @param table dd::Table instance
488  @param is_upgrade True if this is called during upgrade. Warning will be
489  sent to error log instead of the client.
490 
491 */
492 void warn_on_deprecated_prefix_key_partition(THD *thd, const char *schema_name,
493  const char *orig_table_name,
494  const Table *table,
495  const bool is_upgrade);
496 
497 } // namespace dd
498 #endif // DD_TABLE_INCLUDED
bool get_field_numeric_precision(const Create_field *field, uint *numeric_precision)
Helper method to get numeric precision for types using Create_field type object.
Definition: dd_table.cc:396
std::unique_ptr< dd::Table > create_dd_user_table(THD *thd, const dd::Schema &sch_obj, const dd::String_type &table_name, HA_CREATE_INFO *create_info, const List< Create_field > &create_fields, const KEY *keyinfo, uint keys, Alter_info::enum_enable_or_disable keys_onoff, const FOREIGN_KEY *fk_keyinfo, uint fk_keys, const Sql_check_constraint_spec_list *check_cons_spec, handler *file)
Prepares a dd::Table object from mysql_prepare_create_table() output and return it to the caller...
Definition: dd_table.cc:2324
dd::enum_column_types get_new_field_type(enum_field_types type)
Convert to and from new enum types in DD framework to current MySQL server enum types.
Definition: dd_table.cc:117
char * str
Definition: mysql_lex_string.h:35
Definition: mysql_lex_string.h:34
bool get_field_datetime_precision(const Create_field *field, uint *datetime_precision)
Helper method to get datetime precision for types using Create_field type object. ...
Definition: dd_table.cc:447
enum_enable_or_disable
Definition: sql_alter.h:334
bool is_generated_foreign_key_name(const char *table_name, size_t table_name_length, handlerton *hton, const dd::Foreign_key &fk)
Check if foreign key name is generated one.
Definition: dd_table.cc:2417
bool fix_row_type(THD *thd, dd::Table *table_def, row_type correct_row_type)
Update row format for the table with the value value supplied by caller function. ...
Definition: dd_table.cc:2645
std::unique_ptr< dd::Table > create_tmp_table(THD *thd, const dd::Schema &sch_obj, const dd::String_type &table_name, HA_CREATE_INFO *create_info, const List< Create_field > &create_fields, const KEY *keyinfo, uint keys, Alter_info::enum_enable_or_disable keys_onoff, const Sql_check_constraint_spec_list *check_cons_spec, handler *file)
Prepares a dd::Table object for a temporary table from mysql_prepare_create_table() output...
Definition: dd_table.cc:2374
Some integer typedefs for easier portability.
Class to represent the check constraint specifications obtained from the SQL statement parse...
Definition: sql_check_constraint.h:42
bool table_exists(dd::cache::Dictionary_client *client, const char *schema_name, const char *name, bool *exists)
Check if a table or view exists.
Definition: dd_table.cc:2400
Char_string_template< String_type_allocator > String_type
Definition: string_type.h:50
void warn_on_deprecated_prefix_key_partition(THD *thd, const char *schema_name, const char *orig_table_name, const Table *table, const bool is_upgrade)
Throw deprecation warnings if table uses prefix keys in the partitioning function.
Definition: dd_table.cc:2923
Encrypt_result is_tablespace_encrypted(THD *thd, const Table &t, bool *is_general_tablespace)
Predicate to determine if a table resides in an encrypted tablespace.
Definition: dd_table.cc:2733
enum_column_types
Definition: column.h:53
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4023
bool table_storage_engine(THD *thd, const T *obj, handlerton **hton)
Get the storage engine handlerton for the given table or tablespace.
Definition: dd_table.cc:2557
Definition: table.h:1313
static int exists(node_address *name, node_list const *nodes, u_int with_uid)
Definition: node_list.cc:101
bool recreate_table(THD *thd, const char *schema_name, const char *table_name)
Regenerate a metadata locked table.
Definition: dd_table.cc:2582
bool is_encrypted(const String_type &type)
Definition: dd_table.h:420
Template for structs to hold results from functions.
Definition: result_type.h:32
Definition: aggregate_check.h:523
Definition: dictionary_client.h:145
static const char FIELD_NAME_SEPARATOR_CHAR
Definition: dd_table.h:63
enum_field_types
Column types for MySQL.
Definition: field_types.h:52
static uint keys
Definition: hp_test2.cc:45
unsigned int uint
Definition: uca-dump.cc:29
case opt name
Definition: sslopt-case.h:32
bool table_legacy_db_type(THD *thd, const char *schema_name, const char *table_name, enum legacy_db_type *db_type)
Get the legacy db type from the options of the given table.
Definition: dd_table.cc:2510
bool has_primary_key(const Table &t)
Predicate which indicates if the table has real (non-hidden) primary key.
Definition: dd_table.cc:2767
legacy_db_type
Definition: handler.h:630
uint32_t uint32
Definition: my_inttypes.h:66
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2293
Definition: m_ctype.h:359
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:389
Abstract base class for tables and views.
Definition: abstract_table.h:68
bool invalid_tablespace_usage(THD *thd, const dd::String_type &schema_name, const dd::String_type &table_name, const HA_CREATE_INFO *create_info)
Checking if the table is being created in a restricted tablespace.
Definition: dd_table.cc:1876
size_t length
Definition: mysql_lex_string.h:36
dd::String_type get_sql_type_by_field_info(THD *thd, enum_field_types field_type, uint32 field_length, uint32 decimals, bool maybe_null, bool is_unsigned, const CHARSET_INFO *field_charset)
Function returns string representing column type by ST_FIELD_INFO.
Definition: dd_table.cc:2624
std::unique_ptr< dd::Table > create_table(THD *thd, const dd::Schema &sch_obj, const dd::String_type &table_name, HA_CREATE_INFO *create_info, const List< Create_field > &create_fields, const KEY *keyinfo, uint keys, Alter_info::enum_enable_or_disable keys_onoff, const FOREIGN_KEY *fk_keyinfo, uint fk_keys, const Sql_check_constraint_spec_list *check_cons_spec, handler *file)
Prepares a dd::Table object from mysql_prepare_create_table() output and return it to the caller...
Definition: dd_table.cc:2354
static MI_KEYDEF keyinfo[10]
Definition: mi_test1.cc:46
bool fill_dd_columns_from_create_fields(THD *thd, dd::Abstract_table *tab_obj, const List< Create_field > &create_fields, handler *file)
Add column objects to dd::Abstract_table according to list of Create_field objects.
Definition: dd_table.cc:488
Create_field is a description a field/column that may or may not exists in a table.
Definition: create_field.h:49
Definition: key.h:111
Definition: key.h:41
Definition: handler.h:2653
dd::String_type get_sql_type_by_create_field(TABLE *table, const Create_field &field)
Function returns string representing column type by Create_field.
Definition: dd_table.cc:229
Definition: schema.h:61
Definition: os0file.h:85
static const char CHECK_CONSTRAINT_NAME_SUBSTR[]
Definition: dd_table.h:64
Definition: test_sql_stmt.cc:150
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:39
bool is_generated_check_constraint_name(const char *table_name, size_t table_name_length, const char *cc_name, size_t cc_name_length)
Check if name of check constraint is generated one.
Definition: dd_table.cc:2774
A table definition from the master.
Definition: rpl_utility.h:247
bool get_field_numeric_scale(const Create_field *field, uint *scale)
Helper method to get numeric scale for types using Create_field type object.
Definition: dd_table.cc:363
bool rename_check_constraints(const char *old_table_name, dd::Table *new_tab)
Rename generated check constraint names to match the new name of the table.
Definition: dd_table.cc:2786
bool rename_foreign_keys(THD *thd, const char *old_db, const char *old_table_name, handlerton *hton, const char *new_db, dd::Table *new_tab)
Rename foreign keys which have generated names to match the new name of the table.
Definition: dd_table.cc:2448
bool drop_table(THD *thd, const char *schema_name, const char *name, const dd::Table &table_def)
Definition: dd_table.cc:2394
Definition: table.h:46
Definition: foreign_key.h:46
bool uses_general_tablespace(const Table &t)
Check if table uses general tablespace.
Definition: dd_table.cc:2909
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:799
row_type
Definition: handler.h:666
const char * table_name
Definition: rules_table_service.cc:55