| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2016, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/dict0dd.ic |
| 29 |
Data dictionary access |
| 30 |
|
| 31 |
Created 10/2016 Jimmy Yamg |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "sql_table.h" |
| 35 |
#ifndef UNIV_HOTBACKUP |
| 36 |
#include "dict0crea.h" |
| 37 |
#include "dict0priv.h" |
| 38 |
#include "field.h" |
| 39 |
#include "sql/table.h" |
| 40 |
#include "sql_base.h" |
| 41 |
|
| 42 |
/** Get the explicit dd::Tablespace::id of a partition. |
| 43 |
@param[in] partition partition or subpartition |
| 44 |
@return the explicit dd::Tablespace::id |
| 45 |
@retval dd::INVALID_OBJECT_ID if there is no explicit tablespace */ |
| 46 |
inline dd::Object_id dd_get_space_id(const dd::Partition &partition) { |
| 47 |
ut_ad(dd_part_is_stored(&partition)); |
| 48 |
ut_ad(dd_table_is_partitioned(partition.table())); |
| 49 |
ut_ad((partition.table().subpartition_type() == dd::Table::ST_NONE) == |
| 50 |
(partition.parent() == nullptr)); |
| 51 |
|
| 52 |
dd::Object_id id = partition.tablespace_id(); |
| 53 |
if (id == dd::INVALID_OBJECT_ID) { |
| 54 |
if (const dd::Partition *parent = partition.parent()) { |
| 55 |
/* If there is no explicit TABLESPACE for the |
| 56 |
subpartition, fall back to the TABLESPACE |
| 57 |
of the partition. */ |
| 58 |
id = parent->tablespace_id(); |
| 59 |
} |
| 60 |
} |
| 61 |
if (id == dd::INVALID_OBJECT_ID) { |
| 62 |
/* If there is no explicit TABLESPACE for the partition, |
| 63 |
fall back to the TABLESPACE of the table. */ |
| 64 |
id = partition.table().tablespace_id(); |
| 65 |
} |
| 66 |
return (id); |
| 67 |
} |
| 68 |
|
| 69 |
/** Look up a column in a table using the system_charset_info collation. |
| 70 |
@param[in] dd_table data dictionary table |
| 71 |
@param[in] name column name |
| 72 |
@return the column |
| 73 |
@retval nullptr if not found */ |
| 74 |
inline const dd::Column *dd_find_column(const dd::Table *dd_table, |
| 75 |
const char *name) { |
| 76 |
for (const dd::Column *c : dd_table->columns()) { |
| 77 |
if (!my_strcasecmp(system_charset_info, c->name().c_str(), name)) { |
| 78 |
return (c); |
| 79 |
} |
| 80 |
} |
| 81 |
return (nullptr); |
| 82 |
} |
| 83 |
|
| 84 |
/** Add a hidden column when creating a table. |
| 85 |
@param[in,out] dd_table table containing user columns and indexes |
| 86 |
@param[in] name hidden column name |
| 87 |
@param[in] length length of the column, in bytes |
| 88 |
@param[in] type column type |
| 89 |
@return the added column, or NULL if there already was a column by that name */ |
| 90 |
inline dd::Column *dd_add_hidden_column(dd::Table *dd_table, const char *name, |
| 91 |
uint length, |
| 92 |
dd::enum_column_types type) { |
| 93 |
if (const dd::Column *c = dd_find_column(dd_table, name)) { |
| 94 |
my_error(ER_WRONG_COLUMN_NAME, MYF(0), c->name().c_str()); |
| 95 |
return (nullptr); |
| 96 |
} |
| 97 |
|
| 98 |
dd::Column *col = dd_table->add_column(); |
| 99 |
col->set_hidden(dd::Column::enum_hidden_type::HT_HIDDEN_SE); |
| 100 |
col->set_name(name); |
| 101 |
col->set_type(type); |
| 102 |
col->set_nullable(false); |
| 103 |
col->set_char_length(length); |
| 104 |
col->set_collation_id(my_charset_bin.number); |
| 105 |
|
| 106 |
return (col); |
| 107 |
} |
| 108 |
|
| 109 |
inline bool dd_drop_hidden_column(dd::Table *dd_table, const char *name) { |
| 110 |
#ifdef UNIV_DEBUG |
| 111 |
const dd::Column *col = dd_find_column(dd_table, name); |
| 112 |
ut_ad(col != nullptr); |
| 113 |
ut_ad(dd_column_is_dropped(col)); |
| 114 |
#endif |
| 115 |
return dd_table->drop_column(name); |
| 116 |
} |
| 117 |
|
| 118 |
/** Add a hidden index element at the end. |
| 119 |
@param[in,out] index created index metadata |
| 120 |
@param[in] column column of the index */ |
| 121 |
inline void dd_add_hidden_element(dd::Index *index, const dd::Column *column) { |
| 122 |
dd::Index_element *e = index->add_element(const_cast<dd::Column *>(column)); |
| 123 |
e->set_hidden(true); |
| 124 |
e->set_order(dd::Index_element::ORDER_ASC); |
| 125 |
} |
| 126 |
|
| 127 |
/** Initialize a hidden unique B-tree index. |
| 128 |
@param[in,out] index created index metadata |
| 129 |
@param[in] name name of the index |
| 130 |
@param[in] column column of the index |
| 131 |
@return the initialized index */ |
| 132 |
inline dd::Index *dd_set_hidden_unique_index(dd::Index *index, const char *name, |
| 133 |
const dd::Column *column) { |
| 134 |
index->set_name(name); |
| 135 |
index->set_hidden(true); |
| 136 |
index->set_algorithm(dd::Index::IA_BTREE); |
| 137 |
index->set_type(dd::Index::IT_UNIQUE); |
| 138 |
index->set_engine(innobase_hton_name); |
| 139 |
dd_add_hidden_element(index, column); |
| 140 |
return (index); |
| 141 |
} |
| 142 |
|
| 143 |
inline dict_table_t *dd_table_open_on_id_in_mem(table_id_t table_id, |
| 144 |
bool dict_locked) { |
| 145 |
dict_table_t *table; |
| 146 |
|
| 147 |
if (!dict_locked) { |
| 148 |
dict_sys_mutex_enter(); |
| 149 |
} |
| 150 |
|
| 151 |
ut_ad(dict_sys_mutex_own()); |
| 152 |
|
| 153 |
/* Look for the table ID in the hash table */ |
| 154 |
const auto table_id_hash_value = ut::hash_uint64(table_id); |
| 155 |
|
| 156 |
HASH_SEARCH(id_hash, dict_sys->table_id_hash, table_id_hash_value, |
| 157 |
dict_table_t *, table, ut_ad(table->cached), |
| 158 |
table->id == table_id); |
| 159 |
|
| 160 |
ut_ad(table == nullptr || table->cached); |
| 161 |
|
| 162 |
if (table != nullptr) { |
| 163 |
table->acquire(); |
| 164 |
} |
| 165 |
|
| 166 |
if (!dict_locked) { |
| 167 |
dict_sys_mutex_exit(); |
| 168 |
} |
| 169 |
|
| 170 |
return (table); |
| 171 |
} |
| 172 |
|
| 173 |
/** Returns a cached table object based on table name. |
| 174 |
@param[in] name table name |
| 175 |
@param[in] dict_locked true=data dictionary locked |
| 176 |
@return table, NULL if does not exist */ |
| 177 |
inline dict_table_t *dd_table_open_on_name_in_mem(const char *name, |
| 178 |
bool dict_locked) { |
| 179 |
dict_table_t *table = nullptr; |
| 180 |
|
| 181 |
if (!dict_locked) { |
| 182 |
dict_sys_mutex_enter(); |
| 183 |
} |
| 184 |
|
| 185 |
table = dict_table_check_if_in_cache_low(name); |
| 186 |
|
| 187 |
ut_ad(!table || table->cached); |
| 188 |
|
| 189 |
if (table != nullptr) { |
| 190 |
if (table->can_be_evicted) { |
| 191 |
dict_move_to_mru(table); |
| 192 |
} |
| 193 |
|
| 194 |
table->acquire_with_lock(); |
| 195 |
} |
| 196 |
|
| 197 |
if (!dict_locked) { |
| 198 |
dict_sys_mutex_exit(); |
| 199 |
} |
| 200 |
|
| 201 |
return (table); |
| 202 |
} |
| 203 |
|
| 204 |
/** Check whether there exist a column named as "FTS_DOC_ID", which is |
| 205 |
reserved for InnoDB FTS Doc ID |
| 206 |
@param[in] thd MySQL thread handle |
| 207 |
@param[in] form information on table |
| 208 |
columns and indexes |
| 209 |
@param[out] doc_id_col Doc ID column number if |
| 210 |
there exist a FTS_DOC_ID column, |
| 211 |
ULINT_UNDEFINED if column is of the |
| 212 |
wrong type/name/size |
| 213 |
@return true if there exist a "FTS_DOC_ID" column */ |
| 214 |
inline bool create_table_check_doc_id_col(THD *thd, const TABLE *form, |
| 215 |
ulint *doc_id_col) { |
| 216 |
for (ulint i = 0; i < form->s->fields; i++) { |
| 217 |
const Field *field; |
| 218 |
ulint col_type; |
| 219 |
ulint col_len; |
| 220 |
ulint unsigned_type; |
| 221 |
|
| 222 |
field = form->field[i]; |
| 223 |
if (!field->stored_in_db) continue; |
| 224 |
|
| 225 |
col_type = get_innobase_type_from_mysql_type(&unsigned_type, field); |
| 226 |
|
| 227 |
col_len = field->pack_length(); |
| 228 |
|
| 229 |
if (innobase_strcasecmp(field->field_name, FTS_DOC_ID_COL_NAME) == 0) { |
| 230 |
/* Note the name is case sensitive due to |
| 231 |
our internal query parser */ |
| 232 |
if (col_type == DATA_INT && !field->is_nullable() && |
| 233 |
col_len == sizeof(doc_id_t) && |
| 234 |
(strcmp(field->field_name, FTS_DOC_ID_COL_NAME) == 0)) { |
| 235 |
*doc_id_col = i; |
| 236 |
} else { |
| 237 |
push_warning_printf(thd, Sql_condition::SL_WARNING, |
| 238 |
ER_ILLEGAL_HA_CREATE_OPTION, |
| 239 |
"InnoDB: FTS_DOC_ID column must be" |
| 240 |
" of BIGINT NOT NULL type, and named" |
| 241 |
" in all capitalized characters"); |
| 242 |
my_error(ER_WRONG_COLUMN_NAME, MYF(0), field->field_name); |
| 243 |
*doc_id_col = ULINT_UNDEFINED; |
| 244 |
} |
| 245 |
|
| 246 |
return (true); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
return (false); |
| 251 |
} |
| 252 |
|
| 253 |
/** Return a display name for the row format |
| 254 |
@param[in] row_format Row Format |
| 255 |
@return row format name */ |
| 256 |
inline const char *get_row_format_name(enum row_type row_format) { |
| 257 |
switch (row_format) { |
| 258 |
case ROW_TYPE_COMPACT: |
| 259 |
return ("ROW_FORMAT=COMPACT"); |
| 260 |
case ROW_TYPE_COMPRESSED: |
| 261 |
return ("ROW_FORMAT=COMPRESSED"); |
| 262 |
case ROW_TYPE_DYNAMIC: |
| 263 |
return ("ROW_FORMAT=DYNAMIC"); |
| 264 |
case ROW_TYPE_REDUNDANT: |
| 265 |
return ("ROW_FORMAT=REDUNDANT"); |
| 266 |
case ROW_TYPE_DEFAULT: |
| 267 |
return ("ROW_FORMAT=DEFAULT"); |
| 268 |
case ROW_TYPE_FIXED: |
| 269 |
return ("ROW_FORMAT=FIXED"); |
| 270 |
case ROW_TYPE_PAGED: |
| 271 |
return ("ROW_FORMAT=PAGED"); |
| 272 |
case ROW_TYPE_NOT_USED: |
| 273 |
break; |
| 274 |
} |
| 275 |
return ("ROW_FORMAT"); |
| 276 |
} |
| 277 |
|
| 278 |
/** Acquire a shared metadata lock. |
| 279 |
@param[in,out] thd current thread |
| 280 |
@param[out] mdl metadata lock |
| 281 |
@param[in] db schema name |
| 282 |
@param[in] table table name |
| 283 |
@retval false if acquired |
| 284 |
@retval true if failed (my_error() will have been called) */ |
| 285 |
[[nodiscard]] static inline bool dd_mdl_acquire(THD *thd, MDL_ticket **mdl, |
| 286 |
const char *db, |
| 287 |
const char *table) { |
| 288 |
bool ret = false; |
| 289 |
char table_name[MAX_TABLE_NAME_LEN + 1]; |
| 290 |
const char *table_p = table; |
| 291 |
|
| 292 |
char db_name[MAX_DATABASE_NAME_LEN + 1]; |
| 293 |
const char *db_p = db; |
| 294 |
|
| 295 |
if (innobase_get_lower_case_table_names() == 2) { |
| 296 |
ulint len = strlen(table); |
| 297 |
ut_memcpy(table_name, table, len); |
| 298 |
table_name[len] = '\0'; |
| 299 |
innobase_casedn_str(table_name); |
| 300 |
table_p = table_name; |
| 301 |
|
| 302 |
len = strlen(db); |
| 303 |
ut_memcpy(db_name, db, len); |
| 304 |
db_name[len] = '\0'; |
| 305 |
innobase_casedn_str(db_name); |
| 306 |
db_p = db_name; |
| 307 |
} else { |
| 308 |
#ifndef _WIN32 |
| 309 |
if (innobase_get_lower_case_table_names() == 1) { |
| 310 |
ulint len = strlen(table); |
| 311 |
ut_memcpy(table_name, table, len); |
| 312 |
table_name[len] = '\0'; |
| 313 |
innobase_casedn_str(table_name); |
| 314 |
table_p = table_name; |
| 315 |
|
| 316 |
len = strlen(db); |
| 317 |
ut_memcpy(db_name, db, len); |
| 318 |
db_name[len] = '\0'; |
| 319 |
innobase_casedn_str(db_name); |
| 320 |
db_p = db_name; |
| 321 |
} |
| 322 |
#endif /* !_WIN32 */ |
| 323 |
} |
| 324 |
|
| 325 |
ret = dd::acquire_shared_table_mdl(thd, db_p, table_p, false, mdl); |
| 326 |
|
| 327 |
return (ret); |
| 328 |
} |
| 329 |
|
| 330 |
/** Get the version attribute. |
| 331 |
@param[in] dd_table dd::Table |
| 332 |
@return table dynamic metadata version if exists, otherwise 0 */ |
| 333 |
inline uint64_t dd_get_version(const dd::Table *dd_table) { |
| 334 |
uint64_t version = 0; |
| 335 |
const dd::Properties &p = dd_table->se_private_data(); |
| 336 |
|
| 337 |
if (!p.exists(dd_table_key_strings[DD_TABLE_VERSION]) || |
| 338 |
p.get(dd_table_key_strings[DD_TABLE_VERSION], |
| 339 |
reinterpret_cast<uint64_t *>(&version))) { |
| 340 |
return (0); |
| 341 |
} |
| 342 |
|
| 343 |
return (version); |
| 344 |
} |
| 345 |
#endif /* !UNIV_HOTBACKUP */ |