Skip to content

Commit

Permalink
Remove useless fields
Browse files Browse the repository at this point in the history
  • Loading branch information
JaySon-Huang committed Aug 21, 2024
1 parent cf5a204 commit 69c7428
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 106 deletions.
3 changes: 0 additions & 3 deletions dbms/src/Debug/MockTiDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ TiDB::TableInfoPtr MockTiDB::parseColumns(
index_info.id = 1;
index_info.is_primary = true;
index_info.idx_name = "PRIMARY";
index_info.tbl_name = tbl_name;
index_info.is_unique = true;
index_info.index_type = 1;
index_info.idx_cols.resize(string_tokens.count());
Expand All @@ -296,8 +295,6 @@ TiDB::TableInfoPtr MockTiDB::parseColumns(
table_info.pk_is_handle = true;
}

table_info.comment = "Mocked.";

// set storage engine type
std::transform(engine_type.begin(), engine_type.end(), engine_type.begin(), [](unsigned char c) {
return std::tolower(c);
Expand Down
118 changes: 55 additions & 63 deletions dbms/src/TiDB/Schema/TiDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ namespace DB
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
extern const int INCORRECT_DATA;
} // namespace ErrorCodes
extern const UInt8 TYPE_CODE_LITERAL;
extern const UInt8 LITERAL_NIL;

Expand Down Expand Up @@ -378,27 +379,29 @@ try
json->set("origin_default", origin_default_value);
json->set("default", default_value);
json->set("default_bit", default_bit_value);
Poco::JSON::Object::Ptr tp_json = new Poco::JSON::Object();
tp_json->set("Tp", static_cast<Int32>(tp));
tp_json->set("Flag", flag);
tp_json->set("Flen", flen);
tp_json->set("Decimal", decimal);
tp_json->set("Charset", charset);
tp_json->set("Collate", collate);
if (!elems.empty())
{
Poco::JSON::Array::Ptr elem_arr = new Poco::JSON::Array();
for (const auto & elem : elems)
elem_arr->add(elem.first);
tp_json->set("Elems", elem_arr);
}
else
{
tp_json->set("Elems", Poco::Dynamic::Var());
// "type" field
Poco::JSON::Object::Ptr tp_json = new Poco::JSON::Object();
tp_json->set("Tp", static_cast<Int32>(tp));
tp_json->set("Flag", flag);
tp_json->set("Flen", flen);
tp_json->set("Decimal", decimal);
tp_json->set("Charset", charset);
tp_json->set("Collate", collate);
if (!elems.empty())
{
Poco::JSON::Array::Ptr elem_arr = new Poco::JSON::Array();
for (const auto & elem : elems)
elem_arr->add(elem.first);
tp_json->set("Elems", elem_arr);
}
else
{
tp_json->set("Elems", Poco::Dynamic::Var());
}
json->set("type", tp_json);
}
json->set("type", tp_json);
json->set("state", static_cast<Int32>(state));
json->set("comment", comment);

#ifndef NDEBUG
// Check stringify in Debug mode
Expand Down Expand Up @@ -427,28 +430,30 @@ try
default_value = json->get("default");
if (!json->isNull("default_bit"))
default_bit_value = json->get("default_bit");
auto type_json = json->getObject("type");
tp = static_cast<TP>(type_json->getValue<Int32>("Tp"));
flag = type_json->getValue<UInt32>("Flag");
flen = type_json->getValue<Int64>("Flen");
decimal = type_json->getValue<Int64>("Decimal");
if (!type_json->isNull("Elems"))
{
auto elems_arr = type_json->getArray("Elems");
size_t elems_size = elems_arr->size();
for (size_t i = 1; i <= elems_size; i++)
// type
auto type_json = json->getObject("type");
tp = static_cast<TP>(type_json->getValue<Int32>("Tp"));
flag = type_json->getValue<UInt32>("Flag");
flen = type_json->getValue<Int64>("Flen");
decimal = type_json->getValue<Int64>("Decimal");
if (!type_json->isNull("Elems"))
{
elems.push_back(std::make_pair(elems_arr->getElement<String>(i - 1), static_cast<Int16>(i)));
auto elems_arr = type_json->getArray("Elems");
size_t elems_size = elems_arr->size();
for (size_t i = 1; i <= elems_size; i++)
{
elems.push_back(std::make_pair(elems_arr->getElement<String>(i - 1), static_cast<Int16>(i)));
}
}
/// need to do this check for forward compatibility
if (!type_json->isNull("Charset"))
charset = type_json->get("Charset");
/// need to do this check for forward compatibility
if (!type_json->isNull("Collate"))
collate = type_json->get("Collate");
}
/// need to do this check for forward compatibility
if (!type_json->isNull("Charset"))
charset = type_json->get("Charset");
/// need to do this check for forward compatibility
if (!type_json->isNull("Collate"))
collate = type_json->get("Collate");
state = static_cast<SchemaState>(json->getValue<Int32>("state"));
comment = json->getValue<String>("comment");
}
catch (const Poco::Exception & e)
{
Expand All @@ -475,7 +480,6 @@ try
name_json->set("O", name);
name_json->set("L", name);
json->set("name", name_json);
json->set("comment", comment);

#ifndef NDEBUG
// Check stringify in Debug mode
Expand All @@ -497,8 +501,6 @@ try
{
id = json->getValue<TableID>("id");
name = json->getObject("name")->getValue<String>("L");
if (json->has("comment"))
comment = json->getValue<String>("comment");
}
catch (const Poco::Exception & e)
{
Expand Down Expand Up @@ -763,16 +765,10 @@ catch (const Poco::Exception & e)
///////////////////////

IndexInfo::IndexInfo(Poco::JSON::Object::Ptr json)
: id(0)
, state(TiDB::SchemaState::StateNone)
, index_type(0)
, is_unique(true)
, is_primary(true)
, is_invisible(true)
, is_global(true)
{
deserialize(json);
}

Poco::JSON::Object::Ptr IndexInfo::getJSONObject() const
try
{
Expand All @@ -785,11 +781,6 @@ try
idx_name_json->set("L", idx_name);
json->set("idx_name", idx_name_json);

Poco::JSON::Object::Ptr tbl_name_json = new Poco::JSON::Object();
tbl_name_json->set("O", tbl_name);
tbl_name_json->set("L", tbl_name);
json->set("tbl_name", tbl_name_json);

Poco::JSON::Array::Ptr cols_array = new Poco::JSON::Array();
for (const auto & col : idx_cols)
{
Expand Down Expand Up @@ -823,7 +814,6 @@ try
{
id = json->getValue<Int64>("id");
idx_name = json->getObject("idx_name")->getValue<String>("L");
tbl_name = json->getObject("tbl_name")->getValue<String>("L");

auto cols_array = json->getArray("idx_cols");
idx_cols.clear();
Expand Down Expand Up @@ -904,7 +894,6 @@ try
json->set("state", static_cast<Int32>(state));
json->set("pk_is_handle", pk_is_handle);
json->set("is_common_handle", is_common_handle);
json->set("comment", comment);
json->set("update_timestamp", update_timestamp);
if (is_partition_table)
{
Expand Down Expand Up @@ -969,26 +958,28 @@ try
}
}

auto index_arr = obj->getArray("index_info");
index_infos.clear();
if (!index_arr.isNull())
bool has_primary_index = false;
if (auto index_arr = obj->getArray("index_info"); !index_arr.isNull())
{
for (size_t i = 0; i < index_arr->size(); i++)
{
auto index_info_json = index_arr->getObject(i);
IndexInfo index_info(index_info_json);
// We only keep the "primary index" in tiflash now
if (index_info.is_primary)
index_infos.emplace_back(index_info);
{
has_primary_index = true;
// always put the primary_index at the front of all index_info
index_infos.insert(index_infos.begin(), std::move(index_info));
}
}
}

state = static_cast<SchemaState>(obj->getValue<Int32>("state"));
pk_is_handle = obj->getValue<bool>("pk_is_handle");
if (obj->has("is_common_handle"))
is_common_handle = obj->getValue<bool>("is_common_handle");
if (!is_common_handle)
index_infos.clear();
comment = obj->getValue<String>("comment");
if (obj->has("update_timestamp"))
update_timestamp = obj->getValue<Timestamp>("update_timestamp");
auto partition_obj = obj->getObject("partition");
Expand Down Expand Up @@ -1019,12 +1010,13 @@ try
replica_info.deserialize(replica_obj);
}
}
if (is_common_handle && index_infos.size() != 1)
if (is_common_handle && !has_primary_index)
{
throw DB::Exception(
std::string(__PRETTY_FUNCTION__)
+ ": Parse TiDB schema JSON failed (TableInfo): clustered index without primary key info, json: "
+ JSONToString(obj));
DB::ErrorCodes::INCORRECT_DATA,
"{}: Parse TiDB schema JSON failed (TableInfo): clustered index without primary key info, json: ",
__PRETTY_FUNCTION__,
JSONToString(obj));
}
}
catch (const Poco::Exception & e)
Expand Down
7 changes: 1 addition & 6 deletions dbms/src/TiDB/Schema/TiDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ struct ColumnInfo
// Elems is the element list for enum and set type.
std::vector<std::pair<std::string, Int16>> elems;
SchemaState state = StateNone;
String comment;

#ifdef M
#error "Please undefine macro M first."
Expand Down Expand Up @@ -167,7 +166,6 @@ struct PartitionDefinition
TableID id = DB::InvalidTableID;
String name;
// LessThan []string `json:"less_than"`
String comment;
};

struct PartitionInfo
Expand Down Expand Up @@ -252,7 +250,6 @@ struct IndexInfo

Int64 id = -1;
String idx_name;
String tbl_name;
std::vector<IndexColumnInfo> idx_cols;
SchemaState state = StatePublic;
Int32 index_type = -1;
Expand Down Expand Up @@ -292,14 +289,12 @@ struct TableInfo
std::vector<ColumnInfo> columns;
/// index_infos stores the index info from TiDB. But we do not store all
/// the index infos because most of the index info is useless in TiFlash.
/// If is_common_handle = true, the primary index info is stored
/// otherwise, all of the index info are ignored
/// Only the primary index info is stored now
std::vector<IndexInfo> index_infos;
SchemaState state = StateNone;
bool pk_is_handle = false;
/// when is_common_handle = true, it means this table is a clustered index table
bool is_common_handle = false;
String comment;
Timestamp update_timestamp = 0;
bool is_partition_table = false;
TableID belonging_table_id = DB::InvalidTableID;
Expand Down
Loading

0 comments on commit 69c7428

Please sign in to comment.