Skip to content

Commit

Permalink
to #54769172 support import tablespace if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
luoyanxin authored and jianhua.sjh committed Apr 24, 2024
1 parent 31feedb commit a6d7022
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 46 deletions.
44 changes: 44 additions & 0 deletions mysql-test/suite/innodb/include/import_tablespace_ifnotexists.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Export Table and Import from saved files .cfg and .ibd
# Caller should create t1 table definition and populate table

let $MYSQLD_DATADIR = `SELECT @@datadir`;

if(!$source_db) {
let $source_db = test;
}

if(!$dest_db) {
let $dest_db = test;
}

eval FLUSH TABLES $source_db.t1 FOR EXPORT;

--copy_file $MYSQLD_DATADIR/$source_db/t1.cfg $MYSQLD_DATADIR/t1.cfg_back
--copy_file $MYSQLD_DATADIR/$source_db/t1.ibd $MYSQLD_DATADIR/t1.ibd_back

UNLOCK TABLES;

if($source_db != $dest_db) {
eval USE $dest_db;
let $create1 = query_get_value(SHOW CREATE TABLE $source_db.t1, Create Table, 1);
eval $create1;
}

eval ALTER TABLE $dest_db.t1 DISCARD TABLESPACE;

--move_file $MYSQLD_DATADIR/t1.cfg_back $MYSQLD_DATADIR/$dest_db/t1.cfg
--move_file $MYSQLD_DATADIR/t1.ibd_back $MYSQLD_DATADIR/$dest_db/t1.ibd

eval ALTER TABLE $dest_db.t1 IMPORT TABLESPACE;
--error ER_TABLESPACE_EXISTS
eval ALTER TABLE $dest_db.t1 IMPORT TABLESPACE;
eval ALTER TABLE $dest_db.t1 IMPORT TABLESPACE IF NOT EXISTS;

eval CHECK TABLE $dest_db.t1;
eval SHOW CREATE TABLE $dest_db.t1;
eval SELECT * FROM $dest_db.t1;


