Skip to content

Commit 838a14d

Browse files
fix: sql and ns client desc result (#341)
* fix sql/ns client desc result
1 parent c6553b7 commit 838a14d

File tree

5 files changed

+100
-130
lines changed

5 files changed

+100
-130
lines changed

src/catalog/schema_adapter.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ class SchemaAdapter {
8383
}
8484
for (int32_t i = 0; i < index.size(); i++) {
8585
const ::openmldb::common::ColumnKey& key = index.Get(i);
86-
::hybridse::type::IndexDef* index = output->Add();
87-
index->set_name(key.index_name());
88-
index->mutable_first_keys()->CopyFrom(key.col_name());
86+
::hybridse::type::IndexDef* index_def = output->Add();
87+
index_def->set_name(key.index_name());
88+
index_def->mutable_first_keys()->CopyFrom(key.col_name());
8989
if (key.has_ts_name() && !key.ts_name().empty()) {
90-
index->set_second_key(key.ts_name());
91-
index->set_ts_offset(0);
90+
index_def->set_second_key(key.ts_name());
91+
index_def->set_ts_offset(0);
9292
}
9393
if (key.has_ttl()) {
9494
auto ttl_type = key.ttl().ttl_type();
@@ -97,14 +97,14 @@ class SchemaAdapter {
9797
LOG(WARNING) << "not found " << ::openmldb::type::TTLType_Name(ttl_type);
9898
return false;
9999
}
100-
index->set_ttl_type(it->second);
100+
index_def->set_ttl_type(it->second);
101101
if (ttl_type == ::openmldb::type::kAbsAndLat || ttl_type == ::openmldb::type::kAbsOrLat) {
102-
index->add_ttl(key.ttl().abs_ttl());
103-
index->add_ttl(key.ttl().lat_ttl());
102+
index_def->add_ttl(key.ttl().abs_ttl());
103+
index_def->add_ttl(key.ttl().lat_ttl());
104104
} else if (ttl_type == ::openmldb::type::kAbsoluteTime) {
105-
index->add_ttl(key.ttl().abs_ttl());
105+
index_def->add_ttl(key.ttl().abs_ttl());
106106
} else {
107-
index->add_ttl(key.ttl().lat_ttl());
107+
index_def->add_ttl(key.ttl().lat_ttl());
108108
}
109109
}
110110
}

src/cmd/display.h

Lines changed: 60 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <utility>
2626
#include <vector>
2727

28+
#include "base/texttable.h"
2829
#include "cmd/sdk_iterator.h"
2930
#include "codec/flat_array.h"
3031
#include "codec/row_codec.h"
@@ -52,88 +53,61 @@ static void TransferString(std::vector<std::string>* vec) {
5253
});
5354
}
5455

