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;
2 add implement in inplace_alter_table;
3 add implement in commit_inplace_alter_table;
  • Loading branch information
lujiashun committed Nov 1, 2022
1 parent e431339 commit ac6b03d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
41 changes: 28 additions & 13 deletions storage/tianmu/handler/tianmu_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1498,9 +1498,15 @@ int TianmuHandler::reset() {
enum_alter_inplace_result TianmuHandler::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_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
// support alter table column type
if (ha_alter_info->handler_flags & Alter_inplace_info::ALTER_STORED_COLUMN_TYPE)
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
// support alter table column exceeded length
Expand All @@ -1517,13 +1523,16 @@ enum_alter_inplace_result TianmuHandler::check_if_supported_inplace_alter([[mayb

bool TianmuHandler::inplace_alter_table(TABLE *altered_table, Alter_inplace_info *ha_alter_info) {
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_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 @@ -1533,21 +1542,25 @@ bool TianmuHandler::inplace_alter_table(TABLE *altered_table, Alter_inplace_info

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

return true;
DBUG_RETURN(true);
}

bool TianmuHandler::commit_inplace_alter_table([[maybe_unused]] TABLE *altered_table, Alter_inplace_info *ha_alter_info,
bool commit) {
if (!commit) {
TIANMU_LOG(LogCtl_Level::INFO, "Alter table failed : %s%s", table_name_.c_str(), " rollback");
return true;
TIANMU_LOG(LogCtl_Level::INFO, "Alter table failed : %s%s", m_table_name.c_str(), " rollback");
DBUG_RETURN(true);
}
if ((ha_alter_info->handler_flags & TIANMU_SUPPORTED_ALTER_TABLE_OPTIONS) &&
(ha_alter_info->create_info->used_fields & HA_CREATE_USED_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 All @@ -1561,12 +1574,14 @@ bool TianmuHandler::commit_inplace_alter_table([[maybe_unused]] TABLE *altered_t
std::unordered_set<std::string> s;
for (auto &it : fs::directory_iterator(tab_dir / common::COLUMN_DIR)) {
auto target = fs::read_symlink(it.path()).string();
if (!target.empty()) s.insert(target);
if (!target.empty())
s.insert(target);
}

for (auto &it : fs::directory_iterator(bak_dir / common::COLUMN_DIR)) {
auto target = fs::read_symlink(it.path()).string();
if (target.empty()) continue;
if (target.empty())
continue;
auto search = s.find(target);
if (search == s.end()) {
fs::remove_all(target);
Expand All @@ -1578,9 +1593,9 @@ bool TianmuHandler::commit_inplace_alter_table([[maybe_unused]] TABLE *altered_t
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 TianmuHandler 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 ac6b03d

Please sign in to comment.