Skip to content

Commit

Permalink
Merge branch 'master' into fix-index
Browse files Browse the repository at this point in the history
  • Loading branch information
czpmango authored Nov 17, 2021
2 parents cf6ff5b + 5eda4b8 commit e9945ba
Show file tree
Hide file tree
Showing 49 changed files with 1,450 additions and 1,089 deletions.
10 changes: 10 additions & 0 deletions src/common/datatypes/Edge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ void Edge::clear() {
props.clear();
}

bool Edge::keyEqual(const Edge& rhs) const {
if (type != rhs.type && type != -rhs.type) {
return false;
}
if (type == rhs.type) {
return src == rhs.src && dst == rhs.dst && ranking == rhs.ranking;
}
return src == rhs.dst && dst == rhs.src && ranking == rhs.ranking;
}

} // namespace nebula

namespace std {
Expand Down
2 changes: 2 additions & 0 deletions src/common/datatypes/Edge.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ struct Edge {
bool contains(const Value& key) const;

const Value& value(const std::string& key) const;

bool keyEqual(const Edge& rhs) const;
};

inline std::ostream& operator<<(std::ostream& os, const Edge& v) { return os << v.toString(); }
Expand Down
82 changes: 52 additions & 30 deletions src/common/expression/PathBuildExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,60 @@ const Value& PathBuildExpression::eval(ExpressionContext& ctx) {

for (size_t i = 1; i < items_.size(); ++i) {
auto& value = items_[i]->eval(ctx);
if (value.isEdge()) {
if (!buildPath(value, path)) {
return Value::kNullBadData;
}
}

result_ = path;
return result_;
}

bool PathBuildExpression::buildPath(const Value& value, Path& path) const {
switch (value.type()) {
case Value::Type::EDGE: {
Step step;
if (!path.steps.empty()) {
const auto& lastStep = path.steps.back();
const auto& edge = value.getEdge();
if (lastStep.dst.vid != edge.src) {
return Value::kNullBadData;
}
getEdge(value, path.steps.back().dst.vid, step);
} else {
getEdge(value, path.src.vid, step);
}
Step step;
getEdge(value, step);
path.steps.emplace_back(std::move(step));
} else if (value.isVertex()) {
break;
}
case Value::Type::VERTEX: {
if (path.steps.empty()) {
return Value::kNullBadData;
if (path.src.vid == value.getVertex().vid) {
return true;
}
return false;
}
auto& lastStep = path.steps.back();
const auto& vert = value.getVertex();
if (lastStep.dst.vid != vert.vid) {
return Value::kNullBadData;
}
getVertex(value, lastStep.dst);
} else if (value.isPath()) {
break;
}
case Value::Type::PATH: {
const auto& p = value.getPath();
if (!path.steps.empty()) {
auto& lastStep = path.steps.back();
if (lastStep.dst.vid != p.src.vid) {
return Value::kNullBadData;
}
lastStep.dst = p.src;
}
path.steps.insert(path.steps.end(), p.steps.begin(), p.steps.end());
} else {
if ((i & 1) == 1 || path.steps.empty() || !getVertex(value, path.steps.back().dst)) {
return Value::kNullBadData;
break;
}
case Value::Type::LIST: {
for (const auto& val : value.getList().values) {
if (!buildPath(val, path)) {
return false;
}
}
break;
}
default: {
return false;
}
}

result_ = path;
return result_;
return true;
}

bool PathBuildExpression::getVertex(const Value& value, Vertex& vertex) const {
Expand All @@ -84,18 +98,26 @@ bool PathBuildExpression::getVertex(const Value& value, Vertex& vertex) const {
return false;
}

bool PathBuildExpression::getEdge(const Value& value, Step& step) const {
if (value.isEdge()) {
const auto& edge = value.getEdge();
bool PathBuildExpression::getEdge(const Value& value, const Value& lastStepVid, Step& step) const {
if (!value.isEdge()) {
return false;
}

const auto& edge = value.getEdge();
if (lastStepVid == edge.src) {
step.type = edge.type;
step.name = edge.name;
step.ranking = edge.ranking;
step.props = edge.props;
step.dst.vid = edge.dst;
return true;
} else {
step.type = -edge.type;
step.name = edge.name;
step.ranking = edge.ranking;
step.props = edge.props;
step.dst.vid = edge.src;
}

return false;
return true;
}

bool PathBuildExpression::operator==(const Expression& rhs) const {
Expand Down
4 changes: 3 additions & 1 deletion src/common/expression/PathBuildExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ class PathBuildExpression final : public Expression {

bool getVertex(const Value& value, Vertex& vertex) const;

bool getEdge(const Value& value, Step& step) const;
bool getEdge(const Value& value, const Value& lastStepVid, Step& step) const;

bool buildPath(const Value& value, Path& path) const;

private:
std::vector<Expression*> items_;
Expand Down
4 changes: 2 additions & 2 deletions src/common/expression/test/ExpressionContextMock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ std::unordered_map<std::string, Value> ExpressionContextMock::vals_ = {
{"srcProperty", Value(13)},
{"dstProperty", Value(3)},

{"path_src", Value("1")},
{"path_src", Value(Vertex("1", {}))},
{"path_edge1", Value(Edge("1", "2", 1, "edge", 0, {}))},
{"path_v1", Value("2")},
{"path_v1", Value(Vertex("2", {}))},
{"path_edge2", Value(Edge("2", "3", 1, "edge", 0, {}))},
{"path_v2", Value(Vertex("3", {}))},
{"path_edge3", Value(Edge("3", "4", 1, "edge", 0, {}))},
Expand Down
43 changes: 43 additions & 0 deletions src/common/function/FunctionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ std::unordered_map<std::string, std::vector<TypeSignature>> FunctionManager::typ
Value::Type::FLOAT},
Value::Type::LIST),
}},
{"is_edge", {TypeSignature({Value::Type::EDGE}, Value::Type::BOOL)}},
};

// static
Expand Down Expand Up @@ -1945,6 +1946,41 @@ FunctionManager::FunctionManager() {
}
};
}
{
auto &attr = functions_["none_direct_dst"];
attr.minArity_ = 1;
attr.maxArity_ = 1;
attr.isPure_ = true;
attr.body_ = [](const auto &args) -> Value {
switch (args[0].get().type()) {
case Value::Type::NULLVALUE: {
return Value::kNullValue;
}
case Value::Type::EDGE: {
const auto &edge = args[0].get().getEdge();
return edge.dst;
}
case Value::Type::VERTEX: {
const auto &v = args[0].get().getVertex();
return v.vid;
}
case Value::Type::LIST: {
const auto &listVal = args[0].get().getList();
auto &lastVal = listVal.values.back();
if (lastVal.isEdge()) {
return lastVal.getEdge().dst;
} else if (lastVal.isVertex()) {
return lastVal.getVertex().vid;
} else {
return Value::kNullBadType;
}
}
default: {
return Value::kNullBadType;
}
}
};
}
{
auto &attr = functions_["rank"];
attr.minArity_ = 1;
Expand Down Expand Up @@ -2636,6 +2672,13 @@ FunctionManager::FunctionManager() {
return List(vals);
};
}
{
auto &attr = functions_["is_edge"];
attr.minArity_ = 1;
attr.maxArity_ = 1;
attr.isPure_ = true;
attr.body_ = [](const auto &args) -> Value { return args[0].get().isEdge(); };
}
} // NOLINT

// static
Expand Down
103 changes: 74 additions & 29 deletions src/graph/context/Iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,26 +324,54 @@ const Value& GetNeighborsIter::getTagProp(const std::string& tag, const std::str
return Value::kNullValue;
}

auto& tagPropIndices = currentDs_->tagPropsMap;
auto index = tagPropIndices.find(tag);
if (index == tagPropIndices.end()) {
return Value::kEmpty;
}
auto propIndex = index->second.propIndices.find(prop);
if (propIndex == index->second.propIndices.end()) {
return Value::kEmpty;
}
auto colId = index->second.colIdx;
size_t colId = 0;
size_t propId = 0;
auto& row = *currentRow_;
DCHECK_GT(row.size(), colId);
if (row[colId].empty()) {
if (tag == "*") {
for (auto& index : currentDs_->tagPropsMap) {
auto propIndex = index.second.propIndices.find(prop);
if (propIndex != index.second.propIndices.end()) {
colId = index.second.colIdx;
propId = propIndex->second;
DCHECK_GT(row.size(), colId);
if (row[colId].empty()) {
continue;
}
if (!row[colId].isList()) {
return Value::kNullBadType;
}
auto& list = row[colId].getList();
auto& val = list.values[propId];
if (val.empty()) {
continue;
} else {
return val;
}
}
}
return Value::kEmpty;
} else {
auto& tagPropIndices = currentDs_->tagPropsMap;
auto index = tagPropIndices.find(tag);
if (index == tagPropIndices.end()) {
return Value::kEmpty;
}
auto propIndex = index->second.propIndices.find(prop);
if (propIndex == index->second.propIndices.end()) {
return Value::kEmpty;
}
colId = index->second.colIdx;
propId = propIndex->second;
DCHECK_GT(row.size(), colId);
if (row[colId].empty()) {
return Value::kEmpty;
}
if (!row[colId].isList()) {
return Value::kNullBadType;
}
auto& list = row[colId].getList();
return list.values[propId];
}
if (!row[colId].isList()) {
return Value::kNullBadType;
}
auto& list = row[colId].getList();
return list.values[propIndex->second];
}

const Value& GetNeighborsIter::getEdgeProp(const std::string& edge, const std::string& prop) const {
Expand Down Expand Up @@ -688,20 +716,37 @@ const Value& PropIter::getProp(const std::string& name, const std::string& prop)
return Value::kNullValue;
}
auto& propsMap = dsIndex_.propsMap;
auto index = propsMap.find(name);
if (index == propsMap.end()) {
return Value::kEmpty;
}

auto propIndex = index->second.find(prop);
if (propIndex == index->second.end()) {
VLOG(1) << "No prop found : " << prop;
size_t colId = 0;
auto& row = *iter_;
if (name == "*") {
for (auto& index : propsMap) {
auto propIndex = index.second.find(prop);
if (propIndex == index.second.end()) {
continue;
}
colId = propIndex->second;
DCHECK_GT(row.size(), colId);
auto& val = row[colId];
if (val.empty()) {
continue;
} else {
return val;
}
}
return Value::kNullValue;
} else {
auto index = propsMap.find(name);
if (index == propsMap.end()) {
return Value::kEmpty;
}
auto propIndex = index->second.find(prop);
if (propIndex == index->second.end()) {
return Value::kNullValue;
}
colId = propIndex->second;
DCHECK_GT(row.size(), colId);
return row[colId];
}
auto colId = propIndex->second;
auto& row = *iter_;
DCHECK_GT(row.size(), colId);
return row[colId];
}

Value PropIter::getVertex(const std::string& name) const {
Expand Down
2 changes: 2 additions & 0 deletions src/graph/context/Iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ class SequentialIter : public Iterator {

// Notice: We only use this interface when return results to client.
friend class DataCollectExecutor;
friend class AppendVerticesExecutor;
friend class TraverseExecutor;
Row&& moveRow() { return std::move(*iter_); }

void doReset(size_t pos) override;
Expand Down
2 changes: 2 additions & 0 deletions src/graph/executor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ nebula_add_library(
query/InnerJoinExecutor.cpp
query/IndexScanExecutor.cpp
query/AssignExecutor.cpp
query/TraverseExecutor.cpp
query/AppendVerticesExecutor.cpp
algo/ConjunctPathExecutor.cpp
algo/BFSShortestPathExecutor.cpp
algo/ProduceSemiShortestPathExecutor.cpp
Expand Down
Loading

0 comments on commit e9945ba

Please sign in to comment.