Skip to content

Commit

Permalink
feat: support showing jobs sorted by id (#3371)
Browse files Browse the repository at this point in the history
  • Loading branch information
emo-coder authored and dl239 committed Sep 19, 2023
1 parent b70c29e commit 1518214
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/sdk/job_table_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "sdk/job_table_helper.h"

#include <algorithm>
#include <unordered_map>
#include <utility>
#include "codec/schema_codec.h"
Expand Down Expand Up @@ -164,6 +165,21 @@ std::shared_ptr<hybridse::sdk::ResultSet> JobTableHelper::MakeResultSet(
vec.push_back("TaskManager");
records.emplace_back(std::move(vec));
}

// sort jobs by id(asc)
std::sort(records.begin(), records.end(),
[](const std::vector<std::string>& vec1, const std::vector<std::string>& vec2) {
if (vec1.empty()) {
return true;
}
if (vec2.empty()) {
return false;
}
uint64_t id1, id2;
if (!absl::SimpleAtoi(vec1[0], &id1) || !absl::SimpleAtoi(vec2[0], &id2)) {
return vec1[0] < vec2[0];
}
return id1 < id2;});
*status = {};
return ResultSetSQL::MakeResultSet(schema, records, status);
}
Expand Down
44 changes: 44 additions & 0 deletions src/sdk/sql_cluster_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "codec/fe_row_codec.h"
#include "gflags/gflags.h"
#include "gtest/gtest.h"
#include "sdk/job_table_helper.h"
#include "sdk/mini_cluster.h"
#include "sdk/sql_cluster_router.h"
#include "sdk/sql_router.h"
Expand Down Expand Up @@ -99,6 +100,49 @@ class SQLClusterDDLTest : public SQLClusterTest {
std::string db;
};

TEST_F(SQLClusterDDLTest, TestShowSortedJobs) {
std::string name = "job_info" + GenRand();
::hybridse::sdk::Status status;

std::string sql;
sql = "create table " + name +
"("
"id int, job_type string, state string, start_time timestamp, end_time timestamp, "
"parameter string, cluster string, application_id string, error string, "
"index(key=id));";
ASSERT_TRUE(router->ExecuteDDL(db, sql, &status)) << "ddl: " << sql;
ASSERT_TRUE(router->RefreshCatalog());

std::vector<int> randint;
for (int i = 1; i < 100; i++) {
randint.push_back(i);
}
std::random_shuffle(randint.begin(), randint.end());
for (uint64_t i = 0; i < randint.size(); i++) {
std::string id = std::to_string(randint[i]);
sql = "insert into " + name +
" values(" + id + ", \"Type\", \"State\", 0, " + id + ", "
"\"/tmp/sql-000000000000000000" + id + "\", \"local[*]\", \"local-0000000000000" + id + "\", \"\");";
router->ExecuteSQL(db, sql, &status);
ASSERT_TRUE(status.IsOK());
}

auto rs = router->ExecuteSQL(db, "select * from " + name + ";", &status);
ASSERT_TRUE(status.IsOK());

rs = JobTableHelper::MakeResultSet(rs, "", &status);
ASSERT_TRUE(status.IsOK());

int id_current = 0, id_next;
while (rs->Next()) {
ASSERT_TRUE(rs->GetInt32(0, &id_next));
ASSERT_LT(id_current, id_next);
id_current = id_next;
}

ASSERT_TRUE(router->ExecuteDDL(db, "drop table " + name + ";", &status));
}

TEST_F(SQLClusterDDLTest, TestCreateTableLike) {
::hybridse::sdk::Status status;

Expand Down

0 comments on commit 1518214

Please sign in to comment.