if($source_db != $dest_db) {
eval DROP TABLE $dest_db.t1;
}
32 changes: 32 additions & 0 deletions mysql-test/suite/innodb/r/import-tablespace-ifnotexists.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# ALTER TABLE ... IMPORT TABLESPACE IF NOT EXISTS
#
CREATE TABLE t1 (c1 VARCHAR(32), c2 VARCHAR(32), c3 VARCHAR(32),
PRIMARY KEY (c1, c2, c3))
ENGINE=InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO t1 VALUES ('Test Data -1', 'Test Data -2', 'Test Data -3');
FLUSH TABLES test.t1 FOR EXPORT;
UNLOCK TABLES;
ALTER TABLE test.t1 DISCARD TABLESPACE;
ALTER TABLE test.t1 IMPORT TABLESPACE;
ALTER TABLE test.t1 IMPORT TABLESPACE;
ERROR HY000: Tablespace 'test/t1' exists.
ALTER TABLE test.t1 IMPORT TABLESPACE IF NOT EXISTS;
Warnings:
Warning 1813 InnoDB: Tablespace 'test/t1' exists.
CHECK TABLE test.t1;
Table Op Msg_type Msg_text
test.t1 check status OK
SHOW CREATE TABLE test.t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` varchar(32) NOT NULL,
`c2` varchar(32) NOT NULL,
`c3` varchar(32) NOT NULL,
PRIMARY KEY (`c1`,`c2`,`c3`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT * FROM test.t1;
c1 c2 c3
Test Data -1 Test Data -2 Test Data -3
DROP TABLE t1;
# Test End
16 changes: 16 additions & 0 deletions mysql-test/suite/innodb/t/import-tablespace-ifnotexists.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Test "ALTER TABLE ... IMPORT TABLESPACE IF NOT EXISTS" in InnoDB
#--source include/have_innodb.inc

--echo #
--echo # ALTER TABLE ... IMPORT TABLESPACE IF NOT EXISTS
--echo #

CREATE TABLE t1 (c1 VARCHAR(32), c2 VARCHAR(32), c3 VARCHAR(32),
PRIMARY KEY (c1, c2, c3))
ENGINE=InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO t1 VALUES ('Test Data -1', 'Test Data -2', 'Test Data -3');

--source suite/innodb/include/import_tablespace_ifnotexists.inc

DROP TABLE t1;
3 changes: 2 additions & 1 deletion sql/handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4911,11 +4911,12 @@ int handler::ha_enable_indexes(uint mode) {
*/

int handler::ha_discard_or_import_tablespace(bool discard,
uint option,
dd::Table *table_def) {
assert(table_share->tmp_table != NO_TMP_TABLE || m_lock_type == F_WRLCK);
mark_trx_read_write();

return discard_or_import_tablespace(discard, table_def);
return discard_or_import_tablespace(discard, option, table_def);
}

bool handler::ha_prepare_inplace_alter_table(TABLE *altered_table,
Expand Down
16 changes: 10 additions & 6 deletions sql/handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,9 @@ enum enum_alter_inplace_result {
#define HA_LEX_CREATE_INTERNAL_TMP_TABLE 8
#define HA_MAX_REC_LENGTH 65535U

/* Alter table import tablespace if not exists */
#define HA_LEX_IMPORT_TABLESPACE_IF_NOT_EXISTS 1

/**
Options for the START TRANSACTION statement.
Expand Down Expand Up @@ -2910,8 +2913,8 @@ constexpr const decltype(handlerton::flags) HTON_SUPPORTS_ENGINE_ATTRIBUTE{
1 << 17};

/** Engine supports Generated invisible primary key. */
constexpr const decltype(
handlerton::flags) HTON_SUPPORTS_GENERATED_INVISIBLE_PK{1 << 18};
constexpr const decltype(handlerton::flags)
HTON_SUPPORTS_GENERATED_INVISIBLE_PK{1 << 18};

/** Whether the secondary engine supports DDLs. No meaning if the engine is not
* secondary. */
Expand Down Expand Up @@ -4006,9 +4009,7 @@ class Ft_hints {
@return pointer to ft_hints struct
*/
struct ft_hints *get_hints() {
return &hints;
}
struct ft_hints *get_hints() { return &hints; }
};

/**
Expand Down Expand Up @@ -4770,7 +4771,8 @@ class handler {
bool ha_check_and_repair(THD *thd);
int ha_disable_indexes(uint mode);
int ha_enable_indexes(uint mode);
int ha_discard_or_import_tablespace(bool discard, dd::Table *table_def);
int ha_discard_or_import_tablespace(bool discard, uint import_tablespace_option,
dd::Table *table_def);
int ha_rename_table(const char *from, const char *to,
const dd::Table *from_table_def, dd::Table *to_table_def);
int ha_delete_table(const char *name, const dd::Table *table_def);
Expand Down Expand Up @@ -6769,6 +6771,7 @@ class handler {
Discard or import tablespace.
@param [in] discard Indicates whether this is discard operation.
@param[in] option if value is HA_LEX_IMPORT_TABLESPACE_IF_NOT_EXISTS will ignore import tablespace if tablespace exists
@param [in,out] table_def dd::Table object describing the table
in which tablespace needs to be discarded
or imported. This object can be adjusted by
Expand All @@ -6781,6 +6784,7 @@ class handler {
*/

virtual int discard_or_import_tablespace(bool discard [[maybe_unused]],
uint option [[maybe_unused]],
dd::Table *table_def
[[maybe_unused]]) {
set_my_errno(HA_ERR_WRONG_COMMAND);
Expand Down
49 changes: 36 additions & 13 deletions sql/parse_tree_nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -4564,12 +4564,24 @@ class PT_alter_table_import_partition_tablespace final

public:
explicit PT_alter_table_import_partition_tablespace(
const List<String> *opt_partition_list)
: super(Alter_info::ALTER_IMPORT_TABLESPACE, opt_partition_list) {}
const List<String> *opt_partition_list,
uint alter_table_import_tablespace_option)
: super(Alter_info::ALTER_IMPORT_TABLESPACE, opt_partition_list),
m_alter_table_import_tablespace_option(
alter_table_import_tablespace_option) {}

bool contextualize(Table_ddl_parse_context *pc) override {
pc->alter_info->import_tablespace_option =
m_alter_table_import_tablespace_option;
return super::contextualize(pc);
}

Sql_cmd *make_cmd(Table_ddl_parse_context *pc) override {
return new (pc->mem_root) Sql_cmd_discard_import_tablespace(pc->alter_info);
}

private:
const uint m_alter_table_import_tablespace_option;
};

class PT_alter_table_discard_tablespace final
Expand All @@ -4590,12 +4602,23 @@ class PT_alter_table_import_tablespace final
typedef PT_alter_table_standalone_action super;

public:
PT_alter_table_import_tablespace()
: super(Alter_info::ALTER_IMPORT_TABLESPACE) {}
PT_alter_table_import_tablespace(uint alter_table_import_tablespace_option)
: super(Alter_info::ALTER_IMPORT_TABLESPACE),
m_alter_table_import_tablespace_option(
alter_table_import_tablespace_option) {}

bool contextualize(Table_ddl_parse_context *pc) override {
pc->alter_info->import_tablespace_option =
m_alter_table_import_tablespace_option;
return super::contextualize(pc);
}

Sql_cmd *make_cmd(Table_ddl_parse_context *pc) override {
return new (pc->mem_root) Sql_cmd_discard_import_tablespace(pc->alter_info);
}

private:
const uint m_alter_table_import_tablespace_option;
};

class PT_alter_table_stmt final : public PT_table_ddl_stmt_base {
Expand Down Expand Up @@ -4963,9 +4986,9 @@ class PT_alter_tablespace_option final
const Option_type m_value;
};

typedef PT_alter_tablespace_option<decltype(
Tablespace_options::autoextend_size),
&Tablespace_options::autoextend_size>
typedef PT_alter_tablespace_option<
decltype(Tablespace_options::autoextend_size),
&Tablespace_options::autoextend_size>
PT_alter_tablespace_option_autoextend_size;

typedef PT_alter_tablespace_option<decltype(Tablespace_options::extent_size),
Expand All @@ -4980,14 +5003,14 @@ typedef PT_alter_tablespace_option<decltype(Tablespace_options::max_size),
&Tablespace_options::max_size>
PT_alter_tablespace_option_max_size;

typedef PT_alter_tablespace_option<decltype(
Tablespace_options::redo_buffer_size),
&Tablespace_options::redo_buffer_size>
typedef PT_alter_tablespace_option<
decltype(Tablespace_options::redo_buffer_size),
&Tablespace_options::redo_buffer_size>
PT_alter_tablespace_option_redo_buffer_size;

typedef PT_alter_tablespace_option<decltype(
Tablespace_options::undo_buffer_size),
&Tablespace_options::undo_buffer_size>
typedef PT_alter_tablespace_option<
decltype(Tablespace_options::undo_buffer_size),
&Tablespace_options::undo_buffer_size>
PT_alter_tablespace_option_undo_buffer_size;

typedef PT_alter_tablespace_option<
Expand Down
4 changes: 4 additions & 0 deletions sql/sql_alter.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@ class Alter_info {
*/
enum_with_validation with_validation;

// Option for alter table import tablespace
uint import_tablespace_option;

/// "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;
Expand All @@ -466,6 +469,7 @@ class Alter_info {
requested_algorithm(ALTER_TABLE_ALGORITHM_DEFAULT),
requested_lock(ALTER_TABLE_LOCK_DEFAULT),
with_validation(ALTER_VALIDATION_DEFAULT),
import_tablespace_option(0),
new_db_name(LEX_CSTRING{nullptr, 0}),
new_table_name(LEX_CSTRING{nullptr, 0}) {}

Expand Down
17 changes: 8 additions & 9 deletions sql/sql_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11193,9 +11193,9 @@ bool mysql_create_like_table(THD *thd, Table_ref *table, Table_ref *src_table,
goto err;
}
} else // Case 1
if (write_bin_log(thd, true, thd->query().str, thd->query().length,
is_trans))
goto err;
if (write_bin_log(thd, true, thd->query().str, thd->query().length,
is_trans))
goto err;
}
/*
Case 3 and 4 does nothing under RBR
Expand Down Expand Up @@ -11382,6 +11382,7 @@ bool Sql_cmd_discard_import_tablespace::mysql_discard_or_import_tablespace(

bool discard = (m_alter_info->flags & Alter_info::ALTER_DISCARD_TABLESPACE);
error = table_list->table->file->ha_discard_or_import_tablespace(discard,
m_alter_info->import_tablespace_option,
table_def);

THD_STAGE_INFO(thd, stage_end);
Expand Down Expand Up @@ -14681,7 +14682,6 @@ bool prepare_fields_and_keys(THD *thd, const dd::Table *src_table, TABLE *table,
/* Reset auto_increment value if it was dropped */
if ((field->auto_flags & Field::NEXT_NUMBER) &&
!(used_fields & HA_CREATE_USED_AUTO)) {

added_auto_inc_field -= 1; // IPK: AUTO_INCREMENT field is dropped

create_info->auto_increment_value = 0;
Expand Down Expand Up @@ -14826,7 +14826,6 @@ bool prepare_fields_and_keys(THD *thd, const dd::Table *src_table, TABLE *table,
*/
if (def->change && !def->after) continue;


/**
RDS IPK :
If this is an AUTO_INCREMENT field,
Expand Down Expand Up @@ -14909,8 +14908,9 @@ bool prepare_fields_and_keys(THD *thd, const dd::Table *src_table, TABLE *table,
// If no implicit row id be found, then putting new column in the last
new_create_list.push_back(def);
} else
// If no implicit row id in this table, then putting new column in the last
new_create_list.push_back(def);
// If no implicit row id in this table, then putting new column in the
// last
new_create_list.push_back(def);
} else {
const Create_field *find;
if (def->change) {
Expand Down Expand Up @@ -14994,8 +14994,7 @@ bool prepare_fields_and_keys(THD *thd, const dd::Table *src_table, TABLE *table,
while (drop_idx < drop_list.size()) {
const Alter_drop *drop = drop_list[drop_idx];
if (drop->type == Alter_drop::KEY &&
!my_strcasecmp(system_charset_info, key_name, drop->name))
{
!my_strcasecmp(system_charset_info, key_name, drop->name)) {
/* RDS IPK : If dropped PK/UK, reduce the number */
if (key_info->flags & HA_NOSAME) added_unique_key--;
break;
Expand Down
13 changes: 9 additions & 4 deletions sql/sql_yacc.yy
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,7 @@ void warn_about_deprecated_binary(THD *thd)
signed_num
opt_ignore_unknown_user
opt_consensus_log_index
opt_import_tablespace_if_not_exists


%type <order_direction>
Expand Down Expand Up @@ -8903,9 +8904,9 @@ standalone_alter_commands:
{
$$= NEW_PTN PT_alter_table_discard_tablespace;
}
| IMPORT TABLESPACE_SYM
| IMPORT TABLESPACE_SYM opt_import_tablespace_if_not_exists
{
$$= NEW_PTN PT_alter_table_import_tablespace;
$$= NEW_PTN PT_alter_table_import_tablespace($3);
}
/*
This part was added for release 5.1 by Mikael Ronström.
Expand Down Expand Up @@ -8985,9 +8986,9 @@ standalone_alter_commands:
$$= NEW_PTN PT_alter_table_discard_partition_tablespace($3);
}
| IMPORT PARTITION_SYM all_or_alt_part_name_list
TABLESPACE_SYM
TABLESPACE_SYM opt_import_tablespace_if_not_exists
{
$$= NEW_PTN PT_alter_table_import_partition_tablespace($3);
$$= NEW_PTN PT_alter_table_import_partition_tablespace($3, $5);
}
| SECONDARY_LOAD_SYM
{
Expand Down Expand Up @@ -9273,6 +9274,10 @@ alter_lock_option_value:
}
}
;
opt_import_tablespace_if_not_exists:
/* empty */ { $$= 0; }
| IF not EXISTS { $$=HA_LEX_IMPORT_TABLESPACE_IF_NOT_EXISTS; }
;

opt_column:
/* empty */
Expand Down
Loading

0 comments on commit a6d7022

Please sign in to comment.