55-
__attribute__((unused)) static void PrintSchema(
56-
const google::protobuf::RepeatedPtrField<::openmldb::common::ColumnDesc>& column_desc_field) {
57-
std::vector<std::string> row;
58-
row.push_back("#");
59-
row.push_back("name");
60-
row.push_back("type");
61-
::baidu::common::TPrinter tp(row.size(), FLAGS_max_col_display_length);
62-
tp.AddRow(row);
63-
uint32_t idx = 0;
64-
for (const auto& column_desc : column_desc_field) {
65-
row.clear();
66-
row.push_back(std::to_string(idx));
67-
row.push_back(column_desc.name());
68-
auto iter = ::openmldb::codec::DATA_TYPE_STR_MAP.find(column_desc.data_type());
69-
if (iter != ::openmldb::codec::DATA_TYPE_STR_MAP.end()) {
70-
row.push_back(iter->second);
71-
} else {
72-
row.push_back("-");
73-
}
74-
tp.AddRow(row);
75-
idx++;
76-
}
77-
tp.Print(true);
78-
}
79-
8056
__attribute__((unused)) static void PrintSchema(
8157
const google::protobuf::RepeatedPtrField<::openmldb::common::ColumnDesc>& column_desc,
8258
const google::protobuf::RepeatedPtrField<::openmldb::common::ColumnDesc>& added_column_desc) {
83-
std::vector<std::string> row;
84-
row.push_back("#");
85-
row.push_back("name");
86-
row.push_back("type");
87-
::baidu::common::TPrinter tp(row.size(), FLAGS_max_col_display_length);
88-
tp.AddRow(row);
89-
uint32_t idx = 0;
90-
for (const auto& column_desc : column_desc) {
91-
row.clear();
92-
row.push_back(std::to_string(idx));
93-
row.push_back(column_desc.name());
94-
auto iter = ::openmldb::codec::DATA_TYPE_STR_MAP.find(column_desc.data_type());
95-
if (iter != ::openmldb::codec::DATA_TYPE_STR_MAP.end()) {
96-
row.push_back(iter->second);
97-
} else {
98-
row.push_back("-");
99-
}
100-
tp.AddRow(row);
101-
idx++;
59+
::hybridse::base::TextTable t('-', ' ', ' ');
60+
t.add("#");
61+
t.add("Field");
62+
t.add("Type");
63+
t.add("Null");
64+
t.end_of_row();
65+
66+
for (int i = 0; i < column_desc.size(); i++) {
67+
const auto& column = column_desc.Get(i);
68+
t.add(std::to_string(i + 1));
69+
t.add(column.name());
70+
// kXXX discard k
71+
t.add(DataType_Name(column.data_type()).substr(1));
72+
t.add(column.not_null() ? "NO" : "YES");
73+
t.end_of_row();
10274
}
103-
for (const auto& column_desc : added_column_desc) {
104-
row.clear();
105-
row.push_back(std::to_string(idx));
106-
row.push_back(column_desc.name());
107-
auto iter = ::openmldb::codec::DATA_TYPE_STR_MAP.find(column_desc.data_type());
108-
if (iter != ::openmldb::codec::DATA_TYPE_STR_MAP.end()) {
109-
row.push_back(iter->second);
110-
} else {
111-
row.push_back("-");
112-
}
113-
tp.AddRow(row);
114-
idx++;
75+
76+
for (int i = 0; i < added_column_desc.size(); i++) {
77+
const auto& column = added_column_desc.Get(i);
78+
t.add(std::to_string(i + 1));
79+
t.add(column.name());
80+
// kXXX discard k
81+
t.add(DataType_Name(column.data_type()).substr(1));
82+
t.add(column.not_null() ? "NO" : "YES");
83+
t.end_of_row();
11584
}
116-
tp.Print(true);
85+
std::cout << t;
86+
}
87+
88+
__attribute__((unused)) static void PrintSchema(
89+
const google::protobuf::RepeatedPtrField<::openmldb::common::ColumnDesc>& column_desc_field) {
90+
PrintSchema(column_desc_field, {});
11791
}
11892

11993
__attribute__((unused)) static void PrintColumnKey(
12094
const google::protobuf::RepeatedPtrField<::openmldb::common::ColumnKey>& column_key_field) {
121-
std::vector<std::string> row;
122-
row.push_back("#");
123-
row.push_back("index_name");
124-
row.push_back("col_name");
125-
row.push_back("ts_col");
126-
row.push_back("ttl");
127-
::baidu::common::TPrinter tp(row.size(), FLAGS_max_col_display_length);
128-
tp.AddRow(row);
129-
uint32_t idx = 0;
130-
for (const auto& column_key : column_key_field) {
95+
::hybridse::base::TextTable t('-', ' ', ' ');
96+
t.add("#");
97+
t.add("name");
98+
t.add("keys");
99+
t.add("ts");
100+
t.add("ttl");
101+
t.add("ttl_type");
102+
t.end_of_row();
103+
104+
for (int i = 0; i < column_key_field.size(); i++) {
105+
const auto& column_key = column_key_field.Get(i);
131106
if (column_key.flag() == 1) {
132107
continue;
133108
}
134-
row.clear();
135-
row.push_back(std::to_string(idx));
136-
row.push_back(column_key.index_name());
109+
t.add(std::to_string(i + 1));
110+
t.add(column_key.index_name());
137111
std::string key;
138112
for (const auto& name : column_key.col_name()) {
139113
if (key.empty()) {
@@ -142,26 +116,29 @@ __attribute__((unused)) static void PrintColumnKey(
142116
key += "|" + name;
143117
}
144118
}
145-
if (key.empty()) {
146-
key = column_key.index_name();
147-
}
148-
row.push_back(key);
119+
t.add((key.empty() ? column_key.index_name() : key));
120+
149121
if (!column_key.ts_name().empty()) {
150-
row.push_back(column_key.ts_name());
122+
t.add(column_key.ts_name());
151123
} else {
152-
row.push_back("-");
124+
t.add("-");
153125
}
126+
154127
if (column_key.has_ttl()) {
155-
::openmldb::storage::TTLSt cur_ttl_st(column_key.ttl());
156-
cur_ttl_st.abs_ttl = cur_ttl_st.abs_ttl / (60 * 1000);
157-
row.push_back(cur_ttl_st.ToString());
128+
std::ostringstream oss;
129+
auto& ttl = column_key.ttl();
130+
// TODO(hw): it's better to construct from common::TTLSt, but the abs will be measured in minutes. Needs fix
131+
storage::TTLSt ttl_st(ttl.abs_ttl(), ttl.lat_ttl(), storage::TTLSt::ConvertTTLType(ttl.ttl_type()));
132+
t.add(ttl_st.ToString());
133+
t.add(TTLType_Name(ttl_st.GetProtoTTLType()));
158134
} else {
159-
row.push_back("-");
135+
t.add("-"); // ttl
136+
t.add("-"); // ttl_type
160137
}
161-
tp.AddRow(row);
162-
idx++;
138+
139+
t.end_of_row();
163140
}
164-
tp.Print(true);
141+
std::cout << t;
165142
}
166143

167144
__attribute__((unused)) static void ShowTableRows(bool is_compress, ::openmldb::codec::SDKCodec* codec,

src/cmd/sql_cmd.h

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ void PrintTableIndex(std::ostream &stream, const ::hybridse::vm::IndexList &inde
166166
t.add(std::to_string(i + 1));
167167
t.add(index.name());
168168
t.add(index.first_keys(0));
169-
std::string ts_name = index.second_key();
169+
const std::string &ts_name = index.second_key();
170170
if (ts_name.empty()) {
171171
t.add("-");
172172
} else {
@@ -228,7 +228,7 @@ void PrintItems(std::ostream &stream, const std::string &head, const std::vector
228228
::hybridse::base::TextTable t('-', ' ', ' ');
229229
t.add(head);
230230
t.end_of_row();
231-
for (auto item : items) {
231+
for (const auto &item : items) {
232232
t.add(item);
233233
t.end_of_row();
234234
}
@@ -251,7 +251,7 @@ void PrintItems(const std::vector<std::pair<std::string, std::string>> &items, s
251251
t.add("DB");
252252
t.add("SP");
253253
t.end_of_row();
254-
for (auto item : items) {
254+
for (const auto &item : items) {
255255
t.add(item.first);
256256
t.add(item.second);
257257
t.end_of_row();
@@ -267,7 +267,7 @@ void PrintItems(const std::vector<std::pair<std::string, std::string>> &items, s
267267

268268
void PrintProcedureSchema(const std::string &head, const ::hybridse::sdk::Schema &sdk_schema, std::ostream &stream) {
269269
try {
270-
const ::hybridse::sdk::SchemaImpl &schema_impl = dynamic_cast<const ::hybridse::sdk::SchemaImpl &>(sdk_schema);
270+
const auto &schema_impl = dynamic_cast<const ::hybridse::sdk::SchemaImpl &>(sdk_schema);
271271
auto &schema = schema_impl.GetSchema();
272272
if (schema.empty()) {
273273
stream << "Empty set" << std::endl;
@@ -283,15 +283,15 @@ void PrintProcedureSchema(const std::string &head, const ::hybridse::sdk::Schema
283283
t.end_of_row();
284284

285285
for (uint32_t i = 0; i < items_size; i++) {
286-
auto column = schema.Get(i);
286+
const auto &column = schema.Get(i);
287287
t.add(std::to_string(i + 1));
288288
t.add(column.name());
289289
t.add(::hybridse::type::Type_Name(column.type()));
290290
t.add(column.is_constant() ? "YES" : "NO");
291291
t.end_of_row();
292292
}
293293
stream << t << std::endl;
294-
} catch (std::bad_cast&) {
294+
} catch (std::bad_cast &) {
295295
return;
296296
}
297297
}
@@ -346,12 +346,9 @@ void HandleCmd(const hybridse::node::CmdPlanNode *cmd_node) {
346346
std::cerr << "table " << cmd_node->GetArgs()[0] << " does not exist" << std::endl;
347347
return;
348348
}
349-
::hybridse::vm::Schema output_schema;
350-
::openmldb::catalog::SchemaAdapter::ConvertSchema(table->column_desc(), &output_schema);
351-
PrintTableSchema(std::cout, output_schema);
352-
::hybridse::vm::IndexList index_list;
353-
::openmldb::catalog::SchemaAdapter::ConvertIndex(table->column_key(), &index_list);
354-
PrintTableIndex(std::cout, index_list);
349+
350+
PrintSchema(table->column_desc());
351+
PrintColumnKey(table->column_key());
355352
break;
356353
}
357354

@@ -461,8 +458,7 @@ void HandleCmd(const hybridse::node::CmdPlanNode *cmd_node) {
461458
std::string error;
462459
std::vector<std::shared_ptr<hybridse::sdk::ProcedureInfo>> sp_infos = cs->GetProcedureInfo(&error);
463460
std::vector<std::pair<std::string, std::string>> pairs;
464-
for (uint32_t i = 0; i < sp_infos.size(); i++) {
465-
auto &sp_info = sp_infos.at(i);
461+
for (auto &sp_info : sp_infos) {
466462
pairs.push_back(std::make_pair(sp_info->GetDbName(), sp_info->GetSpName()));
467463
}
468464
PrintItems(pairs, std::cout);
@@ -542,7 +538,7 @@ void HandleSQL(const std::string &sql) {
542538
hybridse::node::PlanNode *node = plan_trees[0];
543539
switch (node->GetType()) {
544540
case hybridse::node::kPlanTypeCmd: {
545-
hybridse::node::CmdPlanNode *cmd = dynamic_cast<hybridse::node::CmdPlanNode *>(node);
541+
auto *cmd = dynamic_cast<hybridse::node::CmdPlanNode *>(node);
546542
HandleCmd(cmd);
547543
return;
548544
}
@@ -579,8 +575,7 @@ void HandleSQL(const std::string &sql) {
579575
std::cout << "please use database first" << std::endl;
580576
return;
581577
}
582-
hybridse::node::CreateIndexPlanNode *create_index_node =
583-
dynamic_cast<hybridse::node::CreateIndexPlanNode *>(node);
578+
auto *create_index_node = dynamic_cast<hybridse::node::CreateIndexPlanNode *>(node);
584579
HandleCreateIndex(create_index_node->create_index_node_);
585580
return;
586581
}

src/storage/schema.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,18 @@
2828
#include "proto/tablet.pb.h"
2929
#include "proto/type.pb.h"
3030

31-
namespace openmldb {
32-
namespace storage {
31+
namespace openmldb::storage {
3332

3433
static constexpr uint32_t MAX_INDEX_NUM = 200;
3534

3635
enum TTLType { kAbsoluteTime = 1, kRelativeTime = 2, kLatestTime = 3, kAbsAndLat = 4, kAbsOrLat = 5 };
3736

37+
// ttl unit: millisecond
3838
struct TTLSt {
3939
TTLSt() : abs_ttl(0), lat_ttl(0), ttl_type(::openmldb::storage::TTLType::kAbsoluteTime) {}
4040
TTLSt(uint64_t abs, uint64_t lat, ::openmldb::storage::TTLType type) : abs_ttl(abs), lat_ttl(lat), ttl_type(type) {}
4141

42+
// TODO(hw): needs fix, common::TTLSt::abs_ttl should be ms
4243
explicit TTLSt(const ::openmldb::common::TTLSt& ttl) : abs_ttl(ttl.abs_ttl() * 60 * 1000), lat_ttl(ttl.lat_ttl()) {
4344
ttl_type = ConvertTTLType(ttl.ttl_type());
4445
}
@@ -76,13 +77,13 @@ struct TTLSt {
7677
bool NeedGc() const {
7778
switch (ttl_type) {
7879
case TTLType::kAbsoluteTime:
79-
return abs_ttl == 0 ? false : true;
80+
return abs_ttl != 0;
8081
case TTLType::kLatestTime:
81-
return lat_ttl == 0 ? false : true;
82+
return lat_ttl != 0;
8283
case TTLType::kAbsAndLat:
83-
return abs_ttl == 0 || lat_ttl == 0 ? false : true;
84+
return !(abs_ttl == 0 || lat_ttl == 0);
8485
case TTLType::kAbsOrLat:
85-
return abs_ttl == 0 && lat_ttl == 0 ? false : true;
86+
return !(abs_ttl == 0 && lat_ttl == 0);
8687
default:
8788
return true;
8889
}
@@ -119,7 +120,7 @@ struct TTLSt {
119120
std::string ToString() const {
120121
switch (ttl_type) {
121122
case TTLType::kAbsoluteTime:
122-
return std::to_string(abs_ttl) + "min";
123+
return std::to_string(abs_ttl / (60 * 1000)) + "min";
123124
case TTLType::kLatestTime:
124125
return std::to_string(lat_ttl);
125126
case TTLType::kAbsAndLat:
@@ -319,7 +320,7 @@ class TableSt {
319320
if (pid < partitions->size()) {
320321
return partitions->at(pid);
321322
}
322-
return PartitionSt();
323+
return {};
323324
}
324325

325326
bool SetPartition(const PartitionSt& partition_st);
@@ -344,5 +345,4 @@ class TableSt {
344345
std::shared_ptr<std::vector<PartitionSt>> partitions_;
345346
};
346347

347-
} // namespace storage
348-
} // namespace openmldb
348+
} // namespace openmldb::storage

0 commit comments

Comments
 (0)