Skip to content

Commit

Permalink
feat(tianmu): support 'ALTER TABLE t1 CHARACTER SET = ...' clause. (s…
Browse files Browse the repository at this point in the history
…toneatom#848)

[summary]
1 add implement in check_if_supported_inplace_alter of tianmu handler;
2 add implement in inplace_alter_table of tianmu handler;
3 add implement in commit_inplace_alter_table of tianmu handler;
  • Loading branch information
lujiashun committed Nov 3, 2022
1 parent 8050671 commit 478cf5e
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 10 deletions.
164 changes: 164 additions & 0 deletions mysql-test/suite/tianmu/r/issue848.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
create database test_issue848;
use test_issue848;
CREATE TABLE `test_alter_table_option_charset` (
`c_tinyint` tinyint DEFAULT NULL COMMENT 'tinyint',
`c_smallint` smallint DEFAULT NULL COMMENT 'smallint',
`c_mediumint` mediumint DEFAULT NULL COMMENT 'mediumint',
`c_int` int DEFAULT NULL COMMENT 'int',
`c_bigint` bigint DEFAULT NULL COMMENT 'bigint',
`c_float` float DEFAULT NULL COMMENT 'float',
`c_double` double DEFAULT NULL COMMENT 'double',
`c_decimal` decimal(10,5) DEFAULT NULL COMMENT 'decimal',
`c_date` date DEFAULT NULL COMMENT 'date',
`c_datetime` datetime DEFAULT NULL COMMENT 'datetime',
`c_timestamp` timestamp NULL DEFAULT NULL COMMENT 'timestamp',
`c_time` time DEFAULT NULL COMMENT 'time',
`c_char` char(10) DEFAULT NULL COMMENT 'char',
`c_varchar` varchar(10) DEFAULT NULL COMMENT 'varchar',
`c_blob` blob COMMENT 'blob',
`c_text` text COMMENT 'text',
`c_longblob` longblob COMMENT 'longblob'
) ENGINE=TIANMU DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
show create table test_alter_table_option_charset;
Table Create Table
test_alter_table_option_charset CREATE TABLE `test_alter_table_option_charset` (
`c_tinyint` tinyint(4) DEFAULT NULL COMMENT 'tinyint',
`c_smallint` smallint(6) DEFAULT NULL COMMENT 'smallint',
`c_mediumint` mediumint(9) DEFAULT NULL COMMENT 'mediumint',
`c_int` int(11) DEFAULT NULL COMMENT 'int',
`c_bigint` bigint(20) DEFAULT NULL COMMENT 'bigint',
`c_float` float DEFAULT NULL COMMENT 'float',
`c_double` double DEFAULT NULL COMMENT 'double',
`c_decimal` decimal(10,5) DEFAULT NULL COMMENT 'decimal',
`c_date` date DEFAULT NULL COMMENT 'date',
`c_datetime` datetime DEFAULT NULL COMMENT 'datetime',
`c_timestamp` timestamp NULL DEFAULT NULL COMMENT 'timestamp',
`c_time` time DEFAULT NULL COMMENT 'time',
`c_char` char(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'char',
`c_varchar` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'varchar',
`c_blob` blob COMMENT 'blob',
`c_text` text COLLATE utf8mb4_unicode_ci COMMENT 'text',
`c_longblob` longblob COMMENT 'longblob'
) ENGINE=TIANMU DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
ALTER TABLE test_alter_table_option_charset DEFAULT CHARACTER SET gbk;
show create table test_alter_table_option_charset;
Table Create Table
test_alter_table_option_charset CREATE TABLE `test_alter_table_option_charset` (
`c_tinyint` tinyint(4) DEFAULT NULL COMMENT 'tinyint',
`c_smallint` smallint(6) DEFAULT NULL COMMENT 'smallint',
`c_mediumint` mediumint(9) DEFAULT NULL COMMENT 'mediumint',
`c_int` int(11) DEFAULT NULL COMMENT 'int',
`c_bigint` bigint(20) DEFAULT NULL COMMENT 'bigint',
`c_float` float DEFAULT NULL COMMENT 'float',
`c_double` double DEFAULT NULL COMMENT 'double',
`c_decimal` decimal(10,5) DEFAULT NULL COMMENT 'decimal',
`c_date` date DEFAULT NULL COMMENT 'date',
`c_datetime` datetime DEFAULT NULL COMMENT 'datetime',
`c_timestamp` timestamp NULL DEFAULT NULL COMMENT 'timestamp',
`c_time` time DEFAULT NULL COMMENT 'time',
`c_char` char(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'char',
`c_varchar` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'varchar',
`c_blob` blob COMMENT 'blob',
`c_text` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT 'text',
`c_longblob` longblob COMMENT 'longblob'
) ENGINE=TIANMU DEFAULT CHARSET=gbk
insert into test_alter_table_option_charset(c_tinyint,c_smallint,c_mediumint,c_int,c_bigint,c_float,c_double,c_decimal,c_date,c_datetime,c_timestamp,c_time,c_char,c_varchar,c_text)
values(105,105,105,105,105,5.2,10.88,105.083,'2016-02-25','2016-02-25 10:20:01','2016-02-25 05:20:01','10:20:01','stoneatom1','hello1','bcdefghijklmn');
ALTER TABLE test_alter_table_option_charset CHARACTER SET latin1;
show create table test_alter_table_option_charset;
Table Create Table
test_alter_table_option_charset CREATE TABLE `test_alter_table_option_charset` (
`c_tinyint` tinyint(4) DEFAULT NULL COMMENT 'tinyint',
`c_smallint` smallint(6) DEFAULT NULL COMMENT 'smallint',
`c_mediumint` mediumint(9) DEFAULT NULL COMMENT 'mediumint',
`c_int` int(11) DEFAULT NULL COMMENT 'int',
`c_bigint` bigint(20) DEFAULT NULL COMMENT 'bigint',
`c_float` float DEFAULT NULL COMMENT 'float',
`c_double` double DEFAULT NULL COMMENT 'double',
`c_decimal` decimal(10,5) DEFAULT NULL COMMENT 'decimal',
`c_date` date DEFAULT NULL COMMENT 'date',
`c_datetime` datetime DEFAULT NULL COMMENT 'datetime',
`c_timestamp` timestamp NULL DEFAULT NULL COMMENT 'timestamp',
`c_time` time DEFAULT NULL COMMENT 'time',
`c_char` char(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'char',
`c_varchar` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'varchar',
`c_blob` blob COMMENT 'blob',
`c_text` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT 'text',
`c_longblob` longblob COMMENT 'longblob'
) ENGINE=TIANMU DEFAULT CHARSET=latin1
alter table test_alter_table_option_charset add column ex_column char(30);
show create table test_alter_table_option_charset;
Table Create Table
test_alter_table_option_charset CREATE TABLE `test_alter_table_option_charset` (
`c_tinyint` tinyint(4) DEFAULT NULL COMMENT 'tinyint',
`c_smallint` smallint(6) DEFAULT NULL COMMENT 'smallint',
`c_mediumint` mediumint(9) DEFAULT NULL COMMENT 'mediumint',
`c_int` int(11) DEFAULT NULL COMMENT 'int',
`c_bigint` bigint(20) DEFAULT NULL COMMENT 'bigint',
`c_float` float DEFAULT NULL COMMENT 'float',
`c_double` double DEFAULT NULL COMMENT 'double',
`c_decimal` decimal(10,5) DEFAULT NULL COMMENT 'decimal',
`c_date` date DEFAULT NULL COMMENT 'date',
`c_datetime` datetime DEFAULT NULL COMMENT 'datetime',
`c_timestamp` timestamp NULL DEFAULT NULL COMMENT 'timestamp',
`c_time` time DEFAULT NULL COMMENT 'time',
`c_char` char(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'char',
`c_varchar` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'varchar',
`c_blob` blob COMMENT 'blob',
`c_text` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT 'text',
`c_longblob` longblob COMMENT 'longblob',
`ex_column` char(30) DEFAULT NULL
) ENGINE=TIANMU DEFAULT CHARSET=latin1
insert into test_alter_table_option_charset(c_tinyint,c_smallint,c_mediumint,c_int,c_bigint,c_float,c_double,c_decimal,c_date,c_datetime,c_timestamp,c_time,c_char,c_varchar,c_text,ex_column)
values(106,106,106,106,106,6.2,60.88,106.083,'2017-02-25','2017-02-25 10:20:01','2017-02-25 05:20:01','10:21:01','stoneatom2','hello2','ddddbcdefghijklmn','ex_column_char');
ALTER TABLE test_alter_table_option_charset CHARACTER SET latin2;
show create table test_alter_table_option_charset;
Table Create Table
test_alter_table_option_charset CREATE TABLE `test_alter_table_option_charset` (
`c_tinyint` tinyint(4) DEFAULT NULL COMMENT 'tinyint',
`c_smallint` smallint(6) DEFAULT NULL COMMENT 'smallint',
`c_mediumint` mediumint(9) DEFAULT NULL COMMENT 'mediumint',
`c_int` int(11) DEFAULT NULL COMMENT 'int',
`c_bigint` bigint(20) DEFAULT NULL COMMENT 'bigint',
`c_float` float DEFAULT NULL COMMENT 'float',
`c_double` double DEFAULT NULL COMMENT 'double',
`c_decimal` decimal(10,5) DEFAULT NULL COMMENT 'decimal',
`c_date` date DEFAULT NULL COMMENT 'date',
`c_datetime` datetime DEFAULT NULL COMMENT 'datetime',
`c_timestamp` timestamp NULL DEFAULT NULL COMMENT 'timestamp',
`c_time` time DEFAULT NULL COMMENT 'time',
`c_char` char(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'char',
`c_varchar` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'varchar',
`c_blob` blob COMMENT 'blob',
`c_text` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT 'text',
`c_longblob` longblob COMMENT 'longblob',
`ex_column` char(30) CHARACTER SET latin1 DEFAULT NULL
) ENGINE=TIANMU DEFAULT CHARSET=latin2
alter table test_alter_table_option_charset add column ex_column2 varchar(30);
show create table test_alter_table_option_charset;
Table Create Table
test_alter_table_option_charset CREATE TABLE `test_alter_table_option_charset` (
`c_tinyint` tinyint(4) DEFAULT NULL COMMENT 'tinyint',
`c_smallint` smallint(6) DEFAULT NULL COMMENT 'smallint',
`c_mediumint` mediumint(9) DEFAULT NULL COMMENT 'mediumint',
`c_int` int(11) DEFAULT NULL COMMENT 'int',
`c_bigint` bigint(20) DEFAULT NULL COMMENT 'bigint',
`c_float` float DEFAULT NULL COMMENT 'float',
`c_double` double DEFAULT NULL COMMENT 'double',
`c_decimal` decimal(10,5) DEFAULT NULL COMMENT 'decimal',
`c_date` date DEFAULT NULL COMMENT 'date',
`c_datetime` datetime DEFAULT NULL COMMENT 'datetime',
`c_timestamp` timestamp NULL DEFAULT NULL COMMENT 'timestamp',
`c_time` time DEFAULT NULL COMMENT 'time',
`c_char` char(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'char',
`c_varchar` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'varchar',
`c_blob` blob COMMENT 'blob',
`c_text` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT 'text',
`c_longblob` longblob COMMENT 'longblob',
`ex_column` char(30) CHARACTER SET latin1 DEFAULT NULL,
`ex_column2` varchar(30) DEFAULT NULL
) ENGINE=TIANMU DEFAULT CHARSET=latin2
insert into test_alter_table_option_charset(c_tinyint,c_smallint,c_mediumint,c_int,c_bigint,c_float,c_double,c_decimal,c_date,c_datetime,c_timestamp,c_time,c_char,c_varchar,c_text,ex_column,ex_column2)
values(107,107,107,107,107,7.2,80.88,105.083,'2018-02-25','2018-02-25 10:20:01','2018-02-25 05:20:01','10:22:01','stoneatom3','hello3','ijklmnbcdefgh',"ex_column_char", 'ex_column_varchar2');
drop table test_alter_table_option_charset;
drop database test_issue848;
49 changes: 49 additions & 0 deletions mysql-test/suite/tianmu/t/issue848.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--source include/have_tianmu.inc
create database test_issue848;
use test_issue848;
CREATE TABLE `test_alter_table_option_charset` (
`c_tinyint` tinyint DEFAULT NULL COMMENT 'tinyint',
`c_smallint` smallint DEFAULT NULL COMMENT 'smallint',
`c_mediumint` mediumint DEFAULT NULL COMMENT 'mediumint',
`c_int` int DEFAULT NULL COMMENT 'int',
`c_bigint` bigint DEFAULT NULL COMMENT 'bigint',
`c_float` float DEFAULT NULL COMMENT 'float',
`c_double` double DEFAULT NULL COMMENT 'double',
`c_decimal` decimal(10,5) DEFAULT NULL COMMENT 'decimal',
`c_date` date DEFAULT NULL COMMENT 'date',
`c_datetime` datetime DEFAULT NULL COMMENT 'datetime',
`c_timestamp` timestamp NULL DEFAULT NULL COMMENT 'timestamp',
`c_time` time DEFAULT NULL COMMENT 'time',
`c_char` char(10) DEFAULT NULL COMMENT 'char',
`c_varchar` varchar(10) DEFAULT NULL COMMENT 'varchar',
`c_blob` blob COMMENT 'blob',
`c_text` text COMMENT 'text',
`c_longblob` longblob COMMENT 'longblob'
) ENGINE=TIANMU DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

show create table test_alter_table_option_charset;

ALTER TABLE test_alter_table_option_charset DEFAULT CHARACTER SET gbk;
show create table test_alter_table_option_charset;
insert into test_alter_table_option_charset(c_tinyint,c_smallint,c_mediumint,c_int,c_bigint,c_float,c_double,c_decimal,c_date,c_datetime,c_timestamp,c_time,c_char,c_varchar,c_text)
values(105,105,105,105,105,5.2,10.88,105.083,'2016-02-25','2016-02-25 10:20:01','2016-02-25 05:20:01','10:20:01','stoneatom1','hello1','bcdefghijklmn');

ALTER TABLE test_alter_table_option_charset CHARACTER SET latin1;
show create table test_alter_table_option_charset;

alter table test_alter_table_option_charset add column ex_column char(30);
show create table test_alter_table_option_charset;
insert into test_alter_table_option_charset(c_tinyint,c_smallint,c_mediumint,c_int,c_bigint,c_float,c_double,c_decimal,c_date,c_datetime,c_timestamp,c_time,c_char,c_varchar,c_text,ex_column)
values(106,106,106,106,106,6.2,60.88,106.083,'2017-02-25','2017-02-25 10:20:01','2017-02-25 05:20:01','10:21:01','stoneatom2','hello2','ddddbcdefghijklmn','ex_column_char');


ALTER TABLE test_alter_table_option_charset CHARACTER SET latin2;
show create table test_alter_table_option_charset;

alter table test_alter_table_option_charset add column ex_column2 varchar(30);
show create table test_alter_table_option_charset;
insert into test_alter_table_option_charset(c_tinyint,c_smallint,c_mediumint,c_int,c_bigint,c_float,c_double,c_decimal,c_date,c_datetime,c_timestamp,c_time,c_char,c_varchar,c_text,ex_column,ex_column2)
values(107,107,107,107,107,7.2,80.88,105.083,'2018-02-25','2018-02-25 10:20:01','2018-02-25 05:20:01','10:22:01','stoneatom3','hello3','ijklmnbcdefgh',"ex_column_char", 'ex_column_varchar2');

drop table test_alter_table_option_charset;
drop database test_issue848;
36 changes: 26 additions & 10 deletions storage/tianmu/handler/tianmu_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const Alter_inplace_info::HA_ALTER_FLAGS ha_tianmu::TIANMU_SUPPORTED_ALTER_ADD_D
Alter_inplace_info::ADD_COLUMN | Alter_inplace_info::DROP_COLUMN | Alter_inplace_info::ALTER_STORED_COLUMN_ORDER;
const Alter_inplace_info::HA_ALTER_FLAGS ha_tianmu::TIANMU_SUPPORTED_ALTER_COLUMN_NAME =
Alter_inplace_info::ALTER_COLUMN_DEFAULT | Alter_inplace_info::ALTER_COLUMN_NAME;
const Alter_inplace_info::HA_ALTER_FLAGS ha_tianmu::TIANMU_SUPPORTED_ALTER_TABLE_OPTIONS =
Alter_inplace_info::CHANGE_CREATE_OPTION;
/////////////////////////////////////////////////////////////////////
//
// NOTICE: ALL EXCEPTIONS SHOULD BE CAUGHT in the handler API!!!
Expand Down Expand Up @@ -1271,7 +1273,7 @@ int ha_tianmu::truncate() {
DBUG_RETURN(ret);
}

uint ha_tianmu::max_supported_key_part_length(HA_CREATE_INFO *create_info) const {
uint ha_tianmu::max_supported_key_part_length([[maybe_unused]] HA_CREATE_INFO *create_info) const {
if (tianmu_sysvar_large_prefix)
return (Tianmu::common::TIANMU_MAX_INDEX_COL_LEN_LARGE);
else
Expand Down Expand Up @@ -1501,6 +1503,11 @@ int ha_tianmu::reset() {
enum_alter_inplace_result ha_tianmu::check_if_supported_inplace_alter([[maybe_unused]] TABLE *altered_table,
Alter_inplace_info *ha_alter_info) {
DBUG_ENTER(__PRETTY_FUNCTION__);
if ((ha_alter_info->handler_flags & TIANMU_SUPPORTED_ALTER_TABLE_OPTIONS) &&
(ha_alter_info->create_info->used_fields & HA_CREATE_USED_DEFAULT_CHARSET)) {
DBUG_RETURN(HA_ALTER_INPLACE_EXCLUSIVE_LOCK);
}

if ((ha_alter_info->handler_flags & ~TIANMU_SUPPORTED_ALTER_ADD_DROP_ORDER) &&
(ha_alter_info->handler_flags != TIANMU_SUPPORTED_ALTER_COLUMN_NAME)) {
// support alter table column type
Expand All @@ -1519,14 +1526,18 @@ enum_alter_inplace_result ha_tianmu::check_if_supported_inplace_alter([[maybe_un
}

bool ha_tianmu::inplace_alter_table(TABLE *altered_table, Alter_inplace_info *ha_alter_info) {
DBUG_ENTER(__PRETTY_FUNCTION__);
try {
if (!(ha_alter_info->handler_flags & ~TIANMU_SUPPORTED_ALTER_ADD_DROP_ORDER)) {
if ((ha_alter_info->handler_flags & TIANMU_SUPPORTED_ALTER_TABLE_OPTIONS) &&
(ha_alter_info->create_info->used_fields & HA_CREATE_USED_DEFAULT_CHARSET)) {
DBUG_RETURN(false);
} else if (!(ha_alter_info->handler_flags & ~TIANMU_SUPPORTED_ALTER_ADD_DROP_ORDER)) {
std::vector<Field *> v_old(table_share->field, table_share->field + table_share->fields);
std::vector<Field *> v_new(altered_table->s->field, altered_table->s->field + altered_table->s->fields);
ha_rcengine_->PrepareAlterTable(table_name_, v_new, v_old, ha_thd());
return false;
DBUG_RETURN(false);
} else if (ha_alter_info->handler_flags == TIANMU_SUPPORTED_ALTER_COLUMN_NAME) {
return false;
DBUG_RETURN(false);
}
} catch (std::exception &e) {
TIANMU_LOG(LogCtl_Level::ERROR, "An exception is caught: %s", e.what());
Expand All @@ -1536,21 +1547,26 @@ bool ha_tianmu::inplace_alter_table(TABLE *altered_table, Alter_inplace_info *ha

my_message(static_cast<int>(common::ErrorCode::UNKNOWN_ERROR), "Unable to inplace alter table", MYF(0));

return true;
DBUG_RETURN(true);
}

bool ha_tianmu::commit_inplace_alter_table([[maybe_unused]] TABLE *altered_table, Alter_inplace_info *ha_alter_info,
bool commit) {
DBUG_ENTER(__PRETTY_FUNCTION__);
if (!commit) {
TIANMU_LOG(LogCtl_Level::INFO, "Alter table failed : %s%s", table_name_.c_str(), " rollback");
return true;
}
DBUG_RETURN(true);
if ((ha_alter_info->handler_flags & TIANMU_SUPPORTED_ALTER_TABLE_OPTIONS) &&
(ha_alter_info->create_info->used_fields & HA_CREATE_USED_DEFAULT_CHARSET)) {
DBUG_RETURN(false);
}
if (ha_alter_info->handler_flags == TIANMU_SUPPORTED_ALTER_COLUMN_NAME) {
return false;
DBUG_RETURN(false);
}
if ((ha_alter_info->handler_flags & ~TIANMU_SUPPORTED_ALTER_ADD_DROP_ORDER)) {
TIANMU_LOG(LogCtl_Level::INFO, "Altered table not support type %lu", ha_alter_info->handler_flags);
return true;
DBUG_RETURN(true);
}
fs::path tmp_dir(table_name_ + ".tmp");
fs::path tab_dir(table_name_ + common::TIANMU_EXT);
Expand Down Expand Up @@ -1583,9 +1599,9 @@ bool ha_tianmu::commit_inplace_alter_table([[maybe_unused]] TABLE *altered_table
TIANMU_LOG(LogCtl_Level::ERROR, "file system error: %s %s|%s", e.what(), e.path1().string().c_str(),
e.path2().string().c_str());
my_message(static_cast<int>(common::ErrorCode::UNKNOWN_ERROR), "Failed to commit alter table", MYF(0));
return true;
DBUG_RETURN(true);
}
return false;
DBUG_RETURN(false);
}
/*
key: mysql format, may be union key, need changed to kvstore key format
Expand Down
1 change: 1 addition & 0 deletions storage/tianmu/handler/tianmu_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class ha_tianmu final : public handler {
public:
static const Alter_inplace_info::HA_ALTER_FLAGS TIANMU_SUPPORTED_ALTER_ADD_DROP_ORDER;
static const Alter_inplace_info::HA_ALTER_FLAGS TIANMU_SUPPORTED_ALTER_COLUMN_NAME;
static const Alter_inplace_info::HA_ALTER_FLAGS TIANMU_SUPPORTED_ALTER_TABLE_OPTIONS;

protected:
int set_cond_iter();
Expand Down

0 comments on commit 478cf5e

Please sign in to comment.