Skip to content

Commit 25997c4

Browse files
AieeSophie-Xie
andauthored
Fix crash when use executeJson() with profile (#3998)
* Fix profile json response * Add UT Co-authored-by: Sophie <84560950+Sophie-Xie@users.noreply.github.com>
1 parent 19958f6 commit 25997c4

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

src/common/graph/Response.h

+29-20
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@
185185

186186
namespace nebula {
187187

188-
#define X(EnumName, EnumNumber) EnumName = EnumNumber,
188+
#define X(EnumName, EnumNumber) EnumName = (EnumNumber),
189189

190190
enum class ErrorCode { ErrorCodeEnums };
191191

@@ -295,7 +295,9 @@ struct ProfilingStats {
295295
ProfilingStatsObj.insert("rows", rows);
296296
ProfilingStatsObj.insert("execDurationInUs", execDurationInUs);
297297
ProfilingStatsObj.insert("totalDurationInUs", totalDurationInUs);
298-
ProfilingStatsObj.insert("otherStats", folly::toDynamic(*otherStats));
298+
if (otherStats) {
299+
ProfilingStatsObj.insert("otherStats", folly::toDynamic(*otherStats));
300+
}
299301

300302
return ProfilingStatsObj;
301303
}
@@ -323,7 +325,7 @@ struct PlanNodeBranchInfo {
323325
}
324326

325327
// True if loop body or then branch of select
326-
bool isDoBranch{0};
328+
bool isDoBranch{false};
327329
// select/loop node id
328330
int64_t conditionNodeId{-1};
329331

@@ -407,22 +409,29 @@ struct PlanNodeDescription {
407409
planNodeDescObj.insert("id", id);
408410
planNodeDescObj.insert("outputVar", outputVar);
409411

410-
auto descriptionObj = folly::dynamic::array();
411-
descriptionObj.resize(description->size());
412-
std::transform(
413-
description->begin(), description->end(), descriptionObj.begin(), [](const auto &ele) {
414-
return ele.toJson();
415-
});
416-
planNodeDescObj.insert("description", descriptionObj);
417-
418-
auto profilesObj = folly::dynamic::array();
419-
profilesObj.resize(profiles->size());
420-
std::transform(profiles->begin(), profiles->end(), profilesObj.begin(), [](const auto &ele) {
421-
return ele.toJson();
422-
});
423-
planNodeDescObj.insert("profiles", profilesObj);
424-
planNodeDescObj.insert("branchInfo", branchInfo->toJson());
425-
planNodeDescObj.insert("dependencies", folly::toDynamic(*dependencies));
412+
if (description) {
413+
auto descriptionObj = folly::dynamic::array();
414+
descriptionObj.resize(description->size());
415+
std::transform(
416+
description->begin(), description->end(), descriptionObj.begin(), [](const auto &ele) {
417+
return ele.toJson();
418+
});
419+
planNodeDescObj.insert("description", descriptionObj);
420+
}
421+
if (profiles) {
422+
auto profilesObj = folly::dynamic::array();
423+
profilesObj.resize(profiles->size());
424+
std::transform(profiles->begin(), profiles->end(), profilesObj.begin(), [](const auto &ele) {
425+
return ele.toJson();
426+
});
427+
planNodeDescObj.insert("profiles", profilesObj);
428+
}
429+
if (branchInfo) {
430+
planNodeDescObj.insert("branchInfo", branchInfo->toJson());
431+
}
432+
if (dependencies) {
433+
planNodeDescObj.insert("dependencies", folly::toDynamic(*dependencies));
434+
}
426435

427436
return planNodeDescObj;
428437
}
@@ -536,7 +545,7 @@ struct ExecutionResponse {
536545
std::unique_ptr<PlanDescription> planDesc{nullptr};
537546
std::unique_ptr<std::string> comment{nullptr};
538547

539-
// Return the response as a JSON string
548+
// Returns the response as a JSON string
540549
// only errorCode and latencyInUs are required fields, the rest are optional
541550
// if the dataset contains a value of TIME or DATETIME, it will be returned in UTC.
542551
//

src/common/graph/tests/ResponseEncodeDecodeTest.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,26 @@ TEST(ResponseEncodeDecodeTest, Basic) {
8989
}
9090

9191
TEST(ResponseEncodeDecodeTest, ToJson) {
92+
// PlanNodeDescription
93+
{
94+
// Dummy data
95+
PlanNodeDescription pnd;
96+
pnd.name = "name";
97+
pnd.id = 100;
98+
pnd.outputVar = "outputVar";
99+
pnd.description = nullptr;
100+
pnd.profiles = nullptr;
101+
pnd.branchInfo = nullptr;
102+
pnd.dependencies = nullptr;
103+
104+
folly::dynamic jsonObj = pnd.toJson();
105+
folly::dynamic expect = folly::dynamic::object();
106+
expect.insert("name", "name");
107+
expect.insert("id", 100);
108+
expect.insert("outputVar", "outputVar");
109+
110+
ASSERT_EQ(jsonObj, expect);
111+
}
92112
// plan description
93113
{
94114
std::vector<PlanDescription> pds;

0 commit comments

Comments
 (0)