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 @@ -16,6 +16,7 @@

#include "sdk/job_table_helper.h"

#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 <unordered_map>
#include <utility>
#include "codec/schema_codec.h"
Expand Down Expand Up @@ -164,6 +165,21 @@
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;

Check warning on line 173 in src/sdk/job_table_helper.cc

View check run for this annotation

Codecov / codecov/patch

src/sdk/job_table_helper.cc#L170-L173

Added lines #L170 - L173 were not covered by tests
}
if (vec2.empty()) {
return false;

Check warning on line 176 in src/sdk/job_table_helper.cc

View check run for this annotation

Codecov / codecov/patch

src/sdk/job_table_helper.cc#L175-L176

Added lines #L175 - L176 were not covered by tests
}
uint64_t id1, id2;
if (!absl::SimpleAtoi(vec1[0], &id1) || !absl::SimpleAtoi(vec2[0], &id2)) {
return vec1[0] < vec2[0];

Check warning on line 180 in src/sdk/job_table_helper.cc

View check run for this annotation

Codecov / codecov/patch

src/sdk/job_table_helper.cc#L179-L180

Added lines #L179 - L180 were not covered by tests
}
return id1 < id2;});

Check warning on line 182 in src/sdk/job_table_helper.cc

View check run for this annotation

Codecov / codecov/patch

src/sdk/job_table_helper.cc#L182

Added line #L182 was not covered by tests
*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 @@ -25,6 +25,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 @@ -98,6 +99,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
Loading