Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support showing jobs sorted by id #3371

Merged
merged 12 commits into from
Aug 7, 2023
16 changes: 16 additions & 0 deletions src/sdk/job_table_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "sdk/job_table_helper.h"

#include <unordered_map>
#include <algorithm>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reorder the header file

#include <utility>
#include "codec/schema_codec.h"
#include "sdk/result_set_sql.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
45 changes: 45 additions & 0 deletions src/sdk/sql_cluster_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include <unistd.h>
#include <time.h>
#include <algorithm>
#include <memory>
#include <string>
Expand All @@ -27,6 +28,7 @@
#include "gtest/gtest.h"
#include "sdk/mini_cluster.h"
#include "sdk/sql_cluster_router.h"
#include "sdk/job_table_helper.h"
dl239 marked this conversation as resolved.
Show resolved Hide resolved
#include "sdk/sql_router.h"
#include "sdk/sql_sdk_test.h"
#include "vm/catalog.h"
Expand Down Expand Up @@ -98,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, TestIfExists) {
std::string name = "test" + GenRand();
::hybridse::sdk::Status status;
Expand Down