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

Fix/plan compare #4210

Merged
merged 12 commits into from
Apr 27, 2022
16 changes: 8 additions & 8 deletions src/graph/planner/plan/Admin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace graph {

std::unique_ptr<PlanNodeDescription> CreateSpace::explain() const {
auto desc = SingleDependencyNode::explain();
addDescription("ifNotExists", util::toJson(ifNotExists_), desc.get());
addDescription("ifNotExists", folly::toJson(util::toJson(ifNotExists_)), desc.get());
addDescription("spaceDesc", folly::toJson(util::toJson(spaceDesc_)), desc.get());
return desc;
}
Expand All @@ -30,14 +30,14 @@ std::unique_ptr<PlanNodeDescription> CreateSpaceAsNode::explain() const {
std::unique_ptr<PlanNodeDescription> DropSpace::explain() const {
auto desc = SingleDependencyNode::explain();
addDescription("spaceName", spaceName_, desc.get());
addDescription("ifExists", util::toJson(ifExists_), desc.get());
addDescription("ifExists", folly::toJson(util::toJson(ifExists_)), desc.get());
return desc;
}

std::unique_ptr<PlanNodeDescription> ClearSpace::explain() const {
auto desc = SingleDependencyNode::explain();
addDescription("spaceName", spaceName_, desc.get());
addDescription("ifExists", util::toJson(ifExists_), desc.get());
addDescription("ifExists", folly::toJson(util::toJson(ifExists_)), desc.get());
return desc;
}

Expand All @@ -61,7 +61,7 @@ std::unique_ptr<PlanNodeDescription> DropSnapshot::explain() const {

std::unique_ptr<PlanNodeDescription> ShowParts::explain() const {
auto desc = SingleDependencyNode::explain();
addDescription("spaceId", folly::to<std::string>(spaceId_), desc.get());
addDescription("spaceId", folly::toJson(util::toJson(spaceId_)), desc.get());
addDescription("partIds", folly::toJson(util::toJson(partIds_)), desc.get());
return desc;
}
Expand Down Expand Up @@ -89,13 +89,13 @@ std::unique_ptr<PlanNodeDescription> GetConfig::explain() const {

std::unique_ptr<PlanNodeDescription> CreateNode::explain() const {
auto desc = SingleDependencyNode::explain();
addDescription("ifNotExist", util::toJson(ifNotExist_), desc.get());
addDescription("ifNotExist", folly::toJson(util::toJson(ifNotExist_)), desc.get());
return desc;
}

std::unique_ptr<PlanNodeDescription> DropNode::explain() const {
auto desc = SingleDependencyNode::explain();
addDescription("ifExist", util::toJson(ifExist_), desc.get());
addDescription("ifExist", folly::toJson(util::toJson(ifExist_)), desc.get());
return desc;
}

Expand Down Expand Up @@ -169,7 +169,7 @@ std::unique_ptr<PlanNodeDescription> ListUserRoles::explain() const {

std::unique_ptr<PlanNodeDescription> ListRoles::explain() const {
auto desc = SingleDependencyNode::explain();
addDescription("space", util::toJson(space_), desc.get());
addDescription("space", folly::toJson(util::toJson(space_)), desc.get());
return desc;
}

Expand All @@ -183,7 +183,7 @@ std::unique_ptr<PlanNodeDescription> SubmitJob::explain() const {

std::unique_ptr<PlanNodeDescription> ShowQueries::explain() const {
auto desc = SingleDependencyNode::explain();
addDescription("isAll", util::toJson(isAll()), desc.get());
addDescription("isAll", folly::toJson(util::toJson(isAll())), desc.get());
return desc;
}

Expand Down
20 changes: 10 additions & 10 deletions src/graph/planner/plan/Algo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ namespace graph {

std::unique_ptr<PlanNodeDescription> BFSShortestPath::explain() const {
auto desc = BinaryInputNode::explain();
addDescription("LeftNextVidVar", util::toJson(leftVidVar_), desc.get());
addDescription("RightNextVidVar", util::toJson(rightVidVar_), desc.get());
addDescription("steps", util::toJson(steps_), desc.get());
addDescription("LeftNextVidVar", folly::toJson(util::toJson(leftVidVar_)), desc.get());
addDescription("RightNextVidVar", folly::toJson(util::toJson(rightVidVar_)), desc.get());
addDescription("steps", folly::toJson(util::toJson(steps_)), desc.get());
return desc;
}

std::unique_ptr<PlanNodeDescription> MultiShortestPath::explain() const {
auto desc = BinaryInputNode::explain();
addDescription("LeftNextVidVar", util::toJson(leftVidVar_), desc.get());
addDescription("RightNextVidVar", util::toJson(rightVidVar_), desc.get());
addDescription("steps", util::toJson(steps_), desc.get());
addDescription("LeftNextVidVar", folly::toJson(util::toJson(leftVidVar_)), desc.get());
addDescription("RightNextVidVar", folly::toJson(util::toJson(rightVidVar_)), desc.get());
addDescription("steps", folly::toJson(util::toJson(steps_)), desc.get());
return desc;
}

std::unique_ptr<PlanNodeDescription> ProduceAllPaths::explain() const {
auto desc = BinaryInputNode::explain();
addDescription("LeftNextVidVar", util::toJson(leftVidVar_), desc.get());
addDescription("RightNextVidVar", util::toJson(rightVidVar_), desc.get());
addDescription("noloop ", util::toJson(noLoop_), desc.get());
addDescription("steps", util::toJson(steps_), desc.get());
addDescription("LeftNextVidVar", folly::toJson(util::toJson(leftVidVar_)), desc.get());
addDescription("RightNextVidVar", folly::toJson(util::toJson(rightVidVar_)), desc.get());
addDescription("noloop ", folly::toJson(util::toJson(noLoop_)), desc.get());
addDescription("steps", folly::toJson(util::toJson(steps_)), desc.get());
return desc;
}

Expand Down
6 changes: 3 additions & 3 deletions src/graph/planner/plan/Maintain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace graph {
std::unique_ptr<PlanNodeDescription> CreateSchemaNode::explain() const {
auto desc = SingleDependencyNode::explain();
addDescription("name", name_, desc.get());
addDescription("ifNotExists", util::toJson(ifNotExists_), desc.get());
addDescription("ifNotExists", folly::toJson(util::toJson(ifNotExists_)), desc.get());
addDescription("schema", folly::toJson(util::toJson(schema_)), desc.get());
return desc;
}
Expand All @@ -38,7 +38,7 @@ std::unique_ptr<PlanNodeDescription> DescSchemaNode::explain() const {
std::unique_ptr<PlanNodeDescription> DropSchemaNode::explain() const {
auto desc = SingleDependencyNode::explain();
addDescription("name", name_, desc.get());
addDescription("ifExists", util::toJson(ifExists_), desc.get());
addDescription("ifExists", folly::toJson(util::toJson(ifExists_)), desc.get());
return desc;
}

Expand Down Expand Up @@ -67,7 +67,7 @@ std::unique_ptr<PlanNodeDescription> DescIndexNode::explain() const {
std::unique_ptr<PlanNodeDescription> DropIndexNode::explain() const {
auto desc = SingleDependencyNode::explain();
addDescription("indexName", indexName_, desc.get());
addDescription("ifExists", util::toJson(ifExists_), desc.get());
addDescription("ifExists", folly::toJson(util::toJson(ifExists_)), desc.get());
return desc;
}

Expand Down
4 changes: 2 additions & 2 deletions src/graph/planner/plan/Mutate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace graph {
std::unique_ptr<PlanNodeDescription> InsertVertices::explain() const {
auto desc = SingleDependencyNode::explain();
addDescription("spaceId", folly::to<std::string>(spaceId_), desc.get());
addDescription("ifNotExists", util::toJson(ifNotExists_), desc.get());
addDescription("ifNotExists", folly::toJson(util::toJson(ifNotExists_)), desc.get());

folly::dynamic tagPropsArr = folly::dynamic::array();
for (const auto &p : tagPropNames_) {
Expand All @@ -31,7 +31,7 @@ std::unique_ptr<PlanNodeDescription> InsertVertices::explain() const {
std::unique_ptr<PlanNodeDescription> InsertEdges::explain() const {
auto desc = SingleDependencyNode::explain();
addDescription("spaceId", folly::to<std::string>(spaceId_), desc.get());
addDescription("ifNotExists", util::toJson(ifNotExists_), desc.get());
addDescription("ifNotExists", folly::toJson(util::toJson(ifNotExists_)), desc.get());
addDescription("propNames", folly::toJson(util::toJson(propNames_)), desc.get());
addDescription("edges", folly::toJson(util::toJson(edges_)), desc.get());
return desc;
Expand Down
18 changes: 9 additions & 9 deletions src/graph/planner/plan/Query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int64_t Explore::limit(QueryContext* qctx) const {
std::unique_ptr<PlanNodeDescription> Explore::explain() const {
auto desc = SingleInputNode::explain();
addDescription("space", folly::to<std::string>(space_), desc.get());
addDescription("dedup", util::toJson(dedup_), desc.get());
addDescription("dedup", folly::toJson(util::toJson(dedup_)), desc.get());
addDescription(
"limit", folly::to<std::string>(limit_ == nullptr ? "" : limit_->toString()), desc.get());
std::string filter = filter_ == nullptr ? "" : filter_->toString();
Expand Down Expand Up @@ -60,7 +60,7 @@ std::unique_ptr<PlanNodeDescription> GetNeighbors::explain() const {
addDescription(
"statProps", statProps_ ? folly::toJson(util::toJson(*statProps_)) : "", desc.get());
addDescription("exprs", exprs_ ? folly::toJson(util::toJson(*exprs_)) : "", desc.get());
addDescription("random", util::toJson(random_), desc.get());
addDescription("random", folly::toJson(util::toJson(random_)), desc.get());
return desc;
}

Expand Down Expand Up @@ -137,7 +137,7 @@ void GetVertices::cloneMembers(const GetVertices& gv) {
std::unique_ptr<PlanNodeDescription> GetEdges::explain() const {
auto desc = Explore::explain();
addDescription("src", src_ ? src_->toString() : "", desc.get());
addDescription("type", util::toJson(type_), desc.get());
addDescription("type", folly::toJson(util::toJson(type_)), desc.get());
addDescription("ranking", ranking_ ? ranking_->toString() : "", desc.get());
addDescription("dst", dst_ ? dst_->toString() : "", desc.get());
addDescription("props", props_ ? folly::toJson(util::toJson(*props_)) : "", desc.get());
Expand Down Expand Up @@ -174,8 +174,8 @@ void GetEdges::cloneMembers(const GetEdges& ge) {

std::unique_ptr<PlanNodeDescription> IndexScan::explain() const {
auto desc = Explore::explain();
addDescription("schemaId", util::toJson(schemaId_), desc.get());
addDescription("isEdge", util::toJson(isEdge_), desc.get());
addDescription("schemaId", folly::toJson(util::toJson(schemaId_)), desc.get());
addDescription("isEdge", folly::toJson(util::toJson(isEdge_)), desc.get());
addDescription("returnCols", folly::toJson(util::toJson(returnCols_)), desc.get());
addDescription("indexCtx", folly::toJson(util::toJson(contexts_)), desc.get());
return desc;
Expand Down Expand Up @@ -605,8 +605,8 @@ void DataCollect::cloneMembers(const DataCollect& l) {
std::unique_ptr<PlanNodeDescription> Join::explain() const {
auto desc = SingleDependencyNode::explain();
folly::dynamic inputVar = folly::dynamic::object();
inputVar.insert("leftVar", util::toJson(leftVar_));
inputVar.insert("rightVar", util::toJson(rightVar_));
inputVar.insert("leftVar", folly::toJson(util::toJson(leftVar_)));
inputVar.insert("rightVar", folly::toJson(util::toJson(rightVar_)));
addDescription("inputVar", folly::toJson(inputVar), desc.get());
addDescription("hashKeys", folly::toJson(util::toJson(hashKeys_)), desc.get());
addDescription("probeKeys", folly::toJson(util::toJson(probeKeys_)), desc.get());
Expand Down Expand Up @@ -741,7 +741,7 @@ std::unique_ptr<PlanNodeDescription> Traverse::explain() const {
addDescription("steps", range_ != nullptr ? range_->toString() : "", desc.get());
addDescription("vertex filter", vFilter_ != nullptr ? vFilter_->toString() : "", desc.get());
addDescription("edge filter", eFilter_ != nullptr ? eFilter_->toString() : "", desc.get());
addDescription("if_track_previous_path", util::toJson(trackPrevPath_), desc.get());
addDescription("if_track_previous_path", folly::toJson(util::toJson(trackPrevPath_)), desc.get());
return desc;
}

Expand Down Expand Up @@ -769,7 +769,7 @@ void AppendVertices::cloneMembers(const AppendVertices& a) {
std::unique_ptr<PlanNodeDescription> AppendVertices::explain() const {
auto desc = GetVertices::explain();
addDescription("vertex_filter", vFilter_ != nullptr ? vFilter_->toString() : "", desc.get());
addDescription("if_track_previous_path", util::toJson(trackPrevPath_), desc.get());
addDescription("if_track_previous_path", folly::toJson(util::toJson(trackPrevPath_)), desc.get());
return desc;
}

Expand Down
62 changes: 45 additions & 17 deletions src/graph/util/ToJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@
namespace nebula {
namespace util {

std::string toJson(const std::string &str) {
folly::dynamic toJson(const std::string &str) {
return str;
}

std::string toJson(int32_t i) {
return folly::to<std::string>(i);
folly::dynamic toJson(int32_t i) {
return i;
}

std::string toJson(int64_t i) {
return folly::to<std::string>(i);
folly::dynamic toJson(int64_t i) {
return i;
}

std::string toJson(size_t i) {
return folly::to<std::string>(i);
folly::dynamic toJson(size_t i) {
return i;
}

std::string toJson(bool b) {
return b ? "true" : "false";
folly::dynamic toJson(bool b) {
return b;
}

std::string toJson(const HostAddr &addr) {
Expand All @@ -51,8 +51,39 @@ std::string toJson(const List &list) {
return list.toString();
}

std::string toJson(const Value &value) {
return value.toString();
folly::dynamic toJson(const Value &value) {
switch (value.type()) {
case Value::Type::__EMPTY__:
return "__EMPTY__";
case Value::Type::BOOL:
return value.getBool();
case Value::Type::INT:
return value.getInt();
case Value::Type::FLOAT:
return value.getFloat();
case Value::Type::STRING:
return value.getStr();
case Value::Type::DATE:
case Value::Type::TIME:
case Value::Type::DATETIME:
case Value::Type::DURATION:
// TODO store to object
return value.toString();
case Value::Type::NULLVALUE:
return nullptr;
case Value::Type::VERTEX:
case Value::Type::EDGE:
case Value::Type::PATH:
case Value::Type::LIST:
case Value::Type::MAP:
case Value::Type::SET:
case Value::Type::DATASET:
case Value::Type::GEOGRAPHY:
// TODO store to object or array
return value.toString();
}
DLOG(FATAL) << "Impossible reach.";
return folly::dynamic::object();
}

std::string toJson(const EdgeKeyRef *ref) {
Expand Down Expand Up @@ -218,7 +249,7 @@ folly::dynamic toJson(const storage::cpp2::VertexProp &prop) {
folly::dynamic toJson(const storage::cpp2::EdgeProp &prop) {
folly::dynamic obj = folly::dynamic::object();
if (prop.type_ref().is_set()) {
obj.insert("type", toJson(*prop.type_ref()));
obj.insert("type", *prop.type_ref());
}
if (prop.props_ref().is_set()) {
obj.insert("props", toJson(*prop.props_ref()));
Expand Down Expand Up @@ -267,11 +298,8 @@ folly::dynamic toJson(const storage::cpp2::IndexColumnHint &hints) {
obj.insert("column", hints.get_column_name());
auto scanType = apache::thrift::util::enumNameSafe(hints.get_scan_type());
obj.insert("scanType", scanType);
auto rtrim = [](const std::string &str) { return std::string(str.c_str()); };
auto begin = toJson(hints.get_begin_value());
obj.insert("beginValue", rtrim(begin));
auto end = toJson(hints.get_end_value());
obj.insert("endValue", rtrim(end));
obj.insert("beginValue", toJson(hints.get_begin_value()));
obj.insert("endValue", toJson(hints.get_end_value()));
auto includeBegin = toJson(hints.get_include_begin());
obj.insert("includeBegin", includeBegin);
auto includeEnd = toJson(hints.get_include_end());
Expand Down
12 changes: 6 additions & 6 deletions src/graph/util/ToJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ namespace util {
template <typename T>
folly::dynamic toJson(const std::vector<T> &arr);

std::string toJson(const std::string &str);
std::string toJson(int32_t i);
std::string toJson(int64_t i);
std::string toJson(size_t i);
std::string toJson(bool b);
folly::dynamic toJson(const std::string &str);
folly::dynamic toJson(int32_t i);
folly::dynamic toJson(int64_t i);
folly::dynamic toJson(size_t i);
folly::dynamic toJson(bool b);

std::string toJson(const HostAddr &addr);
std::string toJson(const List &list);
std::string toJson(const Value &value);
folly::dynamic toJson(const Value &value);
std::string toJson(const EdgeKeyRef *ref);
std::string toJson(const Expression *expr);

Expand Down
24 changes: 22 additions & 2 deletions tests/common/plan_differ.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
#
# This source code is licensed under Apache 2.0 License.

from codecs import ignore_errors
Aiee marked this conversation as resolved.
Show resolved Hide resolved
import re
import json
import deepdiff
Aiee marked this conversation as resolved.
Show resolved Hide resolved
from json import JSONDecodeError

class PlanDiffer:
ID = "id"
Expand Down Expand Up @@ -128,14 +131,31 @@ def _is_subdict_nested(self, expect, resp):
if extracted_expected_dict == expect:
key_list.append(list(expect.keys())[0])

def _try_convert_json(j):
try:
return json.loads(j)
except:
return j

extracted_resp_dict = {}
if len(key_list) == 1:
extracted_resp_dict = resp

for k in resp:
extracted_resp_dict[k] = _try_convert_json(resp[k])
else:
extracted_resp_dict = self._convert_jsonStr_to_dict(resp, key_list)

for k in extracted_expected_dict:
extracted_expected_dict[k] = _try_convert_json(extracted_expected_dict[k])

def _is_subdict(small, big):
return dict(big, **small) == big
new_big = dict(big, **small)
print(big)
print(small)
print(new_big)
diff = deepdiff.DeepDiff(big, new_big)
print(diff)
return not bool(diff)

return _is_subdict(extracted_expected_dict, extracted_resp_dict)

Expand Down
3 changes: 2 additions & 1 deletion tests/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ def load_csv_data(
create_space(space_desc, sess)

schemas = config['schema']
for line in schemas.splitlines():
sorted_schema_lines = sorted(schemas.splitlines()) # sort to make sure schema is created in fixed order
for line in sorted_schema_lines:
resp_ok(sess, line.strip(), True)

# wait heartbeat_interval_secs + 1 seconds for schema synchronization
Expand Down
Loading