1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
|
/* Copyright (c) 2010, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#ifndef SQL_ALTER_TABLE_H
#define SQL_ALTER_TABLE_H
#include <assert.h>
#include <stddef.h>
#include <sys/types.h>
#include <functional> // std::function
#include <optional>
#include "lex_string.h"
#include "field_types.h"
#include "my_io.h"
#include "my_sqlcommand.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "sql/dd/types/column.h"
#include "sql/gis/srid.h"
#include "sql/mdl.h" // MDL_request
#include "sql/mem_root_array.h" // Mem_root_array
#include "sql/sql_check_constraint.h" // Sql_check_constraint_spec_list
#include "sql/sql_cmd.h" // Sql_cmd
#include "sql/sql_cmd_ddl_table.h" // Sql_cmd_ddl_table
#include "sql/sql_list.h" // List
#include "sql/thr_malloc.h"
class Alter_info;
class Create_field;
class FOREIGN_KEY;
class Value_generator;
class Item;
class Key_spec;
class String;
class THD;
class Table_ref;
/**
Class representing DROP COLUMN, DROP KEY, DROP FOREIGN KEY, DROP CHECK
CONSTRAINT and DROP CONSTRAINT clauses in ALTER TABLE statement.
*/
class Alter_drop {
public:
enum drop_type { KEY, COLUMN, FOREIGN_KEY, CHECK_CONSTRAINT, ANY_CONSTRAINT };
const char *name;
drop_type type;
Alter_drop(drop_type par_type, const char *par_name)
: name(par_name), type(par_type) {
assert(par_name != nullptr);
}
};
/**
Class representing SET DEFAULT, DROP DEFAULT, RENAME COLUMN, SET VISIBLE and
SET INVISIBLE clause in ALTER TABLE statement.
*/
class Alter_column {
public:
/// The column name being altered.
const char *name;
/// The default value supplied.
Item *def;
/// The expression to be used to generate the default value.
Value_generator *m_default_val_expr;
/// The new column name.
const char *m_new_name;
enum class Type {
SET_DEFAULT,
DROP_DEFAULT,
RENAME_COLUMN,
SET_COLUMN_VISIBLE,
SET_COLUMN_INVISIBLE
};
public:
/// Type of change requested in ALTER TABLE.
inline Type change_type() const { return m_type; }
/// Constructor used when altering the field's default value with a literal
/// constant or when dropping a field's default value.
Alter_column(const char *par_name, Item *literal)
: name(par_name),
def(literal),
m_default_val_expr(nullptr),
m_new_name(nullptr) {
if (def)
m_type = Type::SET_DEFAULT;
else
m_type = Type::DROP_DEFAULT;
}
/// Constructor used when setting a field's DEFAULT value to an expression.
Alter_column(const char *par_name, Value_generator *gen_def)
: name(par_name),
def(nullptr),
m_default_val_expr(gen_def),
m_new_name(nullptr),
m_type(Type::SET_DEFAULT) {}
/// Constructor used while renaming field name.
Alter_column(const char *old_name, const char *new_name)
: name(old_name),
def(nullptr),
m_new_name(new_name),
m_type(Type::RENAME_COLUMN) {}
/// Constructor used while altering column visibility.
Alter_column(const char *par_name, bool par_is_visible)
: name(par_name), def(nullptr), m_new_name(nullptr) {
m_type = (par_is_visible ? Type::SET_COLUMN_VISIBLE
: Type::SET_COLUMN_INVISIBLE);
}
private:
Type m_type;
};
/// An ALTER INDEX operation that changes the visibility of an index.
class Alter_index_visibility {
public:
Alter_index_visibility(const char *name, bool is_visible)
: m_name(name), m_is_visible(is_visible) {
assert(name != nullptr);
}
const char *name() const { return m_name; }
/// The visibility after the operation is performed.
bool is_visible() const { return m_is_visible; }
private:
const char *m_name;
bool m_is_visible;
};
/**
Class which instances represent RENAME INDEX clauses in
ALTER TABLE statement.
*/
class Alter_rename_key {
public:
const char *old_name;
const char *new_name;
Alter_rename_key(const char *old_name_arg, const char *new_name_arg)
: old_name(old_name_arg), new_name(new_name_arg) {}
};
/**
Class representing ALTER CHECK and ALTER CONSTRAINT clauses in ALTER TABLE
statement.
*/
class Alter_constraint_enforcement {
public:
enum class Type { CHECK_CONSTRAINT, ANY_CONSTRAINT };
const char *name;
Type type;
bool is_enforced;
Alter_constraint_enforcement(Type par_type, const char *par_name,
bool par_is_enforced)
: name(par_name), type(par_type), is_enforced(par_is_enforced) {
assert(par_name != nullptr);
}
};
using CreateFieldApplier = std::function<bool(Create_field *, Alter_info *)>;
/**
Data describing the table being created by CREATE TABLE or
altered by ALTER TABLE.
*/
class Alter_info {
public:
/*
These flags are set by the parser and describes the type of
operation(s) specified by the ALTER TABLE statement.
They do *not* describe the type operation(s) to be executed
by the storage engine. For example, we don't yet know the
type of index to be added/dropped.
*/
enum Alter_info_flag : ulonglong {
/// Set for ADD [COLUMN]
ALTER_ADD_COLUMN = 1ULL << 0,
/// Set for DROP [COLUMN]
ALTER_DROP_COLUMN = 1ULL << 1,
/// Set for CHANGE [COLUMN] | MODIFY [CHANGE]
/// Set by mysql_recreate_table()
ALTER_CHANGE_COLUMN = 1ULL << 2,
/// Set for ADD INDEX | ADD KEY | ADD PRIMARY KEY | ADD UNIQUE KEY |
/// ADD UNIQUE INDEX | ALTER ADD [COLUMN]
ALTER_ADD_INDEX = 1ULL << 3,
/// Set for DROP PRIMARY KEY | DROP FOREIGN KEY | DROP KEY | DROP INDEX
ALTER_DROP_INDEX = 1ULL << 4,
/// Set for RENAME [TO]
ALTER_RENAME = 1ULL << 5,
/// Set for ORDER BY
ALTER_ORDER = 1ULL << 6,
/// Set for table_options
ALTER_OPTIONS = 1ULL << 7,
/// Set for ALTER [COLUMN] ... SET DEFAULT ... | DROP DEFAULT
ALTER_CHANGE_COLUMN_DEFAULT = 1ULL << 8,
/// Set for DISABLE KEYS | ENABLE KEYS
ALTER_KEYS_ONOFF = 1ULL << 9,
/// Set for FORCE
/// Set for ENGINE(same engine)
/// Set by mysql_recreate_table()
ALTER_RECREATE = 1ULL << 10,
/// Set for ADD PARTITION
ALTER_ADD_PARTITION = 1ULL << 11,
/// Set for DROP PARTITION
ALTER_DROP_PARTITION = 1ULL << 12,
/// Set for COALESCE PARTITION
ALTER_COALESCE_PARTITION = 1ULL << 13,
/// Set for REORGANIZE PARTITION ... INTO
ALTER_REORGANIZE_PARTITION = 1ULL << 14,
/// Set for partition_options
ALTER_PARTITION = 1ULL << 15,
/// Set for LOAD INDEX INTO CACHE ... PARTITION
/// Set for CACHE INDEX ... PARTITION
ALTER_ADMIN_PARTITION = 1ULL << 16,
/// Set for REORGANIZE PARTITION
ALTER_TABLE_REORG = 1ULL << 17,
/// Set for REBUILD PARTITION
ALTER_REBUILD_PARTITION = 1ULL << 18,
/// Set for partitioning operations specifying ALL keyword
ALTER_ALL_PARTITION = 1ULL << 19,
/// Set for REMOVE PARTITIONING
ALTER_REMOVE_PARTITIONING = 1ULL << 20,
/// Set for ADD FOREIGN KEY
ADD_FOREIGN_KEY = 1ULL << 21,
/// Set for DROP FOREIGN KEY
DROP_FOREIGN_KEY = 1ULL << 22,
/// Set for EXCHANGE PARTITION
ALTER_EXCHANGE_PARTITION = 1ULL << 23,
/// Set by Sql_cmd_alter_table_truncate_partition::execute()
ALTER_TRUNCATE_PARTITION = 1ULL << 24,
/// Set for ADD [COLUMN] FIRST | AFTER
ALTER_COLUMN_ORDER = 1ULL << 25,
/// Set for RENAME INDEX
ALTER_RENAME_INDEX = 1ULL << 26,
/// Set for discarding the tablespace
ALTER_DISCARD_TABLESPACE = 1ULL << 27,
/// Set for importing the tablespace
ALTER_IMPORT_TABLESPACE = 1ULL << 28,
/// Means that the visibility of an index is changed.
ALTER_INDEX_VISIBILITY = 1ULL << 29,
/// Set for SECONDARY LOAD
ALTER_SECONDARY_LOAD = 1ULL << 30,
/// Set for SECONDARY UNLOAD
ALTER_SECONDARY_UNLOAD = 1ULL << 31,
/// Set for add check constraint.
ADD_CHECK_CONSTRAINT = 1ULL << 32,
/// Set for drop check constraint.
DROP_CHECK_CONSTRAINT = 1ULL << 33,
/// Set for check constraint enforce.
ENFORCE_CHECK_CONSTRAINT = 1ULL << 34,
/// Set for check constraint suspend.
SUSPEND_CHECK_CONSTRAINT = 1ULL << 35,
/// Set for DROP CONSTRAINT.
DROP_ANY_CONSTRAINT = 1ULL << 36,
/// Set for ALTER CONSTRAINT symbol ENFORCED.
ENFORCE_ANY_CONSTRAINT = 1ULL << 37,
/// Set for ALTER CONSTRAINT symbol NOT ENFORCED.
SUSPEND_ANY_CONSTRAINT = 1ULL << 38,
/// Set if ANY engine attribute is used (also in CREATE) Note that
/// this is NOT to be set for SECONDARY_ENGINE_ATTRIBUTE as this flag
/// controls if execution should check if SE supports engine
/// attributes.
ANY_ENGINE_ATTRIBUTE = 1ULL << 39,
/// Set for column visibility attribute alter.
ALTER_COLUMN_VISIBILITY = 1ULL << 40
};
enum enum_enable_or_disable { LEAVE_AS_IS, ENABLE, DISABLE };
/**
The different values of the ALGORITHM clause.
Describes which algorithm to use when altering the table.
*/
enum enum_alter_table_algorithm {
// In-place if supported, copy otherwise.
ALTER_TABLE_ALGORITHM_DEFAULT,
// In-place if supported, error otherwise.
ALTER_TABLE_ALGORITHM_INPLACE,
// Instant if supported, error otherwise.
ALTER_TABLE_ALGORITHM_INSTANT,
// Copy if supported, error otherwise.
ALTER_TABLE_ALGORITHM_COPY
};
/**
The different values of the LOCK clause.
Describes the level of concurrency during ALTER TABLE.
*/
enum enum_alter_table_lock {
// Maximum supported level of concurrency for the given operation.
ALTER_TABLE_LOCK_DEFAULT,
// Allow concurrent reads & writes. If not supported, give error.
ALTER_TABLE_LOCK_NONE,
// Allow concurrent reads only. If not supported, give error.
ALTER_TABLE_LOCK_SHARED,
// Block reads and writes.
ALTER_TABLE_LOCK_EXCLUSIVE
};
/**
Status of validation clause in ALTER TABLE statement. Used during
partitions and GC alterations.
*/
enum enum_with_validation {
/**
Default value, used when it's not specified in the statement.
Means WITH VALIDATION for partitions alterations and WITHOUT VALIDATION
for altering virtual GC.
*/
ALTER_VALIDATION_DEFAULT,
ALTER_WITH_VALIDATION,
ALTER_WITHOUT_VALIDATION
};
/**
Columns, keys and constraints to be dropped.
*/
Mem_root_array<const Alter_drop *> drop_list;
// Columns for ALTER_COLUMN_CHANGE_DEFAULT.
Mem_root_array<const Alter_column *> alter_list;
// List of keys, used by both CREATE and ALTER TABLE.
Mem_root_array<Key_spec *> key_list;
// Keys to be renamed.
Mem_root_array<const Alter_rename_key *> alter_rename_key_list;
/// Indexes whose visibilities are to be changed.
Mem_root_array<const Alter_index_visibility *> alter_index_visibility_list;
/// List of check constraints whose enforcement state is changed.
Mem_root_array<const Alter_constraint_enforcement *>
alter_constraint_enforcement_list;
/// Check constraints specification for CREATE and ALTER TABLE operations.
Sql_check_constraint_spec_list check_constraint_spec_list;
// List of columns, used by both CREATE and ALTER TABLE.
List<Create_field> create_list;
std::vector<CreateFieldApplier> cf_appliers;
// Type of ALTER TABLE operation.
ulonglong flags;
// Enable or disable keys.
enum_enable_or_disable keys_onoff;
// List of partitions.
List<String> partition_names;
// Number of partitions.
uint num_parts;
// Type of ALTER TABLE algorithm.
enum_alter_table_algorithm requested_algorithm;
// Type of ALTER TABLE lock.
enum_alter_table_lock requested_lock;
/*
Whether VALIDATION is asked for an operation. Used during virtual GC and
partitions alterations.
*/
enum_with_validation with_validation;
/// "new_db" (if any) or "db" (if any) or default database from
/// ALTER TABLE [db.]table [ RENAME [TO|AS|=] [new_db.]new_table ]
LEX_CSTRING new_db_name;
/// New table name in the
/// \code
/// RENAME [TO] <table_name>
/// \endcode
/// clause or NULL_STR
LEX_CSTRING new_table_name;
explicit Alter_info(MEM_ROOT *mem_root)
: drop_list(mem_root),
alter_list(mem_root),
key_list(mem_root),
alter_rename_key_list(mem_root),
alter_index_visibility_list(mem_root),
alter_constraint_enforcement_list(mem_root),
check_constraint_spec_list(mem_root),
flags(0),
keys_onoff(LEAVE_AS_IS),
num_parts(0),
requested_algorithm(ALTER_TABLE_ALGORITHM_DEFAULT),
requested_lock(ALTER_TABLE_LOCK_DEFAULT),
with_validation(ALTER_VALIDATION_DEFAULT),
new_db_name(LEX_CSTRING{nullptr, 0}),
new_table_name(LEX_CSTRING{nullptr, 0}) {}
/**
Construct a copy of this object to be used for mysql_alter_table
and mysql_create_table.
Historically, these two functions modify their Alter_info
arguments. This behaviour breaks re-execution of prepared
statements and stored procedures and is compensated by always
supplying a copy of Alter_info to these functions.
@param rhs Alter_info to make copy of
@param mem_root Mem_root for new Alter_info
@note You need to use check the error in THD for out
of memory condition after calling this function.
*/
Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root);
bool add_field(THD *thd, const LEX_STRING *field_name,
enum enum_field_types type, const char *length,
const char *decimal, uint type_modifier, Item *default_value,
Item *on_update_value, LEX_CSTRING *comment,
const char *change, List<String> *interval_list,
const CHARSET_INFO *cs, bool has_explicit_collation,
uint uint_geom_type, Value_generator *gcol_info,
Value_generator *default_val_expr, const char *opt_after,
std::optional<gis::srid_t> srid,
Sql_check_constraint_spec_list *check_cons_list,
dd::Column::enum_hidden_type hidden, bool is_array = false);
private:
Alter_info &operator=(const Alter_info &rhs); // not implemented
Alter_info(const Alter_info &rhs); // not implemented
};
/** Runtime context for ALTER TABLE. */
class Alter_table_ctx {
public:
Alter_table_ctx();
Alter_table_ctx(THD *thd, Table_ref *table_list, uint tables_opened_arg,
const char *new_db_arg, const char *new_name_arg);
~Alter_table_ctx();
/**
@return true if the table is moved to another database, false otherwise.
*/
bool is_database_changed() const { return (new_db != db); }
/**
@return true if the table name is changed, false otherwise.
*/
bool is_table_name_changed() const { return (new_name != table_name); }
/**
@return true if the table is renamed (i.e. its name or database changed),
false otherwise.
*/
bool is_table_renamed() const {
return is_database_changed() || is_table_name_changed();
}
/**
@return path to the original table.
*/
const char *get_path() const {
assert(!tmp_table);
return path;
}
/**
@return path to the temporary table created during ALTER TABLE.
*/
const char *get_tmp_path() const { return tmp_path; }
public:
typedef uint error_if_not_empty_mask;
static const error_if_not_empty_mask DATETIME_WITHOUT_DEFAULT = 1 << 0;
static const error_if_not_empty_mask GEOMETRY_WITHOUT_DEFAULT = 1 << 1;
Create_field *datetime_field;
error_if_not_empty_mask error_if_not_empty;
uint tables_opened;
const char *db;
const char *table_name;
const char *alias;
const char *new_db;
const char *new_name;
const char *new_alias;
char tmp_name[80];
/* Used to remember which foreign keys already existed in the table. */
FOREIGN_KEY *fk_info;
uint fk_count;
/**
Maximum number component used by generated foreign key names in the
old version of table.
*/
uint fk_max_generated_name_number;
/**
Metadata lock request on table's new name when this name or database
are changed.
*/
MDL_request target_mdl_request;
/** Metadata lock request on table's new database if it is changed. */
MDL_request target_db_mdl_request;
private:
char new_filename[FN_REFLEN + 1];
char new_alias_buff[FN_REFLEN + 1];
char path[FN_REFLEN + 1];
char new_path[FN_REFLEN + 1];
char tmp_path[FN_REFLEN + 1];
#ifndef NDEBUG
/** Indicates that we are altering temporary table. Used only in asserts. */
bool tmp_table;
#endif
Alter_table_ctx &operator=(const Alter_table_ctx &rhs); // not implemented
Alter_table_ctx(const Alter_table_ctx &rhs); // not implemented
};
/**
Represents the common properties of the ALTER TABLE statements.
@todo move Alter_info and other ALTER generic structures from Lex here.
*/
class Sql_cmd_common_alter_table : public Sql_cmd_ddl_table {
public:
using Sql_cmd_ddl_table::Sql_cmd_ddl_table;
~Sql_cmd_common_alter_table() override = 0; // force abstract class
enum_sql_command sql_command_code() const final { return SQLCOM_ALTER_TABLE; }
};
inline Sql_cmd_common_alter_table::~Sql_cmd_common_alter_table() = default;
/**
Represents the generic ALTER TABLE statement.
@todo move Alter_info and other ALTER specific structures from Lex here.
*/
class Sql_cmd_alter_table : public Sql_cmd_common_alter_table {
public:
using Sql_cmd_common_alter_table::Sql_cmd_common_alter_table;
bool execute(THD *thd) override;
bool reprepare_on_execute_required() const override;
};
/**
Represents ALTER TABLE IMPORT/DISCARD TABLESPACE statements.
*/
class Sql_cmd_discard_import_tablespace : public Sql_cmd_common_alter_table {
public:
using Sql_cmd_common_alter_table::Sql_cmd_common_alter_table;
bool execute(THD *thd) override;
private:
bool mysql_discard_or_import_tablespace(THD *thd, Table_ref *table_list);
};
/**
Represents ALTER TABLE SECONDARY_LOAD/SECONDARY_UNLOAD statements.
*/
class Sql_cmd_secondary_load_unload final : public Sql_cmd_common_alter_table {
public:
// Inherit the constructors from the parent class.
using Sql_cmd_common_alter_table::Sql_cmd_common_alter_table;
bool execute(THD *thd) override;
private:
bool mysql_secondary_load_or_unload(THD *thd, Table_ref *table_list);
};
#endif
|