Skip to content

Commit

Permalink
feat(tianmu):develop some auto_increment function for tianmu(stoneato…
Browse files Browse the repository at this point in the history
  • Loading branch information
isredstar committed Feb 15, 2023
1 parent 64fc98c commit f23e810
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 16 deletions.
7 changes: 7 additions & 0 deletions sql/sql_optimizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,14 @@ JOIN::optimize(OptimizePhase phase)
if (phase == OptimizePhase::Beginning || phase == OptimizePhase::Before_LOJ_Transform)
{
assert(tables == 0 && primary_tables == 0 && tables_list == (TABLE_LIST*)1);
<<<<<<< HEAD
<<<<<<< HEAD

=======
>>>>>>> 0531e6255... feat(tianmu):improve the code quality and refactoring derived table optimize for tianmu(#1277, #1276, #1258)
=======

>>>>>>> 2d9e2f32a... 112
// to prevent double initialization on EXPLAIN
if (optimized)
DBUG_RETURN(0);
Expand Down
49 changes: 49 additions & 0 deletions sql/sql_select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,15 @@ uint find_shortest_key(TABLE *table, const key_map *usable_keys);
Such queries are executed with a more direct code path.
*/
bool handle_query(THD *thd, LEX *lex, Query_result *result, ulonglong added_options,
<<<<<<< HEAD
<<<<<<< HEAD
ulonglong removed_options, int is_optimize_after_tianmu, int free_join_from_bh)
=======
ulonglong removed_options, int optimize_after_tianmu, int free_join_from_bh)
>>>>>>> 0531e6255... feat(tianmu):improve the code quality and refactoring derived table optimize for tianmu(#1277, #1276, #1258)
=======
ulonglong removed_options, int optimize_after_tianmu, int free_join_from_bh)
>>>>>>> 2d9e2f32a... 112
{
DBUG_ENTER("handle_query");

Expand Down Expand Up @@ -144,8 +152,18 @@ bool handle_query(THD *thd, LEX *lex, Query_result *result, ulonglong added_opti
else
{
res = FALSE;
<<<<<<< HEAD
<<<<<<< HEAD
if (is_optimize_after_tianmu)
res = lex->unit->optimize_after_tianmu(); // optimization after Tianmu
=======
if (optimize_after_tianmu)
res = lex->unit->after_optimize_tianmu(); // optimization after Tianmu
>>>>>>> 0531e6255... feat(tianmu):improve the code quality and refactoring derived table optimize for tianmu(#1277, #1276, #1258)
=======
if (optimize_after_tianmu)
res = lex->unit->optimize_after_tianmu(); // optimization after Tianmu
>>>>>>> 2d9e2f32a... 112
if (!res)
if (unit->prepare(thd, result, SELECT_NO_UNLOCK | added_options,
removed_options))
Expand Down Expand Up @@ -1030,6 +1048,37 @@ bool SELECT_LEX::optimize(THD *thd)
DBUG_RETURN(false);
}

/**
Optimize a query block and all inner query expressions
@param thd thread handler
@returns false if success, true if error
*/

bool SELECT_LEX::optimize_select_for_tianmu(THD *thd)
{
DBUG_ENTER("SELECT_LEX::optimize");

assert(join == NULL);
JOIN *const join_local= new JOIN(thd, this);
if (!join_local)
DBUG_RETURN(true); /* purecov: inspected */

set_join(join_local);

if (join->optimize(OptimizePhase::Before_LOJ_Transform))
DBUG_RETURN(true);

for (SELECT_LEX_UNIT *unit= first_inner_unit(); unit; unit= unit->next_unit())
{
// Derived tables and const subqueries are already optimized
if (!unit->is_optimized() && unit->optimize(thd))
DBUG_RETURN(true);
}

DBUG_RETURN(false);
}

/*****************************************************************************
Go through all combinations of not marked tables and find the one
which uses least records
Expand Down
2 changes: 1 addition & 1 deletion sql/sql_select.h
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ class store_key_const_item :public store_key_item
bool error_if_full_join(JOIN *join);
bool handle_query(THD *thd, LEX *lex, Query_result *result,
ulonglong added_options, ulonglong removed_options,
int is_optimize_after_tianmu=0, int free_join_from_tianmu=0);
int is_optimize_after_tianmu=0, int free_join_from_tianmu=0);

void free_underlaid_joins(THD *thd, SELECT_LEX *select);

Expand Down
8 changes: 6 additions & 2 deletions storage/tianmu/core/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,8 @@ uint32_t Engine::GetNextTableId() {
return seq;
}

std::shared_ptr<TableOption> Engine::GetTableOption(const std::string &table, TABLE *form) {
std::shared_ptr<TableOption> Engine::GetTableOption(const std::string &table, TABLE *form,
HA_CREATE_INFO *create_info) {
auto opt = std::make_shared<TableOption>();

int power = has_pack(form->s->comment);
Expand All @@ -595,10 +596,13 @@ std::shared_ptr<TableOption> Engine::GetTableOption(const std::string &table, TA

opt->path = table + common::TIANMU_EXT;
opt->name = form->s->table_name.str;
opt->create_info = create_info;
return opt;
}

void Engine::CreateTable(const std::string &table, TABLE *form) { TianmuTable::CreateNew(GetTableOption(table, form)); }
void Engine::CreateTable(const std::string &table, TABLE *form, HA_CREATE_INFO *create_info) {
TianmuTable::CreateNew(GetTableOption(table, form, create_info));
}

AttributeTypeInfo Engine::GetAttrTypeInfo(const Field &field) {
bool auto_inc = field.flags & AUTO_INCREMENT_FLAG;
Expand Down
4 changes: 2 additions & 2 deletions storage/tianmu/core/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Engine final {
~Engine();

int Init(uint engine_slot);
void CreateTable(const std::string &table, TABLE *from);
void CreateTable(const std::string &table, TABLE *from, HA_CREATE_INFO *create_info);
int DeleteTable(const char *table, THD *thd);
void TruncateTable(const std::string &table_path, THD *thd);
int RenameTable(Transaction *trans_, const std::string &from, const std::string &to, THD *thd);
Expand Down Expand Up @@ -176,7 +176,7 @@ class Engine final {
static std::unique_ptr<system::IOParameters> CreateIOParameters(const std::string &path, void *arg);
static std::unique_ptr<system::IOParameters> CreateIOParameters(THD *thd, TABLE *table, void *arg);
void LogStat();
std::shared_ptr<TableOption> GetTableOption(const std::string &table, TABLE *form);
std::shared_ptr<TableOption> GetTableOption(const std::string &table, TABLE *form, HA_CREATE_INFO *create_info);
std::shared_ptr<TableShare> getTableShare(const std::string &table_path);
void ProcessDelayedInsert();
void ProcessDelayedMerge();
Expand Down
1 change: 1 addition & 0 deletions storage/tianmu/core/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include "filter.h"

#include "common/assert.h"
#include "core/tools.h"

Expand Down
20 changes: 16 additions & 4 deletions storage/tianmu/core/table_share.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,23 @@ TableShare::TableShare(const fs::path &table_path, const TABLE_SHARE *table_shar

m_columns.reserve(no_cols);

system::TianmuFile fv;
system::TianmuFile fv, fattr;
fv.OpenReadOnly(table_path / common::TABLE_VERSION_FILE);

for (uint i = 0; i < no_cols; i++) {
common::TX_ID xid;
fv.ReadExact(&xid, sizeof(xid));
m_columns.emplace_back(std::make_unique<ColumnShare>(
this, xid, i, table_path / common::COLUMN_DIR / std::to_string(i), table_share->field[i]));
Field *field = table_share->field[i];
auto colpath = table_path / common::COLUMN_DIR / std::to_string(i);
if (field->flags & AUTO_INCREMENT_FLAG) {
COL_VER_HDR hdr{};
fattr.OpenReadOnly(colpath / common::COL_VERSION_DIR / xid.ToString());
fattr.ReadExact(&hdr, sizeof(hdr));
SetAutoIncInfo(hdr.auto_inc_next);
}
m_columns.emplace_back(
std::make_unique<ColumnShare>(this, xid, i, colpath, field));
}

thr_lock_init(&thr_lock);
}

Expand Down Expand Up @@ -114,5 +122,9 @@ unsigned long TableShare::GetCreateTime() {
unsigned long TableShare::GetUpdateTime() {
return system::GetFileTime((table_path / common::TABLE_VERSION_FILE).string());
}

uint64_t TableShare::GetAutoIncInfo() { return autoinc_; }

void TableShare::SetAutoIncInfo(uint64_t auto_inc_value) { autoinc_ = auto_inc_value; }
} // namespace core
} // namespace Tianmu
3 changes: 3 additions & 0 deletions storage/tianmu/core/table_share.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class TableShare final {

unsigned long GetCreateTime();
unsigned long GetUpdateTime();
uint64_t GetAutoIncInfo(); // get auto inc value
void SetAutoIncInfo(uint64_t auto_inc_value);

ColumnShare *GetColumnShare(size_t i) { return m_columns[i].get(); }
void CommitWrite(TianmuTable *t);
Expand All @@ -69,6 +71,7 @@ class TableShare final {

private:
TABLE_META meta;
uint64_t autoinc_;
size_t no_cols;
fs::path table_path;

Expand Down
8 changes: 7 additions & 1 deletion storage/tianmu/core/tianmu_attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ TianmuAttr::TianmuAttr(Transaction *tx, common::TX_ID xid, int a_num, int t_num,
};
}

void TianmuAttr::Create(const fs::path &dir, const AttributeTypeInfo &ati, uint8_t pss, size_t no_rows) {
void TianmuAttr::Create(const fs::path &dir, const AttributeTypeInfo &ati, uint8_t pss, size_t no_rows,
uint64_t auto_inc_value) {
uint32_t no_pack = common::rows2packs(no_rows, pss);

// write meta data(immutable)
Expand Down Expand Up @@ -112,6 +113,11 @@ void TianmuAttr::Create(const fs::path &dir, const AttributeTypeInfo &ati, uint8
dict->SaveData(dir / common::COL_DICT_DIR / std::to_string(1));
}

// auto_increment
if (ati.AutoInc() && auto_inc_value != 0) {
hdr.auto_inc_next = auto_inc_value;
}

// create version directory
fs::create_directory(dir / common::COL_VERSION_DIR);

Expand Down
3 changes: 2 additions & 1 deletion storage/tianmu/core/tianmu_attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ class TianmuAttr final : public mm::TraceableObject, public PhysicalColumn, publ
TianmuAttr &operator=(const TianmuAttr &) = delete;
~TianmuAttr() = default;

static void Create(const fs::path &path, const AttributeTypeInfo &ati, uint8_t pss, size_t no_rows);
static void Create(const fs::path &path, const AttributeTypeInfo &ati, uint8_t pss, size_t no_rows,
uint64_t auto_inc_value = 0);

mm::TO_TYPE TraceableType() const override { return mm::TO_TYPE::TO_TEMPORARY; }
void UpdateData(uint64_t row, Value &v);
Expand Down
13 changes: 10 additions & 3 deletions storage/tianmu/core/tianmu_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void TianmuTable::CreateNew(const std::shared_ptr<TableOption> &opt) {
uint32_t tid = ha_tianmu_engine_->GetNextTableId();
auto &path(opt->path);
uint32_t no_attrs = opt->atis.size();

uint64_t auto_inc_value = opt->create_info->auto_increment_value;
fs::create_directory(path);

TABLE_META meta{common::FILE_MAGIC, common::TABLE_DATA_VERSION, tid, opt->pss};
Expand Down Expand Up @@ -88,7 +88,7 @@ void TianmuTable::CreateNew(const std::shared_ptr<TableOption> &opt) {
auto lnk = column_path / std::to_string(idx);
fs::create_symlink(dir, lnk);

TianmuAttr::Create(lnk, opt->atis[idx], opt->pss, 0);
TianmuAttr::Create(lnk, opt->atis[idx], opt->pss, 0, auto_inc_value);
// TIANMU_LOG(LogCtl_Level::INFO, "Column %zu at %s", idx, dir.c_str());
}
TIANMU_LOG(LogCtl_Level::INFO, "Create table %s, ID = %u", opt->path.c_str(), tid);
Expand Down Expand Up @@ -181,11 +181,18 @@ TianmuTable::TianmuTable(std::string const &p, TableShare *s, Transaction *tx) :
ASSERT(sz == fs::file_size(fs::canonical(fs::read_symlink(m_path / common::TABLE_VERSION_FILE), m_path)),
"bad version file size. expected " + std::to_string(sz));

system::TianmuFile f;
system::TianmuFile f, fattr;
f.OpenReadOnly(m_path / common::TABLE_VERSION_FILE);
f.ReadExact(&m_versions[0], sz);

for (uint32_t i = 0; i < share->NumOfCols(); i++) {
if (share->GetColumnShare(i)->ColType().GetAutoInc()) {
COL_VER_HDR hdr{};
auto colpath = m_path / common::COLUMN_DIR / std::to_string(i);
fattr.OpenReadOnly(colpath / common::COL_VERSION_DIR / m_versions[i].ToString());
fattr.ReadExact(&hdr, sizeof(hdr));
share->SetAutoIncInfo(hdr.auto_inc_next);
}
auto &attr = m_attrs.emplace_back(
std::make_unique<TianmuAttr>(m_tx, m_versions[i], i, share->TabID(), share->GetColumnShare(i)));
attr->TrackAccess();
Expand Down
1 change: 1 addition & 0 deletions storage/tianmu/core/tianmu_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct TableOption {
std::string name;
int id;
uint8_t pss;
HA_CREATE_INFO *create_info;
};

class DataPackLock : public FunctionExecutor {
Expand Down
48 changes: 46 additions & 2 deletions storage/tianmu/handler/ha_tianmu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "core/tools.h"
#include "core/transaction.h"
#include "core/value.h"
#include "core/column_share.h"
#include "ha_tianmu.h"
#include "mm/initializer.h"
#include "system/configuration.h"
Expand Down Expand Up @@ -651,7 +652,12 @@ int ha_tianmu::rename_table(const char *from, const char *to) {
DBUG_RETURN(1);
}

void ha_tianmu::update_create_info([[maybe_unused]] HA_CREATE_INFO *create_info) {}
void ha_tianmu::update_create_info(HA_CREATE_INFO *create_info) {
if (!(create_info->used_fields & HA_CREATE_USED_AUTO)) {
info(HA_STATUS_AUTO);
create_info->auto_increment_value = stats.auto_increment_value;
}
}

/*
::info() is used to return information to the optimizer.
Expand Down Expand Up @@ -731,6 +737,9 @@ int ha_tianmu::info(uint flag) {
my_store_ptr(dup_ref, ref_length, dupkey_pos_);
}

if ((flag & HA_STATUS_AUTO) && table->found_next_number_field)
stats.auto_increment_value = share_->GetAutoIncInfo();

ret = 0;
} catch (std::exception &e) {
my_message(static_cast<int>(common::ErrorCode::UNKNOWN_ERROR), e.what(), MYF(0));
Expand Down Expand Up @@ -1313,7 +1322,7 @@ int ha_tianmu::create(const char *name, TABLE *table_arg, [[maybe_unused]] HA_CR
DBUG_RETURN(ER_WRONG_TABLE_NAME);
}

ha_tianmu_engine_->CreateTable(name, table_arg);
ha_tianmu_engine_->CreateTable(name, table_arg, create_info);
DBUG_RETURN(0);
} catch (common::AutoIncException &e) {
my_message(ER_WRONG_AUTO_KEY, e.what(), MYF(0));
Expand Down Expand Up @@ -1614,6 +1623,9 @@ enum_alter_inplace_result ha_tianmu::check_if_supported_inplace_alter([[maybe_un
// support alter table comment
if (ha_alter_info->create_info->used_fields & HA_CREATE_USED_COMMENT)
DBUG_RETURN(HA_ALTER_INPLACE_EXCLUSIVE_LOCK);
// support alter table auto_increment
if (ha_alter_info->create_info->used_fields & HA_CREATE_USED_AUTO)
DBUG_RETURN(HA_ALTER_INPLACE_NO_LOCK_AFTER_PREPARE);
}

// use copy when add column with not null
Expand Down Expand Up @@ -1661,6 +1673,36 @@ bool ha_tianmu::inplace_alter_table(TABLE *altered_table, Alter_inplace_info *ha
DBUG_RETURN(false);
if (ha_alter_info->create_info->used_fields & HA_CREATE_USED_COMMENT)
DBUG_RETURN(false);
if (ha_alter_info->create_info->used_fields & HA_CREATE_USED_AUTO) {

std::vector<Field *> field_info(table_share->field, table_share->field + table_share->fields);
auto share = ha_tianmu_engine_->GetTableShare(table_share);
fs::path tab_dir = table_name_ + common::TIANMU_EXT;
for (uint i = 0; i < table_share->fields; i++) {

if (field_info[i]->flags & AUTO_INCREMENT_FLAG) {
system::TianmuFile fv, fr, fw;
fv.OpenReadOnly(tab_dir / common::TABLE_VERSION_FILE);
common::TX_ID xid;
fv.ReadExact(&xid, sizeof(xid));

Tianmu::core::COL_VER_HDR hdr{};

fs::path fname = tab_dir / common::COLUMN_DIR / std::to_string(i) / common::COL_VERSION_DIR / xid.ToString();
fw.OpenReadWrite(fname);
fw.ReadExact(&hdr, sizeof(hdr));
uint64_t autoinc_ = ha_alter_info->create_info->auto_increment_value;
hdr.auto_inc_next = autoinc_;
fw.WriteExact(&hdr, sizeof(hdr));
fw.Flush();

share->SetAutoIncInfo(autoinc_);

DBUG_RETURN(false);
}
}
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);
Expand Down Expand Up @@ -1699,6 +1741,8 @@ bool ha_tianmu::commit_inplace_alter_table([[maybe_unused]] TABLE *altered_table
DBUG_RETURN(false);
if (ha_alter_info->create_info->used_fields & HA_CREATE_USED_COMMENT)
DBUG_RETURN(false);
if (ha_alter_info->create_info->used_fields & HA_CREATE_USED_AUTO)
DBUG_RETURN(false);
}
if (ha_alter_info->handler_flags == TIANMU_SUPPORTED_ALTER_COLUMN_NAME) {
DBUG_RETURN(false);
Expand Down

0 comments on commit f23e810

Please sign in to comment.