Skip to content

Commit

Permalink
Fix crash when use executeJson() with profile (#3998)
Browse files Browse the repository at this point in the history
* Fix profile json response

* Add UT

Co-authored-by: Sophie <84560950+Sophie-Xie@users.noreply.github.com>
  • Loading branch information
Aiee and Sophie-Xie committed Mar 13, 2022
1 parent 19958f6 commit 25997c4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
49 changes: 29 additions & 20 deletions src/common/graph/Response.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@

namespace nebula {

#define X(EnumName, EnumNumber) EnumName = EnumNumber,
#define X(EnumName, EnumNumber) EnumName = (EnumNumber),

enum class ErrorCode { ErrorCodeEnums };

Expand Down Expand Up @@ -295,7 +295,9 @@ struct ProfilingStats {
ProfilingStatsObj.insert("rows", rows);
ProfilingStatsObj.insert("execDurationInUs", execDurationInUs);
ProfilingStatsObj.insert("totalDurationInUs", totalDurationInUs);
ProfilingStatsObj.insert("otherStats", folly::toDynamic(*otherStats));
if (otherStats) {
ProfilingStatsObj.insert("otherStats", folly::toDynamic(*otherStats));
}

return ProfilingStatsObj;
}
Expand Down Expand Up @@ -323,7 +325,7 @@ struct PlanNodeBranchInfo {
}

// True if loop body or then branch of select
bool isDoBranch{0};
bool isDoBranch{false};
// select/loop node id
int64_t conditionNodeId{-1};

Expand Down Expand Up @@ -407,22 +409,29 @@ struct PlanNodeDescription {
planNodeDescObj.insert("id", id);
planNodeDescObj.insert("outputVar", outputVar);

auto descriptionObj = folly::dynamic::array();
descriptionObj.resize(description->size());
std::transform(
description->begin(), description->end(), descriptionObj.begin(), [](const auto &ele) {
return ele.toJson();
});
planNodeDescObj.insert("description", descriptionObj);

auto profilesObj = folly::dynamic::array();
profilesObj.resize(profiles->size());
std::transform(profiles->begin(), profiles->end(), profilesObj.begin(), [](const auto &ele) {
return ele.toJson();
});
planNodeDescObj.insert("profiles", profilesObj);
planNodeDescObj.insert("branchInfo", branchInfo->toJson());
planNodeDescObj.insert("dependencies", folly::toDynamic(*dependencies));
if (description) {
auto descriptionObj = folly::dynamic::array();
descriptionObj.resize(description->size());
std::transform(
description->begin(), description->end(), descriptionObj.begin(), [](const auto &ele) {
return ele.toJson();
});
planNodeDescObj.insert("description", descriptionObj);
}
if (profiles) {
auto profilesObj = folly::dynamic::array();
profilesObj.resize(profiles->size());
std::transform(profiles->begin(), profiles->end(), profilesObj.begin(), [](const auto &ele) {
return ele.toJson();
});
planNodeDescObj.insert("profiles", profilesObj);
}
if (branchInfo) {
planNodeDescObj.insert("branchInfo", branchInfo->toJson());
}
if (dependencies) {
planNodeDescObj.insert("dependencies", folly::toDynamic(*dependencies));
}

return planNodeDescObj;
}
Expand Down Expand Up @@ -536,7 +545,7 @@ struct ExecutionResponse {
std::unique_ptr<PlanDescription> planDesc{nullptr};
std::unique_ptr<std::string> comment{nullptr};

// Return the response as a JSON string
// Returns the response as a JSON string
// only errorCode and latencyInUs are required fields, the rest are optional
// if the dataset contains a value of TIME or DATETIME, it will be returned in UTC.
//
Expand Down
20 changes: 20 additions & 0 deletions src/common/graph/tests/ResponseEncodeDecodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ TEST(ResponseEncodeDecodeTest, Basic) {
}

TEST(ResponseEncodeDecodeTest, ToJson) {
// PlanNodeDescription
{
// Dummy data
PlanNodeDescription pnd;
pnd.name = "name";
pnd.id = 100;
pnd.outputVar = "outputVar";
pnd.description = nullptr;
pnd.profiles = nullptr;
pnd.branchInfo = nullptr;
pnd.dependencies = nullptr;

folly::dynamic jsonObj = pnd.toJson();
folly::dynamic expect = folly::dynamic::object();
expect.insert("name", "name");
expect.insert("id", 100);
expect.insert("outputVar", "outputVar");

ASSERT_EQ(jsonObj, expect);
}
// plan description
{
std::vector<PlanDescription> pds;
Expand Down

0 comments on commit 25997c4

Please sign in to comment.